Mercurial > hg > orthanc-webviewer
comparison Orthanc/Toolbox.cpp @ 0:02f7a0400a91
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Feb 2015 13:45:35 +0100 |
parents | |
children | 54d5dd1df2e5 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:02f7a0400a91 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, 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 * 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. | |
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 "PrecompiledHeaders.h" | |
34 #include "Toolbox.h" | |
35 | |
36 #include "OrthancException.h" | |
37 | |
38 #include <stdint.h> | |
39 #include <string.h> | |
40 #include <boost/filesystem.hpp> | |
41 #include <boost/filesystem/fstream.hpp> | |
42 #include <algorithm> | |
43 #include <ctype.h> | |
44 | |
45 #if defined(_WIN32) | |
46 #include <windows.h> | |
47 #include <process.h> // For "_spawnvp()" | |
48 #else | |
49 #include <unistd.h> // For "execvp()" | |
50 #include <sys/wait.h> // For "waitpid()" | |
51 #endif | |
52 | |
53 #if defined(__APPLE__) && defined(__MACH__) | |
54 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ | |
55 #include <limits.h> /* PATH_MAX */ | |
56 #endif | |
57 | |
58 #if defined(__linux) || defined(__FreeBSD_kernel__) | |
59 #include <limits.h> /* PATH_MAX */ | |
60 #include <signal.h> | |
61 #include <unistd.h> | |
62 #endif | |
63 | |
64 #if BOOST_HAS_LOCALE != 1 | |
65 #error Since version 0.7.6, Orthanc entirely relies on boost::locale | |
66 #endif | |
67 | |
68 #include <boost/locale.hpp> | |
69 | |
70 | |
71 | |
72 namespace Orthanc | |
73 { | |
74 void Toolbox::TokenizeString(std::vector<std::string>& result, | |
75 const std::string& value, | |
76 char separator) | |
77 { | |
78 result.clear(); | |
79 | |
80 std::string currentItem; | |
81 | |
82 for (size_t i = 0; i < value.size(); i++) | |
83 { | |
84 if (value[i] == separator) | |
85 { | |
86 result.push_back(currentItem); | |
87 currentItem.clear(); | |
88 } | |
89 else | |
90 { | |
91 currentItem.push_back(value[i]); | |
92 } | |
93 } | |
94 | |
95 result.push_back(currentItem); | |
96 } | |
97 | |
98 | |
99 void Toolbox::CreateNewDirectory(const std::string& path) | |
100 { | |
101 if (boost::filesystem::exists(path)) | |
102 { | |
103 if (!boost::filesystem::is_directory(path)) | |
104 { | |
105 throw OrthancException("Cannot create the directory over an existing file: " + path); | |
106 } | |
107 } | |
108 else | |
109 { | |
110 if (!boost::filesystem::create_directories(path)) | |
111 { | |
112 throw OrthancException("Unable to create the directory: " + path); | |
113 } | |
114 } | |
115 } | |
116 | |
117 | |
118 bool Toolbox::IsExistingFile(const std::string& path) | |
119 { | |
120 return boost::filesystem::exists(path); | |
121 } | |
122 | |
123 | |
124 void Toolbox::ReadFile(std::string& content, | |
125 const std::string& path) | |
126 { | |
127 boost::filesystem::ifstream f; | |
128 f.open(path, std::ifstream::in | std::ifstream::binary); | |
129 if (!f.good()) | |
130 { | |
131 throw OrthancException(ErrorCode_InexistentFile); | |
132 } | |
133 | |
134 // http://www.cplusplus.com/reference/iostream/istream/tellg/ | |
135 f.seekg(0, std::ios::end); | |
136 std::streamsize size = f.tellg(); | |
137 f.seekg(0, std::ios::beg); | |
138 | |
139 content.resize(size); | |
140 if (size != 0) | |
141 { | |
142 f.read(reinterpret_cast<char*>(&content[0]), size); | |
143 } | |
144 | |
145 f.close(); | |
146 } | |
147 | |
148 | |
149 void Toolbox::WriteFile(const std::string& content, | |
150 const std::string& path) | |
151 { | |
152 boost::filesystem::ofstream f; | |
153 f.open(path, std::ofstream::binary); | |
154 if (!f.good()) | |
155 { | |
156 throw OrthancException(ErrorCode_CannotWriteFile); | |
157 } | |
158 | |
159 if (content.size() != 0) | |
160 { | |
161 f.write(content.c_str(), content.size()); | |
162 } | |
163 | |
164 f.close(); | |
165 } | |
166 | |
167 | |
168 | |
169 void Toolbox::RemoveFile(const std::string& path) | |
170 { | |
171 if (boost::filesystem::exists(path)) | |
172 { | |
173 if (boost::filesystem::is_regular_file(path)) | |
174 boost::filesystem::remove(path); | |
175 else | |
176 throw OrthancException("The path is not a regular file: " + path); | |
177 } | |
178 } | |
179 | |
180 | |
181 | |
182 void Toolbox::USleep(uint64_t microSeconds) | |
183 { | |
184 #if defined(_WIN32) | |
185 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); | |
186 #elif defined(__linux) || defined(__APPLE__) || defined(__FreeBSD_kernel__) | |
187 usleep(microSeconds); | |
188 #else | |
189 #error Support your platform here | |
190 #endif | |
191 } | |
192 | |
193 | |
194 Endianness Toolbox::DetectEndianness() | |
195 { | |
196 // http://sourceforge.net/p/predef/wiki/Endianness/ | |
197 | |
198 uint8_t buffer[4]; | |
199 | |
200 buffer[0] = 0x00; | |
201 buffer[1] = 0x01; | |
202 buffer[2] = 0x02; | |
203 buffer[3] = 0x03; | |
204 | |
205 switch (*((uint32_t *)buffer)) | |
206 { | |
207 case 0x00010203: | |
208 return Endianness_Big; | |
209 | |
210 case 0x03020100: | |
211 return Endianness_Little; | |
212 | |
213 default: | |
214 throw OrthancException(ErrorCode_NotImplemented); | |
215 } | |
216 } | |
217 | |
218 | |
219 std::string Toolbox::StripSpaces(const std::string& source) | |
220 { | |
221 size_t first = 0; | |
222 | |
223 while (first < source.length() && | |
224 isspace(source[first])) | |
225 { | |
226 first++; | |
227 } | |
228 | |
229 if (first == source.length()) | |
230 { | |
231 // String containing only spaces | |
232 return ""; | |
233 } | |
234 | |
235 size_t last = source.length(); | |
236 while (last > first && | |
237 isspace(source[last - 1])) | |
238 { | |
239 last--; | |
240 } | |
241 | |
242 assert(first <= last); | |
243 return source.substr(first, last - first); | |
244 } | |
245 } | |
246 |