comparison Orthanc/Core/Toolbox.cpp @ 128:e8cfda4c8a2f

Sync + support of JPEG2000 lossless images with YBR_RCT photometric interpretation
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 15 Apr 2016 21:44:03 +0200
parents 145e654112d6
children 2c73a785c08e
comparison
equal deleted inserted replaced
127:5754d39b011d 128:e8cfda4c8a2f
65 #if defined(__APPLE__) && defined(__MACH__) 65 #if defined(__APPLE__) && defined(__MACH__)
66 #include <mach-o/dyld.h> /* _NSGetExecutablePath */ 66 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
67 #include <limits.h> /* PATH_MAX */ 67 #include <limits.h> /* PATH_MAX */
68 #endif 68 #endif
69 69
70 #if defined(__linux) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) 70 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
71 #include <limits.h> /* PATH_MAX */ 71 #include <limits.h> /* PATH_MAX */
72 #include <signal.h> 72 #include <signal.h>
73 #include <unistd.h> 73 #include <unistd.h>
74 #endif 74 #endif
75 75
130 130
131 void Toolbox::USleep(uint64_t microSeconds) 131 void Toolbox::USleep(uint64_t microSeconds)
132 { 132 {
133 #if defined(_WIN32) 133 #if defined(_WIN32)
134 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); 134 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
135 #elif defined(__linux) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) 135 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
136 usleep(microSeconds); 136 usleep(microSeconds);
137 #else 137 #else
138 #error Support your platform here 138 #error Support your platform here
139 #endif 139 #endif
140 } 140 }
204 result = source; 204 result = source;
205 ToLowerCase(result); 205 ToLowerCase(result);
206 } 206 }
207 207
208 208
209 void Toolbox::ReadFile(std::string& content, 209 static std::streamsize GetStreamSize(std::istream& f)
210 const std::string& path) 210 {
211 {
212 if (!IsRegularFile(path))
213 {
214 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path;
215 throw OrthancException(ErrorCode_RegularFileExpected);
216 }
217
218 boost::filesystem::ifstream f;
219 f.open(path, std::ifstream::in | std::ifstream::binary);
220 if (!f.good())
221 {
222 throw OrthancException(ErrorCode_InexistentFile);
223 }
224
225 // http://www.cplusplus.com/reference/iostream/istream/tellg/ 211 // http://www.cplusplus.com/reference/iostream/istream/tellg/
226 f.seekg(0, std::ios::end); 212 f.seekg(0, std::ios::end);
227 std::streamsize size = f.tellg(); 213 std::streamsize size = f.tellg();
228 f.seekg(0, std::ios::beg); 214 f.seekg(0, std::ios::beg);
229 215
216 return size;
217 }
218
219
220 void Toolbox::ReadFile(std::string& content,
221 const std::string& path)
222 {
223 if (!IsRegularFile(path))
224 {
225 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path;
226 throw OrthancException(ErrorCode_RegularFileExpected);
227 }
228
229 boost::filesystem::ifstream f;
230 f.open(path, std::ifstream::in | std::ifstream::binary);
231 if (!f.good())
232 {
233 throw OrthancException(ErrorCode_InexistentFile);
234 }
235
236 std::streamsize size = GetStreamSize(f);
230 content.resize(size); 237 content.resize(size);
231 if (size != 0) 238 if (size != 0)
232 { 239 {
233 f.read(reinterpret_cast<char*>(&content[0]), size); 240 f.read(reinterpret_cast<char*>(&content[0]), size);
234 } 241 }
235 242
236 f.close(); 243 f.close();
244 }
245
246
247 bool Toolbox::ReadHeader(std::string& header,
248 const std::string& path,
249 size_t headerSize)
250 {
251 if (!IsRegularFile(path))
252 {
253 LOG(ERROR) << std::string("The path does not point to a regular file: ") << path;
254 throw OrthancException(ErrorCode_RegularFileExpected);
255 }
256
257 boost::filesystem::ifstream f;
258 f.open(path, std::ifstream::in | std::ifstream::binary);
259 if (!f.good())
260 {
261 throw OrthancException(ErrorCode_InexistentFile);
262 }
263
264 bool full = true;
265
266 {
267 std::streamsize size = GetStreamSize(f);
268 if (size <= 0)
269 {
270 headerSize = 0;
271 full = false;
272 }
273 else if (static_cast<size_t>(size) < headerSize)
274 {
275 headerSize = size; // Truncate to the size of the file
276 full = false;
277 }
278 }
279
280 header.resize(headerSize);
281 if (headerSize != 0)
282 {
283 f.read(reinterpret_cast<char*>(&header[0]), headerSize);
284 }
285
286 f.close();
287
288 return full;
237 } 289 }
238 290
239 291
240 void Toolbox::WriteFile(const void* content, 292 void Toolbox::WriteFile(const void* content,
241 size_t size, 293 size_t size,
575 std::vector<char> buffer(32768); 627 std::vector<char> buffer(32768);
576 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1)); 628 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1));
577 return std::string(&buffer[0]); 629 return std::string(&buffer[0]);
578 } 630 }
579 631
580 #elif defined(__linux) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) 632 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
581 static std::string GetPathToExecutableInternal() 633 static std::string GetPathToExecutableInternal()
582 { 634 {
583 std::vector<char> buffer(PATH_MAX + 1); 635 std::vector<char> buffer(PATH_MAX + 1);
584 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1); 636 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1);
585 if (bytes == 0) 637 if (bytes == 0)