blob: bfbc7d44d186be32fcdf2c2f48a6422065b403e9 [file] [log] [blame]
srs569420e2a972010-02-04 00:55:04 -05001//
2// C++ Implementation: GUIDData
3//
4// Description: GUIDData class header
5// Implements the GUIDData data structure and support methods
6//
7//
8// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2010
9//
10// Copyright: See COPYING file that comes with this distribution
11//
12//
13
14#define __STDC_LIMIT_MACROS
15#define __STDC_CONSTANT_MACROS
16
17#include <stdio.h>
srs56945a081752010-09-24 20:39:41 -040018#include <time.h>
srs569464cbd172011-03-01 22:03:54 -050019#include <string.h>
srs569420e2a972010-02-04 00:55:04 -050020#include <string>
21#include <iostream>
22#include "guid.h"
23#include "support.h"
24
25using namespace std;
26
27GUIDData::GUIDData(void) {
srs5694ab4b0432010-09-25 20:39:52 -040028 srand((unsigned int) time(0));
srs569420e2a972010-02-04 00:55:04 -050029 Zero();
30} // constructor
31
32GUIDData::GUIDData(const GUIDData & orig) {
srs569464cbd172011-03-01 22:03:54 -050033 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -050034} // copy constructor
35
36GUIDData::GUIDData(const char * orig) {
37 operator=(orig);
38} // copy (from char*) constructor
39
40GUIDData::~GUIDData(void) {
41} // destructor
42
43GUIDData & GUIDData::operator=(const GUIDData & orig) {
srs569464cbd172011-03-01 22:03:54 -050044 memcpy(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -050045 return *this;
46} // GUIDData::operator=(const GUIDData & orig)
47
48// Assign the GUID from a string input value. A GUID is normally formatted
49// with four dashes as element separators, for a total length of 36
50// characters. If the input string is this long or longer, this function
51// assumes standard separator positioning; if the input string is less
52// than 36 characters long, this function assumes the input GUID has
53// been compressed by removal of separators. In either event, there's
54// little in the way of sanity checking, so garbage in = garbage out!
srs56948f1b2d62010-05-23 13:07:19 -040055// One special case: If the first character is 'r' or 'R', a random
56// GUID is assigned.
srs569420e2a972010-02-04 00:55:04 -050057GUIDData & GUIDData::operator=(const string & orig) {
58 string copy, fragment;
59 size_t len;
60 // Break points for segments, either with or without characters separating the segments....
61 size_t longSegs[6] = {0, 9, 14, 19, 24, 36};
62 size_t shortSegs[6] = {0, 8, 12, 16, 20, 32};
srs569455d92612010-03-07 22:16:07 -050063 size_t *segStart = longSegs; // Assume there are separators between segments
srs569420e2a972010-02-04 00:55:04 -050064
srs56948f1b2d62010-05-23 13:07:19 -040065 // If first character is an 'R' or 'r', set a random GUID; otherwise,
66 // try to parse it as a real GUID
67 if ((orig[0] == 'R') || (orig[0] == 'r')) {
68 Randomize();
69 } else {
70 Zero();
srs569420e2a972010-02-04 00:55:04 -050071
srs56948f1b2d62010-05-23 13:07:19 -040072 // Delete stray spaces....
73 copy = DeleteSpaces(orig);
srs569420e2a972010-02-04 00:55:04 -050074
srs56948f1b2d62010-05-23 13:07:19 -040075 // If length is too short, assume there are no separators between segments
76 len = copy.length();
77 if (len < 36) {
78 segStart = shortSegs;
79 };
srs569420e2a972010-02-04 00:55:04 -050080
srs56948f1b2d62010-05-23 13:07:19 -040081 // Extract data fragments at fixed locations and convert to
82 // integral types....
83 if (len >= segStart[1]) {
84 uuidData[3] = StrToHex(copy, 0);
85 uuidData[2] = StrToHex(copy, 2);
86 uuidData[1] = StrToHex(copy, 4);
87 uuidData[0] = StrToHex(copy, 6);
88 } // if
89 if (len >= segStart[2]) {
srs5694058d4a52010-10-12 12:42:47 -040090 uuidData[5] = StrToHex(copy, (unsigned int) segStart[1]);
91 uuidData[4] = StrToHex(copy, (unsigned int) segStart[1] + 2);
srs56948f1b2d62010-05-23 13:07:19 -040092 } // if
93 if (len >= segStart[3]) {
srs5694058d4a52010-10-12 12:42:47 -040094 uuidData[7] = StrToHex(copy, (unsigned int) segStart[2]);
95 uuidData[6] = StrToHex(copy, (unsigned int) segStart[2] + 2);
srs56948f1b2d62010-05-23 13:07:19 -040096 } // if
97 if (len >= segStart[4]) {
srs5694058d4a52010-10-12 12:42:47 -040098 uuidData[8] = StrToHex(copy, (unsigned int) segStart[3]);
99 uuidData[9] = StrToHex(copy, (unsigned int) segStart[3] + 2);
srs56948f1b2d62010-05-23 13:07:19 -0400100 } // if
101 if (len >= segStart[5]) {
srs5694058d4a52010-10-12 12:42:47 -0400102 uuidData[10] = StrToHex(copy, (unsigned int) segStart[4]);
103 uuidData[11] = StrToHex(copy, (unsigned int) segStart[4] + 2);
104 uuidData[12] = StrToHex(copy, (unsigned int) segStart[4] + 4);
105 uuidData[13] = StrToHex(copy, (unsigned int) segStart[4] + 6);
106 uuidData[14] = StrToHex(copy, (unsigned int) segStart[4] + 8);
107 uuidData[15] = StrToHex(copy, (unsigned int) segStart[4] + 10);
srs56948f1b2d62010-05-23 13:07:19 -0400108 } // if
109 } // if/else randomize/set value
srs569420e2a972010-02-04 00:55:04 -0500110
111 return *this;
112} // GUIDData::operator=(const string & orig)
113
114// Assignment from C-style string; rely on C++ casting....
115GUIDData & GUIDData::operator=(const char * orig) {
116 return operator=((string) orig);
117} // GUIDData::operator=(const char * orig)
118
srs569420e2a972010-02-04 00:55:04 -0500119// Erase the contents of the GUID
120void GUIDData::Zero(void) {
srs569464cbd172011-03-01 22:03:54 -0500121 memset(uuidData, 0, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -0500122} // GUIDData::Zero()
123
124// Set a completely random GUID value....
125// The uuid_generate() function returns a value that needs to have its
126// first three fields byte-reversed to conform to Intel's GUID layout.
127// If that function isn't defined (e.g., on Windows), set a completely
128// random GUID -- not completely kosher, but it doesn't seem to cause
129// any problems (so far...)
130void GUIDData::Randomize(void) {
131#ifdef _UUID_UUID_H
132 uuid_generate(uuidData);
133 ReverseBytes(&uuidData[0], 4);
134 ReverseBytes(&uuidData[4], 2);
135 ReverseBytes(&uuidData[6], 2);
136#else
137 int i;
138 for (i = 0; i < 16; i++)
srs56945a081752010-09-24 20:39:41 -0400139 uuidData[i] = (unsigned char) (256.0 * (rand() / (RAND_MAX + 1.0)));
srs569420e2a972010-02-04 00:55:04 -0500140#endif
141} // GUIDData::Randomize
142
143// Equality operator; returns 1 if the GUIDs are equal, 0 if they're unequal
srs56945a081752010-09-24 20:39:41 -0400144int GUIDData::operator==(const GUIDData & orig) const {
srs569464cbd172011-03-01 22:03:54 -0500145 return !memcmp(uuidData, orig.uuidData, sizeof(uuidData));
srs569420e2a972010-02-04 00:55:04 -0500146} // GUIDData::operator==
147
148// Inequality operator; returns 1 if the GUIDs are unequal, 0 if they're equal
srs56945a081752010-09-24 20:39:41 -0400149int GUIDData::operator!=(const GUIDData & orig) const {
srs569420e2a972010-02-04 00:55:04 -0500150 return !operator==(orig);
151} // GUIDData::operator!=
152
153// Return the GUID as a string, suitable for display to the user.
srs56945a081752010-09-24 20:39:41 -0400154string GUIDData::AsString(void) const {
srs569420e2a972010-02-04 00:55:04 -0500155 char theString[40];
156
157 sprintf(theString,
158 "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
159 uuidData[3], uuidData[2], uuidData[1], uuidData[0], uuidData[5],
160 uuidData[4], uuidData[7], uuidData[6], uuidData[8], uuidData[9],
161 uuidData[10], uuidData[11], uuidData[12], uuidData[13], uuidData[14],
162 uuidData[15]);
163 return theString;
164} // GUIDData::AsString(void)
165
166// Delete spaces or braces (which often enclose GUIDs) from the orig string,
167// returning modified string.
srs569455d92612010-03-07 22:16:07 -0500168string GUIDData::DeleteSpaces(string s) {
srs569420e2a972010-02-04 00:55:04 -0500169 size_t position;
170
srs569455d92612010-03-07 22:16:07 -0500171 if (s.length() > 0) {
172 for (position = s.length(); position > 0; position--) {
173 if ((s[position - 1] == ' ') || (s[position - 1] == '{') || (s[position - 1] == '}')) {
174 s.erase(position - 1, 1);
175 } // if
176 } // for
177 } // if
178 return s;
srs569420e2a972010-02-04 00:55:04 -0500179} // GUIDData::DeleteSpaces()
srs56945a081752010-09-24 20:39:41 -0400180
181/*******************************
182 * *
183 * Non-class support functions *
184 * *
185 *******************************/
186
187// Display a GUID as a string....
188ostream & operator<<(ostream & os, const GUIDData & data) {
srs56940873e9d2010-10-07 13:00:45 -0400189// string asString;
srs56945a081752010-09-24 20:39:41 -0400190
191 os << data.AsString();
192 return os;
193} // GUIDData::operator<<()