comparison Core/Toolbox.cpp @ 943:3fb427ac3f53 plugins

integration mainline -> plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Jun 2014 11:40:41 +0200
parents 517e28b420af b3f6fb1130cd
children 743a75b14bef
comparison
equal deleted inserted replaced
939:ac42ca61eee6 943:3fb427ac3f53
58 #include <limits.h> /* PATH_MAX */ 58 #include <limits.h> /* PATH_MAX */
59 #include <signal.h> 59 #include <signal.h>
60 #include <unistd.h> 60 #include <unistd.h>
61 #endif 61 #endif
62 62
63 #if BOOST_HAS_LOCALE == 1 63 #if BOOST_HAS_LOCALE != 1
64 #error Since version 0.7.6, Orthanc entirely relies on boost::locale
65 #endif
66
64 #include <boost/locale.hpp> 67 #include <boost/locale.hpp>
65 #else
66 #include <iconv.h>
67 #endif
68 68
69 #include "../Resources/md5/md5.h" 69 #include "../Resources/md5/md5.h"
70 #include "../Resources/base64/base64.h" 70 #include "../Resources/base64/base64.h"
71 71
72 72
73 #ifdef _MSC_VER 73 #ifdef _MSC_VER
74 // Patch for the missing "_strtoll" symbol when compiling with Visual Studio 74 // Patch for the missing "_strtoll" symbol when compiling with Visual Studio
75 extern "C" 75 extern "C"
76 { 76 {
77 int64_t _strtoi64(const char *nptr, char **endptr, int base); 77 int64_t _strtoi64(const char *nptr, char **endptr, int base);
78 int64_t strtoll(const char *nptr, char **endptr, int base) 78 int64_t strtoll(const char *nptr, char **endptr, int base)
79 { 79 {
80 return _strtoi64(nptr, endptr, base); 80 return _strtoi64(nptr, endptr, base);
81 } 81 }
82 }
83 #endif
84
85
86 #if BOOST_HAS_LOCALE == 0
87 namespace
88 {
89 class IconvRabi
90 {
91 private:
92 iconv_t context_;
93
94 public:
95 IconvRabi(const char* tocode, const char* fromcode)
96 {
97 context_ = iconv_open(tocode, fromcode);
98 if (!context_)
99 {
100 throw Orthanc::OrthancException("Unknown code page");
101 }
102 }
103
104 ~IconvRabi()
105 {
106 iconv_close(context_);
107 }
108
109 std::string Convert(const std::string& source)
110 {
111 if (source.size() == 0)
112 {
113 return "";
114 }
115
116 std::string result;
117 char* sourcePos = const_cast<char*>(&source[0]);
118 size_t sourceLeft = source.size();
119
120 std::vector<char> storage(source.size() + 10);
121
122 while (sourceLeft > 0)
123 {
124 char* tmp = &storage[0];
125 size_t outputLeft = storage.size();
126 size_t err = iconv(context_, &sourcePos, &sourceLeft, &tmp, &outputLeft);
127 if (err < 0)
128 {
129 throw Orthanc::OrthancException("Bad character in sequence");
130 }
131
132 size_t count = storage.size() - outputLeft;
133 result += std::string(&storage[0], count);
134 }
135
136 return result;
137 }
138 };
139 } 82 }
140 #endif 83 #endif
141 84
142 85
143 namespace Orthanc 86 namespace Orthanc
160 103
161 void Toolbox::USleep(uint64_t microSeconds) 104 void Toolbox::USleep(uint64_t microSeconds)
162 { 105 {
163 #if defined(_WIN32) 106 #if defined(_WIN32)
164 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000))); 107 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
165 #elif defined(__linux) || defined(__FreeBSD_kernel__) 108 #elif defined(__linux) || defined(__APPLE__) || defined(__FreeBSD_kernel__)
166 usleep(microSeconds); 109 usleep(microSeconds);
167 #else 110 #else
168 #error Support your platform here 111 #error Support your platform here
169 #endif 112 #endif
170 } 113 }
552 495
553 496
554 std::string Toolbox::ConvertToUtf8(const std::string& source, 497 std::string Toolbox::ConvertToUtf8(const std::string& source,
555 const char* fromEncoding) 498 const char* fromEncoding)
556 { 499 {
557 #if BOOST_HAS_LOCALE == 1
558 try 500 try
559 { 501 {
560 return boost::locale::conv::to_utf<char>(source, fromEncoding); 502 return boost::locale::conv::to_utf<char>(source, fromEncoding);
561 } 503 }
562 catch (std::runtime_error&) 504 catch (std::runtime_error&)
563 { 505 {
564 // Bad input string or bad encoding 506 // Bad input string or bad encoding
565 return ConvertToAscii(source); 507 return ConvertToAscii(source);
566 } 508 }
567 #else
568 IconvRabi iconv("UTF-8", fromEncoding);
569 try
570 {
571 return iconv.Convert(source);
572 }
573 catch (OrthancException)
574 {
575 return ConvertToAscii(source);
576 }
577 #endif
578 } 509 }
579 510
580 511
581 std::string Toolbox::ConvertToAscii(const std::string& source) 512 std::string Toolbox::ConvertToAscii(const std::string& source)
582 { 513 {
583 std::string result; 514 std::string result;
584 515
585 result.reserve(source.size()); 516 result.reserve(source.size() + 1);
586 for (size_t i = 0; i < source.size(); i++) 517 for (size_t i = 0; i < source.size(); i++)
587 { 518 {
588 if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i])) 519 if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i]))
589 { 520 {
590 result.push_back(source[i]); 521 result.push_back(source[i]);