changeset 949:766a57997121

enumeration for encodings
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Jun 2014 15:02:37 +0200
parents b3f6fb1130cd
children 8811abd6aec9
files Core/Enumerations.h Core/Toolbox.cpp Core/Toolbox.h OrthancServer/FromDcmtkBridge.cpp UnitTestsSources/UnitTestsMain.cpp
diffstat 5 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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<char>(source, fromEncoding);
+      return boost::locale::conv::to_utf<char>(source, encoding);
     }
     catch (std::runtime_error&)
     {
--- 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);
 
--- 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
--- 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<unsigned char>(utf8[0]));
   ASSERT_EQ(0xa0, static_cast<unsigned char>(utf8[1]));