checking in the recent efforts at optimizing clam
[feisty_meow.git] / nucleus / tools / simple_utilities / create_guid.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : create_guid                                                       *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *  Purpose:                                                                   *
7 *                                                                             *
8 *    This program generates a globally unique identifier using the operating  *
9 *  system's support.  The resulting id can be used to tag items that must be  *
10 *  uniquely named.                                                            *
11 *                                                                             *
12 *******************************************************************************
13 * Copyright (c) 2006-$now By Author.  This program is free software; you can  *
14 * redistribute it and/or modify it under the terms of the GNU General Public  *
15 * License as published by the Free Software Foundation; either version 2 of   *
16 * the License or (at your option) any later version.  This is online at:      *
17 *     http://www.fsf.org/copyleft/gpl.html                                    *
18 * Please send any updates to: fred@gruntose.com                               *
19 \*****************************************************************************/
20
21 #include <application/application_shell.h>
22 #include <application/hoople_main.h>
23 #include <application/windoze_helper.h>
24 #include <basis/astring.h>
25 #include <loggers/console_logger.h>
26 #include <mathematics/chaos.h>
27 #include <structures/static_memory_gremlin.h>
28 #include <textual/string_manipulation.h>
29
30 #ifdef __WIN32__
31 //  #define DO_GUIDS
32 //hmmm: currently disabled due to problems compiling in cygwin using this header; complains about new.h being missing.
33 #endif
34
35 #ifdef DO_GUIDS
36   #include <comdef.h>
37 #endif
38
39 using namespace application;
40 using namespace basis;
41 using namespace loggers;
42 using namespace mathematics;
43 using namespace structures;
44 using namespace textual;
45
46 #define BASE_LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
47
48 // this is an example GUID in the DCE format:
49 //
50 //    {12345678-1234-1234-1234-123456789012}
51 //
52 // each position can be a hexadecimal digit, ranging from 0 to F.
53 // the full size is measured as 32 nibbles or 16 bytes or 128 bits.
54
55 class create_guid : public application_shell
56 {
57 public:
58   create_guid() : application_shell() {}
59   DEFINE_CLASS_NAME("create_guid");
60   int execute();
61 };
62
63 int create_guid::execute()
64 {
65   FUNCDEF("execute");
66   SETUP_CONSOLE_LOGGER;
67 #ifndef DO_GUIDS
68 // this is completely bogus for the time being.  it just produces a random
69 // number rather than a guid.
70   #define add_random \
71     faux_guid += astring(string_manipulation::hex_to_char \
72         (randomizer().inclusive(0, 0xf)), 1)
73
74   astring faux_guid("{");
75   for (int i = 0; i < 8; i++) add_random;
76   faux_guid += "-";
77   for (int j = 0; j < 3; j++) {
78     for (int i = 0; i < 4; i++) add_random;
79     faux_guid += "-";
80   }
81   for (int i = 0; i < 8; i++) add_random;
82   faux_guid += "}";
83   BASE_LOG(faux_guid.lower());
84 #elif defined (DO_GUIDS)
85   GUID guid;
86   CoCreateGuid(&guid);
87   const int BUFFER_SIZE = 1024;
88   LPOLESTR wide_buffer = new WCHAR[BUFFER_SIZE + 4];
89   StringFromGUID2(guid, wide_buffer, BUFFER_SIZE);
90   const int BYTE_BUFFER_SIZE = BUFFER_SIZE * 2 + 4;
91   char buffer[BYTE_BUFFER_SIZE];
92   WideCharToMultiByte(CP_UTF8, 0, wide_buffer, -1, buffer, BYTE_BUFFER_SIZE,
93       NULL, NULL);
94   astring guid_text = buffer;
95   delete [] wide_buffer;
96   BASE_LOG(guid_text);
97 #else
98   #error unknown operating system; no support for guids.
99 #endif
100
101   return 0;
102 }
103
104 HOOPLE_MAIN(create_guid, )
105
106 #ifdef __BUILD_STATIC_APPLICATION__
107   // static dependencies found by buildor_gen_deps.sh:
108   #include <application/application_shell.cpp>
109   #include <application/command_line.cpp>
110   #include <application/windoze_helper.cpp>
111   #include <basis/astring.cpp>
112   #include <basis/common_outcomes.cpp>
113   #include <basis/environment.cpp>
114   #include <basis/guards.cpp>
115   #include <basis/mutex.cpp>
116   #include <basis/utf_conversion.cpp>
117   #include <configuration/application_configuration.cpp>
118   #include <configuration/configurator.cpp>
119   #include <configuration/ini_configurator.cpp>
120   #include <configuration/ini_parser.cpp>
121   #include <configuration/table_configurator.cpp>
122   #include <configuration/variable_tokenizer.cpp>
123   #include <filesystem/byte_filer.cpp>
124   #include <filesystem/directory.cpp>
125   #include <filesystem/filename.cpp>
126   #include <loggers/combo_logger.cpp>
127   #include <loggers/console_logger.cpp>
128   #include <loggers/critical_events.cpp>
129   #include <loggers/file_logger.cpp>
130   #include <loggers/program_wide_logger.cpp>
131   #include <structures/bit_vector.cpp>
132   #include <structures/checksums.cpp>
133   #include <structures/object_packers.cpp>
134   #include <structures/static_memory_gremlin.cpp>
135   #include <structures/string_hasher.cpp>
136   #include <structures/string_table.cpp>
137   #include <structures/version_record.cpp>
138   #include <textual/byte_formatter.cpp>
139   #include <textual/parser_bits.cpp>
140   #include <textual/string_manipulation.cpp>
141   #include <timely/earth_time.cpp>
142   #include <timely/time_stamp.cpp>
143 #endif // __BUILD_STATIC_APPLICATION__
144