diff --git a/Util/BoostFileSystemFix.h b/Util/BoostFileSystemFix.h index e70833390..998eb75ad 100644 --- a/Util/BoostFileSystemFix.h +++ b/Util/BoostFileSystemFix.h @@ -29,6 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define BOOST_FILE_SYSTEM_FIX_H #include "OSRMException.h" +#include "SimpleLogger.h" #include #include @@ -135,4 +136,17 @@ inline path unique_path(const path &) { return temp_directory_path(); } #define BOOST_FILESYSTEM_VERSION 3 #endif +inline void AssertPathExists(const boost::filesystem::path &path) +{ + if (!boost::filesystem::is_regular_file(path)) + { + SimpleLogger().Write(logDEBUG) << path << " check failed"; + throw OSRMException(path.string() + " not found."); + } + else + { + SimpleLogger().Write(logDEBUG) << path << " exists"; + } +} + #endif /* BOOST_FILE_SYSTEM_FIX_H */ diff --git a/Util/DataStoreOptions.h b/Util/DataStoreOptions.h index bf13dbec1..0a45d105b 100644 --- a/Util/DataStoreOptions.h +++ b/Util/DataStoreOptions.h @@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "BoostFileSystemFix.h" #include "GitDescription.h" +#include "IniFileUtil.h" #include "OSRMException.h" #include "SimpleLogger.h" @@ -37,108 +38,54 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include #include -#include #include -void AssertPathExists(const boost::filesystem::path& path) -{ - if (!boost::filesystem::is_regular_file(path)) - { - SimpleLogger().Write(logDEBUG) << path << " check failed"; - throw OSRMException(path.string() + " not found."); - } else { - SimpleLogger().Write(logDEBUG) << path << " exists"; - } -} - -// support old capitalized option names by down-casing them with a regex replace -inline void PrepareConfigFile( - const boost::filesystem::path & path, - std::string & output -) { - boost::filesystem::fstream config_stream(path); - std::string input_str( - (std::istreambuf_iterator(config_stream)), - std::istreambuf_iterator() - ); - boost::regex regex("^([^=]*)"); //match from start of line to '=' - std::string format("\\L$1\\E"); //replace with downcased substring - output = boost::regex_replace( input_str, regex, format ); -} - // generate boost::program_options object for the routing part -inline bool GenerateDataStoreOptions(const int argc, const char * argv[], ServerPaths & paths) +inline bool GenerateDataStoreOptions(const int argc, const char *argv[], ServerPaths &paths) { // declare a group of options that will be allowed only on command line boost::program_options::options_description generic_options("Options"); - generic_options.add_options() - ("version,v", "Show version") - ("help,h", "Show this help message") - ( - "config,c", - boost::program_options::value( - &paths["config"] - )->default_value("server.ini"), - "Path to a configuration file" - ); + generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")( + "config,c", + boost::program_options::value(&paths["config"]) + ->default_value("server.ini"), + "Path to a configuration file"); // declare a group of options that will be allowed both on command line // as well as in a config file boost::program_options::options_description config_options("Configuration"); - config_options.add_options() - ( - "hsgrdata", - boost::program_options::value(&paths["hsgrdata"]), - ".hsgr file" - ) - ( - "nodesdata", - boost::program_options::value(&paths["nodesdata"]), - ".nodes file" - ) - ( - "edgesdata", - boost::program_options::value(&paths["edgesdata"]), - ".edges file" - ) - ( - "geometry", - boost::program_options::value(&paths["geometry"]), - ".geometry file" - ) - ( - "ramindex", - boost::program_options::value(&paths["ramindex"]), - ".ramIndex file" - ) - ( - "fileindex", - boost::program_options::value(&paths["fileindex"]), - ".fileIndex file" - ) - ( - "namesdata", - boost::program_options::value(&paths["namesdata"]), - ".names file" - ) - ( - "timestamp", - boost::program_options::value(&paths["timestamp"]), - ".timestamp file" - ); + config_options.add_options()( + "hsgrdata", + boost::program_options::value(&paths["hsgrdata"]), + ".hsgr file")("nodesdata", + boost::program_options::value(&paths["nodesdata"]), + ".nodes file")( + "edgesdata", + boost::program_options::value(&paths["edgesdata"]), + ".edges file")("geometry", + boost::program_options::value(&paths["geometry"]), + ".geometry file")( + "ramindex", + boost::program_options::value(&paths["ramindex"]), + ".ramIndex file")( + "fileindex", + boost::program_options::value(&paths["fileindex"]), + ".fileIndex file")( + "namesdata", + boost::program_options::value(&paths["namesdata"]), + ".names file")("timestamp", + boost::program_options::value(&paths["timestamp"]), + ".timestamp file"); // hidden options, will be allowed both on command line and in config // file, but will not be shown to the user boost::program_options::options_description hidden_options("Hidden options"); - hidden_options.add_options() - ( - "base,b", - boost::program_options::value(&paths["base"]), - "base path to .osrm file" - ); + hidden_options.add_options()( + "base,b", + boost::program_options::value(&paths["base"]), + "base path to .osrm file"); // positional option boost::program_options::positional_options_description positional_options; @@ -152,25 +99,24 @@ inline bool GenerateDataStoreOptions(const int argc, const char * argv[], Server config_file_options.add(config_options).add(hidden_options); boost::program_options::options_description visible_options( - boost::filesystem::basename(argv[0]) + " [] " - ); + boost::filesystem::basename(argv[0]) + " [] "); visible_options.add(generic_options).add(config_options); // parse command line options boost::program_options::variables_map option_variables; - boost::program_options::store - ( - boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(positional_options).run(), - option_variables - ); + boost::program_options::store(boost::program_options::command_line_parser(argc, argv) + .options(cmdline_options) + .positional(positional_options) + .run(), + option_variables); - if(option_variables.count("version")) + if (option_variables.count("version")) { SimpleLogger().Write() << g_GIT_DESCRIPTION; return false; } - if(option_variables.count("help")) + if (option_variables.count("help")) { SimpleLogger().Write() << visible_options; return false; @@ -178,22 +124,26 @@ inline bool GenerateDataStoreOptions(const int argc, const char * argv[], Server boost::program_options::notify(option_variables); - const bool parameter_present = - (paths.find("hsgrdata") != paths.end() && !paths.find("hsgrdata" )->second.string().empty()) || - (paths.find("nodesdata") != paths.end() && !paths.find("nodesdata")->second.string().empty()) || - (paths.find("edgesdata") != paths.end() && !paths.find("edgesdata")->second.string().empty()) || - (paths.find("geometry") != paths.end() && !paths.find("geometry" )->second.string().empty()) || - (paths.find("ramindex") != paths.end() && !paths.find("ramindex" )->second.string().empty()) || - (paths.find("fileindex") != paths.end() && !paths.find("fileindex")->second.string().empty()) || - (paths.find("timestamp") != paths.end() && !paths.find("timestamp")->second.string().empty()); + const bool parameter_present = (paths.find("hsgrdata") != paths.end() && + !paths.find("hsgrdata")->second.string().empty()) || + (paths.find("nodesdata") != paths.end() && + !paths.find("nodesdata")->second.string().empty()) || + (paths.find("edgesdata") != paths.end() && + !paths.find("edgesdata")->second.string().empty()) || + (paths.find("geometry") != paths.end() && + !paths.find("geometry")->second.string().empty()) || + (paths.find("ramindex") != paths.end() && + !paths.find("ramindex")->second.string().empty()) || + (paths.find("fileindex") != paths.end() && + !paths.find("fileindex")->second.string().empty()) || + (paths.find("timestamp") != paths.end() && + !paths.find("timestamp")->second.string().empty()); if (parameter_present) { - if ( - ( paths.find("config") != paths.end() && - boost::filesystem::is_regular_file(paths.find("config")->second) - ) || option_variables.count("base") - ) + if ((paths.find("config") != paths.end() && + boost::filesystem::is_regular_file(paths.find("config")->second)) || + option_variables.count("base")) { SimpleLogger().Write(logWARNING) << "conflicting parameters"; SimpleLogger().Write() << visible_options; @@ -203,26 +153,20 @@ inline bool GenerateDataStoreOptions(const int argc, const char * argv[], Server // parse config file ServerPaths::iterator path_iterator = paths.find("config"); - if ( - path_iterator != paths.end() && - boost::filesystem::is_regular_file(path_iterator->second) && - !option_variables.count("base") - ) + if (path_iterator != paths.end() && boost::filesystem::is_regular_file(path_iterator->second) && + !option_variables.count("base")) { SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string(); - std::string config_str; - PrepareConfigFile( path_iterator->second, config_str ); - std::stringstream config_stream( config_str ); - boost::program_options::store( - parse_config_file(config_stream, config_file_options), - option_variables - ); + std::string ini_file_contents = ReadIniFileAndLowerContents(path_iterator->second); + std::stringstream config_stream(ini_file_contents); + boost::program_options::store(parse_config_file(config_stream, config_file_options), + option_variables); boost::program_options::notify(option_variables); } else if (option_variables.count("base")) { path_iterator = paths.find("base"); - BOOST_ASSERT( paths.end() != path_iterator ); + BOOST_ASSERT(paths.end() != path_iterator); std::string base_string = path_iterator->second.string(); path_iterator = paths.find("hsgrdata"); diff --git a/Util/IniFileUtil.h b/Util/IniFileUtil.h new file mode 100644 index 000000000..479e0bdd4 --- /dev/null +++ b/Util/IniFileUtil.h @@ -0,0 +1,48 @@ +/* + +Copyright (c) 2013, Project OSRM, Dennis Luxen, others +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list +of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef INI_FILE_UTIL_H +#define INI_FILE_UTIL_H + +#include +#include + +#include +#include + +// support old capitalized option names by down-casing them with a regex replace +inline std::string ReadIniFileAndLowerContents(const boost::filesystem::path &path) +{ + boost::filesystem::fstream config_stream(path); + std::string input_str((std::istreambuf_iterator(config_stream)), + std::istreambuf_iterator()); + std::regex regex("^([^=]*)"); // match from start of line to '=' + std::string format("\\L$1\\E"); // replace with downcased substring + return std::regex_replace(input_str, regex, format); +} + +#endif // INI_FILE_UTIL_H diff --git a/Util/ProgramOptions.h b/Util/ProgramOptions.h index 47cd0e9bf..9fdd4325f 100644 --- a/Util/ProgramOptions.h +++ b/Util/ProgramOptions.h @@ -29,34 +29,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define PROGAM_OPTIONS_H #include "GitDescription.h" +#include "IniFileUtil.h" #include "OSRMException.h" #include "SimpleLogger.h" #include #include -#include #include #include -#include #include const static unsigned INIT_OK_START_ENGINE = 0; const static unsigned INIT_OK_DO_NOT_START_ENGINE = 1; const static unsigned INIT_FAILED = -1; -// support old capitalized option names by down-casing them with a regex replace -inline void PrepareConfigFile(const boost::filesystem::path &path, std::string &output) -{ - std::ifstream config_stream(path.string().c_str()); - std::string input_str((std::istreambuf_iterator(config_stream)), - std::istreambuf_iterator()); - std::regex regex("^([^=]*)"); // match from start of line to '=' - std::string format("\\L$1\\E"); // replace with downcased substring - output = std::regex_replace(input_str, regex, format); -} - // generate boost::program_options object for the routing part inline unsigned GenerateServerProgramOptions(const int argc, const char *argv[], @@ -166,9 +154,8 @@ inline unsigned GenerateServerProgramOptions(const int argc, !option_variables.count("base")) { SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string(); - std::string config_str; - PrepareConfigFile(path_iterator->second, config_str); - std::stringstream config_stream(config_str); + std::string ini_file_contents = ReadIniFileAndLowerContents(path_iterator->second); + std::stringstream config_stream(ini_file_contents); boost::program_options::store(parse_config_file(config_stream, config_file_options), option_variables); boost::program_options::notify(option_variables); diff --git a/extractor.cpp b/extractor.cpp index 1d867a1e0..b5fadb538 100644 --- a/extractor.cpp +++ b/extractor.cpp @@ -42,7 +42,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include - #include #include #include @@ -131,9 +130,8 @@ int main(int argc, char *argv[]) if (boost::filesystem::is_regular_file(config_file_path)) { SimpleLogger().Write() << "Reading options from: " << config_file_path.string(); - std::string config_str; - PrepareConfigFile(config_file_path.c_str(), config_str); - std::stringstream config_stream(config_str); + std::string ini_file_contents = ReadIniFileAndLowerContents(config_file_path); + std::stringstream config_stream(ini_file_contents); boost::program_options::store(parse_config_file(config_stream, config_file_options), option_variables); boost::program_options::notify(option_variables);