0
|
1 /**
|
59
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU General Public License as
|
|
8 * published by the Free Software Foundation, either version 3 of the
|
|
9 * License, or (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "Uuid.h"
|
|
22
|
|
23 // http://stackoverflow.com/a/1626302
|
|
24
|
|
25 extern "C"
|
|
26 {
|
|
27 #ifdef WIN32
|
|
28 #include <rpc.h>
|
|
29 #else
|
|
30 #include <uuid/uuid.h>
|
|
31 #endif
|
|
32 }
|
|
33
|
82
|
34 #include <boost/filesystem.hpp>
|
|
35
|
59
|
36 namespace Orthanc
|
0
|
37 {
|
|
38 namespace Toolbox
|
|
39 {
|
|
40 std::string GenerateUuid()
|
|
41 {
|
|
42 #ifdef WIN32
|
|
43 UUID uuid;
|
|
44 UuidCreate ( &uuid );
|
|
45
|
|
46 unsigned char * str;
|
|
47 UuidToStringA ( &uuid, &str );
|
|
48
|
|
49 std::string s( ( char* ) str );
|
|
50
|
|
51 RpcStringFreeA ( &str );
|
|
52 #else
|
|
53 uuid_t uuid;
|
|
54 uuid_generate_random ( uuid );
|
|
55 char s[37];
|
|
56 uuid_unparse ( uuid, s );
|
|
57 #endif
|
|
58 return s;
|
|
59 }
|
|
60
|
|
61
|
|
62 bool IsUuid(const std::string& str)
|
|
63 {
|
|
64 if (str.size() != 36)
|
|
65 {
|
|
66 return false;
|
|
67 }
|
|
68
|
|
69 for (size_t i = 0; i < str.length(); i++)
|
|
70 {
|
|
71 if (i == 8 || i == 13 || i == 18 || i == 23)
|
|
72 {
|
|
73 if (str[i] != '-')
|
|
74 return false;
|
|
75 }
|
|
76 else
|
|
77 {
|
|
78 if (!isalnum(str[i]))
|
|
79 return false;
|
|
80 }
|
|
81 }
|
|
82
|
|
83 return true;
|
|
84 }
|
82
|
85
|
|
86
|
|
87 TemporaryFile::TemporaryFile()
|
|
88 {
|
|
89 // We use UUID to create unique path to temporary files
|
|
90 boost::filesystem::path tmp = boost::filesystem::temp_directory_path();
|
|
91 tmp /= "Orthanc-" + Orthanc::Toolbox::GenerateUuid();
|
|
92 path_ = tmp.string();
|
|
93 }
|
|
94
|
|
95
|
|
96 TemporaryFile::~TemporaryFile()
|
|
97 {
|
|
98 boost::filesystem::remove(path_);
|
|
99 }
|
0
|
100 }
|
|
101 }
|