comparison Core/Toolbox.cpp @ 107:3b45473c0a73

replace boost::locale with iconv for debian
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Oct 2012 11:22:20 +0200
parents 8517e2c44283
children fd7b0a3e6260
comparison
equal deleted inserted replaced
106:332fec038d52 107:3b45473c0a73
24 24
25 #include <string.h> 25 #include <string.h>
26 #include <boost/filesystem.hpp> 26 #include <boost/filesystem.hpp>
27 #include <boost/filesystem/fstream.hpp> 27 #include <boost/filesystem/fstream.hpp>
28 #include <algorithm> 28 #include <algorithm>
29 #include <ctype.h>
29 30
30 #if defined(_WIN32) 31 #if defined(_WIN32)
31 #include <windows.h> 32 #include <windows.h>
32 #endif 33 #endif
33 34
40 #include <limits.h> /* PATH_MAX */ 41 #include <limits.h> /* PATH_MAX */
41 #include <signal.h> 42 #include <signal.h>
42 #include <unistd.h> 43 #include <unistd.h>
43 #endif 44 #endif
44 45
46 #if BOOST_HAS_LOCALE == 1
47 #include <boost/locale.hpp>
48 #else
49 #include <iconv.h>
50 #endif
51
45 #include "../Resources/md5/md5.h" 52 #include "../Resources/md5/md5.h"
46 #include "../Resources/base64/base64.h" 53 #include "../Resources/base64/base64.h"
54
55
56 #if BOOST_HAS_LOCALE == 0
57 namespace
58 {
59 class IconvRabi
60 {
61 private:
62 iconv_t context_;
63
64 public:
65 IconvRabi(const char* tocode, const char* fromcode)
66 {
67 context_ = iconv_open(tocode, fromcode);
68 if (!context_)
69 {
70 throw Orthanc::OrthancException("Unknown code page");
71 }
72 }
73
74 ~IconvRabi()
75 {
76 iconv_close(context_);
77 }
78
79 std::string Convert(const std::string& source)
80 {
81 if (source.size() == 0)
82 {
83 return "";
84 }
85
86 std::string result;
87 char* sourcePos = const_cast<char*>(&source[0]);
88 size_t sourceLeft = source.size();
89
90 std::vector<char> storage(source.size() + 10);
91
92 while (sourceLeft > 0)
93 {
94 char* tmp = &storage[0];
95 size_t outputLeft = storage.size();
96 size_t err = iconv(context_, &sourcePos, &sourceLeft, &tmp, &outputLeft);
97 if (err < 0)
98 {
99 throw Orthanc::OrthancException("Bad character in sequence");
100 }
101
102 size_t count = storage.size() - outputLeft;
103 result += std::string(&storage[0], count);
104 }
105
106 return result;
107 }
108 };
109 }
110 #endif
111
47 112
48 namespace Orthanc 113 namespace Orthanc
49 { 114 {
50 static bool finish; 115 static bool finish;
51 116
396 { 461 {
397 boost::filesystem::path p(GetPathToExecutable()); 462 boost::filesystem::path p(GetPathToExecutable());
398 return p.parent_path().string(); 463 return p.parent_path().string();
399 } 464 }
400 465
466
467 std::string Toolbox::ConvertToUtf8(const std::string& source,
468 const char* fromEncoding)
469 {
470 #if BOOST_HAS_LOCALE == 1
471 try
472 {
473 return boost::locale::conv::to_utf<char>(source, fromEncoding);
474 }
475 catch (std::runtime_error&)
476 {
477 // Bad input string or bad encoding
478 return ConvertToAscii(source);
479 }
480 #else
481 IconvRabi iconv("UTF-8", fromEncoding);
482 try
483 {
484 return iconv.Convert(source);
485 }
486 catch (OrthancException)
487 {
488 return ConvertToAscii(source);
489 }
490 #endif
491 }
492
493
494 std::string Toolbox::ConvertToAscii(const std::string& source)
495 {
496 std::string result;
497
498 result.reserve(source.size());
499 for (size_t i = 0; i < source.size(); i++)
500 {
501 if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i]))
502 {
503 result.push_back(source[i]);
504 }
505 }
506
507 return result;
508 }
509
510
401 } 511 }