# HG changeset patch # User Sebastien Jodogne # Date 1349342540 -7200 # Node ID 3b45473c0a7310c8311be5d6ce50cfaafc4081fa # Parent 332fec038d52a4756a4ad8a4c2173e8defc60a1c replace boost::locale with iconv for debian diff -r 332fec038d52 -r 3b45473c0a73 Core/DicomFormat/DicomIntegerPixelAccessor.cpp --- a/Core/DicomFormat/DicomIntegerPixelAccessor.cpp Wed Oct 03 17:53:23 2012 +0200 +++ b/Core/DicomFormat/DicomIntegerPixelAccessor.cpp Thu Oct 04 11:22:20 2012 +0200 @@ -27,6 +27,7 @@ #include "../OrthancException.h" #include #include +#include namespace Orthanc { diff -r 332fec038d52 -r 3b45473c0a73 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Wed Oct 03 17:53:23 2012 +0200 +++ b/Core/Toolbox.cpp Thu Oct 04 11:22:20 2012 +0200 @@ -26,6 +26,7 @@ #include #include #include +#include #if defined(_WIN32) #include @@ -42,9 +43,73 @@ #include #endif +#if BOOST_HAS_LOCALE == 1 +#include +#else +#include +#endif + #include "../Resources/md5/md5.h" #include "../Resources/base64/base64.h" + +#if BOOST_HAS_LOCALE == 0 +namespace +{ + class IconvRabi + { + private: + iconv_t context_; + + public: + IconvRabi(const char* tocode, const char* fromcode) + { + context_ = iconv_open(tocode, fromcode); + if (!context_) + { + throw Orthanc::OrthancException("Unknown code page"); + } + } + + ~IconvRabi() + { + iconv_close(context_); + } + + std::string Convert(const std::string& source) + { + if (source.size() == 0) + { + return ""; + } + + std::string result; + char* sourcePos = const_cast(&source[0]); + size_t sourceLeft = source.size(); + + std::vector storage(source.size() + 10); + + while (sourceLeft > 0) + { + char* tmp = &storage[0]; + size_t outputLeft = storage.size(); + size_t err = iconv(context_, &sourcePos, &sourceLeft, &tmp, &outputLeft); + if (err < 0) + { + throw Orthanc::OrthancException("Bad character in sequence"); + } + + size_t count = storage.size() - outputLeft; + result += std::string(&storage[0], count); + } + + return result; + } + }; +} +#endif + + namespace Orthanc { static bool finish; @@ -398,4 +463,49 @@ return p.parent_path().string(); } + + std::string Toolbox::ConvertToUtf8(const std::string& source, + const char* fromEncoding) + { +#if BOOST_HAS_LOCALE == 1 + try + { + return boost::locale::conv::to_utf(source, fromEncoding); + } + catch (std::runtime_error&) + { + // Bad input string or bad encoding + return ConvertToAscii(source); + } +#else + IconvRabi iconv("UTF-8", fromEncoding); + try + { + return iconv.Convert(source); + } + catch (OrthancException) + { + return ConvertToAscii(source); + } +#endif + } + + + std::string Toolbox::ConvertToAscii(const std::string& source) + { + std::string result; + + result.reserve(source.size()); + for (size_t i = 0; i < source.size(); i++) + { + if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i])) + { + result.push_back(source[i]); + } + } + + return result; + } + + } diff -r 332fec038d52 -r 3b45473c0a73 Core/Toolbox.h --- a/Core/Toolbox.h Wed Oct 03 17:53:23 2012 +0200 +++ b/Core/Toolbox.h Thu Oct 04 11:22:20 2012 +0200 @@ -66,5 +66,10 @@ std::string GetPathToExecutable(); std::string GetDirectoryOfExecutable(); + + std::string ConvertToUtf8(const std::string& source, + const char* fromEncoding); + + std::string ConvertToAscii(const std::string& source); } } diff -r 332fec038d52 -r 3b45473c0a73 OrthancServer/FromDcmtkBridge.cpp --- a/OrthancServer/FromDcmtkBridge.cpp Wed Oct 03 17:53:23 2012 +0200 +++ b/OrthancServer/FromDcmtkBridge.cpp Thu Oct 04 11:22:20 2012 +0200 @@ -24,6 +24,7 @@ #include "FromDcmtkBridge.h" #include "ToDcmtkBridge.h" +#include "../Core/Toolbox.h" #include "../Core/OrthancException.h" #include "../Core/PngWriter.h" #include "../Core/DicomFormat/DicomString.h" @@ -32,7 +33,6 @@ #include -#include #include #include @@ -88,17 +88,7 @@ c != NULL) { std::string s(c); - std::string utf8; - try - { - utf8 = boost::locale::conv::to_utf(s, "ISO-8859-1"); // TODO Parameter? - } - catch (std::runtime_error&) - { - // Bad input string or bad encoding - utf8 = s; - } - + std::string utf8 = Toolbox::ConvertToUtf8(s, "ISO-8859-1"); // TODO Parameter? return new DicomString(utf8); } else diff -r 332fec038d52 -r 3b45473c0a73 Resources/CMake/BoostConfiguration.cmake --- a/Resources/CMake/BoostConfiguration.cmake Wed Oct 03 17:53:23 2012 +0200 +++ b/Resources/CMake/BoostConfiguration.cmake Thu Oct 04 11:22:20 2012 +0200 @@ -1,23 +1,31 @@ if (${STATIC_BUILD}) SET(BOOST_STATIC 1) else() + include(FindBoost) + + SET(BOOST_STATIC 0) + set(Boost_DEBUG 1) + #set(Boost_USE_STATIC_LIBS ON) + find_package(Boost - COMPONENTS filesystem thread system locale) + COMPONENTS filesystem thread system) + + if (NOT Boost_FOUND) + message(FATAL_ERROR "Unable to locate Boost on this system") + endif() - if (${Boost_VERSION} LESS 104800) - # boost::locale is only available from 1.48.00 - message("Too old version of Boost (${Boost_LIB_VERSION}): Building the static version") - SET(BOOST_STATIC 1) - else() - SET(BOOST_STATIC 0) + #if (${Boost_VERSION} LESS 104800) + # boost::locale is only available from 1.48.00 + #message("Too old version of Boost (${Boost_LIB_VERSION}): Building the static version") + # SET(BOOST_STATIC 1) + #endif() - add_definitions( - -DBOOST_FILESYSTEM_VERSION=3 - ) + #add_definitions( + # -DBOOST_FILESYSTEM_VERSION=1 + # ) - include_directories(${Boost_INCLUDE_DIRS}) - link_libraries(${Boost_LIBRARIES}) - endif() + include_directories(${Boost_INCLUDE_DIRS}) + link_libraries(${Boost_LIBRARIES}) endif() @@ -71,6 +79,7 @@ -DBOOST_REGEX_NO_LIB -DBOOST_SYSTEM_NO_LIB -DBOOST_LOCALE_NO_LIB + -DBOOST_HAS_LOCALE=1 ) if (${CMAKE_COMPILER_IS_GNUCXX}) @@ -82,4 +91,8 @@ ) source_group(ThirdParty\\Boost REGULAR_EXPRESSION ${BOOST_SOURCES_DIR}/.*) +else() + add_definitions( + -DBOOST_HAS_LOCALE=0 + ) endif() diff -r 332fec038d52 -r 3b45473c0a73 UnitTests/main.cpp --- a/UnitTests/main.cpp Wed Oct 03 17:53:23 2012 +0200 +++ b/UnitTests/main.cpp Thu Oct 04 11:22:20 2012 +0200 @@ -288,6 +288,38 @@ LOG(INFO) << "I say hello"; } +TEST(Toolbox, ConvertFromLatin1) +{ + // This is a Latin-1 test string + const unsigned char data[10] = { 0xe0, 0xe9, 0xea, 0xe7, 0x26, 0xc6, 0x61, 0x62, 0x63, 0x00 }; + + /*FILE* f = fopen("/tmp/tutu", "w"); + fwrite(&data[0], 9, 1, f); + fclose(f);*/ + + std::string s((char*) &data[0], 10); + ASSERT_EQ("&abc", Toolbox::ConvertToAscii(s)); + + // Open in Emacs, then save with UTF-8 encoding, then "hexdump -C" + std::string utf8 = Toolbox::ConvertToUtf8(s, "ISO-8859-1"); + ASSERT_EQ(15, utf8.size()); + ASSERT_EQ(0xc3, static_cast(utf8[0])); + ASSERT_EQ(0xa0, static_cast(utf8[1])); + ASSERT_EQ(0xc3, static_cast(utf8[2])); + ASSERT_EQ(0xa9, static_cast(utf8[3])); + ASSERT_EQ(0xc3, static_cast(utf8[4])); + ASSERT_EQ(0xaa, static_cast(utf8[5])); + ASSERT_EQ(0xc3, static_cast(utf8[6])); + ASSERT_EQ(0xa7, static_cast(utf8[7])); + ASSERT_EQ(0x26, static_cast(utf8[8])); + ASSERT_EQ(0xc3, static_cast(utf8[9])); + ASSERT_EQ(0x86, static_cast(utf8[10])); + ASSERT_EQ(0x61, static_cast(utf8[11])); + ASSERT_EQ(0x62, static_cast(utf8[12])); + ASSERT_EQ(0x63, static_cast(utf8[13])); + ASSERT_EQ(0x00, static_cast(utf8[14])); // Null-terminated string +} + int main(int argc, char **argv) {