| srs5694 | 20e2a97 | 2010-02-04 00:55:04 -0500 | [diff] [blame] | 1 | // | 
 | 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> | 
 | 18 | #include <string> | 
 | 19 | #include <iostream> | 
 | 20 | #include "guid.h" | 
 | 21 | #include "support.h" | 
 | 22 |  | 
 | 23 | using namespace std; | 
 | 24 |  | 
 | 25 | GUIDData::GUIDData(void) { | 
 | 26 |    Zero(); | 
 | 27 | } // constructor | 
 | 28 |  | 
 | 29 | GUIDData::GUIDData(const GUIDData & orig) { | 
 | 30 |    int i; | 
 | 31 |  | 
 | 32 |    for (i = 0; i < 16; i++) | 
 | 33 |       uuidData[i] = orig.uuidData[i]; | 
 | 34 | } // copy constructor | 
 | 35 |  | 
 | 36 | GUIDData::GUIDData(const char * orig) { | 
 | 37 |    operator=(orig); | 
 | 38 | } // copy (from char*) constructor | 
 | 39 |  | 
 | 40 | GUIDData::~GUIDData(void) { | 
 | 41 | } // destructor | 
 | 42 |  | 
 | 43 | GUIDData & GUIDData::operator=(const GUIDData & orig) { | 
 | 44 |    int i; | 
 | 45 |  | 
 | 46 |    for (i = 0; i < 16; i++) | 
 | 47 |       uuidData[i] = orig.uuidData[i]; | 
 | 48 |    return *this; | 
 | 49 | } // GUIDData::operator=(const GUIDData & orig) | 
 | 50 |  | 
 | 51 | // Assign the GUID from a string input value. A GUID is normally formatted | 
 | 52 | // with four dashes as element separators, for a total length of 36 | 
 | 53 | // characters. If the input string is this long or longer, this function | 
 | 54 | // assumes standard separator positioning; if the input string is less | 
 | 55 | // than 36 characters long, this function assumes the input GUID has | 
 | 56 | // been compressed by removal of separators. In either event, there's | 
 | 57 | // little in the way of sanity checking, so garbage in = garbage out! | 
 | 58 | GUIDData & GUIDData::operator=(const string & orig) { | 
 | 59 |    string copy, fragment; | 
 | 60 |    size_t len; | 
 | 61 |    // Break points for segments, either with or without characters separating the segments.... | 
 | 62 |    size_t longSegs[6] = {0, 9, 14, 19, 24, 36}; | 
 | 63 |    size_t shortSegs[6] = {0, 8, 12, 16, 20, 32}; | 
 | 64 |    size_t *segStart = longSegs; // Assume there are separators between segments */ | 
 | 65 |  | 
 | 66 |    Zero(); | 
 | 67 |  | 
 | 68 |    // Delete stray spaces.... | 
 | 69 |    copy = DeleteSpaces(orig); | 
 | 70 |  | 
 | 71 |    // If length is too short, assume there are no separators between segments | 
 | 72 |    len = copy.length(); | 
 | 73 |    if (len < 36) { | 
 | 74 |       segStart = shortSegs; | 
 | 75 |    }; | 
 | 76 |  | 
 | 77 |    // Extract data fragments at fixed locations and convert to | 
 | 78 |    // integral types.... | 
 | 79 |    if (len >= segStart[1]) { | 
 | 80 |       uuidData[3] = StrToHex(copy, 0); | 
 | 81 |       uuidData[2] = StrToHex(copy, 2); | 
 | 82 |       uuidData[1] = StrToHex(copy, 4); | 
 | 83 |       uuidData[0] = StrToHex(copy, 6); | 
 | 84 |    } // if | 
 | 85 |    if (len >= segStart[2]) { | 
 | 86 |       uuidData[5] = StrToHex(copy, segStart[1]); | 
 | 87 |       uuidData[4] = StrToHex(copy, segStart[1] + 2); | 
 | 88 |    } // if | 
 | 89 |    if (len >= segStart[3]) { | 
 | 90 |       uuidData[7] = StrToHex(copy, segStart[2]); | 
 | 91 |       uuidData[6] = StrToHex(copy, segStart[2] + 2); | 
 | 92 |    } // if | 
 | 93 |    if (len >= segStart[4]) { | 
 | 94 |       uuidData[8] = StrToHex(copy, segStart[3]); | 
 | 95 |       uuidData[9] = StrToHex(copy, segStart[3] + 2); | 
 | 96 |    } // if | 
 | 97 |    if (len >= segStart[5]) { | 
 | 98 |       uuidData[10] = StrToHex(copy, segStart[4]); | 
 | 99 |       uuidData[11] = StrToHex(copy, segStart[4] + 2); | 
 | 100 |       uuidData[12] = StrToHex(copy, segStart[4] + 4); | 
 | 101 |       uuidData[13] = StrToHex(copy, segStart[4] + 6); | 
 | 102 |       uuidData[14] = StrToHex(copy, segStart[4] + 8); | 
 | 103 |       uuidData[15] = StrToHex(copy, segStart[4] + 10); | 
 | 104 |    } // if | 
 | 105 |  | 
 | 106 |    return *this; | 
 | 107 | } // GUIDData::operator=(const string & orig) | 
 | 108 |  | 
 | 109 | // Assignment from C-style string; rely on C++ casting.... | 
 | 110 | GUIDData & GUIDData::operator=(const char * orig) { | 
 | 111 |    return operator=((string) orig); | 
 | 112 | } // GUIDData::operator=(const char * orig) | 
 | 113 |  | 
 | 114 | // Read a GUIDData from stdin, prompting the user along the way for the | 
 | 115 | // correct form. This is a bit more flexible than it claims; it parses | 
 | 116 | // any entry of 32 or 36 characters as a GUID ( | 
 | 117 | GUIDData & GUIDData::GetGUIDFromUser(void) { | 
 | 118 |    string part1, part2, part3, part4, part5; | 
 | 119 |    char line[255]; | 
 | 120 |    int entered = 0; | 
 | 121 |  | 
 | 122 |    cout << "\nA GUID is entered in five segments of from two to six bytes, with\n" | 
 | 123 |         << "dashes between segments.\n"; | 
 | 124 |    cout << "Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n" | 
 | 125 |         << "'R' to generate the entire GUID randomly: "; | 
 | 126 |    cin.get(line, 255); | 
 | 127 |    part1 = DeleteSpaces(line); | 
 | 128 |  | 
 | 129 |    // If user entered 'r' or 'R', generate GUID randomly.... | 
 | 130 |    if ((part1[0] == 'r') || (part1[0] == 'R')) { | 
 | 131 |       Randomize(); | 
 | 132 |       entered = 1; | 
 | 133 |    } // if user entered 'R' or 'r' | 
 | 134 |  | 
 | 135 |    // If string length is right for whole entry, try to parse it.... | 
 | 136 |    if (((part1.length() == 36) || (part1.length() == 32)) && (entered == 0)) { | 
 | 137 |       operator=(part1); | 
 | 138 |       entered = 1; | 
 | 139 |    } // if | 
 | 140 |  | 
 | 141 |    // If neither of the above methods of entry was used, use prompted | 
 | 142 |    // entry.... | 
 | 143 |    if (entered == 0) { | 
 | 144 |       cout << "Enter a two-byte hexadecimal number for the second segment: "; | 
 | 145 |       cin >> part2; | 
 | 146 |       cout << "Enter a two-byte hexadecimal number for the third segment: "; | 
 | 147 |       cin >> part3; | 
 | 148 |       cout << "Enter a two-byte hexadecimal number for the fourth segment: "; | 
 | 149 |       cin >> part4; | 
 | 150 |       cout << "Enter a six-byte hexadecimal number for the fifth segment: "; | 
 | 151 |       cin >> part5; | 
 | 152 |       operator=(part1 += (string) "-" += part2 += (string) "-" += part3 | 
 | 153 |             += (string) "-" += part4 += (string) "-" += part5); | 
 | 154 |    } // if/else | 
 | 155 |    cin.ignore(255, '\n'); | 
 | 156 |    cout << "New GUID: " << AsString() << "\n"; | 
 | 157 |    return *this; | 
 | 158 | } // GUIDData::GetGUIDData(void) | 
 | 159 |  | 
 | 160 | // Erase the contents of the GUID | 
 | 161 | void GUIDData::Zero(void) { | 
 | 162 |    int i; | 
 | 163 |  | 
 | 164 |    for (i = 0; i < 16; i++) { | 
 | 165 |       uuidData[i] = 0; | 
 | 166 |    } // for | 
 | 167 | } // GUIDData::Zero() | 
 | 168 |  | 
 | 169 | // Set a completely random GUID value.... | 
 | 170 | // The uuid_generate() function returns a value that needs to have its | 
 | 171 | // first three fields byte-reversed to conform to Intel's GUID layout. | 
 | 172 | // If that function isn't defined (e.g., on Windows), set a completely | 
 | 173 | // random GUID -- not completely kosher, but it doesn't seem to cause | 
 | 174 | // any problems (so far...) | 
 | 175 | void GUIDData::Randomize(void) { | 
 | 176 | #ifdef _UUID_UUID_H | 
 | 177 |    uuid_generate(uuidData); | 
 | 178 |    ReverseBytes(&uuidData[0], 4); | 
 | 179 |    ReverseBytes(&uuidData[4], 2); | 
 | 180 |    ReverseBytes(&uuidData[6], 2); | 
 | 181 | #else | 
 | 182 |    int i; | 
 | 183 |    for (i = 0; i < 16; i++) | 
 | 184 |       uuidData[i] = rand(); | 
 | 185 | #endif | 
 | 186 | } // GUIDData::Randomize | 
 | 187 |  | 
 | 188 | // Equality operator; returns 1 if the GUIDs are equal, 0 if they're unequal | 
 | 189 | int GUIDData::operator==(const GUIDData & orig) { | 
 | 190 |    int retval = 1; // assume they're equal | 
 | 191 |    int i; | 
 | 192 |  | 
 | 193 |    for (i = 0; i < 16; i++) | 
 | 194 |       if (uuidData[i] != orig.uuidData[i]) | 
 | 195 |          retval = 0; | 
 | 196 |  | 
 | 197 |    return retval; | 
 | 198 | } // GUIDData::operator== | 
 | 199 |  | 
 | 200 | // Inequality operator; returns 1 if the GUIDs are unequal, 0 if they're equal | 
 | 201 | int GUIDData::operator!=(const GUIDData & orig) { | 
 | 202 |    return !operator==(orig); | 
 | 203 | } // GUIDData::operator!= | 
 | 204 |  | 
 | 205 | // Return the GUID as a string, suitable for display to the user. | 
 | 206 | string GUIDData::AsString(void) { | 
 | 207 |    char theString[40]; | 
 | 208 |  | 
 | 209 |    sprintf(theString, | 
 | 210 |            "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", | 
 | 211 |            uuidData[3], uuidData[2], uuidData[1], uuidData[0], uuidData[5], | 
 | 212 |            uuidData[4], uuidData[7], uuidData[6], uuidData[8], uuidData[9], | 
 | 213 |            uuidData[10], uuidData[11], uuidData[12], uuidData[13], uuidData[14], | 
 | 214 |            uuidData[15]); | 
 | 215 |    return theString; | 
 | 216 | } // GUIDData::AsString(void) | 
 | 217 |  | 
 | 218 | // Delete spaces or braces (which often enclose GUIDs) from the orig string, | 
 | 219 | // returning modified string. | 
 | 220 | string GUIDData::DeleteSpaces(const string & orig) { | 
 | 221 |    string copy; | 
 | 222 |    size_t position; | 
 | 223 |  | 
 | 224 |    copy = orig; | 
 | 225 |    for (position = copy.length(); position > 0; position--) { | 
 | 226 |       if ((copy[position - 1] == ' ') || (copy[position - 1] == '{') || (copy[position - 1] == '}')) { | 
 | 227 |          copy.erase(position - 1, 1); | 
 | 228 |       } // if | 
 | 229 |    } // for | 
 | 230 |    return copy; | 
 | 231 | } // GUIDData::DeleteSpaces() |