comparison Core/Toolbox.cpp @ 759:8cfc6119a5bd dicom-rt

integration mainline -> dicom-rt
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Apr 2014 16:04:55 +0200
parents b82292ba2083 203157cb4fde
children e57e08ed510f
comparison
equal deleted inserted replaced
605:b82292ba2083 759:8cfc6119a5bd
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium 4 * Belgium
5 * 5 *
6 * This program is free software: you can redistribute it and/or 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 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 8 * published by the Free Software Foundation, either version 3 of the
40 #include <boost/filesystem/fstream.hpp> 40 #include <boost/filesystem/fstream.hpp>
41 #include <boost/date_time/posix_time/posix_time.hpp> 41 #include <boost/date_time/posix_time/posix_time.hpp>
42 #include <boost/uuid/sha1.hpp> 42 #include <boost/uuid/sha1.hpp>
43 #include <algorithm> 43 #include <algorithm>
44 #include <ctype.h> 44 #include <ctype.h>
45 #include <boost/regex.hpp>
45 46
46 #if defined(_WIN32) 47 #if defined(_WIN32)
47 #include <windows.h> 48 #include <windows.h>
48 #endif 49 #endif
49 50
154 { 155 {
155 finish = true; 156 finish = true;
156 } 157 }
157 #endif 158 #endif
158 159
159 void Toolbox::Sleep(uint32_t seconds)
160 {
161 #if defined(_WIN32)
162 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000));
163 #elif defined(__linux)
164 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000));
165 #else
166 #error Support your platform here
167 #endif
168 }
169
170 void Toolbox::USleep(uint64_t microSeconds) 160 void Toolbox::USleep(uint64_t microSeconds)
171 { 161 {
172 #if defined(_WIN32) 162 #if defined(_WIN32)
173 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); 163 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
174 #elif defined(__linux) 164 #elif defined(__linux)
184 #if defined(_WIN32) 174 #if defined(_WIN32)
185 SetConsoleCtrlHandler(ConsoleControlHandler, true); 175 SetConsoleCtrlHandler(ConsoleControlHandler, true);
186 #else 176 #else
187 signal(SIGINT, SignalHandler); 177 signal(SIGINT, SignalHandler);
188 signal(SIGQUIT, SignalHandler); 178 signal(SIGQUIT, SignalHandler);
179 signal(SIGTERM, SignalHandler);
189 #endif 180 #endif
190 181
191 finish = false; 182 finish = false;
192 while (!finish) 183 while (!finish)
193 { 184 {
197 #if defined(_WIN32) 188 #if defined(_WIN32)
198 SetConsoleCtrlHandler(ConsoleControlHandler, false); 189 SetConsoleCtrlHandler(ConsoleControlHandler, false);
199 #else 190 #else
200 signal(SIGINT, NULL); 191 signal(SIGINT, NULL);
201 signal(SIGQUIT, NULL); 192 signal(SIGQUIT, NULL);
193 signal(SIGTERM, NULL);
202 #endif 194 #endif
203 } 195 }
204 196
205 197
206 198
213 void Toolbox::ToLowerCase(std::string& s) 205 void Toolbox::ToLowerCase(std::string& s)
214 { 206 {
215 std::transform(s.begin(), s.end(), s.begin(), tolower); 207 std::transform(s.begin(), s.end(), s.begin(), tolower);
216 } 208 }
217 209
210
211 void Toolbox::ToUpperCase(std::string& result,
212 const std::string& source)
213 {
214 result = source;
215 ToUpperCase(result);
216 }
217
218 void Toolbox::ToLowerCase(std::string& result,
219 const std::string& source)
220 {
221 result = source;
222 ToLowerCase(result);
223 }
218 224
219 225
220 void Toolbox::ReadFile(std::string& content, 226 void Toolbox::ReadFile(std::string& content,
221 const std::string& path) 227 const std::string& path)
222 { 228 {
446 452
447 453
448 void Toolbox::ComputeMD5(std::string& result, 454 void Toolbox::ComputeMD5(std::string& result,
449 const std::string& data) 455 const std::string& data)
450 { 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 {
451 md5_state_s state; 472 md5_state_s state;
452 md5_init(&state); 473 md5_init(&state);
453 474
454 if (data.size() > 0) 475 if (length > 0)
455 { 476 {
456 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), 477 md5_append(&state,
457 static_cast<int>(data.size())); 478 reinterpret_cast<const md5_byte_t*>(data),
479 static_cast<int>(length));
458 } 480 }
459 481
460 md5_byte_t actualHash[16]; 482 md5_byte_t actualHash[16];
461 md5_finish(&state, actualHash); 483 md5_finish(&state, actualHash);
462 484
726 throw OrthancException(ErrorCode_NotImplemented); 748 throw OrthancException(ErrorCode_NotImplemented);
727 } 749 }
728 } 750 }
729 751
730 752
731 753 std::string Toolbox::WildcardToRegularExpression(const std::string& source)
732 void Toolbox::Split(std::vector<std::string>& result, 754 {
733 const std::string& source, 755 // TODO - Speed up this with a regular expression
734 char delimiter) 756
735 { 757 std::string result = source;
736 if (source.size() == 0) 758
737 { 759 // Escape all special characters
738 result.clear(); 760 boost::replace_all(result, "\\", "\\\\");
739 return; 761 boost::replace_all(result, "^", "\\^");
740 } 762 boost::replace_all(result, ".", "\\.");
741 763 boost::replace_all(result, "$", "\\$");
742 size_t count = 1; 764 boost::replace_all(result, "|", "\\|");
743 for (size_t i = 0; i < source.size(); i++) 765 boost::replace_all(result, "(", "\\(");
744 { 766 boost::replace_all(result, ")", "\\)");
745 if (source[i] == delimiter) 767 boost::replace_all(result, "[", "\\[");
746 { 768 boost::replace_all(result, "]", "\\]");
747 count++; 769 boost::replace_all(result, "+", "\\+");
748 } 770 boost::replace_all(result, "/", "\\/");
749 } 771 boost::replace_all(result, "{", "\\{");
750 772 boost::replace_all(result, "}", "\\}");
773
774 // Convert wildcards '*' and '?' to their regex equivalents
775 boost::replace_all(result, "?", ".");
776 boost::replace_all(result, "*", ".*");
777
778 return result;
779 }
780
781
782
783 void Toolbox::TokenizeString(std::vector<std::string>& result,
784 const std::string& value,
785 char separator)
786 {
751 result.clear(); 787 result.clear();
752 result.resize(count); 788
753 789 std::string currentItem;
754 size_t pos = 0; 790
755 size_t start = 0; 791 for (size_t i = 0; i < value.size(); i++)
756 while (start < source.size()) 792 {
757 { 793 if (value[i] == separator)
758 assert(pos < count); 794 {
759 795 result.push_back(currentItem);
760 size_t end = start; 796 currentItem.clear();
761 while (end < source.size() && 797 }
762 source[end] != delimiter) 798 else
763 { 799 {
764 end++; 800 currentItem.push_back(value[i]);
765 } 801 }
766 802 }
767 result[pos++] = source.substr(start, end - start); 803
768 start = end + 1; 804 result.push_back(currentItem);
769 } 805 }
770 }
771
772 } 806 }
807