# HG changeset patch # User Sebastien Jodogne # Date 1403701357 -7200 # Node ID 766a57997121e1c5be1545fe3e33c4ea7fd62386 # Parent b3f6fb1130cd60680e954fe055ce74214b18bf57 enumeration for encodings diff -r b3f6fb1130cd -r 766a57997121 Core/Enumerations.h --- a/Core/Enumerations.h Wed Jun 25 11:36:41 2014 +0200 +++ b/Core/Enumerations.h Wed Jun 25 15:02:37 2014 +0200 @@ -228,6 +228,13 @@ }; + enum Encoding + { + Encoding_Utf8, + Encoding_Latin1 + }; + + /** * WARNING: Do not change the explicit values in the enumerations * below this point. This would result in incompatible databases diff -r b3f6fb1130cd -r 766a57997121 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Wed Jun 25 11:36:41 2014 +0200 +++ b/Core/Toolbox.cpp Wed Jun 25 15:02:37 2014 +0200 @@ -495,11 +495,27 @@ std::string Toolbox::ConvertToUtf8(const std::string& source, - const char* fromEncoding) + const Encoding sourceEncoding) { + const char* encoding; + + switch (sourceEncoding) + { + case Encoding_Utf8: + // Already in UTF-8: No conversion is required + return source; + + case Encoding_Latin1: + encoding = "ISO-8859-1"; + break; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + try { - return boost::locale::conv::to_utf(source, fromEncoding); + return boost::locale::conv::to_utf(source, encoding); } catch (std::runtime_error&) { diff -r b3f6fb1130cd -r 766a57997121 Core/Toolbox.h --- a/Core/Toolbox.h Wed Jun 25 11:36:41 2014 +0200 +++ b/Core/Toolbox.h Wed Jun 25 15:02:37 2014 +0200 @@ -106,7 +106,7 @@ std::string GetDirectoryOfExecutable(); std::string ConvertToUtf8(const std::string& source, - const char* fromEncoding); + const Encoding sourceEncoding); std::string ConvertToAscii(const std::string& source); diff -r b3f6fb1130cd -r 766a57997121 OrthancServer/FromDcmtkBridge.cpp --- a/OrthancServer/FromDcmtkBridge.cpp Wed Jun 25 11:36:41 2014 +0200 +++ b/OrthancServer/FromDcmtkBridge.cpp Wed Jun 25 15:02:37 2014 +0200 @@ -151,7 +151,7 @@ c != NULL) { std::string s(c); - std::string utf8 = Toolbox::ConvertToUtf8(s, "ISO-8859-1"); // TODO Parameter? + std::string utf8 = Toolbox::ConvertToUtf8(s, Encoding_Latin1); // TODO Parameter? return new DicomString(utf8); } else diff -r b3f6fb1130cd -r 766a57997121 UnitTestsSources/UnitTestsMain.cpp --- a/UnitTestsSources/UnitTestsMain.cpp Wed Jun 25 11:36:41 2014 +0200 +++ b/UnitTestsSources/UnitTestsMain.cpp Wed Jun 25 15:02:37 2014 +0200 @@ -414,7 +414,7 @@ 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"); + std::string utf8 = Toolbox::ConvertToUtf8(s, Encoding_Latin1); ASSERT_EQ(15u, utf8.size()); ASSERT_EQ(0xc3, static_cast(utf8[0])); ASSERT_EQ(0xa0, static_cast(utf8[1]));