diff options
Diffstat (limited to 'src/unstable/uuid.cpp')
-rw-r--r-- | src/unstable/uuid.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/unstable/uuid.cpp b/src/unstable/uuid.cpp new file mode 100644 index 0000000..088450b --- /dev/null +++ b/src/unstable/uuid.cpp | |||
@@ -0,0 +1,117 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/uuid.h" | ||
9 | #include "bu/file.h" | ||
10 | #include "bu/formatter.h" | ||
11 | #include "bu/membuf.h" | ||
12 | #include <string.h> | ||
13 | |||
14 | Bu::Uuid::Uuid() | ||
15 | { | ||
16 | clear(); | ||
17 | } | ||
18 | |||
19 | Bu::Uuid::Uuid( const Uuid &src ) | ||
20 | { | ||
21 | memcpy( data, src.data, 16 ); | ||
22 | } | ||
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 | |||
37 | Bu::Uuid::~Uuid() | ||
38 | { | ||
39 | } | ||
40 | |||
41 | Bu::String Bu::Uuid::toRawString() const | ||
42 | { | ||
43 | return Bu::String( (char *)data, 16 ); | ||
44 | } | ||
45 | |||
46 | Bu::String Bu::Uuid::toString() const | ||
47 | { | ||
48 | Bu::MemBuf mb; | ||
49 | Bu::Formatter f( mb ); | ||
50 | |||
51 | for( int j = 0; j < 16; j++ ) | ||
52 | { | ||
53 | if( j == 4 || j == 6 || j == 8 || j == 10 ) | ||
54 | f << '-'; | ||
55 | f << Bu::Fmt::hex(2).caps(false) << (unsigned int)data[j]; | ||
56 | } | ||
57 | |||
58 | return mb.getString(); | ||
59 | } | ||
60 | |||
61 | Bu::String Bu::Uuid::toUrn() const | ||
62 | { | ||
63 | return "urn:uuid:" + toString(); | ||
64 | } | ||
65 | |||
66 | int Bu::Uuid::getVersion() | ||
67 | { | ||
68 | return (data[6]&((8|4|2|1)<<4))>>4; | ||
69 | } | ||
70 | |||
71 | #define msb( i ) (1<<(7-i)) | ||
72 | |||
73 | void Bu::Uuid::clear() | ||
74 | { | ||
75 | data[7] = msb(0); | ||
76 | } | ||
77 | |||
78 | Bu::Uuid Bu::Uuid::gen() | ||
79 | { | ||
80 | Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); | ||
81 | char dat[36]; | ||
82 | fIn.read( dat, 36 ); | ||
83 | Uuid id; | ||
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 ); | ||
93 | for( int j = 0; j < 36; j++ ) | ||
94 | { | ||
95 | if( dat[j] == '-' ) | ||
96 | continue; | ||
97 | unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10); | ||
98 | data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c); | ||
99 | iNibble++; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | bool Bu::Uuid::operator==( const Uuid &rhs ) const | ||
104 | { | ||
105 | return memcmp( data, rhs.data, 16 ) == 0; | ||
106 | } | ||
107 | |||
108 | template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k ) | ||
109 | { | ||
110 | return __calcHashCode<String>( k.toRawString() ); | ||
111 | } | ||
112 | |||
113 | template<> bool Bu::__cmpHashKeys<Bu::Uuid>( const Bu::Uuid &a, const Bu::Uuid &b ) | ||
114 | { | ||
115 | return a == b; | ||
116 | } | ||
117 | |||