comparison Core/Toolbox.cpp @ 915:703bef350e89

Orthanc now relies entirely on boost::locale
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 24 Jun 2014 12:00:41 +0200
parents 816dccaeb7cf
children 27d256e0b458
comparison
equal deleted inserted replaced
890:816dccaeb7cf 915:703bef350e89
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
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 {