summaryrefslogtreecommitdiff
path: root/src/uuid.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/uuid.cpp')
-rw-r--r--src/uuid.cpp34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/uuid.cpp b/src/uuid.cpp
index d34a989..bcaf377 100644
--- a/src/uuid.cpp
+++ b/src/uuid.cpp
@@ -21,16 +21,29 @@ Bu::Uuid::Uuid( const Uuid &src )
21 memcpy( data, src.data, 16 ); 21 memcpy( data, src.data, 16 );
22} 22}
23 23
24Bu::Uuid::Uuid( const Bu::String &sSrc )
25{
26 if( sSrc.getSize() == 16 )
27 {
28 memcpy( data, sSrc.getStr(), 16 );
29 }
30 else if( sSrc.getSize() == 36 )
31 {
32 // Parse it
33 set( sSrc );
34 }
35}
36
24Bu::Uuid::~Uuid() 37Bu::Uuid::~Uuid()
25{ 38{
26} 39}
27 40
28Bu::String Bu::Uuid::toRawString() 41Bu::String Bu::Uuid::toRawString() const
29{ 42{
30 return Bu::String( (char *)data, 16 ); 43 return Bu::String( (char *)data, 16 );
31} 44}
32 45
33Bu::String Bu::Uuid::toString() 46Bu::String Bu::Uuid::toString() const
34{ 47{
35 Bu::MemBuf mb; 48 Bu::MemBuf mb;
36 Bu::Formatter f( mb ); 49 Bu::Formatter f( mb );
@@ -45,7 +58,7 @@ Bu::String Bu::Uuid::toString()
45 return mb.getString(); 58 return mb.getString();
46} 59}
47 60
48Bu::String Bu::Uuid::toUrn() 61Bu::String Bu::Uuid::toUrn() const
49{ 62{
50 return "urn:uuid:" + toString(); 63 return "urn:uuid:" + toString();
51} 64}
@@ -67,18 +80,23 @@ Bu::Uuid Bu::Uuid::gen()
67 Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); 80 Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read );
68 char dat[36]; 81 char dat[36];
69 fIn.read( dat, 36 ); 82 fIn.read( dat, 36 );
70 int iNibble = 0;
71 Uuid id; 83 Uuid id;
72 memset( id.data, 0, 16 ); 84 id.set( dat );
85 return id;
86}
87
88void Bu::Uuid::set( const Bu::String &sSrc )
89{
90 const char *dat = sSrc.getStr();
91 int iNibble = 0;
92 memset( data, 0, 16 );
73 for( int j = 0; j < 36; j++ ) 93 for( int j = 0; j < 36; j++ )
74 { 94 {
75 if( dat[j] == '-' ) 95 if( dat[j] == '-' )
76 continue; 96 continue;
77 unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10); 97 unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10);
78 id.data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c); 98 data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c);
79 iNibble++; 99 iNibble++;
80 } 100 }
81
82 return id;
83} 101}
84 102