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