diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2009-10-28 13:43:48 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2009-10-28 13:43:48 +0000 |
| commit | 3166bd631a093f42ea44a4b0f4d914cf51518bd4 (patch) | |
| tree | 999682383da45c96e05ddf23a33290928031c34f | |
| parent | baaf14f63872ccfab3cb20f72a5cbb56d8cce8a4 (diff) | |
| download | libbu++-3166bd631a093f42ea44a4b0f4d914cf51518bd4.tar.gz libbu++-3166bd631a093f42ea44a4b0f4d914cf51518bd4.tar.bz2 libbu++-3166bd631a093f42ea44a4b0f4d914cf51518bd4.tar.xz libbu++-3166bd631a093f42ea44a4b0f4d914cf51518bd4.zip | |
Uuid now has a default gen function that will generate a uuid using the best,
easiest method available. On linux right now, this means that it uses the
kernel /proc interface. I'll have to add some fallbacks to this...
Diffstat (limited to '')
| -rw-r--r-- | src/uuid.cpp | 49 | ||||
| -rw-r--r-- | src/uuid.h | 9 |
2 files changed, 57 insertions, 1 deletions
diff --git a/src/uuid.cpp b/src/uuid.cpp index b8de9e1..c064d78 100644 --- a/src/uuid.cpp +++ b/src/uuid.cpp | |||
| @@ -6,16 +6,45 @@ | |||
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #include "bu/uuid.h" | 8 | #include "bu/uuid.h" |
| 9 | #include "bu/file.h" | ||
| 10 | #include "bu/formatter.h" | ||
| 11 | #include "bu/membuf.h" | ||
| 12 | #include <string.h> | ||
| 9 | 13 | ||
| 10 | Bu::Uuid::Uuid() | 14 | Bu::Uuid::Uuid() |
| 11 | { | 15 | { |
| 12 | clear(); | 16 | clear(); |
| 13 | } | 17 | } |
| 14 | 18 | ||
| 19 | Bu::Uuid::Uuid( const Uuid &src ) | ||
| 20 | { | ||
| 21 | memcpy( data, src.data, 16 ); | ||
| 22 | } | ||
| 23 | |||
| 15 | Bu::Uuid::~Uuid() | 24 | Bu::Uuid::~Uuid() |
| 16 | { | 25 | { |
| 17 | } | 26 | } |
| 18 | 27 | ||
| 28 | Bu::FString Bu::Uuid::toRawString() | ||
| 29 | { | ||
| 30 | return Bu::FString( (char *)data, 16 ); | ||
| 31 | } | ||
| 32 | |||
| 33 | Bu::FString Bu::Uuid::toString() | ||
| 34 | { | ||
| 35 | Bu::MemBuf mb; | ||
| 36 | Bu::Formatter f( mb ); | ||
| 37 | |||
| 38 | for( int j = 0; j < 16; j++ ) | ||
| 39 | { | ||
| 40 | if( j == 4 || j == 6 || j == 8 || j == 10 ) | ||
| 41 | f << '-'; | ||
| 42 | f << Bu::Fmt::hex(2).caps(false) << (unsigned int)data[j]; | ||
| 43 | } | ||
| 44 | |||
| 45 | return mb.getString(); | ||
| 46 | } | ||
| 47 | |||
| 19 | #define msb( i ) (1<<(7-i)) | 48 | #define msb( i ) (1<<(7-i)) |
| 20 | 49 | ||
| 21 | void Bu::Uuid::clear() | 50 | void Bu::Uuid::clear() |
| @@ -23,3 +52,23 @@ void Bu::Uuid::clear() | |||
| 23 | data[7] = msb(0); | 52 | data[7] = msb(0); |
| 24 | } | 53 | } |
| 25 | 54 | ||
| 55 | Bu::Uuid Bu::Uuid::gen() | ||
| 56 | { | ||
| 57 | Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); | ||
| 58 | char dat[36]; | ||
| 59 | fIn.read( dat, 36 ); | ||
| 60 | int iNibble = 0; | ||
| 61 | Uuid id; | ||
| 62 | memset( id.data, 0, 16 ); | ||
| 63 | for( int j = 0; j < 36; j++ ) | ||
| 64 | { | ||
| 65 | if( dat[j] == '-' ) | ||
| 66 | continue; | ||
| 67 | unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10); | ||
| 68 | id.data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c); | ||
| 69 | iNibble++; | ||
| 70 | } | ||
| 71 | |||
| 72 | return id; | ||
| 73 | } | ||
| 74 | |||
| @@ -8,14 +8,21 @@ | |||
| 8 | #ifndef BU_UUID_H | 8 | #ifndef BU_UUID_H |
| 9 | #define BU_UUID_H | 9 | #define BU_UUID_H |
| 10 | 10 | ||
| 11 | #include "bu/fstring.h" | ||
| 12 | |||
| 11 | namespace Bu | 13 | namespace Bu |
| 12 | { | 14 | { |
| 13 | class Uuid | 15 | class Uuid |
| 14 | { | 16 | { |
| 15 | public: | 17 | public: |
| 16 | Uuid(); | 18 | Uuid(); |
| 19 | Uuid( const Uuid &src ); | ||
| 17 | virtual ~Uuid(); | 20 | virtual ~Uuid(); |
| 18 | 21 | ||
| 22 | Bu::FString toRawString(); | ||
| 23 | Bu::FString toString(); | ||
| 24 | |||
| 25 | static Uuid gen(); | ||
| 19 | static Uuid genV1(); | 26 | static Uuid genV1(); |
| 20 | static Uuid genV2(); | 27 | static Uuid genV2(); |
| 21 | static Uuid genV3(); | 28 | static Uuid genV3(); |
| @@ -25,7 +32,7 @@ namespace Bu | |||
| 25 | void clear(); | 32 | void clear(); |
| 26 | 33 | ||
| 27 | private: | 34 | private: |
| 28 | unsigned char data[8]; | 35 | unsigned char data[16]; |
| 29 | }; | 36 | }; |
| 30 | }; | 37 | }; |
| 31 | 38 | ||
