Mercurial > hg > orthanc
comparison Core/Toolbox.cpp @ 0:3959d33612cc
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jul 2012 14:32:22 +0200 |
parents | |
children | 3a584803783e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3959d33612cc |
---|---|
1 /** | |
2 * Palantir - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
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. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "Toolbox.h" | |
22 | |
23 #include "PalantirException.h" | |
24 | |
25 #include <string.h> | |
26 #include <boost/filesystem.hpp> | |
27 #include <boost/filesystem/fstream.hpp> | |
28 #include <algorithm> | |
29 | |
30 #if defined(_WIN32) | |
31 #include <windows.h> | |
32 #endif | |
33 | |
34 #if defined(__linux) | |
35 #include <unistd.h> | |
36 #endif | |
37 | |
38 #include <signal.h> | |
39 | |
40 namespace Palantir | |
41 { | |
42 static bool finish; | |
43 | |
44 static void SignalHandler(int) | |
45 { | |
46 finish = true; | |
47 } | |
48 | |
49 | |
50 void Toolbox::Sleep(uint32_t seconds) | |
51 { | |
52 #if defined(_WIN32) | |
53 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000)); | |
54 #elif defined(__linux) | |
55 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000)); | |
56 #else | |
57 #error Support your platform here | |
58 #endif | |
59 } | |
60 | |
61 void Toolbox::USleep(uint64_t microSeconds) | |
62 { | |
63 #if defined(_WIN32) | |
64 ::Sleep(microSeconds / static_cast<uint64_t>(1000)); | |
65 #elif defined(__linux) | |
66 usleep(microSeconds); | |
67 #else | |
68 #error Support your platform here | |
69 #endif | |
70 } | |
71 | |
72 | |
73 void Toolbox::ServerBarrier() | |
74 { | |
75 signal(SIGINT, SignalHandler); | |
76 | |
77 #if !defined(_WIN32) | |
78 signal(SIGQUIT, SignalHandler); | |
79 #endif | |
80 | |
81 finish = false; | |
82 while (!finish) | |
83 { | |
84 USleep(100000); | |
85 } | |
86 | |
87 signal(SIGINT, NULL); | |
88 | |
89 #if !defined(_WIN32) | |
90 signal(SIGQUIT, NULL); | |
91 #endif | |
92 } | |
93 | |
94 | |
95 | |
96 void Toolbox::ToUpperCase(std::string& s) | |
97 { | |
98 std::transform(s.begin(), s.end(), s.begin(), toupper); | |
99 } | |
100 | |
101 | |
102 void Toolbox::ToLowerCase(std::string& s) | |
103 { | |
104 std::transform(s.begin(), s.end(), s.begin(), tolower); | |
105 } | |
106 | |
107 | |
108 | |
109 void Toolbox::ReadFile(std::string& content, | |
110 const std::string& path) | |
111 { | |
112 boost::filesystem::ifstream f; | |
113 f.open(path, std::ifstream::in | std::ios::binary); | |
114 if (!f.good()) | |
115 { | |
116 throw PalantirException("Unable to open a file"); | |
117 } | |
118 | |
119 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
120 f.seekg(0, std::ios::end); | |
121 std::streamsize size = f.tellg(); | |
122 f.seekg(0, std::ios::beg); | |
123 | |
124 content.resize(size); | |
125 if (size != 0) | |
126 { | |
127 f.read(reinterpret_cast<char*>(&content[0]), size); | |
128 } | |
129 | |
130 f.close(); | |
131 } | |
132 | |
133 | |
134 void Toolbox::RemoveFile(const std::string& path) | |
135 { | |
136 if (boost::filesystem::exists(path)) | |
137 { | |
138 if (boost::filesystem::is_regular_file(path)) | |
139 boost::filesystem::remove(path); | |
140 else | |
141 throw PalantirException("The path is not a regular file: " + path); | |
142 } | |
143 } | |
144 | |
145 | |
146 | |
147 void Toolbox::SplitUriComponents(UriComponents& components, | |
148 const std::string& uri) | |
149 { | |
150 static const char URI_SEPARATOR = '/'; | |
151 | |
152 components.clear(); | |
153 | |
154 if (uri.size() == 0 || | |
155 uri[0] != URI_SEPARATOR) | |
156 { | |
157 throw PalantirException(ErrorCode_UriSyntax); | |
158 } | |
159 | |
160 // Count the number of slashes in the URI to make an assumption | |
161 // about the number of components in the URI | |
162 unsigned int estimatedSize = 0; | |
163 for (unsigned int i = 0; i < uri.size(); i++) | |
164 { | |
165 if (uri[i] == URI_SEPARATOR) | |
166 estimatedSize++; | |
167 } | |
168 | |
169 components.reserve(estimatedSize - 1); | |
170 | |
171 unsigned int start = 1; | |
172 unsigned int end = 1; | |
173 while (end < uri.size()) | |
174 { | |
175 // This is the loop invariant | |
176 assert(uri[start - 1] == '/' && (end >= start)); | |
177 | |
178 if (uri[end] == '/') | |
179 { | |
180 components.push_back(std::string(&uri[start], end - start)); | |
181 end++; | |
182 start = end; | |
183 } | |
184 else | |
185 { | |
186 end++; | |
187 } | |
188 } | |
189 | |
190 if (start < uri.size()) | |
191 { | |
192 components.push_back(std::string(&uri[start], end - start)); | |
193 } | |
194 } | |
195 | |
196 | |
197 bool Toolbox::IsChildUri(const UriComponents& baseUri, | |
198 const UriComponents& testedUri) | |
199 { | |
200 if (testedUri.size() < baseUri.size()) | |
201 { | |
202 return false; | |
203 } | |
204 | |
205 for (size_t i = 0; i < baseUri.size(); i++) | |
206 { | |
207 if (baseUri[i] != testedUri[i]) | |
208 return false; | |
209 } | |
210 | |
211 return true; | |
212 } | |
213 | |
214 | |
215 std::string Toolbox::AutodetectMimeType(const std::string& path) | |
216 { | |
217 std::string contentType; | |
218 size_t lastDot = path.rfind('.'); | |
219 size_t lastSlash = path.rfind('/'); | |
220 | |
221 if (lastDot == std::string::npos || | |
222 (lastSlash != std::string::npos && lastDot < lastSlash)) | |
223 { | |
224 // No trailing dot, unable to detect the content type | |
225 } | |
226 else | |
227 { | |
228 const char* extension = &path[lastDot + 1]; | |
229 | |
230 // http://en.wikipedia.org/wiki/Mime_types | |
231 // Text types | |
232 if (!strcmp(extension, "txt")) | |
233 contentType = "text/plain"; | |
234 else if (!strcmp(extension, "html")) | |
235 contentType = "text/html"; | |
236 else if (!strcmp(extension, "xml")) | |
237 contentType = "text/xml"; | |
238 else if (!strcmp(extension, "css")) | |
239 contentType = "text/css"; | |
240 | |
241 // Application types | |
242 else if (!strcmp(extension, "js")) | |
243 contentType = "application/javascript"; | |
244 else if (!strcmp(extension, "json")) | |
245 contentType = "application/json"; | |
246 else if (!strcmp(extension, "pdf")) | |
247 contentType = "application/pdf"; | |
248 | |
249 // Images types | |
250 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) | |
251 contentType = "image/jpeg"; | |
252 else if (!strcmp(extension, "gif")) | |
253 contentType = "image/gif"; | |
254 else if (!strcmp(extension, "png")) | |
255 contentType = "image/png"; | |
256 } | |
257 | |
258 return contentType; | |
259 } | |
260 | |
261 | |
262 std::string Toolbox::FlattenUri(const UriComponents& components, | |
263 size_t fromLevel) | |
264 { | |
265 if (components.size() <= fromLevel) | |
266 { | |
267 return "/"; | |
268 } | |
269 else | |
270 { | |
271 std::string r; | |
272 | |
273 for (size_t i = fromLevel; i < components.size(); i++) | |
274 { | |
275 r += "/" + components[i]; | |
276 } | |
277 | |
278 return r; | |
279 } | |
280 } | |
281 | |
282 | |
283 | |
284 uint64_t Toolbox::GetFileSize(const std::string& path) | |
285 { | |
286 try | |
287 { | |
288 return static_cast<uint64_t>(boost::filesystem::file_size(path)); | |
289 } | |
290 catch (boost::filesystem::filesystem_error) | |
291 { | |
292 throw PalantirException(ErrorCode_InexistentFile); | |
293 } | |
294 } | |
295 } |