comparison Core/Uuid.cpp @ 2141:a260a8ad83f1

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Nov 2016 16:16:26 +0100
parents aa4b8895cd23
children
comparison
equal deleted inserted replaced
2140:aa4b8895cd23 2141:a260a8ad83f1
31 31
32 32
33 #include "PrecompiledHeaders.h" 33 #include "PrecompiledHeaders.h"
34 #include "Uuid.h" 34 #include "Uuid.h"
35 35
36 // http://stackoverflow.com/a/1626302
37
38 extern "C"
39 {
40 #ifdef WIN32
41 #include <rpc.h>
42 #else
43 #include <uuid/uuid.h>
44 #endif
45 }
46
47 #include <boost/filesystem.hpp> 36 #include <boost/filesystem.hpp>
48 37
49 namespace Orthanc 38 namespace Orthanc
50 { 39 {
51 namespace Toolbox
52 {
53 std::string GenerateUuid()
54 {
55 #ifdef WIN32
56 UUID uuid;
57 UuidCreate ( &uuid );
58
59 unsigned char * str;
60 UuidToStringA ( &uuid, &str );
61
62 std::string s( ( char* ) str );
63
64 RpcStringFreeA ( &str );
65 #else
66 uuid_t uuid;
67 uuid_generate_random ( uuid );
68 char s[37];
69 uuid_unparse ( uuid, s );
70 #endif
71 return s;
72 }
73
74
75 bool IsUuid(const std::string& str)
76 {
77 if (str.size() != 36)
78 {
79 return false;
80 }
81
82 for (size_t i = 0; i < str.length(); i++)
83 {
84 if (i == 8 || i == 13 || i == 18 || i == 23)
85 {
86 if (str[i] != '-')
87 return false;
88 }
89 else
90 {
91 if (!isalnum(str[i]))
92 return false;
93 }
94 }
95
96 return true;
97 }
98
99
100 bool StartsWithUuid(const std::string& str)
101 {
102 if (str.size() < 36)
103 {
104 return false;
105 }
106
107 if (str.size() == 36)
108 {
109 return IsUuid(str);
110 }
111
112 assert(str.size() > 36);
113 if (!isspace(str[36]))
114 {
115 return false;
116 }
117
118 return IsUuid(str.substr(0, 36));
119 }
120 }
121
122
123 static std::string CreateTemporaryPath(const char* extension) 40 static std::string CreateTemporaryPath(const char* extension)
124 { 41 {
125 #if BOOST_HAS_FILESYSTEM_V3 == 1 42 #if BOOST_HAS_FILESYSTEM_V3 == 1
126 boost::filesystem::path tmpDir = boost::filesystem::temp_directory_path(); 43 boost::filesystem::path tmpDir = boost::filesystem::temp_directory_path();
127 #elif defined(__linux__) 44 #elif defined(__linux__)