changeset 2017:08ce34cfacad

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 13 Jun 2016 15:49:10 +0200
parents 0ae26237569a
children 300599489cab
files Core/Enumerations.h Core/Images/JpegReader.cpp Core/Images/JpegWriter.cpp Core/Images/PngReader.cpp Core/Images/PngWriter.cpp Core/Toolbox.cpp Core/Toolbox.h OrthancServer/FromDcmtkBridge.cpp UnitTestsSources/UnitTestsMain.cpp
diffstat 9 files changed, 54 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.h	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Enumerations.h	Mon Jun 13 15:49:10 2016 +0200
@@ -385,6 +385,17 @@
     RequestOrigin_Lua
   };
 
+  enum ServerBarrierEvent
+  {
+    ServerBarrierEvent_Stop,
+    ServerBarrierEvent_Reload  // SIGHUP signal: reload configuration file
+  };
+
+  enum FileMode
+  {
+    FileMode_ReadBinary,
+    FileMode_WriteBinary
+  };
 
   /**
    * The value representations Orthanc knows about. They correspond to
--- a/Core/Images/JpegReader.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Images/JpegReader.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -36,6 +36,7 @@
 #include "JpegErrorManager.h"
 #include "../OrthancException.h"
 #include "../Logging.h"
+#include "../Toolbox.h"
 
 namespace Orthanc
 {
@@ -95,7 +96,7 @@
 
   void JpegReader::ReadFromFile(const std::string& filename)
   {
-    FILE* fp = fopen(filename.c_str(), "rb");
+    FILE* fp = Toolbox::OpenFile(filename, FileMode_ReadBinary);
     if (!fp)
     {
       throw OrthancException(ErrorCode_InexistentFile);
--- a/Core/Images/JpegWriter.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Images/JpegWriter.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -118,11 +118,10 @@
                                        PixelFormat format,
                                        const void* buffer)
   {
-    // TODO This will not work on Windows system if the path contains non-ASCII characters
-    FILE* fp = fopen(filename.c_str(), "wb");
+    FILE* fp = Toolbox::OpenFile(filename, FileMode_WriteBinary);
     if (fp == NULL)
     {
-      throw OrthancException(ErrorCode_FullStorage);
+      throw OrthancException(ErrorCode_CannotWriteFile);
     }
 
     std::vector<uint8_t*> lines;
--- a/Core/Images/PngReader.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Images/PngReader.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -49,7 +49,7 @@
 
       FileRabi(const char* filename)
       {
-        fp_ = fopen(filename, "rb");
+        fp_ = Toolbox::OpenFile(filename, FileMode_ReadBinary);
         if (!fp_)
         {
           throw OrthancException(ErrorCode_InexistentFile);
@@ -59,7 +59,9 @@
       ~FileRabi()
       {
         if (fp_)
+        {
           fclose(fp_);
+        }
       }
     };
   }
--- a/Core/Images/PngWriter.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Images/PngWriter.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -211,8 +211,7 @@
   {
     Prepare(width, height, pitch, format, buffer);
 
-    // TODO This will not work on Windows system if the path contains non-ASCII characters
-    FILE* fp = fopen(filename.c_str(), "wb");
+    FILE* fp = Toolbox::OpenFile(filename, FileMode_WriteBinary);
     if (!fp)
     {
       throw OrthancException(ErrorCode_CannotWriteFile);
--- a/Core/Toolbox.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Toolbox.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -1480,5 +1480,30 @@
 
     return false;
   }
+
+
+  FILE* Toolbox::OpenFile(const std::string& path,
+                          FileMode mode)
+  {
+#if defined(_WIN32)
+    // TODO Deal with special characters by converting to the current locale
+#endif
+
+    const char* m;
+    switch (mode)
+    {
+      case FileMode_ReadBinary:
+        m = "rb";
+        break;
+
+      case FileMode_WriteBinary:
+        m = "wb";
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+
+    return fopen(path.c_str(), m);
+  }
 }
-
--- a/Core/Toolbox.h	Mon Jun 13 15:26:20 2016 +0200
+++ b/Core/Toolbox.h	Mon Jun 13 15:49:10 2016 +0200
@@ -47,12 +47,6 @@
   {
   };
 
-  enum ServerBarrierEvent
-  {
-    ServerBarrierEvent_Stop,
-    ServerBarrierEvent_Reload  // SIGHUP signal: reload configuration file
-  };
-
   namespace Toolbox
   {
     ServerBarrierEvent ServerBarrier(const bool& stopFlag);
@@ -202,5 +196,8 @@
     int GetProcessId();
 
     bool IsRegularFile(const std::string& path);
+
+    FILE* OpenFile(const std::string& path,
+                   FileMode mode);
   }
 }
--- a/OrthancServer/FromDcmtkBridge.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/OrthancServer/FromDcmtkBridge.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -115,15 +115,16 @@
   static void LoadEmbeddedDictionary(DcmDataDictionary& dictionary,
                                      EmbeddedResources::FileResourceId resource)
   {
-    Toolbox::TemporaryFile tmp;
+    std::string content;
+    EmbeddedResources::GetFileResource(content, resource);
 
-    FILE* fp = fopen(tmp.GetPath().c_str(), "wb");
-    fwrite(EmbeddedResources::GetFileResourceBuffer(resource), 
-           EmbeddedResources::GetFileResourceSize(resource), 1, fp);
-    fclose(fp);
+    Toolbox::TemporaryFile tmp;
+    tmp.Write(content);
 
     if (!dictionary.loadDictionary(tmp.GetPath().c_str()))
     {
+      LOG(ERROR) << "Cannot read embedded dictionary. Under Windows, make sure that " 
+                 << "your TEMP directory does not contain special characters.";
       throw OrthancException(ErrorCode_InternalError);
     }
   }
--- a/UnitTestsSources/UnitTestsMain.cpp	Mon Jun 13 15:26:20 2016 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Mon Jun 13 15:49:10 2016 +0200
@@ -416,10 +416,6 @@
   // 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));