Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

quuid.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** 
00003 **
00004 ** Implementation of QUuid class
00005 **
00006 ** Copyright (C) 2000-2001 Trolltech AS.  All rights reserved.
00007 **
00008 ** This file is part of the tools module of the Qt GUI Toolkit.
00009 **
00010 ** This file may be distributed under the terms of the Q Public License
00011 ** as defined by Trolltech AS of Norway and appearing in the file
00012 ** LICENSE.QPL included in the packaging of this file.
00013 **
00014 ** This file may be distributed and/or modified under the terms of the
00015 ** GNU General Public License version 2 as published by the Free Software
00016 ** Foundation and appearing in the file LICENSE.GPL included in the
00017 ** packaging of this file.
00018 **
00019 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00020 ** licenses may use this file in accordance with the Qt Commercial License
00021 ** Agreement provided with the Software.
00022 **
00023 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00024 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00025 **
00026 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00027 **   information about Qt Commercial License Agreements.
00028 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00029 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00030 **
00031 ** Contact info@trolltech.com if any conditions of this licensing are
00032 ** not clear to you.
00033 **
00034 **********************************************************************/
00035 
00036 #include "quuid.h"
00037 
00038 #include "qdatastream.h"
00039 
00090 #ifndef QT_NO_QUUID_STRING
00091 
00097 QUuid::QUuid( const QString &text )
00098 {
00099     bool ok;
00100     if ( text.isEmpty() ) {
00101         *this = QUuid();
00102         return;
00103     }
00104     QString temp = text.upper();
00105     if ( temp[0] != '{' )
00106         temp = "{" + text;
00107     if ( text[(int)text.length()-1] != '}' )
00108         temp += "}";
00109 
00110     data1 = temp.mid( 1, 8 ).toULong( &ok, 16 );
00111     if ( !ok ) {
00112         *this = QUuid();
00113         return;
00114     }
00115 
00116     data2 = temp.mid( 10, 4 ).toUInt( &ok, 16 );
00117     if ( !ok ) {
00118         *this = QUuid();
00119         return;
00120     }
00121     data3 = temp.mid( 15, 4 ).toUInt( &ok, 16 );
00122     if ( !ok ) {
00123         *this = QUuid();
00124         return;
00125     }
00126     data4[0] = temp.mid( 20, 2 ).toUInt( &ok, 16 );
00127     if ( !ok ) {
00128         *this = QUuid();
00129         return;
00130     }
00131     data4[1] = temp.mid( 22, 2 ).toUInt( &ok, 16 );
00132     if ( !ok ) {
00133         *this = QUuid();
00134         return;
00135     }
00136     for ( int i = 2; i<8; i++ ) {
00137         data4[i] = temp.mid( 25 + (i-2)*2, 2 ).toUShort( &ok, 16 );
00138         if ( !ok ) {
00139             *this = QUuid();
00140             return;
00141         }
00142     }
00143 }
00144 
00148 QUuid::QUuid( const char *text )
00149 {
00150     *this = QUuid( QString(text) );
00151 }
00152 #endif
00153 
00172 #ifndef QT_NO_QUUID_STRING
00173 
00186 QString QUuid::toString() const
00187 {
00188     QString result;
00189 
00190     result = "{" + QString::number( data1, 16 ).rightJustify( 8, '0' ) + "-";
00191     result += QString::number( (int)data2, 16 ).rightJustify( 4, '0' ) + "-";
00192     result += QString::number( (int)data3, 16 ).rightJustify( 4, '0' ) + "-";
00193     result += QString::number( (int)data4[0], 16 ).rightJustify( 2, '0' );
00194     result += QString::number( (int)data4[1], 16 ).rightJustify( 2, '0' ) + "-";
00195     for ( int i = 2; i < 8; i++ )
00196         result += QString::number( (int)data4[i], 16 ).rightJustify( 2, '0' );
00197 
00198     return result + "}";
00199 }
00200 #endif
00201 
00202 #ifndef QT_NO_DATASTREAM
00203 
00207 QDataStream &operator<<( QDataStream &s, const QUuid &id )
00208 {
00209     s << (Q_UINT32)id.data1;
00210     s << (Q_UINT16)id.data2;
00211     s << (Q_UINT16)id.data3;
00212     for (int i = 0; i < 8; i++ )
00213         s << (Q_UINT8)id.data4[i];
00214     return s;
00215 }
00216 
00221 QDataStream &operator>>( QDataStream &s, QUuid &id )
00222 {
00223     Q_UINT32 u32;
00224     Q_UINT16 u16;
00225     Q_UINT8 u8;
00226     s >> u32;
00227     id.data1 = u32;
00228     s >> u16;
00229     id.data2 = u16;
00230     s >> u16;
00231     id.data3 = u16;
00232     for (int i = 0; i < 8; i++ ) {
00233         s >> u8;
00234         id.data4[i] = u8;
00235     }
00236     return s;
00237 }
00238 #endif
00239 
00244 bool QUuid::isNull() const
00245 {
00246     return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 &&
00247            data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 &&
00248            data1 == 0 && data2 == 0 && data3 == 0;
00249 }
00250 
00285 QUuid::Variant QUuid::variant() const
00286 {
00287     if ( isNull() )
00288         return VarUnknown;
00289     // Check the 3 MSB of data4[0]
00290     if ( (data4[0] & 0x80) == 0x00 ) return NCS;
00291     else if ( (data4[0] & 0xC0) == 0x80 ) return DCE;
00292     else if ( (data4[0] & 0xE0) == 0xC0 ) return Microsoft;
00293     else if ( (data4[0] & 0xE0) == 0xE0 ) return Reserved;
00294     return VarUnknown;
00295 }
00296 
00305 QUuid::Version QUuid::version() const
00306 {
00307     // Check the 4 MSB of data3
00308     Version ver = (Version)(data3>>12);
00309     if ( isNull() 
00310          || (variant() != DCE)
00311          || ver < Time 
00312          || ver > Random )
00313         return VerUnknown;
00314     return ver;
00315 }
00316 
00326 #define ISLESS(f1, f2) if (f1!=f2) return (f1<f2);
00327 bool QUuid::operator<(const QUuid &other ) const
00328 {
00329     if ( variant() != other.variant() )
00330         return FALSE;
00331 
00332     ISLESS( data1, other.data1 );
00333     ISLESS( data2, other.data2 );
00334     ISLESS( data3, other.data3 );
00335     for ( int n = 0; n < 8; n++ ) {
00336         ISLESS( data4[n], other.data4[n] );
00337     }
00338     return FALSE;
00339 }
00340 
00350 #define ISMORE(f1, f2) if (f1!=f2) return (f1>f2);
00351 bool QUuid::operator>(const QUuid &other ) const
00352 {
00353     if ( variant() != other.variant() )
00354         return FALSE;
00355 
00356     ISMORE( data1, other.data1 );
00357     ISMORE( data2, other.data2 );
00358     ISMORE( data3, other.data3 );
00359     for ( int n = 0; n < 8; n++ ) {
00360         ISMORE( data4[n], other.data4[n] );
00361     }
00362     return FALSE;
00363 }
00364 
00379 #if defined(Q_OS_WIN32)
00380 #include <objbase.h> // For CoCreateGuid
00381 QUuid QUuid::createUuid()
00382 {
00383     GUID guid;
00384     CoCreateGuid( &guid );
00385     QUuid result = guid;
00386     return result;
00387 }
00388 #else // !Q_OS_WIN32
00389 #include "qdatetime.h"
00390 #include "stdlib.h" // For srand/rand
00391 QUuid QUuid::createUuid()
00392 {
00393     static const int intbits = sizeof(int)*8;
00394     static int randbits = 0;
00395     if ( !randbits ) {
00396         int max = RAND_MAX;
00397         do { ++randbits; } while ( (max=max>>1) );
00398         srand( (uint)QDateTime::currentDateTime().toTime_t() );
00399         rand(); // Skip first
00400     }
00401 
00402     QUuid result;
00403     uint *data = &(result.data1);
00404     int chunks = 16 / sizeof(uint);
00405     while ( chunks-- ) {
00406         uint randNumber = 0;
00407         for ( int filled = 0; filled < intbits; filled += randbits )
00408             randNumber |= rand()<<filled;
00409          *(data+chunks) = randNumber;
00410     }
00411 
00412     result.data4[0] = (result.data4[0] & 0x3F) | 0x80;  // UV_DCE
00413     result.data3 = (result.data3 & 0x0FFF) | 0x4000;    // UV_Random
00414 
00415     return result;
00416 }
00417 #endif // !Q_OS_WIN32
00418 

Generated on Sat Nov 5 16:16:41 2005 for OPIE by  doxygen 1.4.2