diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/uuid.cpp | 34 | ||||
-rw-r--r-- | src/uuid.h | 8 |
2 files changed, 31 insertions, 11 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 | ||
24 | Bu::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 | |||
24 | Bu::Uuid::~Uuid() | 37 | Bu::Uuid::~Uuid() |
25 | { | 38 | { |
26 | } | 39 | } |
27 | 40 | ||
28 | Bu::String Bu::Uuid::toRawString() | 41 | Bu::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 | ||
33 | Bu::String Bu::Uuid::toString() | 46 | Bu::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 | ||
48 | Bu::String Bu::Uuid::toUrn() | 61 | Bu::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 | |||
88 | void 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 | ||
@@ -17,11 +17,12 @@ namespace Bu | |||
17 | public: | 17 | public: |
18 | Uuid(); | 18 | Uuid(); |
19 | Uuid( const Uuid &src ); | 19 | Uuid( const Uuid &src ); |
20 | Uuid( const Bu::String &sSrc ); | ||
20 | virtual ~Uuid(); | 21 | virtual ~Uuid(); |
21 | 22 | ||
22 | Bu::String toRawString(); | 23 | Bu::String toRawString() const; |
23 | Bu::String toString(); | 24 | Bu::String toString() const; |
24 | Bu::String toUrn(); | 25 | Bu::String toUrn() const; |
25 | 26 | ||
26 | int getVersion(); | 27 | int getVersion(); |
27 | 28 | ||
@@ -35,6 +36,7 @@ namespace Bu | |||
35 | void clear(); | 36 | void clear(); |
36 | 37 | ||
37 | private: | 38 | private: |
39 | void set( const Bu::String &sSrc ); | ||
38 | unsigned char data[16]; | 40 | unsigned char data[16]; |
39 | }; | 41 | }; |
40 | }; | 42 | }; |