Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/UUID.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
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   // Set the two most significant bits (bits 6 and 7) of the ClockSeqHiAndReserved to zero and one, respectively.
00050   mClockSeqHiAndReserved &= 0x3F;
00051   mClockSeqHiAndReserved |= 0x80;
00052 
00053   // Set the four most significant bits (bits 12 through 15) of the TimeHiAndVersion field to the 4-bit version number from Section 4.1.3.
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   // avoid reallocation
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   // 01234567890123456789012345678901234567
00073   // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
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   // TimeLow:
00086   for(int i=0; i<8; ++i)
00087     guid_str[i+1] = hex[ (mTimeLow >> (28-4*i)) & 0xF];
00088 
00089   // TimeMid:
00090   for(int i=0; i<4; ++i)
00091     guid_str[i+10] = hex[ (mTimeMid >> (12-4*i)) & 0xF];
00092 
00093   // TimeHiAndVersion:
00094   for(int i=0; i<4; ++i)
00095     guid_str[i+15] = hex[ (mTimeHiAndVersion >> (12-4*i)) & 0xF];
00096 
00097   // ClockSeqHiAndReserved:
00098   guid_str[20] = hex[ (mClockSeqHiAndReserved >> (4-4*0)) & 0xF];
00099   guid_str[21] = hex[ (mClockSeqHiAndReserved >> (4-4*1)) & 0xF];
00100 
00101   // ClockSeqLow:
00102   guid_str[22] = hex[ (mClockSeqLow >> (4-4*0)) & 0xF];
00103   guid_str[23] = hex[ (mClockSeqLow >> (4-4*1)) & 0xF];  
00104 
00105   // Node:
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   // equivalent to the following formatting:
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   // to lower case, check hex numbers, convert to nybbles
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   // 01234567890123456789012345678901234567
00180   // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
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 

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:32.
Permission is granted to use this page to write and publish articles regarding Visualization Library.