Mercurial > hg > orthanc
annotate Core/Toolbox.cpp @ 1250:2ffe07abd9d8
removal of const on IStorageArea::Read()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 05 Jan 2015 11:14:58 +0100 |
parents | 0479d02c6778 |
children | 6e7e5ed91c2d |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
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 |
890 | 62 #if defined(__linux) || defined(__FreeBSD_kernel__) |
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))); |
930 | 119 #elif defined(__linux) || defined(__APPLE__) || defined(__FreeBSD_kernel__) |
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 |
890 | 506 #elif defined(__linux) || defined(__FreeBSD_kernel__) |
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 | |
589 case Encoding_Arabic: | |
590 encoding = "ISO-8859-6"; | |
591 break; | |
592 | |
593 case Encoding_Greek: | |
594 encoding = "ISO-8859-7"; | |
595 break; | |
596 | |
597 case Encoding_Hebrew: | |
598 encoding = "ISO-8859-8"; | |
599 break; | |
1090
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
600 |
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
601 case Encoding_Japanese: |
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
602 encoding = "SHIFT-JIS"; |
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
603 break; |
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_Chinese: |
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
606 encoding = "GB18030"; |
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_Thai: |
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
610 encoding = "TIS620.2533-0"; |
e494ceb8d763
support more encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1087
diff
changeset
|
611 break; |
1087 | 612 |
949
766a57997121
enumeration for encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
942
diff
changeset
|
613 default: |
957
63973b76a51f
detection of encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
956
diff
changeset
|
614 throw OrthancException(ErrorCode_NotImplemented); |
949
766a57997121
enumeration for encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
942
diff
changeset
|
615 } |
766a57997121
enumeration for encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
942
diff
changeset
|
616 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
617 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
618 { |
949
766a57997121
enumeration for encodings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
942
diff
changeset
|
619 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
|
620 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
621 catch (std::runtime_error&) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
622 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
623 // Bad input string or bad encoding |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
624 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
625 } |
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 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
628 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
629 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
|
630 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
631 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
632 |
942
b3f6fb1130cd
fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
930
diff
changeset
|
633 result.reserve(source.size() + 1); |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
634 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
|
635 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
636 if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i])) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
637 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
638 result.push_back(source[i]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
639 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
640 } |
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 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
643 } |
177 | 644 |
645 void Toolbox::ComputeSHA1(std::string& result, | |
646 const std::string& data) | |
647 { | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
648 boost::uuids::detail::sha1 sha1; |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
649 |
177 | 650 if (data.size() > 0) |
651 { | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
652 sha1.process_bytes(&data[0], data.size()); |
177 | 653 } |
654 | |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
655 unsigned int digest[5]; |
177 | 656 |
657 // 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
|
658 assert(sizeof(unsigned int) == 4 && sizeof(digest) == (160 / 8)); |
177 | 659 |
560
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
660 sha1.get_digest(digest); |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
661 |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
662 result.resize(8 * 5 + 4); |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
663 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
664 digest[0], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
665 digest[1], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
666 digest[2], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
667 digest[3], |
69c024f9c06b
fix of Debian bug #724947
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
483
diff
changeset
|
668 digest[4]); |
177 | 669 } |
670 | |
402 | 671 bool Toolbox::IsSHA1(const std::string& str) |
672 { | |
673 if (str.size() != 44) | |
674 { | |
675 return false; | |
676 } | |
677 | |
678 for (unsigned int i = 0; i < 44; i++) | |
679 { | |
680 if (i == 8 || | |
681 i == 17 || | |
682 i == 26 || | |
683 i == 35) | |
684 { | |
685 if (str[i] != '-') | |
686 return false; | |
687 } | |
688 else | |
689 { | |
690 if (!isalnum(str[i])) | |
691 return false; | |
692 } | |
693 } | |
694 | |
695 return true; | |
696 } | |
697 | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
698 std::string Toolbox::GetNowIsoString() |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
699 { |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
700 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
|
701 return boost::posix_time::to_iso_string(now); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
702 } |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
703 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
704 std::string Toolbox::StripSpaces(const std::string& source) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
705 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
706 size_t first = 0; |
177 | 707 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
708 while (first < source.length() && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
709 isspace(source[first])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
710 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
711 first++; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
712 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
713 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
714 if (first == source.length()) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
715 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
716 // String containing only spaces |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
717 return ""; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
718 } |
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 size_t last = source.length(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
721 while (last > first && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
722 isspace(source[last - 1])) |
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 last--; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
725 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
726 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
727 assert(first <= last); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
728 return source.substr(first, last - first); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
729 } |
336 | 730 |
731 | |
337 | 732 static char Hex2Dec(char c) |
336 | 733 { |
734 return ((c >= '0' && c <= '9') ? c - '0' : | |
735 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10)); | |
736 } | |
737 | |
738 void Toolbox::UrlDecode(std::string& s) | |
739 { | |
740 // http://en.wikipedia.org/wiki/Percent-encoding | |
741 // http://www.w3schools.com/tags/ref_urlencode.asp | |
742 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c | |
743 | |
744 if (s.size() == 0) | |
745 { | |
746 return; | |
747 } | |
748 | |
749 size_t source = 0; | |
750 size_t target = 0; | |
751 | |
752 while (source < s.size()) | |
753 { | |
754 if (s[source] == '%' && | |
755 source + 2 < s.size() && | |
756 isalnum(s[source + 1]) && | |
757 isalnum(s[source + 2])) | |
758 { | |
759 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]); | |
760 source += 3; | |
761 target += 1; | |
762 } | |
763 else | |
764 { | |
765 if (s[source] == '+') | |
766 s[target] = ' '; | |
767 else | |
768 s[target] = s[source]; | |
769 | |
770 source++; | |
771 target++; | |
772 } | |
773 } | |
774 | |
775 s.resize(target); | |
776 } | |
453 | 777 |
778 | |
779 Endianness Toolbox::DetectEndianness() | |
780 { | |
781 // http://sourceforge.net/p/predef/wiki/Endianness/ | |
782 | |
783 uint8_t buffer[4]; | |
784 | |
785 buffer[0] = 0x00; | |
786 buffer[1] = 0x01; | |
787 buffer[2] = 0x02; | |
788 buffer[3] = 0x03; | |
789 | |
790 switch (*((uint32_t *)buffer)) | |
791 { | |
792 case 0x00010203: | |
793 return Endianness_Big; | |
794 | |
795 case 0x03020100: | |
796 return Endianness_Little; | |
797 | |
798 default: | |
799 throw OrthancException(ErrorCode_NotImplemented); | |
800 } | |
801 } | |
608 | 802 |
803 | |
804 std::string Toolbox::WildcardToRegularExpression(const std::string& source) | |
805 { | |
806 // TODO - Speed up this with a regular expression | |
807 | |
808 std::string result = source; | |
809 | |
810 // Escape all special characters | |
811 boost::replace_all(result, "\\", "\\\\"); | |
812 boost::replace_all(result, "^", "\\^"); | |
813 boost::replace_all(result, ".", "\\."); | |
814 boost::replace_all(result, "$", "\\$"); | |
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 | |
825 // Convert wildcards '*' and '?' to their regex equivalents | |
826 boost::replace_all(result, "?", "."); | |
827 boost::replace_all(result, "*", ".*"); | |
828 | |
829 return result; | |
830 } | |
831 | |
832 | |
833 | |
834 void Toolbox::TokenizeString(std::vector<std::string>& result, | |
835 const std::string& value, | |
836 char separator) | |
837 { | |
838 result.clear(); | |
839 | |
840 std::string currentItem; | |
841 | |
842 for (size_t i = 0; i < value.size(); i++) | |
843 { | |
844 if (value[i] == separator) | |
845 { | |
846 result.push_back(currentItem); | |
847 currentItem.clear(); | |
848 } | |
849 else | |
850 { | |
851 currentItem.push_back(value[i]); | |
852 } | |
853 } | |
854 | |
855 result.push_back(currentItem); | |
856 } | |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
857 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
858 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
859 void Toolbox::DecodeDataUriScheme(std::string& mime, |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
860 std::string& content, |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
861 const std::string& source) |
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 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
|
864 boost::regex::icase /* case insensitive search */); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
865 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
866 boost::cmatch what; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
867 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
|
868 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
869 mime = what[1]; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
870 content = what[2]; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
871 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
872 else |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
873 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
874 throw OrthancException(ErrorCode_BadFileFormat); |
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 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
707
diff
changeset
|
877 |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
878 |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
879 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
|
880 { |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
881 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
|
882 { |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
883 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
|
884 { |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
885 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
|
886 } |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
887 } |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
888 else |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
889 { |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
890 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
|
891 { |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
892 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
|
893 } |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
894 } |
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
895 } |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
896 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
897 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
898 bool Toolbox::IsExistingFile(const std::string& path) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
899 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
900 return boost::filesystem::exists(path); |
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 |
1045
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
903 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
904 #if ORTHANC_PUGIXML_ENABLED == 1 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
905 class ChunkedBufferWriter : public pugi::xml_writer |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
906 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
907 private: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
908 ChunkedBuffer buffer_; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
909 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
910 public: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
911 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
|
912 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
913 if (size > 0) |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
914 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
915 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
|
916 } |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
917 } |
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 void Flatten(std::string& s) |
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 buffer_.Flatten(s); |
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 }; |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
926 static void JsonToXmlInternal(pugi::xml_node& target, |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
927 const Json::Value& source, |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
928 const std::string& arrayElement) |
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 // 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
|
931 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
932 switch (source.type()) |
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 case Json::nullValue: |
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 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
|
937 break; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
938 } |
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 case Json::intValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
941 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
942 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
|
943 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
|
944 break; |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
947 case Json::uintValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
948 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
949 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
|
950 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
|
951 break; |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
954 case Json::realValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
955 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
956 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
|
957 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
|
958 break; |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
961 case Json::stringValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
962 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
963 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
|
964 break; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
965 } |
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 case Json::booleanValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
968 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
969 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
|
970 break; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
971 } |
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 case Json::arrayValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
974 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
975 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
|
976 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
977 pugi::xml_node node = target.append_child(); |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
978 node.set_name(arrayElement.c_str()); |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
979 JsonToXmlInternal(node, source[i], arrayElement); |
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 break; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
982 } |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
983 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
984 case Json::objectValue: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
985 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
986 Json::Value::Members members = source.getMemberNames(); |
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 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
|
989 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
990 pugi::xml_node node = target.append_child(); |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
991 node.set_name(members[i].c_str()); |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
992 JsonToXmlInternal(node, source[members[i]], arrayElement); |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
995 break; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
996 } |
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 default: |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
999 throw OrthancException(ErrorCode_NotImplemented); |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1003 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1004 void Toolbox::JsonToXml(std::string& target, |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1005 const Json::Value& source, |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1006 const std::string& rootElement, |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1007 const std::string& arrayElement) |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1008 { |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1009 pugi::xml_document doc; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1010 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1011 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
|
1012 JsonToXmlInternal(n, source, arrayElement); |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1013 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1014 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
|
1015 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
|
1016 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
|
1017 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1018 ChunkedBufferWriter writer; |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1019 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
|
1020 writer.Flatten(target); |
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 |
0bfeeb6d340f
json to xml conversion with pugixml
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
971
diff
changeset
|
1023 #endif |
1050
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1024 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1025 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1026 void Toolbox::ExecuteSystemCommand(const std::string& command, |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1027 const std::vector<std::string>& arguments) |
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 // Convert the arguments as a C array |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1030 std::vector<char*> args(arguments.size() + 2); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1031 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1032 args.front() = const_cast<char*>(command.c_str()); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1033 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1034 for (size_t i = 0; i < arguments.size(); i++) |
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[i + 1] = const_cast<char*>(arguments[i].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 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1039 args.back() = NULL; |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1040 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1041 int status; |
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 #if defined(_WIN32) |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1044 // http://msdn.microsoft.com/en-us/library/275khfab.aspx |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1045 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
|
1046 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1047 #else |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1048 int pid = fork(); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1049 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1050 if (pid == -1) |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1051 { |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1052 // Error in fork() |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1053 LOG(ERROR) << "Cannot fork a child process"; |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1054 throw OrthancException(ErrorCode_SystemCommand); |
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 else if (pid == 0) |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1057 { |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1058 // Execute the system command in the child process |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1059 execvp(command.c_str(), &args[0]); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1060 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1061 // We should never get here |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1062 _exit(1); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1063 } |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1064 else |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1065 { |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1066 // Wait for the system command to exit |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1067 waitpid(pid, &status, 0); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1068 } |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1069 #endif |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1070 |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1071 if (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 LOG(ERROR) << "System command failed with status code " << status; |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1074 throw OrthancException(ErrorCode_SystemCommand); |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1075 } |
64f1842aae2e
Toolbox::ExecuteSystemCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1045
diff
changeset
|
1076 } |
1102
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1077 |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1078 |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1079 bool Toolbox::IsInteger(const std::string& str) |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1080 { |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1081 std::string s = StripSpaces(str); |
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 if (s.size() == 0) |
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 return false; |
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 |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1088 size_t pos = 0; |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1089 if (s[0] == '-') |
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 if (s.size() == 1) |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1092 { |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1093 return false; |
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 |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1096 pos = 1; |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1097 } |
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 while (pos < s.size()) |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1100 { |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1101 if (!isdigit(s[pos])) |
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 return false; |
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 |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1106 pos++; |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1107 } |
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 return true; |
ce6386b37afd
avoid unnecessary exceptions on Orthanc startup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1101
diff
changeset
|
1110 } |
0 | 1111 } |
608 | 1112 |