Merge branch 'master' of feistymeow.org:feisty_meow
[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 _MSC_VER
31   #include <comdef.h>
32 #endif
33
34 using namespace application;
35 using namespace basis;
36 using namespace loggers;
37 using namespace mathematics;
38 using namespace structures;
39 using namespace textual;
40
41 #define BASE_LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
42
43 // this is an example GUID in the DCE format:
44 //
45 //    {12345678-1234-1234-1234-123456789012}
46 //
47 // each position can be a hexadecimal digit, ranging from 0 to F.
48 // the full size is measured as 32 nibbles or 16 bytes or 128 bits.
49
50 class create_guid : public application_shell
51 {
52 public:
53   create_guid() : application_shell() {}
54   DEFINE_CLASS_NAME("create_guid");
55   int execute();
56 };
57
58 int create_guid::execute()
59 {
60   FUNCDEF("execute");
61   SETUP_CONSOLE_LOGGER;
62 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
63
64 // this is completely bogus for the time being.  it just produces a random
65 // number rather than a guid.
66   #define add_random \
67     faux_guid += astring(string_manipulation::hex_to_char \
68         (randomizer().inclusive(0, 0xf)), 1)
69
70   astring faux_guid("{");
71   for (int i = 0; i < 8; i++) add_random;
72   faux_guid += "-";
73   for (int j = 0; j < 3; j++) {
74     for (int i = 0; i < 4; i++) add_random;
75     faux_guid += "-";
76   }
77   for (int i = 0; i < 8; i++) add_random;
78   faux_guid += "}";
79   BASE_LOG(faux_guid.lower());
80 #elif defined (_MSC_VER)
81   GUID guid;
82   CoCreateGuid(&guid);
83   const int BUFFER_SIZE = 1024;
84   LPOLESTR wide_buffer = new WCHAR[BUFFER_SIZE + 4];
85   StringFromGUID2(guid, wide_buffer, BUFFER_SIZE);
86   const int BYTE_BUFFER_SIZE = BUFFER_SIZE * 2 + 4;
87   char buffer[BYTE_BUFFER_SIZE];
88   WideCharToMultiByte(CP_UTF8, 0, wide_buffer, -1, buffer, BYTE_BUFFER_SIZE,
89       NULL, NULL);
90   astring guid_text = buffer;
91   delete [] wide_buffer;
92   BASE_LOG(guid_text);
93 #else
94   #error unknown operating system; no support for guids.
95 #endif
96
97   return 0;
98 }
99
100 HOOPLE_MAIN(create_guid, )
101
102 #ifdef __BUILD_STATIC_APPLICATION__
103   // static dependencies found by buildor_gen_deps.sh:
104   #include <application/application_shell.cpp>
105   #include <application/command_line.cpp>
106   #include <application/windoze_helper.cpp>
107   #include <basis/astring.cpp>
108   #include <basis/common_outcomes.cpp>
109   #include <basis/environment.cpp>
110   #include <basis/guards.cpp>
111   #include <basis/mutex.cpp>
112   #include <basis/utf_conversion.cpp>
113   #include <configuration/application_configuration.cpp>
114   #include <configuration/configurator.cpp>
115   #include <configuration/ini_configurator.cpp>
116   #include <configuration/ini_parser.cpp>
117   #include <configuration/table_configurator.cpp>
118   #include <configuration/variable_tokenizer.cpp>
119   #include <filesystem/byte_filer.cpp>
120   #include <filesystem/directory.cpp>
121   #include <filesystem/filename.cpp>
122   #include <loggers/combo_logger.cpp>
123   #include <loggers/console_logger.cpp>
124   #include <loggers/critical_events.cpp>
125   #include <loggers/file_logger.cpp>
126   #include <loggers/program_wide_logger.cpp>
127   #include <structures/bit_vector.cpp>
128   #include <structures/checksums.cpp>
129   #include <structures/object_packers.cpp>
130   #include <structures/static_memory_gremlin.cpp>
131   #include <structures/string_hasher.cpp>
132   #include <structures/string_table.cpp>
133   #include <structures/version_record.cpp>
134   #include <textual/byte_formatter.cpp>
135   #include <textual/parser_bits.cpp>
136   #include <textual/string_manipulation.cpp>
137   #include <timely/earth_time.cpp>
138   #include <timely/time_stamp.cpp>
139 #endif // __BUILD_STATIC_APPLICATION__
140