Mercurial > hg > orthanc-webviewer
comparison Orthanc/Core/Toolbox.cpp @ 25:15acbf5e7545
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 02 Jun 2015 11:16:30 +0200 |
parents | |
children | 7c6cf09d838b |
comparison
equal
deleted
inserted
replaced
24:ed9acb0f938e | 25:15acbf5e7545 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, 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 * 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. | |
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 "PrecompiledHeaders.h" | |
34 #include "Toolbox.h" | |
35 | |
36 #include "OrthancException.h" | |
37 | |
38 #include <string> | |
39 #include <stdint.h> | |
40 #include <string.h> | |
41 #include <boost/filesystem.hpp> | |
42 #include <boost/filesystem/fstream.hpp> | |
43 #include <boost/uuid/sha1.hpp> | |
44 #include <boost/lexical_cast.hpp> | |
45 #include <algorithm> | |
46 #include <ctype.h> | |
47 | |
48 #if BOOST_HAS_DATE_TIME == 1 | |
49 #include <boost/date_time/posix_time/posix_time.hpp> | |
50 #endif | |
51 | |
52 #if BOOST_HAS_REGEX == 1 | |
53 #include <boost/regex.hpp> | |
54 #endif | |
55 | |
56 #if HAVE_GOOGLE_LOG == 1 | |
57 #include <glog/logging.h> | |
58 #endif | |
59 | |
60 #if defined(_WIN32) | |
61 #include <windows.h> | |
62 #include <process.h> // For "_spawnvp()" | |
63 #else | |
64 #include <unistd.h> // For "execvp()" | |
65 #include <sys/wait.h> // For "waitpid()" | |
66 #endif | |
67 | |
68 #if defined(__APPLE__) && defined(__MACH__) | |
69 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ | |
70 #include <limits.h> /* PATH_MAX */ | |
71 #endif | |
72 | |
73 #if defined(__linux) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) | |
74 #include <limits.h> /* PATH_MAX */ | |
75 #include <signal.h> | |
76 #include <unistd.h> | |
77 #endif | |
78 | |
79 #if BOOST_HAS_LOCALE != 1 | |
80 #error Since version 0.7.6, Orthanc entirely relies on boost::locale | |
81 #endif | |
82 | |
83 #include <boost/locale.hpp> | |
84 | |
85 #include "../Resources/ThirdParty/md5/md5.h" | |
86 #include "../Resources/ThirdParty/base64/base64.h" | |
87 | |
88 | |
89 #if defined(_MSC_VER) && (_MSC_VER < 1800) | |
90 // Patch for the missing "_strtoll" symbol when compiling with Visual Studio < 2013 | |
91 extern "C" | |
92 { | |
93 int64_t _strtoi64(const char *nptr, char **endptr, int base); | |
94 int64_t strtoll(const char *nptr, char **endptr, int base) | |
95 { | |
96 return _strtoi64(nptr, endptr, base); | |
97 } | |
98 } | |
99 #endif | |
100 | |
101 | |
102 #if ORTHANC_PUGIXML_ENABLED == 1 | |
103 #include "ChunkedBuffer.h" | |
104 #include <pugixml.hpp> | |
105 #endif | |
106 | |
107 | |
108 namespace Orthanc | |
109 { | |
110 static bool finish; | |
111 | |
112 #if defined(_WIN32) | |
113 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) | |
114 { | |
115 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx | |
116 finish = true; | |
117 return true; | |
118 } | |
119 #else | |
120 static void SignalHandler(int) | |
121 { | |
122 finish = true; | |
123 } | |
124 #endif | |
125 | |
126 void Toolbox::USleep(uint64_t microSeconds) | |
127 { | |
128 #if defined(_WIN32) | |
129 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); | |
130 #elif defined(__linux) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) | |
131 usleep(microSeconds); | |
132 #else | |
133 #error Support your platform here | |
134 #endif | |
135 } | |
136 | |
137 | |
138 static void ServerBarrierInternal(const bool* stopFlag) | |
139 { | |
140 #if defined(_WIN32) | |
141 SetConsoleCtrlHandler(ConsoleControlHandler, true); | |
142 #else | |
143 signal(SIGINT, SignalHandler); | |
144 signal(SIGQUIT, SignalHandler); | |
145 signal(SIGTERM, SignalHandler); | |
146 #endif | |
147 | |
148 // Active loop that awakens every 100ms | |
149 finish = false; | |
150 while (!(*stopFlag || finish)) | |
151 { | |
152 Toolbox::USleep(100 * 1000); | |
153 } | |
154 | |
155 #if defined(_WIN32) | |
156 SetConsoleCtrlHandler(ConsoleControlHandler, false); | |
157 #else | |
158 signal(SIGINT, NULL); | |
159 signal(SIGQUIT, NULL); | |
160 signal(SIGTERM, NULL); | |
161 #endif | |
162 } | |
163 | |
164 | |
165 void Toolbox::ServerBarrier(const bool& stopFlag) | |
166 { | |
167 ServerBarrierInternal(&stopFlag); | |
168 } | |
169 | |
170 void Toolbox::ServerBarrier() | |
171 { | |
172 const bool stopFlag = false; | |
173 ServerBarrierInternal(&stopFlag); | |
174 } | |
175 | |
176 | |
177 void Toolbox::ToUpperCase(std::string& s) | |
178 { | |
179 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
180 } | |
181 | |
182 | |
183 void Toolbox::ToLowerCase(std::string& s) | |
184 { | |
185 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
186 } | |
187 | |
188 | |
189 void Toolbox::ToUpperCase(std::string& result, | |
190 const std::string& source) | |
191 { | |
192 result = source; | |
193 ToUpperCase(result); | |
194 } | |
195 | |
196 void Toolbox::ToLowerCase(std::string& result, | |
197 const std::string& source) | |
198 { | |
199 result = source; | |
200 ToLowerCase(result); | |
201 } | |
202 | |
203 | |
204 void Toolbox::ReadFile(std::string& content, | |
205 const std::string& path) | |
206 { | |
207 boost::filesystem::ifstream f; | |
208 f.open(path, std::ifstream::in | std::ifstream::binary); | |
209 if (!f.good()) | |
210 { | |
211 throw OrthancException(ErrorCode_InexistentFile); | |
212 } | |
213 | |
214 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
215 f.seekg(0, std::ios::end); | |
216 std::streamsize size = f.tellg(); | |
217 f.seekg(0, std::ios::beg); | |
218 | |
219 content.resize(size); | |
220 if (size != 0) | |
221 { | |
222 f.read(reinterpret_cast<char*>(&content[0]), size); | |
223 } | |
224 | |
225 f.close(); | |
226 } | |
227 | |
228 | |
229 void Toolbox::WriteFile(const std::string& content, | |
230 const std::string& path) | |
231 { | |
232 boost::filesystem::ofstream f; | |
233 f.open(path, std::ofstream::binary); | |
234 if (!f.good()) | |
235 { | |
236 throw OrthancException(ErrorCode_CannotWriteFile); | |
237 } | |
238 | |
239 if (content.size() != 0) | |
240 { | |
241 f.write(content.c_str(), content.size()); | |
242 } | |
243 | |
244 f.close(); | |
245 } | |
246 | |
247 | |
248 | |
249 void Toolbox::RemoveFile(const std::string& path) | |
250 { | |
251 if (boost::filesystem::exists(path)) | |
252 { | |
253 if (boost::filesystem::is_regular_file(path)) | |
254 boost::filesystem::remove(path); | |
255 else | |
256 throw OrthancException("The path is not a regular file: " + path); | |
257 } | |
258 } | |
259 | |
260 | |
261 | |
262 void Toolbox::SplitUriComponents(UriComponents& components, | |
263 const std::string& uri) | |
264 { | |
265 static const char URI_SEPARATOR = '/'; | |
266 | |
267 components.clear(); | |
268 | |
269 if (uri.size() == 0 || | |
270 uri[0] != URI_SEPARATOR) | |
271 { | |
272 throw OrthancException(ErrorCode_UriSyntax); | |
273 } | |
274 | |
275 // Count the number of slashes in the URI to make an assumption | |
276 // about the number of components in the URI | |
277 unsigned int estimatedSize = 0; | |
278 for (unsigned int i = 0; i < uri.size(); i++) | |
279 { | |
280 if (uri[i] == URI_SEPARATOR) | |
281 estimatedSize++; | |
282 } | |
283 | |
284 components.reserve(estimatedSize - 1); | |
285 | |
286 unsigned int start = 1; | |
287 unsigned int end = 1; | |
288 while (end < uri.size()) | |
289 { | |
290 // This is the loop invariant | |
291 assert(uri[start - 1] == '/' && (end >= start)); | |
292 | |
293 if (uri[end] == '/') | |
294 { | |
295 components.push_back(std::string(&uri[start], end - start)); | |
296 end++; | |
297 start = end; | |
298 } | |
299 else | |
300 { | |
301 end++; | |
302 } | |
303 } | |
304 | |
305 if (start < uri.size()) | |
306 { | |
307 components.push_back(std::string(&uri[start], end - start)); | |
308 } | |
309 | |
310 for (size_t i = 0; i < components.size(); i++) | |
311 { | |
312 if (components[i].size() == 0) | |
313 { | |
314 // Empty component, as in: "/coucou//e" | |
315 throw OrthancException(ErrorCode_UriSyntax); | |
316 } | |
317 } | |
318 } | |
319 | |
320 | |
321 void Toolbox::TruncateUri(UriComponents& target, | |
322 const UriComponents& source, | |
323 size_t fromLevel) | |
324 { | |
325 target.clear(); | |
326 | |
327 if (source.size() > fromLevel) | |
328 { | |
329 target.resize(source.size() - fromLevel); | |
330 | |
331 size_t j = 0; | |
332 for (size_t i = fromLevel; i < source.size(); i++, j++) | |
333 { | |
334 target[j] = source[i]; | |
335 } | |
336 | |
337 assert(j == target.size()); | |
338 } | |
339 } | |
340 | |
341 | |
342 | |
343 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
344 const UriComponents& testedUri) | |
345 { | |
346 if (testedUri.size() < baseUri.size()) | |
347 { | |
348 return false; | |
349 } | |
350 | |
351 for (size_t i = 0; i < baseUri.size(); i++) | |
352 { | |
353 if (baseUri[i] != testedUri[i]) | |
354 return false; | |
355 } | |
356 | |
357 return true; | |
358 } | |
359 | |
360 | |
361 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
362 { | |
363 std::string contentType; | |
364 size_t lastDot = path.rfind('.'); | |
365 size_t lastSlash = path.rfind('/'); | |
366 | |
367 if (lastDot == std::string::npos || | |
368 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
369 { | |
370 // No trailing dot, unable to detect the content type | |
371 } | |
372 else | |
373 { | |
374 const char* extension = &path[lastDot + 1]; | |
375 | |
376 // http://en.wikipedia.org/wiki/Mime_types | |
377 // Text types | |
378 if (!strcmp(extension, "txt")) | |
379 contentType = "text/plain"; | |
380 else if (!strcmp(extension, "html")) | |
381 contentType = "text/html"; | |
382 else if (!strcmp(extension, "xml")) | |
383 contentType = "text/xml"; | |
384 else if (!strcmp(extension, "css")) | |
385 contentType = "text/css"; | |
386 | |
387 // Application types | |
388 else if (!strcmp(extension, "js")) | |
389 contentType = "application/javascript"; | |
390 else if (!strcmp(extension, "json")) | |
391 contentType = "application/json"; | |
392 else if (!strcmp(extension, "pdf")) | |
393 contentType = "application/pdf"; | |
394 | |
395 // Images types | |
396 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
397 contentType = "image/jpeg"; | |
398 else if (!strcmp(extension, "gif")) | |
399 contentType = "image/gif"; | |
400 else if (!strcmp(extension, "png")) | |
401 contentType = "image/png"; | |
402 } | |
403 | |
404 return contentType; | |
405 } | |
406 | |
407 | |
408 std::string Toolbox::FlattenUri(const UriComponents& components, | |
409 size_t fromLevel) | |
410 { | |
411 if (components.size() <= fromLevel) | |
412 { | |
413 return "/"; | |
414 } | |
415 else | |
416 { | |
417 std::string r; | |
418 | |
419 for (size_t i = fromLevel; i < components.size(); i++) | |
420 { | |
421 r += "/" + components[i]; | |
422 } | |
423 | |
424 return r; | |
425 } | |
426 } | |
427 | |
428 | |
429 | |
430 uint64_t Toolbox::GetFileSize(const std::string& path) | |
431 { | |
432 try | |
433 { | |
434 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
435 } | |
436 catch (boost::filesystem::filesystem_error) | |
437 { | |
438 throw OrthancException(ErrorCode_InexistentFile); | |
439 } | |
440 } | |
441 | |
442 | |
443 static char GetHexadecimalCharacter(uint8_t value) | |
444 { | |
445 assert(value < 16); | |
446 | |
447 if (value < 10) | |
448 return value + '0'; | |
449 else | |
450 return (value - 10) + 'a'; | |
451 } | |
452 | |
453 | |
454 void Toolbox::ComputeMD5(std::string& result, | |
455 const std::string& data) | |
456 { | |
457 if (data.size() > 0) | |
458 { | |
459 ComputeMD5(result, &data[0], data.size()); | |
460 } | |
461 else | |
462 { | |
463 ComputeMD5(result, NULL, 0); | |
464 } | |
465 } | |
466 | |
467 | |
468 void Toolbox::ComputeMD5(std::string& result, | |
469 const void* data, | |
470 size_t length) | |
471 { | |
472 md5_state_s state; | |
473 md5_init(&state); | |
474 | |
475 if (length > 0) | |
476 { | |
477 md5_append(&state, | |
478 reinterpret_cast<const md5_byte_t*>(data), | |
479 static_cast<int>(length)); | |
480 } | |
481 | |
482 md5_byte_t actualHash[16]; | |
483 md5_finish(&state, actualHash); | |
484 | |
485 result.resize(32); | |
486 for (unsigned int i = 0; i < 16; i++) | |
487 { | |
488 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
489 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
490 } | |
491 } | |
492 | |
493 | |
494 void Toolbox::EncodeBase64(std::string& result, | |
495 const std::string& data) | |
496 { | |
497 result = base64_encode(data); | |
498 } | |
499 | |
500 void Toolbox::DecodeBase64(std::string& result, | |
501 const std::string& data) | |
502 { | |
503 result = base64_decode(data); | |
504 } | |
505 | |
506 | |
507 #if defined(_WIN32) | |
508 static std::string GetPathToExecutableInternal() | |
509 { | |
510 // Yes, this is ugly, but there is no simple way to get the | |
511 // required buffer size, so we use a big constant | |
512 std::vector<char> buffer(32768); | |
513 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1)); | |
514 return std::string(&buffer[0]); | |
515 } | |
516 | |
517 #elif defined(__linux) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) | |
518 static std::string GetPathToExecutableInternal() | |
519 { | |
520 std::vector<char> buffer(PATH_MAX + 1); | |
521 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1); | |
522 if (bytes == 0) | |
523 { | |
524 throw OrthancException("Unable to get the path to the executable"); | |
525 } | |
526 | |
527 return std::string(&buffer[0]); | |
528 } | |
529 | |
530 #elif defined(__APPLE__) && defined(__MACH__) | |
531 static std::string GetPathToExecutableInternal() | |
532 { | |
533 char pathbuf[PATH_MAX + 1]; | |
534 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); | |
535 | |
536 _NSGetExecutablePath( pathbuf, &bufsize); | |
537 | |
538 return std::string(pathbuf); | |
539 } | |
540 | |
541 #else | |
542 #error Support your platform here | |
543 #endif | |
544 | |
545 | |
546 std::string Toolbox::GetPathToExecutable() | |
547 { | |
548 boost::filesystem::path p(GetPathToExecutableInternal()); | |
549 return boost::filesystem::absolute(p).string(); | |
550 } | |
551 | |
552 | |
553 std::string Toolbox::GetDirectoryOfExecutable() | |
554 { | |
555 boost::filesystem::path p(GetPathToExecutableInternal()); | |
556 return boost::filesystem::absolute(p.parent_path()).string(); | |
557 } | |
558 | |
559 | |
560 std::string Toolbox::ConvertToUtf8(const std::string& source, | |
561 const Encoding sourceEncoding) | |
562 { | |
563 const char* encoding; | |
564 | |
565 | |
566 // http://bradleyross.users.sourceforge.net/docs/dicom/doc/src-html/org/dcm4che2/data/SpecificCharacterSet.html | |
567 switch (sourceEncoding) | |
568 { | |
569 case Encoding_Utf8: | |
570 // Already in UTF-8: No conversion is required | |
571 return source; | |
572 | |
573 case Encoding_Ascii: | |
574 return ConvertToAscii(source); | |
575 | |
576 case Encoding_Latin1: | |
577 encoding = "ISO-8859-1"; | |
578 break; | |
579 | |
580 case Encoding_Latin2: | |
581 encoding = "ISO-8859-2"; | |
582 break; | |
583 | |
584 case Encoding_Latin3: | |
585 encoding = "ISO-8859-3"; | |
586 break; | |
587 | |
588 case Encoding_Latin4: | |
589 encoding = "ISO-8859-4"; | |
590 break; | |
591 | |
592 case Encoding_Latin5: | |
593 encoding = "ISO-8859-9"; | |
594 break; | |
595 | |
596 case Encoding_Cyrillic: | |
597 encoding = "ISO-8859-5"; | |
598 break; | |
599 | |
600 case Encoding_Windows1251: | |
601 encoding = "WINDOWS-1251"; | |
602 break; | |
603 | |
604 case Encoding_Arabic: | |
605 encoding = "ISO-8859-6"; | |
606 break; | |
607 | |
608 case Encoding_Greek: | |
609 encoding = "ISO-8859-7"; | |
610 break; | |
611 | |
612 case Encoding_Hebrew: | |
613 encoding = "ISO-8859-8"; | |
614 break; | |
615 | |
616 case Encoding_Japanese: | |
617 encoding = "SHIFT-JIS"; | |
618 break; | |
619 | |
620 case Encoding_Chinese: | |
621 encoding = "GB18030"; | |
622 break; | |
623 | |
624 case Encoding_Thai: | |
625 encoding = "TIS620.2533-0"; | |
626 break; | |
627 | |
628 default: | |
629 throw OrthancException(ErrorCode_NotImplemented); | |
630 } | |
631 | |
632 try | |
633 { | |
634 return boost::locale::conv::to_utf<char>(source, encoding); | |
635 } | |
636 catch (std::runtime_error&) | |
637 { | |
638 // Bad input string or bad encoding | |
639 return ConvertToAscii(source); | |
640 } | |
641 } | |
642 | |
643 | |
644 std::string Toolbox::ConvertToAscii(const std::string& source) | |
645 { | |
646 std::string result; | |
647 | |
648 result.reserve(source.size() + 1); | |
649 for (size_t i = 0; i < source.size(); i++) | |
650 { | |
651 if (source[i] <= 127 && source[i] >= 0 && !iscntrl(source[i])) | |
652 { | |
653 result.push_back(source[i]); | |
654 } | |
655 } | |
656 | |
657 return result; | |
658 } | |
659 | |
660 void Toolbox::ComputeSHA1(std::string& result, | |
661 const std::string& data) | |
662 { | |
663 boost::uuids::detail::sha1 sha1; | |
664 | |
665 if (data.size() > 0) | |
666 { | |
667 sha1.process_bytes(&data[0], data.size()); | |
668 } | |
669 | |
670 unsigned int digest[5]; | |
671 | |
672 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide | |
673 assert(sizeof(unsigned int) == 4 && sizeof(digest) == (160 / 8)); | |
674 | |
675 sha1.get_digest(digest); | |
676 | |
677 result.resize(8 * 5 + 4); | |
678 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", | |
679 digest[0], | |
680 digest[1], | |
681 digest[2], | |
682 digest[3], | |
683 digest[4]); | |
684 } | |
685 | |
686 bool Toolbox::IsSHA1(const std::string& str) | |
687 { | |
688 if (str.size() != 44) | |
689 { | |
690 return false; | |
691 } | |
692 | |
693 for (unsigned int i = 0; i < 44; i++) | |
694 { | |
695 if (i == 8 || | |
696 i == 17 || | |
697 i == 26 || | |
698 i == 35) | |
699 { | |
700 if (str[i] != '-') | |
701 return false; | |
702 } | |
703 else | |
704 { | |
705 if (!isalnum(str[i])) | |
706 return false; | |
707 } | |
708 } | |
709 | |
710 return true; | |
711 } | |
712 | |
713 | |
714 #if BOOST_HAS_DATE_TIME == 1 | |
715 std::string Toolbox::GetNowIsoString() | |
716 { | |
717 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); | |
718 return boost::posix_time::to_iso_string(now); | |
719 } | |
720 #endif | |
721 | |
722 | |
723 std::string Toolbox::StripSpaces(const std::string& source) | |
724 { | |
725 size_t first = 0; | |
726 | |
727 while (first < source.length() && | |
728 isspace(source[first])) | |
729 { | |
730 first++; | |
731 } | |
732 | |
733 if (first == source.length()) | |
734 { | |
735 // String containing only spaces | |
736 return ""; | |
737 } | |
738 | |
739 size_t last = source.length(); | |
740 while (last > first && | |
741 isspace(source[last - 1])) | |
742 { | |
743 last--; | |
744 } | |
745 | |
746 assert(first <= last); | |
747 return source.substr(first, last - first); | |
748 } | |
749 | |
750 | |
751 static char Hex2Dec(char c) | |
752 { | |
753 return ((c >= '0' && c <= '9') ? c - '0' : | |
754 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10)); | |
755 } | |
756 | |
757 void Toolbox::UrlDecode(std::string& s) | |
758 { | |
759 // http://en.wikipedia.org/wiki/Percent-encoding | |
760 // http://www.w3schools.com/tags/ref_urlencode.asp | |
761 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c | |
762 | |
763 if (s.size() == 0) | |
764 { | |
765 return; | |
766 } | |
767 | |
768 size_t source = 0; | |
769 size_t target = 0; | |
770 | |
771 while (source < s.size()) | |
772 { | |
773 if (s[source] == '%' && | |
774 source + 2 < s.size() && | |
775 isalnum(s[source + 1]) && | |
776 isalnum(s[source + 2])) | |
777 { | |
778 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]); | |
779 source += 3; | |
780 target += 1; | |
781 } | |
782 else | |
783 { | |
784 if (s[source] == '+') | |
785 s[target] = ' '; | |
786 else | |
787 s[target] = s[source]; | |
788 | |
789 source++; | |
790 target++; | |
791 } | |
792 } | |
793 | |
794 s.resize(target); | |
795 } | |
796 | |
797 | |
798 Endianness Toolbox::DetectEndianness() | |
799 { | |
800 // http://sourceforge.net/p/predef/wiki/Endianness/ | |
801 | |
802 uint8_t buffer[4]; | |
803 | |
804 buffer[0] = 0x00; | |
805 buffer[1] = 0x01; | |
806 buffer[2] = 0x02; | |
807 buffer[3] = 0x03; | |
808 | |
809 switch (*((uint32_t *)buffer)) | |
810 { | |
811 case 0x00010203: | |
812 return Endianness_Big; | |
813 | |
814 case 0x03020100: | |
815 return Endianness_Little; | |
816 | |
817 default: | |
818 throw OrthancException(ErrorCode_NotImplemented); | |
819 } | |
820 } | |
821 | |
822 | |
823 #if BOOST_HAS_REGEX == 1 | |
824 std::string Toolbox::WildcardToRegularExpression(const std::string& source) | |
825 { | |
826 // TODO - Speed up this with a regular expression | |
827 | |
828 std::string result = source; | |
829 | |
830 // Escape all special characters | |
831 boost::replace_all(result, "\\", "\\\\"); | |
832 boost::replace_all(result, "^", "\\^"); | |
833 boost::replace_all(result, ".", "\\."); | |
834 boost::replace_all(result, "$", "\\$"); | |
835 boost::replace_all(result, "|", "\\|"); | |
836 boost::replace_all(result, "(", "\\("); | |
837 boost::replace_all(result, ")", "\\)"); | |
838 boost::replace_all(result, "[", "\\["); | |
839 boost::replace_all(result, "]", "\\]"); | |
840 boost::replace_all(result, "+", "\\+"); | |
841 boost::replace_all(result, "/", "\\/"); | |
842 boost::replace_all(result, "{", "\\{"); | |
843 boost::replace_all(result, "}", "\\}"); | |
844 | |
845 // Convert wildcards '*' and '?' to their regex equivalents | |
846 boost::replace_all(result, "?", "."); | |
847 boost::replace_all(result, "*", ".*"); | |
848 | |
849 return result; | |
850 } | |
851 #endif | |
852 | |
853 | |
854 | |
855 void Toolbox::TokenizeString(std::vector<std::string>& result, | |
856 const std::string& value, | |
857 char separator) | |
858 { | |
859 result.clear(); | |
860 | |
861 std::string currentItem; | |
862 | |
863 for (size_t i = 0; i < value.size(); i++) | |
864 { | |
865 if (value[i] == separator) | |
866 { | |
867 result.push_back(currentItem); | |
868 currentItem.clear(); | |
869 } | |
870 else | |
871 { | |
872 currentItem.push_back(value[i]); | |
873 } | |
874 } | |
875 | |
876 result.push_back(currentItem); | |
877 } | |
878 | |
879 | |
880 #if BOOST_HAS_REGEX == 1 | |
881 void Toolbox::DecodeDataUriScheme(std::string& mime, | |
882 std::string& content, | |
883 const std::string& source) | |
884 { | |
885 boost::regex pattern("data:([^;]+);base64,([a-zA-Z0-9=+/]*)", | |
886 boost::regex::icase /* case insensitive search */); | |
887 | |
888 boost::cmatch what; | |
889 if (regex_match(source.c_str(), what, pattern)) | |
890 { | |
891 mime = what[1]; | |
892 content = what[2]; | |
893 } | |
894 else | |
895 { | |
896 throw OrthancException(ErrorCode_BadFileFormat); | |
897 } | |
898 } | |
899 #endif | |
900 | |
901 | |
902 void Toolbox::MakeDirectory(const std::string& path) | |
903 { | |
904 if (boost::filesystem::exists(path)) | |
905 { | |
906 if (!boost::filesystem::is_directory(path)) | |
907 { | |
908 throw OrthancException("Cannot create the directory over an existing file: " + path); | |
909 } | |
910 } | |
911 else | |
912 { | |
913 if (!boost::filesystem::create_directories(path)) | |
914 { | |
915 throw OrthancException("Unable to create the directory: " + path); | |
916 } | |
917 } | |
918 } | |
919 | |
920 | |
921 bool Toolbox::IsExistingFile(const std::string& path) | |
922 { | |
923 return boost::filesystem::exists(path); | |
924 } | |
925 | |
926 | |
927 #if ORTHANC_PUGIXML_ENABLED == 1 | |
928 class ChunkedBufferWriter : public pugi::xml_writer | |
929 { | |
930 private: | |
931 ChunkedBuffer buffer_; | |
932 | |
933 public: | |
934 virtual void write(const void *data, size_t size) | |
935 { | |
936 if (size > 0) | |
937 { | |
938 buffer_.AddChunk(reinterpret_cast<const char*>(data), size); | |
939 } | |
940 } | |
941 | |
942 void Flatten(std::string& s) | |
943 { | |
944 buffer_.Flatten(s); | |
945 } | |
946 }; | |
947 | |
948 | |
949 static void JsonToXmlInternal(pugi::xml_node& target, | |
950 const Json::Value& source, | |
951 const std::string& arrayElement) | |
952 { | |
953 // http://jsoncpp.sourceforge.net/value_8h_source.html#l00030 | |
954 | |
955 switch (source.type()) | |
956 { | |
957 case Json::nullValue: | |
958 { | |
959 target.append_child(pugi::node_pcdata).set_value("null"); | |
960 break; | |
961 } | |
962 | |
963 case Json::intValue: | |
964 { | |
965 std::string s = boost::lexical_cast<std::string>(source.asInt()); | |
966 target.append_child(pugi::node_pcdata).set_value(s.c_str()); | |
967 break; | |
968 } | |
969 | |
970 case Json::uintValue: | |
971 { | |
972 std::string s = boost::lexical_cast<std::string>(source.asUInt()); | |
973 target.append_child(pugi::node_pcdata).set_value(s.c_str()); | |
974 break; | |
975 } | |
976 | |
977 case Json::realValue: | |
978 { | |
979 std::string s = boost::lexical_cast<std::string>(source.asFloat()); | |
980 target.append_child(pugi::node_pcdata).set_value(s.c_str()); | |
981 break; | |
982 } | |
983 | |
984 case Json::stringValue: | |
985 { | |
986 target.append_child(pugi::node_pcdata).set_value(source.asString().c_str()); | |
987 break; | |
988 } | |
989 | |
990 case Json::booleanValue: | |
991 { | |
992 target.append_child(pugi::node_pcdata).set_value(source.asBool() ? "true" : "false"); | |
993 break; | |
994 } | |
995 | |
996 case Json::arrayValue: | |
997 { | |
998 for (Json::Value::ArrayIndex i = 0; i < source.size(); i++) | |
999 { | |
1000 pugi::xml_node node = target.append_child(); | |
1001 node.set_name(arrayElement.c_str()); | |
1002 JsonToXmlInternal(node, source[i], arrayElement); | |
1003 } | |
1004 break; | |
1005 } | |
1006 | |
1007 case Json::objectValue: | |
1008 { | |
1009 Json::Value::Members members = source.getMemberNames(); | |
1010 | |
1011 for (size_t i = 0; i < members.size(); i++) | |
1012 { | |
1013 pugi::xml_node node = target.append_child(); | |
1014 node.set_name(members[i].c_str()); | |
1015 JsonToXmlInternal(node, source[members[i]], arrayElement); | |
1016 } | |
1017 | |
1018 break; | |
1019 } | |
1020 | |
1021 default: | |
1022 throw OrthancException(ErrorCode_NotImplemented); | |
1023 } | |
1024 } | |
1025 | |
1026 | |
1027 void Toolbox::JsonToXml(std::string& target, | |
1028 const Json::Value& source, | |
1029 const std::string& rootElement, | |
1030 const std::string& arrayElement) | |
1031 { | |
1032 pugi::xml_document doc; | |
1033 | |
1034 pugi::xml_node n = doc.append_child(rootElement.c_str()); | |
1035 JsonToXmlInternal(n, source, arrayElement); | |
1036 | |
1037 pugi::xml_node decl = doc.prepend_child(pugi::node_declaration); | |
1038 decl.append_attribute("version").set_value("1.0"); | |
1039 decl.append_attribute("encoding").set_value("utf-8"); | |
1040 | |
1041 ChunkedBufferWriter writer; | |
1042 doc.save(writer, " ", pugi::format_default, pugi::encoding_utf8); | |
1043 writer.Flatten(target); | |
1044 } | |
1045 | |
1046 #endif | |
1047 | |
1048 | |
1049 void Toolbox::ExecuteSystemCommand(const std::string& command, | |
1050 const std::vector<std::string>& arguments) | |
1051 { | |
1052 // Convert the arguments as a C array | |
1053 std::vector<char*> args(arguments.size() + 2); | |
1054 | |
1055 args.front() = const_cast<char*>(command.c_str()); | |
1056 | |
1057 for (size_t i = 0; i < arguments.size(); i++) | |
1058 { | |
1059 args[i + 1] = const_cast<char*>(arguments[i].c_str()); | |
1060 } | |
1061 | |
1062 args.back() = NULL; | |
1063 | |
1064 int status; | |
1065 | |
1066 #if defined(_WIN32) | |
1067 // http://msdn.microsoft.com/en-us/library/275khfab.aspx | |
1068 status = static_cast<int>(_spawnvp(_P_OVERLAY, command.c_str(), &args[0])); | |
1069 | |
1070 #else | |
1071 int pid = fork(); | |
1072 | |
1073 if (pid == -1) | |
1074 { | |
1075 // Error in fork() | |
1076 #if HAVE_GOOGLE_LOG == 1 | |
1077 LOG(ERROR) << "Cannot fork a child process"; | |
1078 #endif | |
1079 | |
1080 throw OrthancException(ErrorCode_SystemCommand); | |
1081 } | |
1082 else if (pid == 0) | |
1083 { | |
1084 // Execute the system command in the child process | |
1085 execvp(command.c_str(), &args[0]); | |
1086 | |
1087 // We should never get here | |
1088 _exit(1); | |
1089 } | |
1090 else | |
1091 { | |
1092 // Wait for the system command to exit | |
1093 waitpid(pid, &status, 0); | |
1094 } | |
1095 #endif | |
1096 | |
1097 if (status != 0) | |
1098 { | |
1099 #if HAVE_GOOGLE_LOG == 1 | |
1100 LOG(ERROR) << "System command failed with status code " << status; | |
1101 #endif | |
1102 | |
1103 throw OrthancException(ErrorCode_SystemCommand); | |
1104 } | |
1105 } | |
1106 | |
1107 | |
1108 bool Toolbox::IsInteger(const std::string& str) | |
1109 { | |
1110 std::string s = StripSpaces(str); | |
1111 | |
1112 if (s.size() == 0) | |
1113 { | |
1114 return false; | |
1115 } | |
1116 | |
1117 size_t pos = 0; | |
1118 if (s[0] == '-') | |
1119 { | |
1120 if (s.size() == 1) | |
1121 { | |
1122 return false; | |
1123 } | |
1124 | |
1125 pos = 1; | |
1126 } | |
1127 | |
1128 while (pos < s.size()) | |
1129 { | |
1130 if (!isdigit(s[pos])) | |
1131 { | |
1132 return false; | |
1133 } | |
1134 | |
1135 pos++; | |
1136 } | |
1137 | |
1138 return true; | |
1139 } | |
1140 } | |
1141 |