# HG changeset patch # User Sebastien Jodogne # Date 1407401495 -7200 # Node ID ce6386b37afd70543c0ddf0743be74f7f21e6d51 # Parent e5686a703c63eb3f0a0b62c59a3f0bb90d3eebee avoid unnecessary exceptions on Orthanc startup diff -r e5686a703c63 -r ce6386b37afd Core/EnumerationDictionary.h --- 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 #include #include @@ -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(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(static_cast(value))] = value; diff -r e5686a703c63 -r ce6386b37afd Core/Toolbox.cpp --- 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; + } } diff -r e5686a703c63 -r ce6386b37afd Core/Toolbox.h --- 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& arguments); + + bool IsInteger(const std::string& str); } } diff -r e5686a703c63 -r ce6386b37afd OrthancServer/ServerIndex.cpp --- 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(sleepString); + + if (Toolbox::IsInteger(sleepString)) + { + sleep = boost::lexical_cast(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 << ")"; diff -r e5686a703c63 -r ce6386b37afd OrthancServer/main.cpp --- 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); diff -r e5686a703c63 -r ce6386b37afd UnitTestsSources/UnitTestsMain.cpp --- 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.