Mercurial > hg > orthanc
annotate Core/Toolbox.cpp @ 89:74156c8b6f00
dynamic zlib
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 26 Sep 2012 18:01:41 +0200 |
parents | 8517e2c44283 |
children | 3b45473c0a73 |
rev | line source |
---|---|
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 "Toolbox.h" | |
22 | |
59 | 23 #include "OrthancException.h" |
0 | 24 |
25 #include <string.h> | |
26 #include <boost/filesystem.hpp> | |
27 #include <boost/filesystem/fstream.hpp> | |
28 #include <algorithm> | |
29 | |
30 #if defined(_WIN32) | |
31 #include <windows.h> | |
32 #endif | |
33 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
34 #if defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
35 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
36 #include <limits.h> /* PATH_MAX */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
37 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
38 |
0 | 39 #if defined(__linux) |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
40 #include <limits.h> /* PATH_MAX */ |
10 | 41 #include <signal.h> |
0 | 42 #include <unistd.h> |
43 #endif | |
44 | |
22 | 45 #include "../Resources/md5/md5.h" |
24 | 46 #include "../Resources/base64/base64.h" |
0 | 47 |
59 | 48 namespace Orthanc |
0 | 49 { |
50 static bool finish; | |
51 | |
10 | 52 #if defined(_WIN32) |
22 | 53 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) |
54 { | |
55 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx | |
56 finish = true; | |
57 return true; | |
58 } | |
59 #else | |
0 | 60 static void SignalHandler(int) |
61 { | |
62 finish = true; | |
63 } | |
22 | 64 #endif |
0 | 65 |
66 void Toolbox::Sleep(uint32_t seconds) | |
67 { | |
68 #if defined(_WIN32) | |
69 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000)); | |
70 #elif defined(__linux) | |
71 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000)); | |
72 #else | |
73 #error Support your platform here | |
74 #endif | |
75 } | |
76 | |
77 void Toolbox::USleep(uint64_t microSeconds) | |
78 { | |
79 #if defined(_WIN32) | |
8 | 80 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); |
0 | 81 #elif defined(__linux) |
82 usleep(microSeconds); | |
83 #else | |
84 #error Support your platform here | |
85 #endif | |
86 } | |
87 | |
88 | |
89 void Toolbox::ServerBarrier() | |
90 { | |
10 | 91 #if defined(_WIN32) |
92 SetConsoleCtrlHandler(ConsoleControlHandler, true); | |
93 #else | |
0 | 94 signal(SIGINT, SignalHandler); |
95 signal(SIGQUIT, SignalHandler); | |
96 #endif | |
97 | |
98 finish = false; | |
99 while (!finish) | |
100 { | |
101 USleep(100000); | |
102 } | |
103 | |
10 | 104 #if defined(_WIN32) |
105 SetConsoleCtrlHandler(ConsoleControlHandler, false); | |
106 #else | |
0 | 107 signal(SIGINT, NULL); |
108 signal(SIGQUIT, NULL); | |
109 #endif | |
110 } | |
111 | |
112 | |
113 | |
114 void Toolbox::ToUpperCase(std::string& s) | |
115 { | |
116 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
117 } | |
118 | |
119 | |
120 void Toolbox::ToLowerCase(std::string& s) | |
121 { | |
122 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
123 } | |
124 | |
125 | |
126 | |
127 void Toolbox::ReadFile(std::string& content, | |
128 const std::string& path) | |
129 { | |
130 boost::filesystem::ifstream f; | |
131 f.open(path, std::ifstream::in | std::ios::binary); | |
132 if (!f.good()) | |
133 { | |
59 | 134 throw OrthancException("Unable to open a file"); |
0 | 135 } |
136 | |
137 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
138 f.seekg(0, std::ios::end); | |
139 std::streamsize size = f.tellg(); | |
140 f.seekg(0, std::ios::beg); | |
141 | |
142 content.resize(size); | |
143 if (size != 0) | |
144 { | |
145 f.read(reinterpret_cast<char*>(&content[0]), size); | |
146 } | |
147 | |
148 f.close(); | |
149 } | |
150 | |
151 | |
152 void Toolbox::RemoveFile(const std::string& path) | |
153 { | |
154 if (boost::filesystem::exists(path)) | |
155 { | |
156 if (boost::filesystem::is_regular_file(path)) | |
157 boost::filesystem::remove(path); | |
158 else | |
59 | 159 throw OrthancException("The path is not a regular file: " + path); |
0 | 160 } |
161 } | |
162 | |
163 | |
164 | |
165 void Toolbox::SplitUriComponents(UriComponents& components, | |
166 const std::string& uri) | |
167 { | |
168 static const char URI_SEPARATOR = '/'; | |
169 | |
170 components.clear(); | |
171 | |
172 if (uri.size() == 0 || | |
173 uri[0] != URI_SEPARATOR) | |
174 { | |
59 | 175 throw OrthancException(ErrorCode_UriSyntax); |
0 | 176 } |
177 | |
178 // Count the number of slashes in the URI to make an assumption | |
179 // about the number of components in the URI | |
180 unsigned int estimatedSize = 0; | |
181 for (unsigned int i = 0; i < uri.size(); i++) | |
182 { | |
183 if (uri[i] == URI_SEPARATOR) | |
184 estimatedSize++; | |
185 } | |
186 | |
187 components.reserve(estimatedSize - 1); | |
188 | |
189 unsigned int start = 1; | |
190 unsigned int end = 1; | |
191 while (end < uri.size()) | |
192 { | |
193 // This is the loop invariant | |
194 assert(uri[start - 1] == '/' && (end >= start)); | |
195 | |
196 if (uri[end] == '/') | |
197 { | |
198 components.push_back(std::string(&uri[start], end - start)); | |
199 end++; | |
200 start = end; | |
201 } | |
202 else | |
203 { | |
204 end++; | |
205 } | |
206 } | |
207 | |
208 if (start < uri.size()) | |
209 { | |
210 components.push_back(std::string(&uri[start], end - start)); | |
211 } | |
212 } | |
213 | |
214 | |
215 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
216 const UriComponents& testedUri) | |
217 { | |
218 if (testedUri.size() < baseUri.size()) | |
219 { | |
220 return false; | |
221 } | |
222 | |
223 for (size_t i = 0; i < baseUri.size(); i++) | |
224 { | |
225 if (baseUri[i] != testedUri[i]) | |
226 return false; | |
227 } | |
228 | |
229 return true; | |
230 } | |
231 | |
232 | |
233 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
234 { | |
235 std::string contentType; | |
236 size_t lastDot = path.rfind('.'); | |
237 size_t lastSlash = path.rfind('/'); | |
238 | |
239 if (lastDot == std::string::npos || | |
240 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
241 { | |
242 // No trailing dot, unable to detect the content type | |
243 } | |
244 else | |
245 { | |
246 const char* extension = &path[lastDot + 1]; | |
247 | |
248 // http://en.wikipedia.org/wiki/Mime_types | |
249 // Text types | |
250 if (!strcmp(extension, "txt")) | |
251 contentType = "text/plain"; | |
252 else if (!strcmp(extension, "html")) | |
253 contentType = "text/html"; | |
254 else if (!strcmp(extension, "xml")) | |
255 contentType = "text/xml"; | |
256 else if (!strcmp(extension, "css")) | |
257 contentType = "text/css"; | |
258 | |
259 // Application types | |
260 else if (!strcmp(extension, "js")) | |
261 contentType = "application/javascript"; | |
262 else if (!strcmp(extension, "json")) | |
263 contentType = "application/json"; | |
264 else if (!strcmp(extension, "pdf")) | |
265 contentType = "application/pdf"; | |
266 | |
267 // Images types | |
268 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
269 contentType = "image/jpeg"; | |
270 else if (!strcmp(extension, "gif")) | |
271 contentType = "image/gif"; | |
272 else if (!strcmp(extension, "png")) | |
273 contentType = "image/png"; | |
274 } | |
275 | |
276 return contentType; | |
277 } | |
278 | |
279 | |
280 std::string Toolbox::FlattenUri(const UriComponents& components, | |
281 size_t fromLevel) | |
282 { | |
283 if (components.size() <= fromLevel) | |
284 { | |
285 return "/"; | |
286 } | |
287 else | |
288 { | |
289 std::string r; | |
290 | |
291 for (size_t i = fromLevel; i < components.size(); i++) | |
292 { | |
293 r += "/" + components[i]; | |
294 } | |
295 | |
296 return r; | |
297 } | |
298 } | |
299 | |
300 | |
301 | |
302 uint64_t Toolbox::GetFileSize(const std::string& path) | |
303 { | |
304 try | |
305 { | |
306 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
307 } | |
308 catch (boost::filesystem::filesystem_error) | |
309 { | |
59 | 310 throw OrthancException(ErrorCode_InexistentFile); |
0 | 311 } |
312 } | |
22 | 313 |
314 | |
315 static char GetHexadecimalCharacter(uint8_t value) | |
316 { | |
317 assert(value < 16); | |
318 | |
319 if (value < 10) | |
320 return value + '0'; | |
321 else | |
322 return (value - 10) + 'a'; | |
323 } | |
324 | |
23 | 325 |
22 | 326 void Toolbox::ComputeMD5(std::string& result, |
327 const std::string& data) | |
328 { | |
329 md5_state_s state; | |
330 md5_init(&state); | |
331 | |
332 if (data.size() > 0) | |
333 { | |
334 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), | |
335 static_cast<int>(data.size())); | |
336 } | |
337 | |
338 md5_byte_t actualHash[16]; | |
339 md5_finish(&state, actualHash); | |
340 | |
341 result.resize(32); | |
342 for (unsigned int i = 0; i < 16; i++) | |
343 { | |
344 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
345 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
346 } | |
347 } | |
24 | 348 |
349 | |
350 std::string Toolbox::EncodeBase64(const std::string& data) | |
351 { | |
86 | 352 return base64_encode(data); |
24 | 353 } |
354 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
355 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
356 #if defined(_WIN32) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
357 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
358 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
359 // 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
|
360 // required buffer size, so we use a big constant |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
361 std::vector<char> buffer(32768); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
362 /*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
|
363 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
364 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
365 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
366 #elif defined(__linux) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
367 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
368 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
369 std::vector<char> buffer(PATH_MAX + 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
370 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
|
371 if (bytes == 0) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
372 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
373 throw OrthancException("Unable to get the path to the executable"); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
374 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
375 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
376 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
377 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
378 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
379 #elif defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
380 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
381 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
382 char pathbuf[PATH_MAX + 1]; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
383 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
384 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
385 _NSGetExecutablePath( pathbuf, &bufsize); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
386 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
387 return std::string(pathbuf); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
388 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
389 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
390 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
391 #error Support your platform here |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
392 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
393 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
394 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
395 std::string Toolbox::GetDirectoryOfExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
396 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
397 boost::filesystem::path p(GetPathToExecutable()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
398 return p.parent_path().string(); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
399 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
400 |
0 | 401 } |