• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/GUID.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/GUID.h>
00018 #include <spl/math/Math.h>
00019 #include <spl/text/StringBuffer.h>
00020 #include <spl/UInt32.h>
00021 #include <spl/UInt64.h>
00022 
00023 Guid::Guid()
00024 {
00025         data1 = Math::RandomInt();
00026         data2 = Math::RandomInt() & 0xFFFF;
00027         data3 = Math::RandomInt() & 0xFFFF;
00028         data4 = (((uint64)Math::RandomInt()) << 32) & (uint64)Math::RandomInt();
00029 }
00030 
00031 Guid::Guid( const Array<byte>& bytes )
00032 {
00033         ASSERT(bytes.Length() == GUID_SIZE_BYTES);
00034 
00035         data1 = *((uint32 *)&bytes.Data()[0]);
00036         data2 = *((uint16 *)&bytes.Data()[4]);
00037         data3 = *((uint16 *)&bytes.Data()[6]);
00038         data4 = *((uint64 *)&bytes.Data()[8]);
00039 }
00040 
00041 Guid::Guid( const Guid& guid )
00042 {
00043         data1 = guid.data1;
00044         data2 = guid.data2;
00045         data3 = guid.data3;
00046         data4 = guid.data4;
00047 }
00048 
00049 Guid::~Guid()
00050 {
00051 }
00052 
00053 void Guid::GetBytes( Array<byte>& bytes ) const
00054 {
00055         ASSERT(bytes.Length() == GUID_SIZE_BYTES);
00056 
00057         memcpy(&bytes[0], &data1, 4);
00058         memcpy(&bytes[4], &data2, 2);
00059         memcpy(&bytes[6], &data3, 2);
00060         memcpy(&bytes[8], &data4, 8);
00061 }
00062 
00063 Array<byte> Guid::GetBytes() const
00064 {
00065         Array<byte> bytes(16);
00066         GetBytes(bytes);
00067         return bytes;
00068 }
00069 
00070 StringPtr Guid::ToBase64()
00071 {
00072         byte data[16];
00073         memcpy(&data[0], &data1, 4);
00074         memcpy(&data[4], &data2, 2);
00075         memcpy(&data[6], &data3, 2);
00076         memcpy(&data[8], &data4, 8);
00077 
00078         return String::Base64Encode( (char *)data, 16 );
00079 }
00080 
00081 StringPtr Guid::ToHex()
00082 {
00083         StringBuffer sbuf(40);
00084         sbuf.Append( UInt32::ToString(data1, 16) );
00085         sbuf.Append( '-' );
00086         sbuf.Append( UInt32::ToString(data2, 16) );
00087         sbuf.Append( '-' );
00088         sbuf.Append( UInt32::ToString(data3, 16) );
00089         sbuf.Append( '-' );
00090         sbuf.Append( UInt64::ToString(data4, 16) );
00091 
00092         return sbuf.ToString();
00093 }
00094 
00095 Guid Guid::ParseBase64(const String& str)
00096 {
00097         RefCountPtr<Array<byte> > bytes = String::Base64Decode(str);
00098         ASSERT( bytes->Length() == Guid::Length() );
00099         return Guid(bytes);
00100 }
00101 
00102 Guid Guid::ParseHex(const String& str)
00103 {
00104         throw new NotImplementedException();
00105 }