Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlCore/UUID.hpp>
00033 #include <vlCore/Time.hpp>
00034 #include <vlCore/Random.hpp>
00035 #include <vector>
00036
00037 using namespace vl;
00038
00039
00040 UUID::UUID()
00041 {
00042 memset(this, 0, sizeof(*this));
00043 }
00044
00045 void UUID::generateVersion4(const Random& random)
00046 {
00047 random.fillRandom(this,sizeof(*this));
00048
00049
00050 mClockSeqHiAndReserved &= 0x3F;
00051 mClockSeqHiAndReserved |= 0x80;
00052
00053
00054 mTimeHiAndVersion &= 0x0FFF;
00055 mTimeHiAndVersion |= 0x4000;
00056 }
00057
00058 void UUID::toStdString(std::string& guid_str) const
00059 {
00060 char str[38];
00061 fillString(str, false);
00062
00063 guid_str.resize(38);
00064 for(int i=0; i<38; ++i)
00065 guid_str[i] = str[i];
00066 }
00067
00068 void UUID::fillString(char* guid_str, bool zero_terminate) const
00069 {
00070 const char* hex= "0123456789abcdef";
00071
00072
00073
00074
00075 if (zero_terminate)
00076 guid_str[38] = 0;
00077
00078 guid_str[0] = '{';
00079 guid_str[9] = '-';
00080 guid_str[14] = '-';
00081 guid_str[19] = '-';
00082 guid_str[24] = '-';
00083 guid_str[37] = '}';
00084
00085
00086 for(int i=0; i<8; ++i)
00087 guid_str[i+1] = hex[ (mTimeLow >> (28-4*i)) & 0xF];
00088
00089
00090 for(int i=0; i<4; ++i)
00091 guid_str[i+10] = hex[ (mTimeMid >> (12-4*i)) & 0xF];
00092
00093
00094 for(int i=0; i<4; ++i)
00095 guid_str[i+15] = hex[ (mTimeHiAndVersion >> (12-4*i)) & 0xF];
00096
00097
00098 guid_str[20] = hex[ (mClockSeqHiAndReserved >> (4-4*0)) & 0xF];
00099 guid_str[21] = hex[ (mClockSeqHiAndReserved >> (4-4*1)) & 0xF];
00100
00101
00102 guid_str[22] = hex[ (mClockSeqLow >> (4-4*0)) & 0xF];
00103 guid_str[23] = hex[ (mClockSeqLow >> (4-4*1)) & 0xF];
00104
00105
00106 guid_str[25] = hex[ (mNode[0] >> (4-4*0)) & 0xF];
00107 guid_str[26] = hex[ (mNode[0] >> (4-4*1)) & 0xF];
00108 guid_str[27] = hex[ (mNode[1] >> (4-4*0)) & 0xF];
00109 guid_str[28] = hex[ (mNode[1] >> (4-4*1)) & 0xF];
00110 guid_str[29] = hex[ (mNode[2] >> (4-4*0)) & 0xF];
00111 guid_str[30] = hex[ (mNode[2] >> (4-4*1)) & 0xF];
00112 guid_str[31] = hex[ (mNode[3] >> (4-4*0)) & 0xF];
00113 guid_str[32] = hex[ (mNode[3] >> (4-4*1)) & 0xF];
00114 guid_str[33] = hex[ (mNode[4] >> (4-4*0)) & 0xF];
00115 guid_str[34] = hex[ (mNode[4] >> (4-4*1)) & 0xF];
00116 guid_str[35] = hex[ (mNode[5] >> (4-4*0)) & 0xF];
00117 guid_str[36] = hex[ (mNode[5] >> (4-4*1)) & 0xF];
00118
00119
00120 #if 0
00121 char guid_cstr[100];
00122 sprintf(guid_cstr, "{%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x}",
00123 mTimeLow, mTimeMid, mTimeHiAndVersion,
00124 mClockSeqHiAndReserved, mClockSeqLow,
00125 mNode[0], mNode[1], mNode[2], mNode[3], mNode[4], mNode[5]);
00126 VL_CHECK( memcmp(guid_str, guid_cstr, 38) == 0 )
00127 #endif
00128 }
00129
00130 std::string UUID::toStdString() const
00131 {
00132 std::string guid_str;
00133 toStdString(guid_str);
00134 return guid_str;
00135 }
00136
00137 bool UUID::fromString(const char* guid_str)
00138 {
00139 char xxx[38];
00140 memcpy(xxx, guid_str, 38);
00141
00142 if (guid_str[0] != '{')
00143 return false;
00144
00145 if (guid_str[37] != '}')
00146 return false;
00147
00148 if (guid_str[9] != '-')
00149 return false;
00150
00151 if (guid_str[14] != '-')
00152 return false;
00153
00154 if (guid_str[19] != '-')
00155 return false;
00156
00157 if (guid_str[24] != '-')
00158 return false;
00159
00160
00161 unsigned int nibble[38];
00162 for(int i=0; i<38; ++i)
00163 {
00164 if (i==0 || i==37 || i==9 || i==14 || i==19 || i==24 )
00165 continue;
00166
00167 if ( guid_str[i]>='0' && guid_str[i]<='9' )
00168 nibble[i] = guid_str[i] - '0';
00169 else
00170 if ( guid_str[i]>='a' && guid_str[i]<='f' )
00171 nibble[i] = guid_str[i] - 'a' + 10;
00172 else
00173 if ( guid_str[i]>='A' && guid_str[i]<='F' )
00174 nibble[i] = guid_str[i] - 'A' + 10;
00175 else
00176 return false;
00177 }
00178
00179
00180
00181
00182 memset(this, 0, sizeof(*this));
00183
00184 mTimeLow = (nibble[1] << 28) | (nibble[2] << 24) | (nibble[3] << 20) | (nibble[4] << 16) |
00185 (nibble[5] << 12) | (nibble[6] << 8) | (nibble[7] << 4) | nibble[8];
00186
00187 mTimeMid = (unsigned short)((nibble[10] << 12) | (nibble[11] << 8) | (nibble[12] << 4) | nibble[13]);
00188
00189 mTimeHiAndVersion = (unsigned short)((nibble[15] << 12) | (nibble[16] << 8) | (nibble[17] << 4) | nibble[18]);
00190
00191 mClockSeqHiAndReserved = (unsigned char)((nibble[20] << 4) | nibble[21]);
00192
00193 mClockSeqLow = (unsigned char)((nibble[22] << 4) | (nibble[23] << 0));
00194
00195 mNode[0] = (unsigned char)((nibble[25] << 4) | nibble[26]);
00196 mNode[1] = (unsigned char)((nibble[27] << 4) | nibble[28]);
00197 mNode[2] = (unsigned char)((nibble[29] << 4) | nibble[30]);
00198 mNode[3] = (unsigned char)((nibble[31] << 4) | nibble[32]);
00199 mNode[4] = (unsigned char)((nibble[33] << 4) | nibble[34]);
00200 mNode[5] = (unsigned char)((nibble[35] << 4) | nibble[36]);
00201
00202 return true;
00203 }
00204
00205