Logo
~Sockets~
~Examples~
~Contact~


Uid.cpp

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #if (defined(__unix__) || defined(unix)) && !defined(USG)
00031 #include <sys/param.h>
00032 #endif
00033 
00034 #include <assert.h>
00035 #if defined(_WIN32) || defined(__CYGWIN__)
00036 #include <objbase.h>
00037 #elif defined MACOSX
00038 #include <string.h>
00039 #include <uuid/uuid.h>
00040 #elif defined __FreeBSD__
00041 # if __FreeBSD_version >= 500000
00042 #  include <uuid.h>
00043 # else
00044 #  error FreeBSD versions prior to 500000 does not support uuid(3)
00045 # endif
00046 #else
00047 #include <uuid/uuid.h>
00048 #endif
00049 
00050 #include "Uid.h"
00051 
00052 #ifdef SOCKETS_NAMESPACE
00053 namespace SOCKETS_NAMESPACE {
00054 #endif
00055 
00056 
00057 //e682119c-dea0-4e09-acf4-e66b8c522e99
00058 
00059 
00060 Uid::Uid()
00061 {
00062 #if defined(_WIN32) || defined(__CYGWIN__)
00063         GUID randomGuid;
00064         // create random GUID
00065         randomGuid = GUID_NULL;
00066         ::CoCreateGuid(&randomGuid);
00067         if (randomGuid == GUID_NULL)
00068         {
00069                 fprintf(stderr,"Couldn't create a random GUID\n");
00070                 return;
00071         }
00072         memcpy(m_bufuid, &randomGuid, 16);
00073 #elif defined __FreeBSD__
00074         uuid_t uid; // uuid_t is a struct
00075         uuid_create(&uid, NULL);
00076         memcpy(m_bufuid, &uid, 16);
00077 #else
00078         uuid_t uid; // uuid_t is defined as unsigned char[16]
00079         uuid_generate(uid);
00080         memcpy(m_bufuid, uid, 16);
00081 #endif
00082 }
00083 
00084 
00085 Uid::Uid(const std::string& uidstr)
00086 {
00087         unsigned uid[16];
00088         sscanf(uidstr.c_str(),"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00089                 &uid[0],&uid[1],&uid[2],&uid[3],
00090                 &uid[4],&uid[5],&uid[6],&uid[7],
00091                 &uid[8],&uid[9],&uid[10],&uid[11],
00092                 &uid[12],&uid[13],&uid[14],&uid[15]);
00093         for (int i = 0; i < 16; i++)
00094                 m_bufuid[i] = (unsigned char)uid[i];
00095 }
00096 
00097 
00098 Uid::Uid(const unsigned char *buf)
00099 {
00100         memcpy(m_bufuid, buf, 16);
00101 }
00102 
00103 
00104 Uid::~Uid()
00105 {
00106 }
00107 
00108 
00109 std::string Uid::GetUid()
00110 {
00111         std::string tmp;
00112         char slask[100]; // Uid::GetUid temporary
00113 
00114         sprintf(slask,"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
00115                 m_bufuid[0],m_bufuid[1],m_bufuid[2],m_bufuid[3],
00116                 m_bufuid[4],m_bufuid[5],m_bufuid[6],m_bufuid[7],
00117                 m_bufuid[8],m_bufuid[9],m_bufuid[10],m_bufuid[11],
00118                 m_bufuid[12],m_bufuid[13],m_bufuid[14],m_bufuid[15]);
00119         tmp = slask;
00120         return tmp;
00121 }
00122 
00123 
00124 const unsigned char *Uid::GetBuf() 
00125 { 
00126         return m_bufuid; 
00127 }
00128 
00129 
00130 #ifdef SOCKETS_NAMESPACE
00131 }
00132 #endif
00133 
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4