create_guid.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : create_GUID                                                       *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    This program generates a globally unique identifier using the operating  *
00009 *  system's support.  The resulting id can be used to tag items that must be  *
00010 *  uniquely named.                                                            *
00011 *                                                                             *
00012 *******************************************************************************
00013 * Copyright (c) 2006-$now By Author.  This program is free software; you can  *
00014 * redistribute it and/or modify it under the terms of the GNU General Public  *
00015 * License as published by the Free Software Foundation; either version 2 of   *
00016 * the License or (at your option) any later version.  This is online at:      *
00017 *     http://www.fsf.org/copyleft/gpl.html                                    *
00018 * Please send any updates to: fred@gruntose.com                               *
00019 \*****************************************************************************/
00020 
00021 #include <basis/chaos.h>
00022 #include <basis/istring.h>
00023 #include <basis/portable.h>
00024 #include <opsystem/application_shell.h>
00025 #include <loggers/console_logger.h>
00026 #include <data_struct/static_memory_gremlin.h>
00027 #include <textual/string_manipulation.h>
00028 
00029 #ifdef __WIN32__
00030   #include <comdef.h>
00031 #endif
00032 
00033 #define BASE_LOG(to_print) program_wide_logger().log(to_print)
00034 
00035 // this is an example GUID in the DCE format:
00036 //
00037 //    {12345678-1234-1234-1234-123456789012}
00038 //
00039 // each position can be a hexadecimal digit, ranging from 0 to F.
00040 // the full size is measured as 32 nibbles or 16 bytes or 128 bits.
00041 
00042 class create_GUID : public application_shell
00043 {
00044 public:
00045   create_GUID() : application_shell("create_GUID") {}
00046   IMPLEMENT_CLASS_NAME("create_GUID");
00047   int execute();
00048 };
00049 
00050 int create_GUID::execute()
00051 {
00052   FUNCDEF("execute");
00053   SET_DEFAULT_CONSOLE_LOGGER;
00054 #ifdef __UNIX__
00055 
00056 // this is completely bogus for the time being.  it just produces a random
00057 // number rather than a guid.
00058   #define add_random \
00059     faux_guid += istring(string_manipulation::hex_to_char \
00060         (randomizer().inclusive(0, 0xf)), 1)
00061 
00062   istring faux_guid("{");
00063   for (int i = 0; i < 8; i++) add_random;
00064   faux_guid += "-";
00065   for (int j = 0; j < 3; j++) {
00066     for (int i = 0; i < 4; i++) add_random;
00067     faux_guid += "-";
00068   }
00069   for (int i = 0; i < 8; i++) add_random;
00070   faux_guid += "}";
00071   BASE_LOG(faux_guid.lower());
00072 #elif defined (__WIN32__)
00073   GUID guid;
00074   CoCreateGuid(&guid);
00075   const int BUFFER_SIZE = 1024;
00076   LPOLESTR wide_buffer = new WCHAR[BUFFER_SIZE + 4];
00077   StringFromGUID2(guid, wide_buffer, BUFFER_SIZE);
00078   const int BYTE_BUFFER_SIZE = BUFFER_SIZE * 2 + 4;
00079   char buffer[BYTE_BUFFER_SIZE];
00080   WideCharToMultiByte(CP_UTF8, 0, wide_buffer, -1, buffer, BYTE_BUFFER_SIZE,
00081       NULL, NULL);
00082   istring guid_text = buffer;
00083   delete [] wide_buffer;
00084   BASE_LOG(guid_text);
00085 #else
00086   #error unknown operating system; no support for guids.
00087 #endif
00088 
00089   return 0;
00090 }
00091 
00092 HOOPLE_MAIN(create_GUID, )
00093 
00094 #ifdef __BUILD_STATIC_APPLICATION__
00095   // static dependencies found by buildor_gen_deps.sh:
00096   #include <basis/array.cpp>
00097   #include <basis/byte_array.cpp>
00098   #include <basis/callstack_tracker.cpp>
00099   #include <basis/chaos.cpp>
00100   #include <basis/convert_utf.cpp>
00101   #include <basis/definitions.cpp>
00102   #include <basis/earth_time.cpp>
00103   #include <basis/guards.cpp>
00104   #include <basis/istring.cpp>
00105   #include <basis/log_base.cpp>
00106   #include <basis/memory_checker.cpp>
00107   #include <basis/mutex.cpp>
00108   #include <basis/object_base.cpp>
00109   #include <basis/outcome.cpp>
00110   #include <basis/packable.cpp>
00111   #include <basis/portable.cpp>
00112   #include <basis/sequence.cpp>
00113   #include <basis/set.cpp>
00114   #include <basis/utility.cpp>
00115   #include <basis/version_record.cpp>
00116   #include <data_struct/amorph.cpp>
00117   #include <data_struct/bit_vector.cpp>
00118   #include <data_struct/byte_hasher.cpp>
00119   #include <data_struct/configurator.cpp>
00120   #include <data_struct/hash_table.cpp>
00121   #include <data_struct/pointer_hash.cpp>
00122   #include <data_struct/stack.cpp>
00123   #include <data_struct/static_memory_gremlin.cpp>
00124   #include <data_struct/string_hash.cpp>
00125   #include <data_struct/string_hasher.cpp>
00126   #include <data_struct/string_table.cpp>
00127   #include <data_struct/symbol_table.cpp>
00128   #include <data_struct/table_configurator.cpp>
00129   #include <loggers/console_logger.cpp>
00130   #include <loggers/file_logger.cpp>
00131   #include <loggers/locked_logger.cpp>
00132   #include <loggers/null_logger.cpp>
00133   #include <loggers/program_wide_logger.cpp>
00134   #include <opsystem/application_base.cpp>
00135   #include <opsystem/application_shell.cpp>
00136   #include <opsystem/byte_filer.cpp>
00137   #include <opsystem/command_line.cpp>
00138   #include <opsystem/critical_events.cpp>
00139   #include <opsystem/directory.cpp>
00140   #include <opsystem/filename.cpp>
00141   #include <opsystem/ini_config.cpp>
00142   #include <opsystem/ini_parser.cpp>
00143   #include <opsystem/path_configuration.cpp>
00144   #include <opsystem/rendezvous.cpp>
00145   #include <textual/byte_format.cpp>
00146   #include <textual/parser_bits.cpp>
00147   #include <textual/string_manipulation.cpp>
00148   #include <textual/tokenizer.cpp>
00149 #endif // __BUILD_STATIC_APPLICATION__
00150 

Generated on Fri Nov 28 04:28:51 2008 for HOOPLE Libraries by  doxygen 1.5.1