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