# HG changeset patch # User Sebastien Jodogne # Date 1465825750 -7200 # Node ID 08ce34cfacad6bd56113f83c7fa587d1961545f9 # Parent 0ae26237569a369b944b0b8fcbd39b433a5d47c5 refactoring diff -r 0ae26237569a -r 08ce34cfacad Core/Enumerations.h --- 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 diff -r 0ae26237569a -r 08ce34cfacad Core/Images/JpegReader.cpp --- 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); diff -r 0ae26237569a -r 08ce34cfacad Core/Images/JpegWriter.cpp --- 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 lines; diff -r 0ae26237569a -r 08ce34cfacad Core/Images/PngReader.cpp --- 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_); + } } }; } diff -r 0ae26237569a -r 08ce34cfacad Core/Images/PngWriter.cpp --- 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); diff -r 0ae26237569a -r 08ce34cfacad Core/Toolbox.cpp --- 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); + } } - diff -r 0ae26237569a -r 08ce34cfacad Core/Toolbox.h --- 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); } } diff -r 0ae26237569a -r 08ce34cfacad OrthancServer/FromDcmtkBridge.cpp --- 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); } } diff -r 0ae26237569a -r 08ce34cfacad UnitTestsSources/UnitTestsMain.cpp --- 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));