aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/uuid.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/unstable/uuid.cpp150
1 files changed, 75 insertions, 75 deletions
diff --git a/src/unstable/uuid.cpp b/src/unstable/uuid.cpp
index aef8bc0..5400f8f 100644
--- a/src/unstable/uuid.cpp
+++ b/src/unstable/uuid.cpp
@@ -18,25 +18,25 @@
18 18
19Bu::Uuid::Uuid() 19Bu::Uuid::Uuid()
20{ 20{
21 clear(); 21 clear();
22} 22}
23 23
24Bu::Uuid::Uuid( const Uuid &src ) 24Bu::Uuid::Uuid( const Uuid &src )
25{ 25{
26 memcpy( data, src.data, 16 ); 26 memcpy( data, src.data, 16 );
27} 27}
28 28
29Bu::Uuid::Uuid( const Bu::String &sSrc ) 29Bu::Uuid::Uuid( const Bu::String &sSrc )
30{ 30{
31 if( sSrc.getSize() == 16 ) 31 if( sSrc.getSize() == 16 )
32 { 32 {
33 memcpy( data, sSrc.getStr(), 16 ); 33 memcpy( data, sSrc.getStr(), 16 );
34 } 34 }
35 else if( sSrc.getSize() == 36 ) 35 else if( sSrc.getSize() == 36 )
36 { 36 {
37 // Parse it 37 // Parse it
38 set( sSrc ); 38 set( sSrc );
39 } 39 }
40} 40}
41 41
42Bu::Uuid::~Uuid() 42Bu::Uuid::~Uuid()
@@ -45,130 +45,130 @@ Bu::Uuid::~Uuid()
45 45
46Bu::String Bu::Uuid::toRawString() const 46Bu::String Bu::Uuid::toRawString() const
47{ 47{
48 return Bu::String( (char *)data, 16 ); 48 return Bu::String( (char *)data, 16 );
49} 49}
50 50
51Bu::String Bu::Uuid::toString() const 51Bu::String Bu::Uuid::toString() const
52{ 52{
53 Bu::MemBuf mb; 53 Bu::MemBuf mb;
54 Bu::Formatter f( mb ); 54 Bu::Formatter f( mb );
55 55
56 for( int j = 0; j < 16; j++ ) 56 for( int j = 0; j < 16; j++ )
57 { 57 {
58 if( j == 4 || j == 6 || j == 8 || j == 10 ) 58 if( j == 4 || j == 6 || j == 8 || j == 10 )
59 f << '-'; 59 f << '-';
60 f << Bu::Fmt::hex(2).caps(false) << (unsigned int)data[j]; 60 f << Bu::Fmt::hex(2).caps(false) << (unsigned int)data[j];
61 } 61 }
62 62
63 return mb.getString(); 63 return mb.getString();
64} 64}
65 65
66Bu::String Bu::Uuid::toUrn() const 66Bu::String Bu::Uuid::toUrn() const
67{ 67{
68 return "urn:uuid:" + toString(); 68 return "urn:uuid:" + toString();
69} 69}
70 70
71int Bu::Uuid::getVersion() 71int Bu::Uuid::getVersion()
72{ 72{
73 return (data[6]&((8|4|2|1)<<4))>>4; 73 return (data[6]&((8|4|2|1)<<4))>>4;
74} 74}
75 75
76#define msb( i ) (1<<(7-i)) 76#define msb( i ) (1<<(7-i))
77 77
78void Bu::Uuid::clear() 78void Bu::Uuid::clear()
79{ 79{
80 data[7] = msb(0); 80 data[7] = msb(0);
81} 81}
82 82
83Bu::Uuid Bu::Uuid::generate( Bu::Uuid::Type eType ) 83Bu::Uuid Bu::Uuid::generate( Bu::Uuid::Type eType )
84{ 84{
85 switch( eType ) 85 switch( eType )
86 { 86 {
87 case Version1: 87 case Version1:
88 case Version2: 88 case Version2:
89 case Version3: 89 case Version3:
90 case Version4: 90 case Version4:
91 case Version5: 91 case Version5:
92 default: 92 default:
93 case System: 93 case System:
94#if defined(linux) 94#if defined(linux)
95 Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); 95 Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read );
96 char dat[36]; 96 char dat[36];
97 fIn.read( dat, 36 ); 97 fIn.read( dat, 36 );
98 Uuid id; 98 Uuid id;
99 id.set( dat ); 99 id.set( dat );
100 return id; 100 return id;
101#elif defined(WIN32) 101#elif defined(WIN32)
102 UUID uuid; 102 UUID uuid;
103 UuidCreate( &uuid ); 103 UuidCreate( &uuid );
104 Uuid id; 104 Uuid id;
105 id.data[0] = ((unsigned char *)&uuid.Data1)[3]; 105 id.data[0] = ((unsigned char *)&uuid.Data1)[3];
106 id.data[1] = ((unsigned char *)&uuid.Data1)[2]; 106 id.data[1] = ((unsigned char *)&uuid.Data1)[2];
107 id.data[2] = ((unsigned char *)&uuid.Data1)[1]; 107 id.data[2] = ((unsigned char *)&uuid.Data1)[1];
108 id.data[3] = ((unsigned char *)&uuid.Data1)[0]; 108 id.data[3] = ((unsigned char *)&uuid.Data1)[0];
109 id.data[4] = ((unsigned char *)&uuid.Data2)[1]; 109 id.data[4] = ((unsigned char *)&uuid.Data2)[1];
110 id.data[5] = ((unsigned char *)&uuid.Data2)[0]; 110 id.data[5] = ((unsigned char *)&uuid.Data2)[0];
111 id.data[6] = ((unsigned char *)&uuid.Data3)[1]; 111 id.data[6] = ((unsigned char *)&uuid.Data3)[1];
112 id.data[7] = ((unsigned char *)&uuid.Data3)[0]; 112 id.data[7] = ((unsigned char *)&uuid.Data3)[0];
113 memcpy( id.data+8, uuid.Data4, 8 ); 113 memcpy( id.data+8, uuid.Data4, 8 );
114 114
115 return id; 115 return id;
116#else 116#else
117# error We should be using one of the other fallbacks, but your platform is not supported yet. Sorry. 117# error We should be using one of the other fallbacks, but your platform is not supported yet. Sorry.
118#endif 118#endif
119 } 119 }
120} 120}
121 121
122void Bu::Uuid::set( const Bu::String &sSrc ) 122void Bu::Uuid::set( const Bu::String &sSrc )
123{ 123{
124 const char *dat = sSrc.getStr(); 124 const char *dat = sSrc.getStr();
125 int iNibble = 0; 125 int iNibble = 0;
126 memset( data, 0, 16 ); 126 memset( data, 0, 16 );
127 for( int j = 0; j < 36; j++ ) 127 for( int j = 0; j < 36; j++ )
128 { 128 {
129 if( dat[j] == '-' ) 129 if( dat[j] == '-' )
130 continue; 130 continue;
131 unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10); 131 unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10);
132 data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c); 132 data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c);
133 iNibble++; 133 iNibble++;
134 } 134 }
135} 135}
136 136
137bool Bu::Uuid::operator==( const Uuid &rhs ) const 137bool Bu::Uuid::operator==( const Uuid &rhs ) const
138{ 138{
139 return memcmp( data, rhs.data, 16 ) == 0; 139 return memcmp( data, rhs.data, 16 ) == 0;
140} 140}
141 141
142bool Bu::Uuid::operator!=( const Uuid &rhs ) const 142bool Bu::Uuid::operator!=( const Uuid &rhs ) const
143{ 143{
144 return !(*this == rhs); 144 return !(*this == rhs);
145} 145}
146 146
147Bu::Uuid &Bu::Uuid::operator=( const Uuid &rhs ) 147Bu::Uuid &Bu::Uuid::operator=( const Uuid &rhs )
148{ 148{
149 memcpy( data, rhs.data, 16 ); 149 memcpy( data, rhs.data, 16 );
150 return *this; 150 return *this;
151} 151}
152 152
153template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k ) 153template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k )
154{ 154{
155 return __calcHashCode<String>( k.toRawString() ); 155 return __calcHashCode<String>( k.toRawString() );
156} 156}
157 157
158template<> bool Bu::__cmpHashKeys<Bu::Uuid>( const Bu::Uuid &a, const Bu::Uuid &b ) 158template<> bool Bu::__cmpHashKeys<Bu::Uuid>( const Bu::Uuid &a, const Bu::Uuid &b )
159{ 159{
160 return a == b; 160 return a == b;
161} 161}
162 162
163Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, Uuid &u ) 163Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, Uuid &u )
164{ 164{
165 ar.read( u.data, 16 ); 165 ar.read( u.data, 16 );
166 return ar; 166 return ar;
167} 167}
168 168
169Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, const Uuid &u ) 169Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, const Uuid &u )
170{ 170{
171 ar.write( u.data, 16 ); 171 ar.write( u.data, 16 );
172 return ar; 172 return ar;
173} 173}
174 174