Mercurial > hg > orthanc
annotate Core/Toolbox.cpp @ 700:0fe6632864b9
ubuntu 13.10
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 07 Feb 2014 21:34:09 +0100 |
parents | 01d8611c4a60 |
children | 203157cb4fde |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 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. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "Toolbox.h" | |
34 | |
59 | 35 #include "OrthancException.h" |
0 | 36 |
453 | 37 #include <stdint.h> |
0 | 38 #include <string.h> |
39 #include <boost/filesystem.hpp> | |
40 #include <boost/filesystem/fstream.hpp> | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
41 #include <boost/date_time/posix_time/posix_time.hpp> |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
42 #include <boost/uuid/sha1.hpp> |
0 | 43 #include <algorithm> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
44 #include <ctype.h> |
608 | 45 #include <boost/regex.hpp> |
0 | 46 |
47 #if defined(_WIN32) | |
48 #include <windows.h> | |
49 #endif | |
50 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
51 #if defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
52 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
53 #include <limits.h> /* PATH_MAX */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
54 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
55 |
0 | 56 #if defined(__linux) |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
57 #include <limits.h> /* PATH_MAX */ |
10 | 58 #include <signal.h> |
0 | 59 #include <unistd.h> |
60 #endif | |
61 | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
62 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
63 #include <boost/locale.hpp> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
64 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
65 #include <iconv.h> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
66 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
67 |
22 | 68 #include "../Resources/md5/md5.h" |
24 | 69 #include "../Resources/base64/base64.h" |
0 | 70 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
71 |
570 | 72 #ifdef _MSC_VER |
73 // Patch for the missing "_strtoll" symbol when compiling with Visual Studio | |
74 extern "C" | |
75 { | |
76 int64_t _strtoi64(const char *nptr, char **endptr, int base); | |
77 int64_t strtoll(const char *nptr, char **endptr, int base) | |
78 { | |
79 return _strtoi64(nptr, endptr, base); | |
80 } | |
81 } | |
568 | 82 #endif |
83 | |
84 | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
85 #if BOOST_HAS_LOCALE == 0 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
86 namespace |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
87 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
88 class IconvRabi |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
89 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
90 private: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
91 iconv_t context_; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
92 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
93 public: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
94 IconvRabi(const char* tocode, const char* fromcode) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
95 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
96 context_ = iconv_open(tocode, fromcode); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
97 if (!context_) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
98 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
99 throw Orthanc::OrthancException("Unknown code page"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
100 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
101 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
102 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
103 ~IconvRabi() |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
104 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
105 iconv_close(context_); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
106 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
107 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
108 std::string Convert(const std::string& source) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
109 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
110 if (source.size() == 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
111 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
112 return ""; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
113 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
114 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
115 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
116 char* sourcePos = const_cast<char*>(&source[0]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
117 size_t sourceLeft = source.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
118 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
119 std::vector<char> storage(source.size() + 10); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
120 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
121 while (sourceLeft > 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
122 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
123 char* tmp = &storage[0]; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
124 size_t outputLeft = storage.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
125 size_t err = iconv(context_, &sourcePos, &sourceLeft, &tmp, &outputLeft); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
126 if (err < 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
127 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
128 throw Orthanc::OrthancException("Bad character in sequence"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
129 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
130 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
131 size_t count = storage.size() - outputLeft; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
132 result += std::string(&storage[0], count); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
133 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
134 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
135 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
136 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
137 }; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
138 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
139 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
140 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
141 |
59 | 142 namespace Orthanc |
0 | 143 { |
144 static bool finish; | |
145 | |
10 | 146 #if defined(_WIN32) |
22 | 147 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) |
148 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
149 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
150 finish = true; |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
151 return true; |
22 | 152 } |
153 #else | |
0 | 154 static void SignalHandler(int) |
155 { | |
156 finish = true; | |
157 } | |
22 | 158 #endif |
0 | 159 |
160 void Toolbox::Sleep(uint32_t seconds) | |
161 { | |
162 #if defined(_WIN32) | |
163 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000)); | |
164 #elif defined(__linux) | |
165 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000)); | |
166 #else | |
167 #error Support your platform here | |
168 #endif | |
169 } | |
170 | |
171 void Toolbox::USleep(uint64_t microSeconds) | |
172 { | |
173 #if defined(_WIN32) | |
8 | 174 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); |
0 | 175 #elif defined(__linux) |
176 usleep(microSeconds); | |
177 #else | |
178 #error Support your platform here | |
179 #endif | |
180 } | |
181 | |
182 | |
183 void Toolbox::ServerBarrier() | |
184 { | |
10 | 185 #if defined(_WIN32) |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
186 SetConsoleCtrlHandler(ConsoleControlHandler, true); |
10 | 187 #else |
0 | 188 signal(SIGINT, SignalHandler); |
189 signal(SIGQUIT, SignalHandler); | |
686 | 190 signal(SIGTERM, SignalHandler); |
0 | 191 #endif |
192 | |
193 finish = false; | |
194 while (!finish) | |
195 { | |
196 USleep(100000); | |
197 } | |
198 | |
10 | 199 #if defined(_WIN32) |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
200 SetConsoleCtrlHandler(ConsoleControlHandler, false); |
10 | 201 #else |
0 | 202 signal(SIGINT, NULL); |
203 signal(SIGQUIT, NULL); | |
686 | 204 signal(SIGTERM, NULL); |
0 | 205 #endif |
206 } | |
207 | |
208 | |
209 | |
210 void Toolbox::ToUpperCase(std::string& s) | |
211 { | |
212 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
213 } | |
214 | |
215 | |
216 void Toolbox::ToLowerCase(std::string& s) | |
217 { | |
218 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
219 } | |
220 | |
221 | |
690
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
222 void Toolbox::ToUpperCase(std::string& result, |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
223 const std::string& source) |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
224 { |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
225 result = source; |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
226 ToUpperCase(result); |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
227 } |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
228 |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
229 void Toolbox::ToLowerCase(std::string& result, |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
230 const std::string& source) |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
231 { |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
232 result = source; |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
233 ToLowerCase(result); |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
234 } |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
235 |
0 | 236 |
237 void Toolbox::ReadFile(std::string& content, | |
238 const std::string& path) | |
239 { | |
240 boost::filesystem::ifstream f; | |
483
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
241 f.open(path, std::ifstream::in | std::ifstream::binary); |
0 | 242 if (!f.good()) |
243 { | |
483
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
244 throw OrthancException(ErrorCode_InexistentFile); |
0 | 245 } |
246 | |
247 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
248 f.seekg(0, std::ios::end); | |
249 std::streamsize size = f.tellg(); | |
250 f.seekg(0, std::ios::beg); | |
251 | |
252 content.resize(size); | |
253 if (size != 0) | |
254 { | |
255 f.read(reinterpret_cast<char*>(&content[0]), size); | |
256 } | |
257 | |
258 f.close(); | |
259 } | |
260 | |
261 | |
483
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
262 void Toolbox::WriteFile(const std::string& content, |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
263 const std::string& path) |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
264 { |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
265 boost::filesystem::ofstream f; |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
266 f.open(path, std::ofstream::binary); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
267 if (!f.good()) |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
268 { |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
269 throw OrthancException(ErrorCode_CannotWriteFile); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
270 } |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
271 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
272 if (content.size() != 0) |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
273 { |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
274 f.write(content.c_str(), content.size()); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
275 } |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
276 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
277 f.close(); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
278 } |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
279 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
280 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
281 |
0 | 282 void Toolbox::RemoveFile(const std::string& path) |
283 { | |
284 if (boost::filesystem::exists(path)) | |
285 { | |
286 if (boost::filesystem::is_regular_file(path)) | |
287 boost::filesystem::remove(path); | |
288 else | |
59 | 289 throw OrthancException("The path is not a regular file: " + path); |
0 | 290 } |
291 } | |
292 | |
293 | |
294 | |
295 void Toolbox::SplitUriComponents(UriComponents& components, | |
296 const std::string& uri) | |
297 { | |
298 static const char URI_SEPARATOR = '/'; | |
299 | |
300 components.clear(); | |
301 | |
302 if (uri.size() == 0 || | |
303 uri[0] != URI_SEPARATOR) | |
304 { | |
59 | 305 throw OrthancException(ErrorCode_UriSyntax); |
0 | 306 } |
307 | |
308 // Count the number of slashes in the URI to make an assumption | |
309 // about the number of components in the URI | |
310 unsigned int estimatedSize = 0; | |
311 for (unsigned int i = 0; i < uri.size(); i++) | |
312 { | |
313 if (uri[i] == URI_SEPARATOR) | |
314 estimatedSize++; | |
315 } | |
316 | |
317 components.reserve(estimatedSize - 1); | |
318 | |
319 unsigned int start = 1; | |
320 unsigned int end = 1; | |
321 while (end < uri.size()) | |
322 { | |
323 // This is the loop invariant | |
324 assert(uri[start - 1] == '/' && (end >= start)); | |
325 | |
326 if (uri[end] == '/') | |
327 { | |
328 components.push_back(std::string(&uri[start], end - start)); | |
329 end++; | |
330 start = end; | |
331 } | |
332 else | |
333 { | |
334 end++; | |
335 } | |
336 } | |
337 | |
338 if (start < uri.size()) | |
339 { | |
340 components.push_back(std::string(&uri[start], end - start)); | |
341 } | |
207 | 342 |
343 for (size_t i = 0; i < components.size(); i++) | |
344 { | |
345 if (components[i].size() == 0) | |
346 { | |
347 // Empty component, as in: "/coucou//e" | |
348 throw OrthancException(ErrorCode_UriSyntax); | |
349 } | |
350 } | |
0 | 351 } |
352 | |
353 | |
354 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
355 const UriComponents& testedUri) | |
356 { | |
357 if (testedUri.size() < baseUri.size()) | |
358 { | |
359 return false; | |
360 } | |
361 | |
362 for (size_t i = 0; i < baseUri.size(); i++) | |
363 { | |
364 if (baseUri[i] != testedUri[i]) | |
365 return false; | |
366 } | |
367 | |
368 return true; | |
369 } | |
370 | |
371 | |
372 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
373 { | |
374 std::string contentType; | |
375 size_t lastDot = path.rfind('.'); | |
376 size_t lastSlash = path.rfind('/'); | |
377 | |
378 if (lastDot == std::string::npos || | |
379 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
380 { | |
381 // No trailing dot, unable to detect the content type | |
382 } | |
383 else | |
384 { | |
385 const char* extension = &path[lastDot + 1]; | |
386 | |
387 // http://en.wikipedia.org/wiki/Mime_types | |
388 // Text types | |
389 if (!strcmp(extension, "txt")) | |
390 contentType = "text/plain"; | |
391 else if (!strcmp(extension, "html")) | |
392 contentType = "text/html"; | |
393 else if (!strcmp(extension, "xml")) | |
394 contentType = "text/xml"; | |
395 else if (!strcmp(extension, "css")) | |
396 contentType = "text/css"; | |
397 | |
398 // Application types | |
399 else if (!strcmp(extension, "js")) | |
400 contentType = "application/javascript"; | |
401 else if (!strcmp(extension, "json")) | |
402 contentType = "application/json"; | |
403 else if (!strcmp(extension, "pdf")) | |
404 contentType = "application/pdf"; | |
405 | |
406 // Images types | |
407 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
408 contentType = "image/jpeg"; | |
409 else if (!strcmp(extension, "gif")) | |
410 contentType = "image/gif"; | |
411 else if (!strcmp(extension, "png")) | |
412 contentType = "image/png"; | |
413 } | |
414 | |
415 return contentType; | |
416 } | |
417 | |
418 | |
419 std::string Toolbox::FlattenUri(const UriComponents& components, | |
420 size_t fromLevel) | |
421 { | |
422 if (components.size() <= fromLevel) | |
423 { | |
424 return "/"; | |
425 } | |
426 else | |
427 { | |
428 std::string r; | |
429 | |
430 for (size_t i = fromLevel; i < components.size(); i++) | |
431 { | |
432 r += "/" + components[i]; | |
433 } | |
434 | |
435 return r; | |
436 } | |
437 } | |
438 | |
439 | |
440 | |
441 uint64_t Toolbox::GetFileSize(const std::string& path) | |
442 { | |
443 try | |
444 { | |
445 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
446 } | |
447 catch (boost::filesystem::filesystem_error) | |
448 { | |
59 | 449 throw OrthancException(ErrorCode_InexistentFile); |
0 | 450 } |
451 } | |
22 | 452 |
453 | |
454 static char GetHexadecimalCharacter(uint8_t value) | |
455 { | |
456 assert(value < 16); | |
457 | |
458 if (value < 10) | |
459 return value + '0'; | |
460 else | |
461 return (value - 10) + 'a'; | |
462 } | |
463 | |
23 | 464 |
22 | 465 void Toolbox::ComputeMD5(std::string& result, |
466 const std::string& data) | |
467 { | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
468 if (data.size() > 0) |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
469 { |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
470 ComputeMD5(result, &data[0], data.size()); |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
471 } |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
472 else |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
473 { |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
474 ComputeMD5(result, NULL, 0); |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
475 } |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
476 } |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
477 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
478 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
479 void Toolbox::ComputeMD5(std::string& result, |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
480 const void* data, |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
481 size_t length) |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
482 { |
22 | 483 md5_state_s state; |
484 md5_init(&state); | |
485 | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
486 if (length > 0) |
22 | 487 { |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
488 md5_append(&state, |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
489 reinterpret_cast<const md5_byte_t*>(data), |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
490 static_cast<int>(length)); |
22 | 491 } |
492 | |
493 md5_byte_t actualHash[16]; | |
494 md5_finish(&state, actualHash); | |
495 | |
496 result.resize(32); | |
497 for (unsigned int i = 0; i < 16; i++) | |
498 { | |
499 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
500 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
501 } | |
502 } | |
24 | 503 |
504 | |
505 std::string Toolbox::EncodeBase64(const std::string& data) | |
506 { | |
86 | 507 return base64_encode(data); |
24 | 508 } |
509 | |
365 | 510 std::string Toolbox::DecodeBase64(const std::string& data) |
511 { | |
512 return base64_decode(data); | |
513 } | |
514 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
515 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
516 #if defined(_WIN32) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
517 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
518 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
519 // Yes, this is ugly, but there is no simple way to get the |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
520 // required buffer size, so we use a big constant |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
521 std::vector<char> buffer(32768); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
522 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1)); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
523 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
524 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
525 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
526 #elif defined(__linux) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
527 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
528 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
529 std::vector<char> buffer(PATH_MAX + 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
530 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
531 if (bytes == 0) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
532 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
533 throw OrthancException("Unable to get the path to the executable"); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
534 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
535 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
536 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
537 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
538 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
539 #elif defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
540 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
541 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
542 char pathbuf[PATH_MAX + 1]; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
543 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
544 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
545 _NSGetExecutablePath( pathbuf, &bufsize); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
546 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
547 return std::string(pathbuf); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
548 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
549 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
550 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
551 #error Support your platform here |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
552 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
553 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
554 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
555 std::string Toolbox::GetDirectoryOfExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
556 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
557 boost::filesystem::path p(GetPathToExecutable()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
558 return p.parent_path().string(); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
559 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
560 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
561 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
562 std::string Toolbox::ConvertToUtf8(const std::string& source, |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
563 const char* fromEncoding) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
564 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
565 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
566 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
567 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
568 return boost::locale::conv::to_utf<char>(source, fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
569 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
570 catch (std::runtime_error&) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
571 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
572 // Bad input string or bad encoding |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
573 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
574 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
575 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
576 IconvRabi iconv("UTF-8", fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
577 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
578 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
579 return iconv.Convert(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
580 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
581 catch (OrthancException) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
582 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
583 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
584 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
585 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
586 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
587 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
588 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
589 std::string Toolbox::ConvertToAscii(const std::string& source) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
590 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
591 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
592 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
593 result.reserve(source.size()); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
594 for (size_t i = 0; i < source.size(); i++) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
595 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
596 if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i])) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
597 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
598 result.push_back(source[i]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
599 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
600 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
601 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
602 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
603 } |
177 | 604 |
605 void Toolbox::ComputeSHA1(std::string& result, | |
606 const std::string& data) | |
607 { | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
608 boost::uuids::detail::sha1 sha1; |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
609 |
177 | 610 if (data.size() > 0) |
611 { | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
612 sha1.process_bytes(&data[0], data.size()); |
177 | 613 } |
614 | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
615 unsigned int digest[5]; |
177 | 616 |
617 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
618 assert(sizeof(unsigned int) == 4 && sizeof(digest) == (160 / 8)); |
177 | 619 |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
620 sha1.get_digest(digest); |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
621 |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
622 result.resize(8 * 5 + 4); |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
623 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
624 digest[0], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
625 digest[1], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
626 digest[2], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
627 digest[3], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
628 digest[4]); |
177 | 629 } |
630 | |
402 | 631 bool Toolbox::IsSHA1(const std::string& str) |
632 { | |
633 if (str.size() != 44) | |
634 { | |
635 return false; | |
636 } | |
637 | |
638 for (unsigned int i = 0; i < 44; i++) | |
639 { | |
640 if (i == 8 || | |
641 i == 17 || | |
642 i == 26 || | |
643 i == 35) | |
644 { | |
645 if (str[i] != '-') | |
646 return false; | |
647 } | |
648 else | |
649 { | |
650 if (!isalnum(str[i])) | |
651 return false; | |
652 } | |
653 } | |
654 | |
655 return true; | |
656 } | |
657 | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
658 std::string Toolbox::GetNowIsoString() |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
659 { |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
660 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
661 return boost::posix_time::to_iso_string(now); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
662 } |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
663 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
664 std::string Toolbox::StripSpaces(const std::string& source) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
665 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
666 size_t first = 0; |
177 | 667 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
668 while (first < source.length() && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
669 isspace(source[first])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
670 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
671 first++; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
672 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
673 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
674 if (first == source.length()) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
675 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
676 // String containing only spaces |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
677 return ""; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
678 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
679 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
680 size_t last = source.length(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
681 while (last > first && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
682 isspace(source[last - 1])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
683 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
684 last--; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
685 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
686 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
687 assert(first <= last); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
688 return source.substr(first, last - first); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
689 } |
336 | 690 |
691 | |
337 | 692 static char Hex2Dec(char c) |
336 | 693 { |
694 return ((c >= '0' && c <= '9') ? c - '0' : | |
695 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10)); | |
696 } | |
697 | |
698 void Toolbox::UrlDecode(std::string& s) | |
699 { | |
700 // http://en.wikipedia.org/wiki/Percent-encoding | |
701 // http://www.w3schools.com/tags/ref_urlencode.asp | |
702 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c | |
703 | |
704 if (s.size() == 0) | |
705 { | |
706 return; | |
707 } | |
708 | |
709 size_t source = 0; | |
710 size_t target = 0; | |
711 | |
712 while (source < s.size()) | |
713 { | |
714 if (s[source] == '%' && | |
715 source + 2 < s.size() && | |
716 isalnum(s[source + 1]) && | |
717 isalnum(s[source + 2])) | |
718 { | |
719 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]); | |
720 source += 3; | |
721 target += 1; | |
722 } | |
723 else | |
724 { | |
725 if (s[source] == '+') | |
726 s[target] = ' '; | |
727 else | |
728 s[target] = s[source]; | |
729 | |
730 source++; | |
731 target++; | |
732 } | |
733 } | |
734 | |
735 s.resize(target); | |
736 } | |
453 | 737 |
738 | |
739 Endianness Toolbox::DetectEndianness() | |
740 { | |
741 // http://sourceforge.net/p/predef/wiki/Endianness/ | |
742 | |
743 uint8_t buffer[4]; | |
744 | |
745 buffer[0] = 0x00; | |
746 buffer[1] = 0x01; | |
747 buffer[2] = 0x02; | |
748 buffer[3] = 0x03; | |
749 | |
750 switch (*((uint32_t *)buffer)) | |
751 { | |
752 case 0x00010203: | |
753 return Endianness_Big; | |
754 | |
755 case 0x03020100: | |
756 return Endianness_Little; | |
757 | |
758 default: | |
759 throw OrthancException(ErrorCode_NotImplemented); | |
760 } | |
761 } | |
608 | 762 |
763 | |
764 std::string Toolbox::WildcardToRegularExpression(const std::string& source) | |
765 { | |
766 // TODO - Speed up this with a regular expression | |
767 | |
768 std::string result = source; | |
769 | |
770 // Escape all special characters | |
771 boost::replace_all(result, "\\", "\\\\"); | |
772 boost::replace_all(result, "^", "\\^"); | |
773 boost::replace_all(result, ".", "\\."); | |
774 boost::replace_all(result, "$", "\\$"); | |
775 boost::replace_all(result, "|", "\\|"); | |
776 boost::replace_all(result, "(", "\\("); | |
777 boost::replace_all(result, ")", "\\)"); | |
778 boost::replace_all(result, "[", "\\["); | |
779 boost::replace_all(result, "]", "\\]"); | |
780 boost::replace_all(result, "+", "\\+"); | |
781 boost::replace_all(result, "/", "\\/"); | |
782 boost::replace_all(result, "{", "\\{"); | |
783 boost::replace_all(result, "}", "\\}"); | |
784 | |
785 // Convert wildcards '*' and '?' to their regex equivalents | |
786 boost::replace_all(result, "?", "."); | |
787 boost::replace_all(result, "*", ".*"); | |
788 | |
789 return result; | |
790 } | |
791 | |
792 | |
793 | |
794 void Toolbox::TokenizeString(std::vector<std::string>& result, | |
795 const std::string& value, | |
796 char separator) | |
797 { | |
798 result.clear(); | |
799 | |
800 std::string currentItem; | |
801 | |
802 for (size_t i = 0; i < value.size(); i++) | |
803 { | |
804 if (value[i] == separator) | |
805 { | |
806 result.push_back(currentItem); | |
807 currentItem.clear(); | |
808 } | |
809 else | |
810 { | |
811 currentItem.push_back(value[i]); | |
812 } | |
813 } | |
814 | |
815 result.push_back(currentItem); | |
816 } | |
0 | 817 } |
608 | 818 |