aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/uuid.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/unstable/uuid.cpp
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/unstable/uuid.cpp')
-rw-r--r--src/unstable/uuid.cpp117
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
14Bu::Uuid::Uuid()
15{
16 clear();
17}
18
19Bu::Uuid::Uuid( const Uuid &src )
20{
21 memcpy( data, src.data, 16 );
22}
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
37Bu::Uuid::~Uuid()
38{
39}
40
41Bu::String Bu::Uuid::toRawString() const
42{
43 return Bu::String( (char *)data, 16 );
44}
45
46Bu::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
61Bu::String Bu::Uuid::toUrn() const
62{
63 return "urn:uuid:" + toString();
64}
65
66int Bu::Uuid::getVersion()
67{
68 return (data[6]&((8|4|2|1)<<4))>>4;
69}
70
71#define msb( i ) (1<<(7-i))
72
73void Bu::Uuid::clear()
74{
75 data[7] = msb(0);
76}
77
78Bu::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
88void 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
103bool Bu::Uuid::operator==( const Uuid &rhs ) const
104{
105 return memcmp( data, rhs.data, 16 ) == 0;
106}
107
108template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k )
109{
110 return __calcHashCode<String>( k.toRawString() );
111}
112
113template<> bool Bu::__cmpHashKeys<Bu::Uuid>( const Bu::Uuid &a, const Bu::Uuid &b )
114{
115 return a == b;
116}
117