diff Core/Toolbox.cpp @ 947:c2c28dd17e87 query-retrieve

integration mainline -> query-retrieve
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Jun 2014 12:09:38 +0200
parents b3f6fb1130cd
children 3fb427ac3f53 e57e08ed510f 766a57997121
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Wed Apr 16 16:34:09 2014 +0200
+++ b/Core/Toolbox.cpp	Wed Jun 25 12:09:38 2014 +0200
@@ -30,6 +30,7 @@
  **/
 
 
+#include "PrecompiledHeaders.h"
 #include "Toolbox.h"
 
 #include "OrthancException.h"
@@ -53,17 +54,17 @@
 #include <limits.h>      /* PATH_MAX */
 #endif
 
-#if defined(__linux)
+#if defined(__linux) || defined(__FreeBSD_kernel__)
 #include <limits.h>      /* PATH_MAX */
 #include <signal.h>
 #include <unistd.h>
 #endif
 
-#if BOOST_HAS_LOCALE == 1
+#if BOOST_HAS_LOCALE != 1
+#error Since version 0.7.6, Orthanc entirely relies on boost::locale
+#endif
+
 #include <boost/locale.hpp>
-#else
-#include <iconv.h>
-#endif
 
 #include "../Resources/md5/md5.h"
 #include "../Resources/base64/base64.h"
@@ -73,68 +74,11 @@
 // Patch for the missing "_strtoll" symbol when compiling with Visual Studio
 extern "C"
 {
-int64_t _strtoi64(const char *nptr, char **endptr, int base);
-int64_t strtoll(const char *nptr, char **endptr, int base)
-{
-    return _strtoi64(nptr, endptr, base);
-} 
-}
-#endif
-
-
-#if BOOST_HAS_LOCALE == 0
-namespace
-{
-  class IconvRabi
+  int64_t _strtoi64(const char *nptr, char **endptr, int base);
+  int64_t strtoll(const char *nptr, char **endptr, int base)
   {
-  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<char*>(&source[0]);
-      size_t sourceLeft = source.size();
-
-      std::vector<char> 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;
-    }
-  };
+    return _strtoi64(nptr, endptr, base);
+  } 
 }
 #endif
 
@@ -161,7 +105,7 @@
   {
 #if defined(_WIN32)
     ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
-#elif defined(__linux)
+#elif defined(__linux) || defined(__APPLE__) || defined(__FreeBSD_kernel__)
     usleep(microSeconds);
 #else
 #error Support your platform here
@@ -491,14 +435,16 @@
   }
 
 
-  std::string Toolbox::EncodeBase64(const std::string& data)
+  void Toolbox::EncodeBase64(std::string& result, 
+                             const std::string& data)
   {
-    return base64_encode(data);
+    result = base64_encode(data);
   }
 
-  std::string Toolbox::DecodeBase64(const std::string& data)
+  void Toolbox::DecodeBase64(std::string& result, 
+                             const std::string& data)
   {
-    return base64_decode(data);
+    result = base64_decode(data);
   }
 
 
@@ -512,7 +458,7 @@
     return std::string(&buffer[0]);
   }
 
-#elif defined(__linux)
+#elif defined(__linux) || defined(__FreeBSD_kernel__)
   std::string Toolbox::GetPathToExecutable()
   {
     std::vector<char> buffer(PATH_MAX + 1);
@@ -551,7 +497,6 @@
   std::string Toolbox::ConvertToUtf8(const std::string& source,
                                      const char* fromEncoding)
   {
-#if BOOST_HAS_LOCALE == 1
     try
     {
       return boost::locale::conv::to_utf<char>(source, fromEncoding);
@@ -561,17 +506,6 @@
       // 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
   }
 
 
@@ -579,7 +513,7 @@
   {
     std::string result;
 
-    result.reserve(source.size());
+    result.reserve(source.size() + 1);
     for (size_t i = 0; i < source.size(); i++)
     {
       if (source[i] < 128 && source[i] >= 0 && !iscntrl(source[i]))
@@ -803,5 +737,44 @@
 
     result.push_back(currentItem);
   }
+
+
+  void Toolbox::DecodeDataUriScheme(std::string& mime,
+                                    std::string& content,
+                                    const std::string& source)
+  {
+    boost::regex pattern("data:([^;]+);base64,([a-zA-Z0-9=+/]*)",
+                         boost::regex::icase /* case insensitive search */);
+
+    boost::cmatch what;
+    if (regex_match(source.c_str(), what, pattern))
+    {
+      mime = what[1];
+      content = what[2];
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadFileFormat);
+    }
+  }
+
+
+  void Toolbox::CreateDirectory(const std::string& path)
+  {
+    if (boost::filesystem::exists(path))
+    {
+      if (!boost::filesystem::is_directory(path))
+      {
+        throw OrthancException("Cannot create the directory over an existing file: " + path);
+      }
+    }
+    else
+    {
+      if (!boost::filesystem::create_directories(path))
+      {
+        throw OrthancException("Unable to create the directory: " + path);
+      }
+    }
+  }
 }