Mercurial > hg > orthanc-webviewer
comparison Orthanc/Core/Toolbox.cpp @ 145:d850500b8ca6
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 08 Nov 2016 10:15:05 +0100 |
parents | 9362080e5e3d |
children | 4217644ac904 |
comparison
equal
deleted
inserted
replaced
144:daf99382bc18 | 145:d850500b8ca6 |
---|---|
109 #endif | 109 #endif |
110 | 110 |
111 | 111 |
112 namespace Orthanc | 112 namespace Orthanc |
113 { | 113 { |
114 void Toolbox::USleep(uint64_t microSeconds) | |
115 { | |
116 #if defined(_WIN32) | |
117 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); | |
118 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__native_client__) | |
119 usleep(microSeconds); | |
120 #else | |
121 #error Support your platform here | |
122 #endif | |
123 } | |
124 | |
125 | |
114 #if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 | 126 #if !defined(ORTHANC_SANDBOXED) || ORTHANC_SANDBOXED != 1 |
115 static bool finish_; | 127 static bool finish_; |
116 static ServerBarrierEvent barrierEvent_; | 128 static ServerBarrierEvent barrierEvent_; |
117 | 129 |
118 #if defined(_WIN32) | 130 #if defined(_WIN32) |
131 } | 143 } |
132 | 144 |
133 finish_ = true; | 145 finish_ = true; |
134 } | 146 } |
135 #endif | 147 #endif |
136 | |
137 | |
138 void Toolbox::USleep(uint64_t microSeconds) | |
139 { | |
140 #if defined(_WIN32) | |
141 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); | |
142 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) | |
143 usleep(microSeconds); | |
144 #else | |
145 #error Support your platform here | |
146 #endif | |
147 } | |
148 | 148 |
149 | 149 |
150 static ServerBarrierEvent ServerBarrierInternal(const bool* stopFlag) | 150 static ServerBarrierEvent ServerBarrierInternal(const bool* stopFlag) |
151 { | 151 { |
152 #if defined(_WIN32) | 152 #if defined(_WIN32) |
310 void Toolbox::WriteFile(const void* content, | 310 void Toolbox::WriteFile(const void* content, |
311 size_t size, | 311 size_t size, |
312 const std::string& path) | 312 const std::string& path) |
313 { | 313 { |
314 boost::filesystem::ofstream f; | 314 boost::filesystem::ofstream f; |
315 f.open(path, std::ofstream::binary); | 315 f.open(path, std::ofstream::out | std::ofstream::binary); |
316 if (!f.good()) | 316 if (!f.good()) |
317 { | 317 { |
318 throw OrthancException(ErrorCode_CannotWriteFile); | 318 throw OrthancException(ErrorCode_CannotWriteFile); |
319 } | 319 } |
320 | 320 |
321 if (size != 0) | 321 if (size != 0) |
322 { | 322 { |
323 f.write(reinterpret_cast<const char*>(content), size); | 323 f.write(reinterpret_cast<const char*>(content), size); |
324 | |
325 if (!f.good()) | |
326 { | |
327 f.close(); | |
328 throw OrthancException(ErrorCode_FileStorageCannotWrite); | |
329 } | |
324 } | 330 } |
325 | 331 |
326 f.close(); | 332 f.close(); |
327 } | 333 } |
328 #endif | 334 #endif |
833 catch (std::runtime_error&) | 839 catch (std::runtime_error&) |
834 { | 840 { |
835 // Bad input string or bad encoding | 841 // Bad input string or bad encoding |
836 return ConvertToAscii(source); | 842 return ConvertToAscii(source); |
837 } | 843 } |
844 } | |
845 | |
846 | |
847 bool Toolbox::IsAsciiString(const void* data, | |
848 size_t size) | |
849 { | |
850 const uint8_t* p = reinterpret_cast<const uint8_t*>(data); | |
851 | |
852 for (size_t i = 0; i < size; i++, p++) | |
853 { | |
854 if (*p > 127 || (*p != 0 && iscntrl(*p))) | |
855 { | |
856 return false; | |
857 } | |
858 } | |
859 | |
860 return true; | |
838 } | 861 } |
839 | 862 |
840 | 863 |
841 std::string Toolbox::ConvertToAscii(const std::string& source) | 864 std::string Toolbox::ConvertToAscii(const std::string& source) |
842 { | 865 { |