-rw-r--r-- | qmake/tools/quuid.cpp | 280 |
1 files changed, 234 insertions, 46 deletions
diff --git a/qmake/tools/quuid.cpp b/qmake/tools/quuid.cpp index fd99abf..199bfa5 100644 --- a/qmake/tools/quuid.cpp +++ b/qmake/tools/quuid.cpp | |||
@@ -1,10 +1,10 @@ | |||
1 | /**************************************************************************** | 1 | /**************************************************************************** |
2 | ** $Id$ | 2 | ** |
3 | ** | 3 | ** |
4 | ** Implementation of QUuid class | 4 | ** Implementation of QUuid class |
5 | ** | 5 | ** |
6 | ** Copyright (C) 2000-2001 Trolltech AS. All rights reserved. | 6 | ** Copyright (C) 2000-2001 Trolltech AS. All rights reserved. |
7 | ** | 7 | ** |
8 | ** This file is part of the tools module of the Qt GUI Toolkit. | 8 | ** This file is part of the tools module of the Qt GUI Toolkit. |
9 | ** | 9 | ** |
10 | ** This file may be distributed under the terms of the Q Public License | 10 | ** This file may be distributed under the terms of the Q Public License |
@@ -30,71 +30,87 @@ | |||
30 | ** | 30 | ** |
31 | ** Contact info@trolltech.com if any conditions of this licensing are | 31 | ** Contact info@trolltech.com if any conditions of this licensing are |
32 | ** not clear to you. | 32 | ** not clear to you. |
33 | ** | 33 | ** |
34 | **********************************************************************/ | 34 | **********************************************************************/ |
35 | 35 | ||
36 | #include "quuid.h" | 36 | #include "quuid.h" |
37 | 37 | ||
38 | #include <qdatastream.h> | 38 | #include "qdatastream.h" |
39 | 39 | ||
40 | /*! | 40 | /*! |
41 | \class QUuid quuid.h | 41 | \class QUuid quuid.h |
42 | \reentrant | 42 | \brief The QUuid class defines a Universally Unique Identifier (UUID). |
43 | \brief The QUuid class defines a Universally Unique Identifier (UUID). | ||
44 | 43 | ||
45 | \internal | 44 | \reentrant |
46 | 45 | ||
47 | For objects or declarations that need to be identified uniquely, UUIDs (also known as GUIDs) are widely | 46 | For objects or declarations that must be uniquely identified, |
48 | used in order to assign a fixed and easy to compare value to this object or declaration. The 128bit value | 47 | UUIDs (also known as GUIDs) are widely used in order to assign a |
49 | of an UUID is generated by an algorithm that guarantees a value that is unique in time and space. | 48 | fixed and easy to compare value to the object or declaration. The |
49 | 128-bit value of a UUID is generated by an algorithm that | ||
50 | guarantees that the value is unique. | ||
50 | 51 | ||
51 | In Qt, UUIDs are wrapped by the QUuid struct which provides convenience functions for comparing and coping | 52 | In Qt, UUIDs are wrapped by the QUuid struct which provides |
52 | this value. The QUuid struct is used in Qt's component model to identify interfaces. Most platforms provide a tool to | 53 | convenience functions for handling UUIDs. Most platforms provide a |
53 | generate new UUIDs (uuidgen, guidgen), and the Qt distribution includes a graphical tool \e quuidgen that generates | 54 | tool to generate new UUIDs, for example, uuidgen and guidgen. |
54 | the UUIDs in a programmer friendly format. | ||
55 | 55 | ||
56 | \sa QUnknownInterface | 56 | UUIDs generated by QUuid, are based on the \c Random version of the |
57 | \c DCE (Distributed Computing Environment) standard. | ||
58 | |||
59 | UUIDs can be constructed from numeric values or from strings, or | ||
60 | using the static createUuid() function. They can be converted to a | ||
61 | string with toString(). UUIDs have a variant() and a version(), | ||
62 | and null UUIDs return TRUE from isNull(). | ||
57 | */ | 63 | */ |
58 | 64 | ||
59 | /*! | 65 | /*! |
60 | \fn QUuid::QUuid() | 66 | \fn QUuid::QUuid() |
61 | 67 | ||
62 | Creates the null UUID {00000000-0000-0000-0000-000000000000}. | 68 | Creates the null UUID {00000000-0000-0000-0000-000000000000}. |
63 | */ | 69 | */ |
64 | 70 | ||
65 | /*! | 71 | /*! |
66 | \fn QUuid::QUuid( uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8 ) | 72 | \fn QUuid::QUuid( uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8 ) |
67 | 73 | ||
68 | Creates an UUID with the value specified by the parameters, \a l, \a | 74 | Creates a UUID with the value specified by the parameters, \a l, |
69 | w1, \a w2, \a b1, \a b2, \a b3, \a b4, \a b5, \a b6, \a b7, \a b8. | 75 | \a w1, \a w2, \a b1, \a b2, \a b3, \a b4, \a b5, \a b6, \a b7, \a |
76 | b8. | ||
70 | 77 | ||
71 | Example: | 78 | Example: |
72 | \code | 79 | \code |
73 | // {67C8770B-44F1-410A-AB9A-F9B5446F13EE} | 80 | // {67C8770B-44F1-410A-AB9A-F9B5446F13EE} |
74 | QUuid IID_MyInterface( 0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee ) | 81 | QUuid IID_MyInterface( 0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee ) |
75 | \endcode | 82 | \endcode |
76 | */ | 83 | */ |
77 | 84 | ||
78 | /*! | 85 | /*! |
79 | \fn QUuid::QUuid( const QUuid &orig ) | 86 | \fn QUuid::QUuid( const QUuid &orig ) |
80 | 87 | ||
81 | Creates a copy of the QUuid \a orig. | 88 | Creates a copy of the QUuid \a orig. |
82 | */ | 89 | */ |
83 | #ifndef QT_NO_QUUID_STRING | 90 | #ifndef QT_NO_QUUID_STRING |
84 | /*! | 91 | /*! |
85 | Creates a QUuid object from the string \a text. Right now, the function | 92 | Creates a QUuid object from the string \a text. The function can |
86 | can only convert the format {12345678-1234-1234-1234-123456789ABC} and | 93 | only convert a string in the format |
87 | will create the null UUID when the conversion fails. | 94 | {HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH} (where 'H' stands for a hex |
95 | digit). If the conversion fails a null UUID is created. | ||
88 | */ | 96 | */ |
89 | QUuid::QUuid( const QString &text ) | 97 | QUuid::QUuid( const QString &text ) |
90 | { | 98 | { |
91 | bool ok; | 99 | bool ok; |
100 | if ( text.isEmpty() ) { | ||
101 | *this = QUuid(); | ||
102 | return; | ||
103 | } | ||
92 | QString temp = text.upper(); | 104 | QString temp = text.upper(); |
105 | if ( temp[0] != '{' ) | ||
106 | temp = "{" + text; | ||
107 | if ( text[(int)text.length()-1] != '}' ) | ||
108 | temp += "}"; | ||
93 | 109 | ||
94 | data1 = temp.mid( 1, 8 ).toULong( &ok, 16 ); | 110 | data1 = temp.mid( 1, 8 ).toULong( &ok, 16 ); |
95 | if ( !ok ) { | 111 | if ( !ok ) { |
96 | *this = QUuid(); | 112 | *this = QUuid(); |
97 | return; | 113 | return; |
98 | } | 114 | } |
99 | 115 | ||
100 | data2 = temp.mid( 10, 4 ).toUInt( &ok, 16 ); | 116 | data2 = temp.mid( 10, 4 ).toUInt( &ok, 16 ); |
@@ -122,53 +138,55 @@ QUuid::QUuid( const QString &text ) | |||
122 | if ( !ok ) { | 138 | if ( !ok ) { |
123 | *this = QUuid(); | 139 | *this = QUuid(); |
124 | return; | 140 | return; |
125 | } | 141 | } |
126 | } | 142 | } |
127 | } | 143 | } |
128 | 144 | ||
129 | /*! | 145 | /*! |
130 | \overload | 146 | \internal |
131 | */ | 147 | */ |
132 | QUuid::QUuid( const char *text ) | 148 | QUuid::QUuid( const char *text ) |
133 | { | 149 | { |
134 | *this = QUuid( QString(text) ); | 150 | *this = QUuid( QString(text) ); |
135 | } | 151 | } |
136 | #endif | 152 | #endif |
137 | /*! | 153 | /*! |
138 | \fn QUuid QUuid::operator=(const QUuid &uuid ) | 154 | \fn QUuid QUuid::operator=(const QUuid &uuid ) |
139 | 155 | ||
140 | Assigns the value of \a uuid to this QUuid object. | 156 | Assigns the value of \a uuid to this QUuid object. |
141 | */ | 157 | */ |
142 | 158 | ||
143 | /*! | 159 | /*! |
144 | \fn bool QUuid::operator==(const QUuid &other) const | 160 | \fn bool QUuid::operator==(const QUuid &other) const |
145 | 161 | ||
146 | Returns TRUE if this QUuid and the \a other QUuid are identical, otherwise returns FALSE. | 162 | Returns TRUE if this QUuid and the \a other QUuid are identical; |
163 | otherwise returns FALSE. | ||
147 | */ | 164 | */ |
148 | 165 | ||
149 | /*! | 166 | /*! |
150 | \fn bool QUuid::operator!=(const QUuid &other) const | 167 | \fn bool QUuid::operator!=(const QUuid &other) const |
151 | 168 | ||
152 | Returns TRUE if this QUuid and the \a other QUuid are different, otherwise returns FALSE. | 169 | Returns TRUE if this QUuid and the \a other QUuid are different; |
170 | otherwise returns FALSE. | ||
153 | */ | 171 | */ |
154 | #ifndef QT_NO_QUUID_STRING | 172 | #ifndef QT_NO_QUUID_STRING |
155 | /*! | 173 | /*! |
156 | \fn QUuid::operator QString() const | 174 | \fn QUuid::operator QString() const |
157 | 175 | ||
158 | Returns the string representation of the uuid. | 176 | Returns the string representation of the uuid. |
159 | 177 | ||
160 | \sa toString() | 178 | \sa toString() |
161 | */ | 179 | */ |
162 | 180 | ||
163 | /*! | 181 | /*! |
164 | QString QUuid::toString() const | 182 | QString QUuid::toString() const |
165 | 183 | ||
166 | Returns the string representation of the uuid. | 184 | Returns the string representation of the uuid. |
167 | */ | 185 | */ |
168 | QString QUuid::toString() const | 186 | QString QUuid::toString() const |
169 | { | 187 | { |
170 | QString result; | 188 | QString result; |
171 | 189 | ||
172 | result = "{" + QString::number( data1, 16 ).rightJustify( 8, '0' ) + "-"; | 190 | result = "{" + QString::number( data1, 16 ).rightJustify( 8, '0' ) + "-"; |
173 | result += QString::number( (int)data2, 16 ).rightJustify( 4, '0' ) + "-"; | 191 | result += QString::number( (int)data2, 16 ).rightJustify( 4, '0' ) + "-"; |
174 | result += QString::number( (int)data3, 16 ).rightJustify( 4, '0' ) + "-"; | 192 | result += QString::number( (int)data3, 16 ).rightJustify( 4, '0' ) + "-"; |
@@ -178,32 +196,32 @@ QString QUuid::toString() const | |||
178 | result += QString::number( (int)data4[i], 16 ).rightJustify( 2, '0' ); | 196 | result += QString::number( (int)data4[i], 16 ).rightJustify( 2, '0' ); |
179 | 197 | ||
180 | return result + "}"; | 198 | return result + "}"; |
181 | } | 199 | } |
182 | #endif | 200 | #endif |
183 | 201 | ||
184 | #ifndef QT_NO_DATASTREAM | 202 | #ifndef QT_NO_DATASTREAM |
185 | /*! | 203 | /*! |
186 | \relates QUuid | 204 | \relates QUuid |
187 | Writes the \a id to the datastream \a s. | 205 | Writes the uuid \a id to the datastream \a s. |
188 | */ | 206 | */ |
189 | QDataStream &operator<<( QDataStream &s, const QUuid &id ) | 207 | QDataStream &operator<<( QDataStream &s, const QUuid &id ) |
190 | { | 208 | { |
191 | s << (Q_UINT32)id.data1; | 209 | s << (Q_UINT32)id.data1; |
192 | s << (Q_UINT16)id.data2; | 210 | s << (Q_UINT16)id.data2; |
193 | s << (Q_UINT16)id.data3; | 211 | s << (Q_UINT16)id.data3; |
194 | for (int i = 0; i < 8; i++ ) | 212 | for (int i = 0; i < 8; i++ ) |
195 | s << (Q_UINT8)id.data4[i]; | 213 | s << (Q_UINT8)id.data4[i]; |
196 | return s; | 214 | return s; |
197 | } | 215 | } |
198 | 216 | ||
199 | /*! | 217 | /*! |
200 | \relates QUuid | 218 | \relates QUuid |
201 | Reads a universally unique id from from the stream \a s into \a id. | 219 | Reads uuid from from the stream \a s into \a id. |
202 | */ | 220 | */ |
203 | QDataStream &operator>>( QDataStream &s, QUuid &id ) | 221 | QDataStream &operator>>( QDataStream &s, QUuid &id ) |
204 | { | 222 | { |
205 | Q_UINT32 u32; | 223 | Q_UINT32 u32; |
206 | Q_UINT16 u16; | 224 | Q_UINT16 u16; |
207 | Q_UINT8 u8; | 225 | Q_UINT8 u8; |
208 | s >> u32; | 226 | s >> u32; |
209 | id.data1 = u32; | 227 | id.data1 = u32; |
@@ -215,16 +233,186 @@ QDataStream &operator>>( QDataStream &s, QUuid &id ) | |||
215 | s >> u8; | 233 | s >> u8; |
216 | id.data4[i] = u8; | 234 | id.data4[i] = u8; |
217 | } | 235 | } |
218 | return s; | 236 | return s; |
219 | } | 237 | } |
220 | #endif | 238 | #endif |
221 | 239 | ||
222 | /*! | 240 | /*! |
223 | Returns TRUE if this is the null UUID {00000000-0000-0000-0000-000000000000}, otherwise returns FALSE. | 241 | Returns TRUE if this is the null UUID |
242 | {00000000-0000-0000-0000-000000000000}; otherwise returns FALSE. | ||
224 | */ | 243 | */ |
225 | bool QUuid::isNull() const | 244 | bool QUuid::isNull() const |
226 | { | 245 | { |
227 | return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && | 246 | return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && |
228 | data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && | 247 | data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && |
229 | data1 == 0 && data2 == 0 && data3 == 0; | 248 | data1 == 0 && data2 == 0 && data3 == 0; |
230 | } | 249 | } |
250 | |||
251 | /*! | ||
252 | \enum QUuid::Variant | ||
253 | |||
254 | This enum defines the variant of the UUID, which is the scheme | ||
255 | which defines the layout of the 128-bits value. | ||
256 | |||
257 | \value VarUnknown Variant is unknown | ||
258 | \value NCS Reserved for NCS (Network Computing System) backward compatibility | ||
259 | \value DCE Distributed Computing Environment, the scheme used by QUuid | ||
260 | \value Microsoft Reserved for Microsoft backward compatibility (GUID) | ||
261 | \value Reserved Reserved for future definition | ||
262 | */ | ||
263 | |||
264 | /*! | ||
265 | \enum QUuid::Version | ||
266 | |||
267 | This enum defines the version of the UUID. | ||
268 | |||
269 | \value VerUnknown Version is unknown | ||
270 | \value Time Time-based, by using timestamp, clock sequence, and | ||
271 | MAC network card address (if available) for the node sections | ||
272 | \value EmbeddedPOSIX DCE Security version, with embedded POSIX UUIDs | ||
273 | \value Name Name-based, by using values from a name for all sections | ||
274 | \value Random Random-based, by using random numbers for all sections | ||
275 | */ | ||
276 | |||
277 | /*! | ||
278 | \fn QUuid::Variant QUuid::variant() const | ||
279 | |||
280 | Returns the variant of the UUID. | ||
281 | The null UUID is considered to be of an unknown variant. | ||
282 | |||
283 | \sa version() | ||
284 | */ | ||
285 | QUuid::Variant QUuid::variant() const | ||
286 | { | ||
287 | if ( isNull() ) | ||
288 | return VarUnknown; | ||
289 | // Check the 3 MSB of data4[0] | ||
290 | if ( (data4[0] & 0x80) == 0x00 ) return NCS; | ||
291 | else if ( (data4[0] & 0xC0) == 0x80 ) return DCE; | ||
292 | else if ( (data4[0] & 0xE0) == 0xC0 ) return Microsoft; | ||
293 | else if ( (data4[0] & 0xE0) == 0xE0 ) return Reserved; | ||
294 | return VarUnknown; | ||
295 | } | ||
296 | |||
297 | /*! | ||
298 | \fn QUuid::Version QUuid::version() const | ||
299 | |||
300 | Returns the version of the UUID, if the UUID is of the DCE | ||
301 | variant; otherwise returns VerUnknown. | ||
302 | |||
303 | \sa variant() | ||
304 | */ | ||
305 | QUuid::Version QUuid::version() const | ||
306 | { | ||
307 | // Check the 4 MSB of data3 | ||
308 | Version ver = (Version)(data3>>12); | ||
309 | if ( isNull() | ||
310 | || (variant() != DCE) | ||
311 | || ver < Time | ||
312 | || ver > Random ) | ||
313 | return VerUnknown; | ||
314 | return ver; | ||
315 | } | ||
316 | |||
317 | /*! | ||
318 | \fn bool QUuid::operator<(const QUuid &other) const | ||
319 | |||
320 | Returns TRUE if this QUuid is of the same variant, | ||
321 | and lexicographically before the \a other QUuid; | ||
322 | otherwise returns FALSE. | ||
323 | |||
324 | \sa variant() | ||
325 | */ | ||
326 | #define ISLESS(f1, f2) if (f1!=f2) return (f1<f2); | ||
327 | bool QUuid::operator<(const QUuid &other ) const | ||
328 | { | ||
329 | if ( variant() != other.variant() ) | ||
330 | return FALSE; | ||
331 | |||
332 | ISLESS( data1, other.data1 ); | ||
333 | ISLESS( data2, other.data2 ); | ||
334 | ISLESS( data3, other.data3 ); | ||
335 | for ( int n = 0; n < 8; n++ ) { | ||
336 | ISLESS( data4[n], other.data4[n] ); | ||
337 | } | ||
338 | return FALSE; | ||
339 | } | ||
340 | |||
341 | /*! | ||
342 | \fn bool QUuid::operator>(const QUuid &other) const | ||
343 | |||
344 | Returns TRUE if this QUuid is of the same variant, | ||
345 | and lexicographically after the \a other QUuid; | ||
346 | otherwise returns FALSE. | ||
347 | |||
348 | \sa variant() | ||
349 | */ | ||
350 | #define ISMORE(f1, f2) if (f1!=f2) return (f1>f2); | ||
351 | bool QUuid::operator>(const QUuid &other ) const | ||
352 | { | ||
353 | if ( variant() != other.variant() ) | ||
354 | return FALSE; | ||
355 | |||
356 | ISMORE( data1, other.data1 ); | ||
357 | ISMORE( data2, other.data2 ); | ||
358 | ISMORE( data3, other.data3 ); | ||
359 | for ( int n = 0; n < 8; n++ ) { | ||
360 | ISMORE( data4[n], other.data4[n] ); | ||
361 | } | ||
362 | return FALSE; | ||
363 | } | ||
364 | |||
365 | /*! | ||
366 | \fn QUuid QUuid::createUuid() | ||
367 | |||
368 | Returns a new UUID of \c DCE variant, and \c Random type. The | ||
369 | UUIDs generated are based on the platform specific pseudo-random | ||
370 | generator, which is usually not a cryptographic-quality random | ||
371 | number generator. Therefore, a UUID is not guaranteed to be unique | ||
372 | cross application instances. | ||
373 | |||
374 | On Windows, the new UUID is extremely likely to be unique on the | ||
375 | same or any other system, networked or not. | ||
376 | |||
377 | \sa variant(), version() | ||
378 | */ | ||
379 | #if defined(Q_OS_WIN32) | ||
380 | #include <objbase.h> // For CoCreateGuid | ||
381 | QUuid QUuid::createUuid() | ||
382 | { | ||
383 | GUID guid; | ||
384 | CoCreateGuid( &guid ); | ||
385 | QUuid result = guid; | ||
386 | return result; | ||
387 | } | ||
388 | #else // !Q_OS_WIN32 | ||
389 | #include "qdatetime.h" | ||
390 | #include "stdlib.h" // For srand/rand | ||
391 | QUuid QUuid::createUuid() | ||
392 | { | ||
393 | static const int intbits = sizeof(int)*8; | ||
394 | static int randbits = 0; | ||
395 | if ( !randbits ) { | ||
396 | int max = RAND_MAX; | ||
397 | do { ++randbits; } while ( (max=max>>1) ); | ||
398 | srand( (uint)QDateTime::currentDateTime().toTime_t() ); | ||
399 | rand(); // Skip first | ||
400 | } | ||
401 | |||
402 | QUuid result; | ||
403 | uint *data = &(result.data1); | ||
404 | int chunks = 16 / sizeof(uint); | ||
405 | while ( chunks-- ) { | ||
406 | uint randNumber = 0; | ||
407 | for ( int filled = 0; filled < intbits; filled += randbits ) | ||
408 | randNumber |= rand()<<filled; | ||
409 | *(data+chunks) = randNumber; | ||
410 | } | ||
411 | |||
412 | result.data4[0] = (result.data4[0] & 0x3F) | 0x80;// UV_DCE | ||
413 | result.data3 = (result.data3 & 0x0FFF) | 0x4000;// UV_Random | ||
414 | |||
415 | return result; | ||
416 | } | ||
417 | #endif // !Q_OS_WIN32 | ||
418 | |||