From 3166bd631a093f42ea44a4b0f4d914cf51518bd4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 28 Oct 2009 13:43:48 +0000 Subject: 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... --- src/uuid.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/uuid.h | 9 ++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) 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 @@ */ #include "bu/uuid.h" +#include "bu/file.h" +#include "bu/formatter.h" +#include "bu/membuf.h" +#include Bu::Uuid::Uuid() { clear(); } +Bu::Uuid::Uuid( const Uuid &src ) +{ + memcpy( data, src.data, 16 ); +} + Bu::Uuid::~Uuid() { } +Bu::FString Bu::Uuid::toRawString() +{ + return Bu::FString( (char *)data, 16 ); +} + +Bu::FString Bu::Uuid::toString() +{ + Bu::MemBuf mb; + Bu::Formatter f( mb ); + + for( int j = 0; j < 16; j++ ) + { + if( j == 4 || j == 6 || j == 8 || j == 10 ) + f << '-'; + f << Bu::Fmt::hex(2).caps(false) << (unsigned int)data[j]; + } + + return mb.getString(); +} + #define msb( i ) (1<<(7-i)) void Bu::Uuid::clear() @@ -23,3 +52,23 @@ void Bu::Uuid::clear() data[7] = msb(0); } +Bu::Uuid Bu::Uuid::gen() +{ + Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); + char dat[36]; + fIn.read( dat, 36 ); + int iNibble = 0; + Uuid id; + memset( id.data, 0, 16 ); + for( int j = 0; j < 36; j++ ) + { + if( dat[j] == '-' ) + continue; + unsigned char c = (dat[j]>='0'&&dat[j]<='9')?(dat[j]-'0'):(dat[j]-'a'+10); + id.data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c); + iNibble++; + } + + return id; +} + diff --git a/src/uuid.h b/src/uuid.h index ef4f768..dce4829 100644 --- a/src/uuid.h +++ b/src/uuid.h @@ -8,14 +8,21 @@ #ifndef BU_UUID_H #define BU_UUID_H +#include "bu/fstring.h" + namespace Bu { class Uuid { public: Uuid(); + Uuid( const Uuid &src ); virtual ~Uuid(); + Bu::FString toRawString(); + Bu::FString toString(); + + static Uuid gen(); static Uuid genV1(); static Uuid genV2(); static Uuid genV3(); @@ -25,7 +32,7 @@ namespace Bu void clear(); private: - unsigned char data[8]; + unsigned char data[16]; }; }; -- cgit v1.2.3