Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Toolbox.h @ 5807:8279eaab0d1d attach-custom-data
merged default -> attach-custom-data
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Tue, 24 Sep 2024 11:39:52 +0200 |
parents | f7adfb22e20e |
children |
comparison
equal
deleted
inserted
replaced
5085:79f98ee4f04b | 5807:8279eaab0d1d |
---|---|
1 /** | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
4 * Department, University Hospital of Liege, Belgium | 4 * Department, University Hospital of Liege, Belgium |
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium | 5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | 6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | 8 * |
8 * This program is free software: you can redistribute it and/or | 9 * This program is free software: you can redistribute it and/or |
9 * modify it under the terms of the GNU Lesser General Public License | 10 * modify it under the terms of the GNU Lesser General Public License |
10 * as published by the Free Software Foundation, either version 3 of | 11 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | 12 * the License, or (at your option) any later version. |
65 | 66 |
66 #if ORTHANC_ENABLE_PUGIXML == 1 | 67 #if ORTHANC_ENABLE_PUGIXML == 1 |
67 # include <pugixml.hpp> | 68 # include <pugixml.hpp> |
68 #endif | 69 #endif |
69 | 70 |
71 #include <boost/date_time/posix_time/posix_time.hpp> | |
72 | |
70 | 73 |
71 namespace Orthanc | 74 namespace Orthanc |
72 { | 75 { |
73 typedef std::vector<std::string> UriComponents; | 76 typedef std::vector<std::string> UriComponents; |
74 | 77 |
117 const UriComponents& testedUri); | 120 const UriComponents& testedUri); |
118 | 121 |
119 static std::string FlattenUri(const UriComponents& components, | 122 static std::string FlattenUri(const UriComponents& components, |
120 size_t fromLevel = 0); | 123 size_t fromLevel = 0); |
121 | 124 |
125 static std::string JoinUri(const std::string& base, const std::string& uri); | |
126 | |
122 #if ORTHANC_ENABLE_MD5 == 1 | 127 #if ORTHANC_ENABLE_MD5 == 1 |
123 static void ComputeMD5(std::string& result, | 128 static void ComputeMD5(std::string& result, |
124 const std::string& data); | 129 const std::string& data); |
125 | 130 |
126 static void ComputeMD5(std::string& result, | 131 static void ComputeMD5(std::string& result, |
127 const void* data, | 132 const void* data, |
128 size_t size); | 133 size_t size); |
134 | |
135 static void ComputeMD5(std::string& result, | |
136 const std::set<std::string>& data); | |
129 #endif | 137 #endif |
130 | 138 |
131 static void ComputeSHA1(std::string& result, | 139 static void ComputeSHA1(std::string& result, |
132 const std::string& data); | 140 const std::string& data); |
133 | 141 |
179 | 187 |
180 static Endianness DetectEndianness(); | 188 static Endianness DetectEndianness(); |
181 | 189 |
182 static std::string WildcardToRegularExpression(const std::string& s); | 190 static std::string WildcardToRegularExpression(const std::string& s); |
183 | 191 |
192 // TokenizeString result might contain empty strings (not SplitString) | |
184 static void TokenizeString(std::vector<std::string>& result, | 193 static void TokenizeString(std::vector<std::string>& result, |
185 const std::string& source, | 194 const std::string& source, |
186 char separator); | 195 char separator); |
187 | 196 |
197 // SplitString result won't contain empty strings (compared to TokenizeString) | |
198 static void SplitString(std::vector<std::string>& result, | |
199 const std::string& source, | |
200 char separator); | |
201 | |
202 // SplitString result won't contain empty strings (compared to TokenizeString) | |
203 static void SplitString(std::set<std::string>& result, | |
204 const std::string& source, | |
205 char separator); | |
206 | |
188 static void JoinStrings(std::string& result, | 207 static void JoinStrings(std::string& result, |
189 std::set<std::string>& source, | 208 const std::set<std::string>& source, |
190 const char* separator); | 209 const char* separator); |
191 | 210 |
192 static void JoinStrings(std::string& result, | 211 static void JoinStrings(std::string& result, |
193 std::vector<std::string>& source, | 212 const std::vector<std::string>& source, |
194 const char* separator); | 213 const char* separator); |
195 | 214 |
196 // returns true if all element of 'needles' are found in 'haystack' | 215 // returns true if all element of 'needles' are found in 'haystack' |
197 template <typename T> static bool IsSetInSet(const std::set<T>& needles, const std::set<T>& haystack) | 216 template <typename T> static bool IsSetInSet(const std::set<T>& needles, const std::set<T>& haystack) |
198 { | 217 { |
241 { | 260 { |
242 target.erase(*it); | 261 target.erase(*it); |
243 } | 262 } |
244 } | 263 } |
245 | 264 |
265 // returns true if all element of 'needles' are found in 'haystack' | |
266 template <typename T> static void GetIntersection(std::set<T>& target, const std::set<T>& a, const std::set<T>& b) | |
267 { | |
268 target.clear(); | |
269 | |
270 for (typename std::set<T>::const_iterator it = a.begin(); | |
271 it != a.end(); ++it) | |
272 { | |
273 if (b.count(*it) > 0) | |
274 { | |
275 target.insert(*it); | |
276 } | |
277 } | |
278 } | |
279 | |
280 | |
246 #if ORTHANC_ENABLE_PUGIXML == 1 | 281 #if ORTHANC_ENABLE_PUGIXML == 1 |
247 static void JsonToXml(std::string& target, | 282 static void JsonToXml(std::string& target, |
248 const Json::Value& source, | 283 const Json::Value& source, |
249 const std::string& rootElement = "root", | 284 const std::string& rootElement = "root", |
250 const std::string& arrayElement = "item"); | 285 const std::string& arrayElement = "item"); |
339 | 374 |
340 static void WriteStyledJson(std::string& target, | 375 static void WriteStyledJson(std::string& target, |
341 const Json::Value& source); | 376 const Json::Value& source); |
342 | 377 |
343 static void RemoveSurroundingQuotes(std::string& value); | 378 static void RemoveSurroundingQuotes(std::string& value); |
379 | |
380 class ORTHANC_PUBLIC ElapsedTimer | |
381 { | |
382 boost::posix_time::ptime start_; | |
383 public: | |
384 explicit ElapsedTimer(); | |
385 | |
386 uint64_t GetElapsedMilliseconds(); | |
387 uint64_t GetElapsedMicroseconds(); | |
388 uint64_t GetElapsedNanoseconds(); | |
389 | |
390 std::string GetHumanElapsedDuration(); | |
391 std::string GetHumanTransferSpeed(bool full, uint64_t sizeInBytes); | |
392 | |
393 void Restart(); | |
394 }; | |
395 | |
396 // This is a helper class to measure and log time spend e.g in a method. | |
397 // This should be used only during debugging and should likely not ever used in a release. | |
398 // By default, you should use it as a RAII but you may force Restart/StopAndLog manually if needed. | |
399 class ORTHANC_PUBLIC ElapsedTimeLogger | |
400 { | |
401 private: | |
402 ElapsedTimer timer_; | |
403 const std::string message_; | |
404 bool logged_; | |
405 | |
406 public: | |
407 explicit ElapsedTimeLogger(const std::string& message); | |
408 ~ElapsedTimeLogger(); | |
409 | |
410 void Restart(); | |
411 void StopAndLog(); | |
412 }; | |
413 | |
414 static std::string GetHumanFileSize(uint64_t sizeInBytes); | |
415 | |
416 static std::string GetHumanDuration(uint64_t durationInNanoseconds); | |
417 | |
418 static std::string GetHumanTransferSpeed(bool full, uint64_t sizeInBytes, uint64_t durationInNanoseconds); | |
419 | |
420 static bool ParseVersion(unsigned int& major, | |
421 unsigned int& minor, | |
422 unsigned int& revision, | |
423 const char* version); | |
424 | |
425 static bool IsVersionAbove(const char* version, | |
426 unsigned int major, | |
427 unsigned int minor, | |
428 unsigned int revision); | |
344 }; | 429 }; |
345 } | 430 } |
346 | 431 |
347 | 432 |
348 | 433 |