From febb9c275b5247df596876e4eea7fa17b7ec45e7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 22 Aug 2018 17:26:08 +0200 Subject: Add support for UUID generation --- libbutl/uuid.cxx | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 libbutl/uuid.cxx (limited to 'libbutl/uuid.cxx') diff --git a/libbutl/uuid.cxx b/libbutl/uuid.cxx new file mode 100644 index 0000000..ab42ad8 --- /dev/null +++ b/libbutl/uuid.cxx @@ -0,0 +1,88 @@ +// file : libbutl/uuid.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include // ENOTSUP + +#include // sprintf() scanf() +#include // strlen() +#include +#include + +using namespace std; + +namespace butl +{ + array uuid:: + c_string (bool upper) const + { + array r; + + sprintf (r.data (), + (upper + ? "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X" + : "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"), + time_low, + time_mid, + time_hiv, + clock_seq_hir, + clock_seq_low, + node[0], node[1], node[2], node[3], node[4], node[5]); + + return r; + } + + void uuid:: + assign (const char* s) + { + if (s != nullptr && strlen (s) == 36 && s[8] == '-') + { + if (sscanf (s, + "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", + &time_low, + &time_mid, + &time_hiv, + &clock_seq_hir, + &clock_seq_low, + &node[0], &node[1], &node[2], + &node[3], &node[4], &node[5]) == 11) + return; + } + + throw invalid_argument ("invalid UUID string representation"); + } + + uuid_system_generator uuid::system_generator; + + // Utility function used by platform-specified uuid-*.cxx implementations. + // + void + uuid_throw_weak () + { + throw system_error (ENOTSUP, + generic_category (), + "strong UUID uniqueness cannot be guaranteed"); + } + +#ifdef BUILD2_BOOTSTRAP + uuid uuid_system_generator:: + generate (bool) + { + throw system_error (ENOTSUP, + generic_category (), + "no UUID generation support during bootstrap"); + } + + void uuid_system_generator:: + initialize () + { + } + + void uuid_system_generator:: + terminate () + { + } +#endif // BUILD2_BOOTSTRAP +} -- cgit v1.1