comparison Orthanc/Core/Toolbox.cpp @ 78:d6da56f86e5a

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