CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/FWCore/Utilities/src/Guid.cc

Go to the documentation of this file.
00001 //      ====================================================================
00002 //
00003 //      Guid.cpp
00004 //      --------------------------------------------------------------------
00005 //
00006 //      Package   : Persistent Guid to identify objects in the persistent
00007 //              world.
00008 //
00009 //      Author    : Markus Frank
00010 //      
00011 //      ====================================================================
00012 #include "Guid.h"
00013 #include <cstdio>
00014 #include <cstring>
00015 #include "uuid/uuid.h"
00016 
00017 namespace edm {
00018   static char const* fmt_Guid = 
00019     "%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
00020   
00021   //{ 0x0,0x0,0x0,{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}};
00022   static Guid const
00023     clid_null(std::string("00000000-0000-0000-0000-000000000000"));
00024   
00025   Guid const& Guid::null() {
00026     return clid_null;
00027   }
00028   
00030   void Guid::create(Guid& guid)   {
00031     uuid_t me_;
00032     ::uuid_generate_time(me_);
00033     unsigned int   *d1 = reinterpret_cast<unsigned int*>(me_);
00034     unsigned short *d2 = reinterpret_cast<unsigned short*>(me_+4);
00035     unsigned short *d3 = reinterpret_cast<unsigned short*>(me_+6);
00036     guid.Data1 = *d1;
00037     guid.Data2 = *d2;
00038     guid.Data3 = *d3;
00039     for (int i = 0; i < 8; ++i){
00040       guid.Data4[i]=me_[i+8];
00041     }
00042   }
00043   
00044   std::string const Guid::toString() const {
00045     char text[128];
00046     ::sprintf(text, fmt_Guid,
00047               Data1, Data2, Data3, 
00048               Data4[0], Data4[1], Data4[2], Data4[3], 
00049               Data4[4], Data4[5], Data4[6], Data4[7]);
00050     return text;
00051   }
00052   
00053   Guid const& Guid::fromString(std::string const& source) {
00054     // Note: This looks funny, but the specs for sscanf formats say
00055     //       that the space of a pointer in the ellipsis may only be
00056     //       integer or short. Hence one has to reserve a bit more space
00057     //       otherwise the stack gets corrupted.
00058     unsigned char d[8];
00059     ::sscanf( source.c_str(), fmt_Guid, &Data1, &Data2, &Data3, 
00060               &Data4[0], &Data4[1], &Data4[2], &Data4[3], &d[0], &d[1], &d[2], &d[3]);
00061     //*(int*)&Data4[4] = *(int*)d;
00062     unsigned int      * p = reinterpret_cast<unsigned int *>(&Data4[4]);
00063     unsigned int const* q = reinterpret_cast<unsigned int const*>(&d[0]);
00064     *p = *q;
00065     return *this;
00066   }
00067   
00068   bool Guid::operator<(Guid const& g) const {
00069     return ::memcmp(&g.Data1, &Data1, 16) < 0;
00070   }
00071 }