0
|
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)
|
10
|
35 #include <signal.h>
|
0
|
36 #include <unistd.h>
|
|
37 #endif
|
|
38
|
|
39
|
|
40 namespace Palantir
|
|
41 {
|
|
42 static bool finish;
|
|
43
|
10
|
44 #if defined(_WIN32)
|
|
45 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
|
|
46 {
|
|
47 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
|
|
48 finish = true;
|
|
49 return true;
|
|
50 }
|
|
51 #else
|
0
|
52 static void SignalHandler(int)
|
|
53 {
|
|
54 finish = true;
|
|
55 }
|
10
|
56 #endif
|
0
|
57
|
|
58 void Toolbox::Sleep(uint32_t seconds)
|
|
59 {
|
|
60 #if defined(_WIN32)
|
|
61 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000));
|
|
62 #elif defined(__linux)
|
|
63 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000));
|
|
64 #else
|
|
65 #error Support your platform here
|
|
66 #endif
|
|
67 }
|
|
68
|
|
69 void Toolbox::USleep(uint64_t microSeconds)
|
|
70 {
|
|
71 #if defined(_WIN32)
|
8
|
72 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
|
0
|
73 #elif defined(__linux)
|
|
74 usleep(microSeconds);
|
|
75 #else
|
|
76 #error Support your platform here
|
|
77 #endif
|
|
78 }
|
|
79
|
|
80
|
|
81 void Toolbox::ServerBarrier()
|
|
82 {
|
10
|
83 #if defined(_WIN32)
|
|
84 SetConsoleCtrlHandler(ConsoleControlHandler, true);
|
|
85 #else
|
0
|
86 signal(SIGINT, SignalHandler);
|
|
87 signal(SIGQUIT, SignalHandler);
|
|
88 #endif
|
|
89
|
|
90 finish = false;
|
|
91 while (!finish)
|
|
92 {
|
|
93 USleep(100000);
|
|
94 }
|
|
95
|
10
|
96 #if defined(_WIN32)
|
|
97 SetConsoleCtrlHandler(ConsoleControlHandler, false);
|
|
98 #else
|
0
|
99 signal(SIGINT, NULL);
|
|
100 signal(SIGQUIT, NULL);
|
|
101 #endif
|
|
102 }
|
|
103
|
|
104
|
|
105
|
|
106 void Toolbox::ToUpperCase(std::string& s)
|
|
107 {
|
|
108 std::transform(s.begin(), s.end(), s.begin(), toupper);
|
|
109 }
|
|
110
|
|
111
|
|
112 void Toolbox::ToLowerCase(std::string& s)
|
|
113 {
|
|
114 std::transform(s.begin(), s.end(), s.begin(), tolower);
|
|
115 }
|
|
116
|
|
117
|
|
118
|
|
119 void Toolbox::ReadFile(std::string& content,
|
|
120 const std::string& path)
|
|
121 {
|
|
122 boost::filesystem::ifstream f;
|
|
123 f.open(path, std::ifstream::in | std::ios::binary);
|
|
124 if (!f.good())
|
|
125 {
|
|
126 throw PalantirException("Unable to open a file");
|
|
127 }
|
|
128
|
|
129 // http://www.cplusplus.com/reference/iostream/istream/tellg/
|
|
130 f.seekg(0, std::ios::end);
|
|
131 std::streamsize size = f.tellg();
|
|
132 f.seekg(0, std::ios::beg);
|
|
133
|
|
134 content.resize(size);
|
|
135 if (size != 0)
|
|
136 {
|
|
137 f.read(reinterpret_cast<char*>(&content[0]), size);
|
|
138 }
|
|
139
|
|
140 f.close();
|
|
141 }
|
|
142
|
|
143
|
|
144 void Toolbox::RemoveFile(const std::string& path)
|
|
145 {
|
|
146 if (boost::filesystem::exists(path))
|
|
147 {
|
|
148 if (boost::filesystem::is_regular_file(path))
|
|
149 boost::filesystem::remove(path);
|
|
150 else
|
|
151 throw PalantirException("The path is not a regular file: " + path);
|
|
152 }
|
|
153 }
|
|
154
|
|
155
|
|
156
|
|
157 void Toolbox::SplitUriComponents(UriComponents& components,
|
|
158 const std::string& uri)
|
|
159 {
|
|
160 static const char URI_SEPARATOR = '/';
|
|
161
|
|
162 components.clear();
|
|
163
|
|
164 if (uri.size() == 0 ||
|
|
165 uri[0] != URI_SEPARATOR)
|
|
166 {
|
|
167 throw PalantirException(ErrorCode_UriSyntax);
|
|
168 }
|
|
169
|
|
170 // Count the number of slashes in the URI to make an assumption
|
|
171 // about the number of components in the URI
|
|
172 unsigned int estimatedSize = 0;
|
|
173 for (unsigned int i = 0; i < uri.size(); i++)
|
|
174 {
|
|
175 if (uri[i] == URI_SEPARATOR)
|
|
176 estimatedSize++;
|
|
177 }
|
|
178
|
|
179 components.reserve(estimatedSize - 1);
|
|
180
|
|
181 unsigned int start = 1;
|
|
182 unsigned int end = 1;
|
|
183 while (end < uri.size())
|
|
184 {
|
|
185 // This is the loop invariant
|
|
186 assert(uri[start - 1] == '/' && (end >= start));
|
|
187
|
|
188 if (uri[end] == '/')
|
|
189 {
|
|
190 components.push_back(std::string(&uri[start], end - start));
|
|
191 end++;
|
|
192 start = end;
|
|
193 }
|
|
194 else
|
|
195 {
|
|
196 end++;
|
|
197 }
|
|
198 }
|
|
199
|
|
200 if (start < uri.size())
|
|
201 {
|
|
202 components.push_back(std::string(&uri[start], end - start));
|
|
203 }
|
|
204 }
|
|
205
|
|
206
|
|
207 bool Toolbox::IsChildUri(const UriComponents& baseUri,
|
|
208 const UriComponents& testedUri)
|
|
209 {
|
|
210 if (testedUri.size() < baseUri.size())
|
|
211 {
|
|
212 return false;
|
|
213 }
|
|
214
|
|
215 for (size_t i = 0; i < baseUri.size(); i++)
|
|
216 {
|
|
217 if (baseUri[i] != testedUri[i])
|
|
218 return false;
|
|
219 }
|
|
220
|
|
221 return true;
|
|
222 }
|
|
223
|
|
224
|
|
225 std::string Toolbox::AutodetectMimeType(const std::string& path)
|
|
226 {
|
|
227 std::string contentType;
|
|
228 size_t lastDot = path.rfind('.');
|
|
229 size_t lastSlash = path.rfind('/');
|
|
230
|
|
231 if (lastDot == std::string::npos ||
|
|
232 (lastSlash != std::string::npos && lastDot < lastSlash))
|
|
233 {
|
|
234 // No trailing dot, unable to detect the content type
|
|
235 }
|
|
236 else
|
|
237 {
|
|
238 const char* extension = &path[lastDot + 1];
|
|
239
|
|
240 // http://en.wikipedia.org/wiki/Mime_types
|
|
241 // Text types
|
|
242 if (!strcmp(extension, "txt"))
|
|
243 contentType = "text/plain";
|
|
244 else if (!strcmp(extension, "html"))
|
|
245 contentType = "text/html";
|
|
246 else if (!strcmp(extension, "xml"))
|
|
247 contentType = "text/xml";
|
|
248 else if (!strcmp(extension, "css"))
|
|
249 contentType = "text/css";
|
|
250
|
|
251 // Application types
|
|
252 else if (!strcmp(extension, "js"))
|
|
253 contentType = "application/javascript";
|
|
254 else if (!strcmp(extension, "json"))
|
|
255 contentType = "application/json";
|
|
256 else if (!strcmp(extension, "pdf"))
|
|
257 contentType = "application/pdf";
|
|
258
|
|
259 // Images types
|
|
260 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
|
|
261 contentType = "image/jpeg";
|
|
262 else if (!strcmp(extension, "gif"))
|
|
263 contentType = "image/gif";
|
|
264 else if (!strcmp(extension, "png"))
|
|
265 contentType = "image/png";
|
|
266 }
|
|
267
|
|
268 return contentType;
|
|
269 }
|
|
270
|
|
271
|
|
272 std::string Toolbox::FlattenUri(const UriComponents& components,
|
|
273 size_t fromLevel)
|
|
274 {
|
|
275 if (components.size() <= fromLevel)
|
|
276 {
|
|
277 return "/";
|
|
278 }
|
|
279 else
|
|
280 {
|
|
281 std::string r;
|
|
282
|
|
283 for (size_t i = fromLevel; i < components.size(); i++)
|
|
284 {
|
|
285 r += "/" + components[i];
|
|
286 }
|
|
287
|
|
288 return r;
|
|
289 }
|
|
290 }
|
|
291
|
|
292
|
|
293
|
|
294 uint64_t Toolbox::GetFileSize(const std::string& path)
|
|
295 {
|
|
296 try
|
|
297 {
|
|
298 return static_cast<uint64_t>(boost::filesystem::file_size(path));
|
|
299 }
|
|
300 catch (boost::filesystem::filesystem_error)
|
|
301 {
|
|
302 throw PalantirException(ErrorCode_InexistentFile);
|
|
303 }
|
|
304 }
|
|
305 }
|