feisty meow concerns codebase  2.140
create_guid.cpp
Go to the documentation of this file.
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 
24 #include <basis/astring.h>
25 #include <loggers/console_logger.h>
26 #include <mathematics/chaos.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");
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:
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>
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>
131  #include <structures/bit_vector.cpp>
132  #include <structures/checksums.cpp>
136  #include <structures/string_table.cpp>
138  #include <textual/byte_formatter.cpp>
139  #include <textual/parser_bits.cpp>
141  #include <timely/earth_time.cpp>
142  #include <timely/time_stamp.cpp>
143 #endif // __BUILD_STATIC_APPLICATION__
144 
The application_shell is a base object for console programs.
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
#define SETUP_CONSOLE_LOGGER
< a macro that retasks the program-wide logger as a console_logger.
#define add_random
#define BASE_LOG(to_print)
Definition: create_guid.cpp:46
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
const int BUFFER_SIZE
Definition: redirecter.cpp:46
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
Aids in achievement of platform independence.