From 663c6d9c9113a92fc3c79b51ea986ff30189868d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 25 Jul 2011 18:07:32 +0000 Subject: Some tweaks to the uuid class to make it easier to work with, next for it is probably going to be archiver operators. --- src/uuid.cpp | 34 ++++++++++++++++++++++++++-------- 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 ) memcpy( data, src.data, 16 ); } +Bu::Uuid::Uuid( const Bu::String &sSrc ) +{ + if( sSrc.getSize() == 16 ) + { + memcpy( data, sSrc.getStr(), 16 ); + } + else if( sSrc.getSize() == 36 ) + { + // Parse it + set( sSrc ); + } +} + Bu::Uuid::~Uuid() { } -Bu::String Bu::Uuid::toRawString() +Bu::String Bu::Uuid::toRawString() const { return Bu::String( (char *)data, 16 ); } -Bu::String Bu::Uuid::toString() +Bu::String Bu::Uuid::toString() const { Bu::MemBuf mb; Bu::Formatter f( mb ); @@ -45,7 +58,7 @@ Bu::String Bu::Uuid::toString() return mb.getString(); } -Bu::String Bu::Uuid::toUrn() +Bu::String Bu::Uuid::toUrn() const { return "urn:uuid:" + toString(); } @@ -67,18 +80,23 @@ 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 ); + id.set( dat ); + return id; +} + +void Bu::Uuid::set( const Bu::String &sSrc ) +{ + const char *dat = sSrc.getStr(); + int iNibble = 0; + memset( 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); + data[iNibble/2] |= (iNibble%2==0)?(c<<4):(c); iNibble++; } - - return id; } diff --git a/src/uuid.h b/src/uuid.h index ee7469b..1ffecc2 100644 --- a/src/uuid.h +++ b/src/uuid.h @@ -17,11 +17,12 @@ namespace Bu public: Uuid(); Uuid( const Uuid &src ); + Uuid( const Bu::String &sSrc ); virtual ~Uuid(); - Bu::String toRawString(); - Bu::String toString(); - Bu::String toUrn(); + Bu::String toRawString() const; + Bu::String toString() const; + Bu::String toUrn() const; int getVersion(); @@ -35,6 +36,7 @@ namespace Bu void clear(); private: + void set( const Bu::String &sSrc ); unsigned char data[16]; }; }; -- cgit v1.2.3