changeset 1102:ce6386b37afd

avoid unnecessary exceptions on Orthanc startup
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 07 Aug 2014 10:51:35 +0200
parents e5686a703c63
children bec1eccf976c
files Core/EnumerationDictionary.h Core/Toolbox.cpp Core/Toolbox.h OrthancServer/ServerIndex.cpp OrthancServer/main.cpp UnitTestsSources/UnitTestsMain.cpp
diffstat 6 files changed, 73 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/Core/EnumerationDictionary.h	Thu Aug 07 10:21:43 2014 +0200
+++ b/Core/EnumerationDictionary.h	Thu Aug 07 10:51:35 2014 +0200
@@ -34,6 +34,7 @@
 
 #include "OrthancException.h"
 
+#include "Toolbox.h"
 #include <boost/lexical_cast.hpp>
 #include <string>
 #include <map>
@@ -57,22 +58,13 @@
       {
         // Check if these values are free
         if (enumerationToString_.find(value) != enumerationToString_.end() ||
-            stringToEnumeration_.find(str) != stringToEnumeration_.end())
+            stringToEnumeration_.find(str) != stringToEnumeration_.end() ||
+            Toolbox::IsInteger(str) /* Prevent the registration of a number */)
         {
           throw OrthancException(ErrorCode_BadRequest);
         }
 
-        // Prevent the registration of a number
-        try
-        {
-          boost::lexical_cast<int>(str);
-          throw OrthancException(ErrorCode_BadRequest);
-        }
-        catch (boost::bad_lexical_cast)
-        {
-          // OK, the string is not a number
-        }
-
+        // OK, the string is free and is not a number
         enumerationToString_[value] = str;
         stringToEnumeration_[str] = value;
         stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value;
--- a/Core/Toolbox.cpp	Thu Aug 07 10:21:43 2014 +0200
+++ b/Core/Toolbox.cpp	Thu Aug 07 10:51:35 2014 +0200
@@ -1067,5 +1067,39 @@
       throw OrthancException(ErrorCode_SystemCommand);
     }
   }
+
+  
+  bool Toolbox::IsInteger(const std::string& str)
+  {
+    std::string s = StripSpaces(str);
+
+    if (s.size() == 0)
+    {
+      return false;
+    }
+
+    size_t pos = 0;
+    if (s[0] == '-')
+    {
+      if (s.size() == 1)
+      {
+        return false;
+      }
+
+      pos = 1;
+    }
+
+    while (pos < s.size())
+    {
+      if (!isdigit(s[pos]))
+      {
+        return false;
+      }
+
+      pos++;
+    }
+
+    return true;
+  }
 }
 
--- a/Core/Toolbox.h	Thu Aug 07 10:21:43 2014 +0200
+++ b/Core/Toolbox.h	Thu Aug 07 10:51:35 2014 +0200
@@ -149,5 +149,7 @@
 
     void ExecuteSystemCommand(const std::string& command,
                               const std::vector<std::string>& arguments);
+
+    bool IsInteger(const std::string& str);
   }
 }
--- a/OrthancServer/ServerIndex.cpp	Thu Aug 07 10:21:43 2014 +0200
+++ b/OrthancServer/ServerIndex.cpp	Thu Aug 07 10:51:35 2014 +0200
@@ -252,18 +252,21 @@
 
   void ServerIndex::FlushThread(ServerIndex* that)
   {
-    unsigned int sleep;
+    // By default, wait for 10 seconds before flushing
+    unsigned int sleep = 10;
 
     try
     {
       boost::mutex::scoped_lock lock(that->mutex_);
       std::string sleepString = that->db_->GetGlobalProperty(GlobalProperty_FlushSleep);
-      sleep = boost::lexical_cast<unsigned int>(sleepString);
+
+      if (Toolbox::IsInteger(sleepString))
+      {
+        sleep = boost::lexical_cast<unsigned int>(sleepString);
+      }
     }
     catch (boost::bad_lexical_cast&)
     {
-      // By default, wait for 10 seconds before flushing
-      sleep = 10;
     }
 
     LOG(INFO) << "Starting the database flushing thread (sleep = " << sleep << ")";
--- a/OrthancServer/main.cpp	Thu Aug 07 10:21:43 2014 +0200
+++ b/OrthancServer/main.cpp	Thu Aug 07 10:51:35 2014 +0200
@@ -373,28 +373,22 @@
 
   google::InitGoogleLogging("Orthanc");
 
+  const char* configurationFile = NULL;
+  for (int i = 1; i < argc; i++)
+  {
+    // Use the first argument that does not start with a "-" as
+    // the configuration file
+    if (argv[i][0] != '-')
+    {
+      configurationFile = argv[i];
+    }
+  }
+
+
   int status = 0;
   try
   {
-    bool isInitialized = false;
-    if (argc >= 2)
-    {
-      for (int i = 1; i < argc; i++)
-      {
-        // Use the first argument that does not start with a "-" as
-        // the configuration file
-        if (argv[i][0] != '-')
-        {
-          OrthancInitialize(argv[i]);
-          isInitialized = true;
-        }
-      }
-    }
-
-    if (!isInitialized)
-    {
-      OrthancInitialize();
-    }
+    OrthancInitialize(configurationFile);
 
     std::string storageDirectoryStr = Configuration::GetGlobalStringParameter("StorageDirectory", "OrthancStorage");
     boost::filesystem::path storageDirectory = Configuration::InterpretStringParameterAsPath(storageDirectoryStr);
--- a/UnitTestsSources/UnitTestsMain.cpp	Thu Aug 07 10:21:43 2014 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Thu Aug 07 10:51:35 2014 +0200
@@ -729,6 +729,19 @@
 #endif
 
 
+TEST(Toolbox, IsInteger)
+{
+  ASSERT_TRUE(Toolbox::IsInteger("00236"));
+  ASSERT_TRUE(Toolbox::IsInteger("-0042"));
+  ASSERT_TRUE(Toolbox::IsInteger("0"));
+  ASSERT_TRUE(Toolbox::IsInteger("-0"));
+
+  ASSERT_FALSE(Toolbox::IsInteger(""));
+  ASSERT_FALSE(Toolbox::IsInteger("42a"));
+  ASSERT_FALSE(Toolbox::IsInteger("42-"));
+}
+
+
 int main(int argc, char **argv)
 {
   // Initialize Google's logging library.