Mercurial > hg > orthanc
annotate Core/Toolbox.cpp @ 524:d80376e73f52
fix of issue #8
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Sep 2013 17:48:00 +0200 |
parents | 8c3573d28868 |
children | b22312081388 69c024f9c06b 3f27814104f7 |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
398 | 3 * Copyright (C) 2012-2013 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 | |
33 #include "Toolbox.h" | |
34 | |
59 | 35 #include "OrthancException.h" |
0 | 36 |
453 | 37 #include <stdint.h> |
0 | 38 #include <string.h> |
39 #include <boost/filesystem.hpp> | |
40 #include <boost/filesystem/fstream.hpp> | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
41 #include <boost/date_time/posix_time/posix_time.hpp> |
0 | 42 #include <algorithm> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
43 #include <ctype.h> |
0 | 44 |
45 #if defined(_WIN32) | |
46 #include <windows.h> | |
47 #endif | |
48 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
49 #if defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
50 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
51 #include <limits.h> /* PATH_MAX */ |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
52 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
53 |
0 | 54 #if defined(__linux) |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
55 #include <limits.h> /* PATH_MAX */ |
10 | 56 #include <signal.h> |
0 | 57 #include <unistd.h> |
58 #endif | |
59 | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
60 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
61 #include <boost/locale.hpp> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
62 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
63 #include <iconv.h> |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
64 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
65 |
22 | 66 #include "../Resources/md5/md5.h" |
24 | 67 #include "../Resources/base64/base64.h" |
177 | 68 #include "../Resources/sha1/sha1.h" |
0 | 69 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
70 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
71 #if BOOST_HAS_LOCALE == 0 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
72 namespace |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
73 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
74 class IconvRabi |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
75 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
76 private: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
77 iconv_t context_; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
78 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
79 public: |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
80 IconvRabi(const char* tocode, const char* fromcode) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
81 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
82 context_ = iconv_open(tocode, fromcode); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
83 if (!context_) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
84 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
85 throw Orthanc::OrthancException("Unknown code page"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
86 } |
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 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
89 ~IconvRabi() |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
90 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
91 iconv_close(context_); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
92 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
93 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
94 std::string Convert(const std::string& source) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
95 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
96 if (source.size() == 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
97 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
98 return ""; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
99 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
100 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
101 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
102 char* sourcePos = const_cast<char*>(&source[0]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
103 size_t sourceLeft = source.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
104 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
105 std::vector<char> storage(source.size() + 10); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
106 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
107 while (sourceLeft > 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
108 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
109 char* tmp = &storage[0]; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
110 size_t outputLeft = storage.size(); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
111 size_t err = iconv(context_, &sourcePos, &sourceLeft, &tmp, &outputLeft); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
112 if (err < 0) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
113 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
114 throw Orthanc::OrthancException("Bad character in sequence"); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
115 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
116 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
117 size_t count = storage.size() - outputLeft; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
118 result += std::string(&storage[0], count); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
119 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
120 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
121 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
122 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
123 }; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
124 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
125 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
126 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
127 |
59 | 128 namespace Orthanc |
0 | 129 { |
130 static bool finish; | |
131 | |
10 | 132 #if defined(_WIN32) |
22 | 133 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType) |
134 { | |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
135 // 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
|
136 finish = true; |
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
137 return true; |
22 | 138 } |
139 #else | |
0 | 140 static void SignalHandler(int) |
141 { | |
142 finish = true; | |
143 } | |
22 | 144 #endif |
0 | 145 |
146 void Toolbox::Sleep(uint32_t seconds) | |
147 { | |
148 #if defined(_WIN32) | |
149 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000)); | |
150 #elif defined(__linux) | |
151 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000)); | |
152 #else | |
153 #error Support your platform here | |
154 #endif | |
155 } | |
156 | |
157 void Toolbox::USleep(uint64_t microSeconds) | |
158 { | |
159 #if defined(_WIN32) | |
8 | 160 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); |
0 | 161 #elif defined(__linux) |
162 usleep(microSeconds); | |
163 #else | |
164 #error Support your platform here | |
165 #endif | |
166 } | |
167 | |
168 | |
169 void Toolbox::ServerBarrier() | |
170 { | |
10 | 171 #if defined(_WIN32) |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
172 SetConsoleCtrlHandler(ConsoleControlHandler, true); |
10 | 173 #else |
0 | 174 signal(SIGINT, SignalHandler); |
175 signal(SIGQUIT, SignalHandler); | |
176 #endif | |
177 | |
178 finish = false; | |
179 while (!finish) | |
180 { | |
181 USleep(100000); | |
182 } | |
183 | |
10 | 184 #if defined(_WIN32) |
473
c9a5d72f8481
changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
453
diff
changeset
|
185 SetConsoleCtrlHandler(ConsoleControlHandler, false); |
10 | 186 #else |
0 | 187 signal(SIGINT, NULL); |
188 signal(SIGQUIT, NULL); | |
189 #endif | |
190 } | |
191 | |
192 | |
193 | |
194 void Toolbox::ToUpperCase(std::string& s) | |
195 { | |
196 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
197 } | |
198 | |
199 | |
200 void Toolbox::ToLowerCase(std::string& s) | |
201 { | |
202 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
203 } | |
204 | |
205 | |
206 | |
207 void Toolbox::ReadFile(std::string& content, | |
208 const std::string& path) | |
209 { | |
210 boost::filesystem::ifstream f; | |
483
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
211 f.open(path, std::ifstream::in | std::ifstream::binary); |
0 | 212 if (!f.good()) |
213 { | |
483
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
214 throw OrthancException(ErrorCode_InexistentFile); |
0 | 215 } |
216 | |
217 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
218 f.seekg(0, std::ios::end); | |
219 std::streamsize size = f.tellg(); | |
220 f.seekg(0, std::ios::beg); | |
221 | |
222 content.resize(size); | |
223 if (size != 0) | |
224 { | |
225 f.read(reinterpret_cast<char*>(&content[0]), size); | |
226 } | |
227 | |
228 f.close(); | |
229 } | |
230 | |
231 | |
483
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
232 void Toolbox::WriteFile(const std::string& content, |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
233 const std::string& path) |
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 boost::filesystem::ofstream f; |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
236 f.open(path, std::ofstream::binary); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
237 if (!f.good()) |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
238 { |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
239 throw OrthancException(ErrorCode_CannotWriteFile); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
240 } |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
241 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
242 if (content.size() != 0) |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
243 { |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
244 f.write(content.c_str(), content.size()); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
245 } |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
246 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
247 f.close(); |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
248 } |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
249 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
250 |
8c3573d28868
export dicom instances to the filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
477
diff
changeset
|
251 |
0 | 252 void Toolbox::RemoveFile(const std::string& path) |
253 { | |
254 if (boost::filesystem::exists(path)) | |
255 { | |
256 if (boost::filesystem::is_regular_file(path)) | |
257 boost::filesystem::remove(path); | |
258 else | |
59 | 259 throw OrthancException("The path is not a regular file: " + path); |
0 | 260 } |
261 } | |
262 | |
263 | |
264 | |
265 void Toolbox::SplitUriComponents(UriComponents& components, | |
266 const std::string& uri) | |
267 { | |
268 static const char URI_SEPARATOR = '/'; | |
269 | |
270 components.clear(); | |
271 | |
272 if (uri.size() == 0 || | |
273 uri[0] != URI_SEPARATOR) | |
274 { | |
59 | 275 throw OrthancException(ErrorCode_UriSyntax); |
0 | 276 } |
277 | |
278 // Count the number of slashes in the URI to make an assumption | |
279 // about the number of components in the URI | |
280 unsigned int estimatedSize = 0; | |
281 for (unsigned int i = 0; i < uri.size(); i++) | |
282 { | |
283 if (uri[i] == URI_SEPARATOR) | |
284 estimatedSize++; | |
285 } | |
286 | |
287 components.reserve(estimatedSize - 1); | |
288 | |
289 unsigned int start = 1; | |
290 unsigned int end = 1; | |
291 while (end < uri.size()) | |
292 { | |
293 // This is the loop invariant | |
294 assert(uri[start - 1] == '/' && (end >= start)); | |
295 | |
296 if (uri[end] == '/') | |
297 { | |
298 components.push_back(std::string(&uri[start], end - start)); | |
299 end++; | |
300 start = end; | |
301 } | |
302 else | |
303 { | |
304 end++; | |
305 } | |
306 } | |
307 | |
308 if (start < uri.size()) | |
309 { | |
310 components.push_back(std::string(&uri[start], end - start)); | |
311 } | |
207 | 312 |
313 for (size_t i = 0; i < components.size(); i++) | |
314 { | |
315 if (components[i].size() == 0) | |
316 { | |
317 // Empty component, as in: "/coucou//e" | |
318 throw OrthancException(ErrorCode_UriSyntax); | |
319 } | |
320 } | |
0 | 321 } |
322 | |
323 | |
324 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
325 const UriComponents& testedUri) | |
326 { | |
327 if (testedUri.size() < baseUri.size()) | |
328 { | |
329 return false; | |
330 } | |
331 | |
332 for (size_t i = 0; i < baseUri.size(); i++) | |
333 { | |
334 if (baseUri[i] != testedUri[i]) | |
335 return false; | |
336 } | |
337 | |
338 return true; | |
339 } | |
340 | |
341 | |
342 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
343 { | |
344 std::string contentType; | |
345 size_t lastDot = path.rfind('.'); | |
346 size_t lastSlash = path.rfind('/'); | |
347 | |
348 if (lastDot == std::string::npos || | |
349 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
350 { | |
351 // No trailing dot, unable to detect the content type | |
352 } | |
353 else | |
354 { | |
355 const char* extension = &path[lastDot + 1]; | |
356 | |
357 // http://en.wikipedia.org/wiki/Mime_types | |
358 // Text types | |
359 if (!strcmp(extension, "txt")) | |
360 contentType = "text/plain"; | |
361 else if (!strcmp(extension, "html")) | |
362 contentType = "text/html"; | |
363 else if (!strcmp(extension, "xml")) | |
364 contentType = "text/xml"; | |
365 else if (!strcmp(extension, "css")) | |
366 contentType = "text/css"; | |
367 | |
368 // Application types | |
369 else if (!strcmp(extension, "js")) | |
370 contentType = "application/javascript"; | |
371 else if (!strcmp(extension, "json")) | |
372 contentType = "application/json"; | |
373 else if (!strcmp(extension, "pdf")) | |
374 contentType = "application/pdf"; | |
375 | |
376 // Images types | |
377 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
378 contentType = "image/jpeg"; | |
379 else if (!strcmp(extension, "gif")) | |
380 contentType = "image/gif"; | |
381 else if (!strcmp(extension, "png")) | |
382 contentType = "image/png"; | |
383 } | |
384 | |
385 return contentType; | |
386 } | |
387 | |
388 | |
389 std::string Toolbox::FlattenUri(const UriComponents& components, | |
390 size_t fromLevel) | |
391 { | |
392 if (components.size() <= fromLevel) | |
393 { | |
394 return "/"; | |
395 } | |
396 else | |
397 { | |
398 std::string r; | |
399 | |
400 for (size_t i = fromLevel; i < components.size(); i++) | |
401 { | |
402 r += "/" + components[i]; | |
403 } | |
404 | |
405 return r; | |
406 } | |
407 } | |
408 | |
409 | |
410 | |
411 uint64_t Toolbox::GetFileSize(const std::string& path) | |
412 { | |
413 try | |
414 { | |
415 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
416 } | |
417 catch (boost::filesystem::filesystem_error) | |
418 { | |
59 | 419 throw OrthancException(ErrorCode_InexistentFile); |
0 | 420 } |
421 } | |
22 | 422 |
423 | |
424 static char GetHexadecimalCharacter(uint8_t value) | |
425 { | |
426 assert(value < 16); | |
427 | |
428 if (value < 10) | |
429 return value + '0'; | |
430 else | |
431 return (value - 10) + 'a'; | |
432 } | |
433 | |
23 | 434 |
22 | 435 void Toolbox::ComputeMD5(std::string& result, |
436 const std::string& data) | |
437 { | |
438 md5_state_s state; | |
439 md5_init(&state); | |
440 | |
441 if (data.size() > 0) | |
442 { | |
443 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), | |
444 static_cast<int>(data.size())); | |
445 } | |
446 | |
447 md5_byte_t actualHash[16]; | |
448 md5_finish(&state, actualHash); | |
449 | |
450 result.resize(32); | |
451 for (unsigned int i = 0; i < 16; i++) | |
452 { | |
453 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
454 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
455 } | |
456 } | |
24 | 457 |
458 | |
459 std::string Toolbox::EncodeBase64(const std::string& data) | |
460 { | |
86 | 461 return base64_encode(data); |
24 | 462 } |
463 | |
365 | 464 std::string Toolbox::DecodeBase64(const std::string& data) |
465 { | |
466 return base64_decode(data); | |
467 } | |
468 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
469 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
470 #if defined(_WIN32) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
471 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
472 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
473 // 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
|
474 // required buffer size, so we use a big constant |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
475 std::vector<char> buffer(32768); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
476 /*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
|
477 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
478 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
479 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
480 #elif defined(__linux) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
481 std::string Toolbox::GetPathToExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
482 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
483 std::vector<char> buffer(PATH_MAX + 1); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
484 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
|
485 if (bytes == 0) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
486 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
487 throw OrthancException("Unable to get the path to the executable"); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
488 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
489 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
490 return std::string(&buffer[0]); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
491 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
492 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
493 #elif defined(__APPLE__) && defined(__MACH__) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
494 std::string Toolbox::GetPathToExecutable() |
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 char pathbuf[PATH_MAX + 1]; |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
497 unsigned int bufsize = static_cast<int>(sizeof(pathbuf)); |
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 _NSGetExecutablePath( pathbuf, &bufsize); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
500 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
501 return std::string(pathbuf); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
502 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
503 |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
504 #else |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
505 #error Support your platform here |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
506 #endif |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
507 |
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::string Toolbox::GetDirectoryOfExecutable() |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
510 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
511 boost::filesystem::path p(GetPathToExecutable()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
512 return p.parent_path().string(); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
513 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
514 |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
515 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
516 std::string Toolbox::ConvertToUtf8(const std::string& source, |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
517 const char* fromEncoding) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
518 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
519 #if BOOST_HAS_LOCALE == 1 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
520 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
521 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
522 return boost::locale::conv::to_utf<char>(source, fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
523 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
524 catch (std::runtime_error&) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
525 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
526 // Bad input string or bad encoding |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
527 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
528 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
529 #else |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
530 IconvRabi iconv("UTF-8", fromEncoding); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
531 try |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
532 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
533 return iconv.Convert(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
534 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
535 catch (OrthancException) |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
536 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
537 return ConvertToAscii(source); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
538 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
539 #endif |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
540 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
541 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
542 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
543 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
|
544 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
545 std::string result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
546 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
547 result.reserve(source.size()); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
548 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
|
549 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
550 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
|
551 { |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
552 result.push_back(source[i]); |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
553 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
554 } |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
555 |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
556 return result; |
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
557 } |
177 | 558 |
559 void Toolbox::ComputeSHA1(std::string& result, | |
560 const std::string& data) | |
561 { | |
562 SHA1 sha1; | |
563 if (data.size() > 0) | |
564 { | |
565 sha1.Input(&data[0], data.size()); | |
566 } | |
567 | |
568 unsigned digest[5]; | |
569 | |
570 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide | |
571 assert(sizeof(unsigned) == 4 && sizeof(digest) == (160 / 8)); | |
572 | |
573 if (sha1.Result(digest)) | |
574 { | |
575 result.resize(8 * 5 + 4); | |
576 sprintf(&result[0], "%08x-%08x-%08x-%08x-%08x", | |
577 digest[0], | |
578 digest[1], | |
579 digest[2], | |
580 digest[3], | |
581 digest[4]); | |
582 } | |
583 else | |
584 { | |
585 throw OrthancException(ErrorCode_InternalError); | |
586 } | |
587 } | |
588 | |
402 | 589 bool Toolbox::IsSHA1(const std::string& str) |
590 { | |
591 if (str.size() != 44) | |
592 { | |
593 return false; | |
594 } | |
595 | |
596 for (unsigned int i = 0; i < 44; i++) | |
597 { | |
598 if (i == 8 || | |
599 i == 17 || | |
600 i == 26 || | |
601 i == 35) | |
602 { | |
603 if (str[i] != '-') | |
604 return false; | |
605 } | |
606 else | |
607 { | |
608 if (!isalnum(str[i])) | |
609 return false; | |
610 } | |
611 } | |
612 | |
613 return true; | |
614 } | |
615 | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
616 std::string Toolbox::GetNowIsoString() |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
617 { |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
618 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
|
619 return boost::posix_time::to_iso_string(now); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
620 } |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
621 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
622 std::string Toolbox::StripSpaces(const std::string& source) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
623 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
624 size_t first = 0; |
177 | 625 |
247
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
626 while (first < source.length() && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
627 isspace(source[first])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
628 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
629 first++; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
630 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
631 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
632 if (first == source.length()) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
633 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
634 // String containing only spaces |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
635 return ""; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
636 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
637 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
638 size_t last = source.length(); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
639 while (last > first && |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
640 isspace(source[last - 1])) |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
641 { |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
642 last--; |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
643 } |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
644 |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
645 assert(first <= last); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
646 return source.substr(first, last - first); |
c9b3ba0fd140
path management in zip files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
647 } |
336 | 648 |
649 | |
337 | 650 static char Hex2Dec(char c) |
336 | 651 { |
652 return ((c >= '0' && c <= '9') ? c - '0' : | |
653 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10)); | |
654 } | |
655 | |
656 void Toolbox::UrlDecode(std::string& s) | |
657 { | |
658 // http://en.wikipedia.org/wiki/Percent-encoding | |
659 // http://www.w3schools.com/tags/ref_urlencode.asp | |
660 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c | |
661 | |
662 if (s.size() == 0) | |
663 { | |
664 return; | |
665 } | |
666 | |
667 size_t source = 0; | |
668 size_t target = 0; | |
669 | |
670 while (source < s.size()) | |
671 { | |
672 if (s[source] == '%' && | |
673 source + 2 < s.size() && | |
674 isalnum(s[source + 1]) && | |
675 isalnum(s[source + 2])) | |
676 { | |
677 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]); | |
678 source += 3; | |
679 target += 1; | |
680 } | |
681 else | |
682 { | |
683 if (s[source] == '+') | |
684 s[target] = ' '; | |
685 else | |
686 s[target] = s[source]; | |
687 | |
688 source++; | |
689 target++; | |
690 } | |
691 } | |
692 | |
693 s.resize(target); | |
694 } | |
453 | 695 |
696 | |
697 Endianness Toolbox::DetectEndianness() | |
698 { | |
699 // http://sourceforge.net/p/predef/wiki/Endianness/ | |
700 | |
701 uint8_t buffer[4]; | |
702 | |
703 buffer[0] = 0x00; | |
704 buffer[1] = 0x01; | |
705 buffer[2] = 0x02; | |
706 buffer[3] = 0x03; | |
707 | |
708 switch (*((uint32_t *)buffer)) | |
709 { | |
710 case 0x00010203: | |
711 return Endianness_Big; | |
712 | |
713 case 0x03020100: | |
714 return Endianness_Little; | |
715 | |
716 default: | |
717 throw OrthancException(ErrorCode_NotImplemented); | |
718 } | |
719 } | |
0 | 720 } |