diff Core/Toolbox.cpp @ 2141:a260a8ad83f1

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Nov 2016 16:16:26 +0100
parents aa4b8895cd23
children fd5875662670
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Wed Nov 09 16:12:47 2016 +0100
+++ b/Core/Toolbox.cpp	Wed Nov 09 16:16:26 2016 +0100
@@ -103,6 +103,20 @@
 #endif
 
 
+
+// Inclusions for UUID
+// http://stackoverflow.com/a/1626302
+
+extern "C"
+{
+#ifdef WIN32
+#include <rpc.h>
+#else
+#include <uuid/uuid.h>
+#endif
+}
+
+
 #if ORTHANC_ENABLE_PUGIXML == 1
 #include "ChunkedBuffer.h"
 #include <pugixml.hpp>
@@ -1238,6 +1252,75 @@
   }
 
 
+  std::string Toolbox::GenerateUuid()
+  {
+#ifdef WIN32
+    UUID uuid;
+    UuidCreate ( &uuid );
+
+    unsigned char * str;
+    UuidToStringA ( &uuid, &str );
+
+    std::string s( ( char* ) str );
+
+    RpcStringFreeA ( &str );
+#else
+    uuid_t uuid;
+    uuid_generate_random ( uuid );
+    char s[37];
+    uuid_unparse ( uuid, s );
+#endif
+    return s;
+  }
+
+
+  bool Toolbox::IsUuid(const std::string& str)
+  {
+    if (str.size() != 36)
+    {
+      return false;
+    }
+
+    for (size_t i = 0; i < str.length(); i++)
+    {
+      if (i == 8 || i == 13 || i == 18 || i == 23)
+      {
+        if (str[i] != '-')
+          return false;
+      }
+      else
+      {
+        if (!isalnum(str[i]))
+          return false;
+      }
+    }
+
+    return true;
+  }
+
+
+  bool Toolbox::StartsWithUuid(const std::string& str)
+  {
+    if (str.size() < 36)
+    {
+      return false;
+    }
+
+    if (str.size() == 36)
+    {
+      return IsUuid(str);
+    }
+
+    assert(str.size() > 36);
+    if (!isspace(str[36]))
+    {
+      return false;
+    }
+
+    return IsUuid(str.substr(0, 36));
+  }
+
+
 #if ORTHANC_SANDBOXED == 0
 
   static bool finish_;