uuid.cpp

Go to the documentation of this file.
00001 /*
00002  * uuid.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "uuid.h"               // always include our own header first!
00035 
00036 #ifdef WIN32
00037 #include "rpc.h"
00038 #endif  // WIN32
00039 
00040 
00041 ////////////////////////////////////////////////////////////////////////////////
00042 //
00043 //      static helper methods
00044 //
00045 ////////////////////////////////////////////////////////////////////////////////
00046 
00047 #ifndef WIN32
00048 static word_t
00049 getr4
00050 (
00051 void
00052 )
00053 throw()
00054 {
00055         static bool initRandom = false;
00056         if (!initRandom) {
00057                 DPRINTF("TODO: link to proper UUID generation libraries!");
00058                 srand(time(NULL) + 7 * getpid());
00059                 initRandom = true;
00060         }
00061 
00062         // return 2-byte (4 hex character) random number
00063         return (rand() % 0x10000);
00064 }
00065 #endif  // WIN32
00066 
00067 
00068 ////////////////////////////////////////////////////////////////////////////////
00069 //
00070 //      public API
00071 //
00072 ////////////////////////////////////////////////////////////////////////////////
00073 
00074 const char *
00075 generateUUID
00076 (
00077 OUT char * buffer
00078 )
00079 {
00080         ASSERT(buffer, "null");
00081 
00082 #ifdef WIN32
00083         UUID uuid;
00084         UuidCreate(&uuid);
00085         unsigned char * rpcString = NULL;
00086         UuidToString(&uuid, &rpcString);
00087         ASSERT_THROW(rpcString, "windows returned null string for UUID");
00088         strcpy(buffer, (const char *) rpcString);
00089         RpcStringFree(&rpcString);
00090 #else   // WIN32
00091         snprintf(buffer, eUUID_Length,
00092             "%04x%04x-%04x-%04x-%04x-%04x%08x",
00093             getr4(), getr4(), getr4(), getr4(), getr4(),
00094             (word_t) getpid(), (dword_t) time(NULL));
00095 #endif  // WIN32
00096         DPRINTF("  uuid: '%s'", buffer);
00097 
00098         return buffer;
00099 }
00100