move common code into its own header

This commit is contained in:
Dennis Luxen 2014-05-13 13:30:52 +02:00
parent b2adb22b2d
commit 9b68821f05
5 changed files with 132 additions and 141 deletions

View File

@ -29,6 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BOOST_FILE_SYSTEM_FIX_H #define BOOST_FILE_SYSTEM_FIX_H
#include "OSRMException.h" #include "OSRMException.h"
#include "SimpleLogger.h"
#include <boost/any.hpp> #include <boost/any.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@ -135,4 +136,17 @@ inline path unique_path(const path &) { return temp_directory_path(); }
#define BOOST_FILESYSTEM_VERSION 3 #define BOOST_FILESYSTEM_VERSION 3
#endif #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 */ #endif /* BOOST_FILE_SYSTEM_FIX_H */

View File

@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "BoostFileSystemFix.h" #include "BoostFileSystemFix.h"
#include "GitDescription.h" #include "GitDescription.h"
#include "IniFileUtil.h"
#include "OSRMException.h" #include "OSRMException.h"
#include "SimpleLogger.h" #include "SimpleLogger.h"
@ -37,108 +38,54 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <boost/any.hpp> #include <boost/any.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/regex.hpp>
#include <string> #include <string>
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<char>(config_stream)),
std::istreambuf_iterator<char>()
);
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 // 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 // declare a group of options that will be allowed only on command line
boost::program_options::options_description generic_options("Options"); boost::program_options::options_description generic_options("Options");
generic_options.add_options() generic_options.add_options()("version,v", "Show version")("help,h", "Show this help message")(
("version,v", "Show version") "config,c",
("help,h", "Show this help message") boost::program_options::value<boost::filesystem::path>(&paths["config"])
( ->default_value("server.ini"),
"config,c", "Path to a configuration file");
boost::program_options::value<boost::filesystem::path>(
&paths["config"]
)->default_value("server.ini"),
"Path to a configuration file"
);
// declare a group of options that will be allowed both on command line // declare a group of options that will be allowed both on command line
// as well as in a config file // as well as in a config file
boost::program_options::options_description config_options("Configuration"); boost::program_options::options_description config_options("Configuration");
config_options.add_options() config_options.add_options()(
( "hsgrdata",
"hsgrdata", boost::program_options::value<boost::filesystem::path>(&paths["hsgrdata"]),
boost::program_options::value<boost::filesystem::path>(&paths["hsgrdata"]), ".hsgr file")("nodesdata",
".hsgr file" boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]),
) ".nodes file")(
( "edgesdata",
"nodesdata", boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]),
boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]), ".edges file")("geometry",
".nodes file" boost::program_options::value<boost::filesystem::path>(&paths["geometry"]),
) ".geometry file")(
( "ramindex",
"edgesdata", boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]), ".ramIndex file")(
".edges file" "fileindex",
) boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
( ".fileIndex file")(
"geometry", "namesdata",
boost::program_options::value<boost::filesystem::path>(&paths["geometry"]), boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
".geometry file" ".names file")("timestamp",
) boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]),
( ".timestamp file");
"ramindex",
boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
".ramIndex file"
)
(
"fileindex",
boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
".fileIndex file"
)
(
"namesdata",
boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
".names file"
)
(
"timestamp",
boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]),
".timestamp file"
);
// hidden options, will be allowed both on command line and in config // hidden options, will be allowed both on command line and in config
// file, but will not be shown to the user // file, but will not be shown to the user
boost::program_options::options_description hidden_options("Hidden options"); boost::program_options::options_description hidden_options("Hidden options");
hidden_options.add_options() hidden_options.add_options()(
( "base,b",
"base,b", boost::program_options::value<boost::filesystem::path>(&paths["base"]),
boost::program_options::value<boost::filesystem::path>(&paths["base"]), "base path to .osrm file");
"base path to .osrm file"
);
// positional option // positional option
boost::program_options::positional_options_description positional_options; 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); config_file_options.add(config_options).add(hidden_options);
boost::program_options::options_description visible_options( boost::program_options::options_description visible_options(
boost::filesystem::basename(argv[0]) + " [<options>] <configuration>" boost::filesystem::basename(argv[0]) + " [<options>] <configuration>");
);
visible_options.add(generic_options).add(config_options); visible_options.add(generic_options).add(config_options);
// parse command line options // parse command line options
boost::program_options::variables_map option_variables; boost::program_options::variables_map option_variables;
boost::program_options::store boost::program_options::store(boost::program_options::command_line_parser(argc, argv)
( .options(cmdline_options)
boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(positional_options).run(), .positional(positional_options)
option_variables .run(),
); option_variables);
if(option_variables.count("version")) if (option_variables.count("version"))
{ {
SimpleLogger().Write() << g_GIT_DESCRIPTION; SimpleLogger().Write() << g_GIT_DESCRIPTION;
return false; return false;
} }
if(option_variables.count("help")) if (option_variables.count("help"))
{ {
SimpleLogger().Write() << visible_options; SimpleLogger().Write() << visible_options;
return false; return false;
@ -178,22 +124,26 @@ inline bool GenerateDataStoreOptions(const int argc, const char * argv[], Server
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);
const bool parameter_present = const bool parameter_present = (paths.find("hsgrdata") != paths.end() &&
(paths.find("hsgrdata") != paths.end() && !paths.find("hsgrdata" )->second.string().empty()) || !paths.find("hsgrdata")->second.string().empty()) ||
(paths.find("nodesdata") != paths.end() && !paths.find("nodesdata")->second.string().empty()) || (paths.find("nodesdata") != paths.end() &&
(paths.find("edgesdata") != paths.end() && !paths.find("edgesdata")->second.string().empty()) || !paths.find("nodesdata")->second.string().empty()) ||
(paths.find("geometry") != paths.end() && !paths.find("geometry" )->second.string().empty()) || (paths.find("edgesdata") != paths.end() &&
(paths.find("ramindex") != paths.end() && !paths.find("ramindex" )->second.string().empty()) || !paths.find("edgesdata")->second.string().empty()) ||
(paths.find("fileindex") != paths.end() && !paths.find("fileindex")->second.string().empty()) || (paths.find("geometry") != paths.end() &&
(paths.find("timestamp") != paths.end() && !paths.find("timestamp")->second.string().empty()); !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 (parameter_present)
{ {
if ( if ((paths.find("config") != paths.end() &&
( paths.find("config") != paths.end() && boost::filesystem::is_regular_file(paths.find("config")->second)) ||
boost::filesystem::is_regular_file(paths.find("config")->second) option_variables.count("base"))
) || option_variables.count("base")
)
{ {
SimpleLogger().Write(logWARNING) << "conflicting parameters"; SimpleLogger().Write(logWARNING) << "conflicting parameters";
SimpleLogger().Write() << visible_options; SimpleLogger().Write() << visible_options;
@ -203,26 +153,20 @@ inline bool GenerateDataStoreOptions(const int argc, const char * argv[], Server
// parse config file // parse config file
ServerPaths::iterator path_iterator = paths.find("config"); ServerPaths::iterator path_iterator = paths.find("config");
if ( if (path_iterator != paths.end() && boost::filesystem::is_regular_file(path_iterator->second) &&
path_iterator != paths.end() && !option_variables.count("base"))
boost::filesystem::is_regular_file(path_iterator->second) &&
!option_variables.count("base")
)
{ {
SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string(); SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string();
std::string config_str; std::string ini_file_contents = ReadIniFileAndLowerContents(path_iterator->second);
PrepareConfigFile( path_iterator->second, config_str ); std::stringstream config_stream(ini_file_contents);
std::stringstream config_stream( config_str ); boost::program_options::store(parse_config_file(config_stream, config_file_options),
boost::program_options::store( option_variables);
parse_config_file(config_stream, config_file_options),
option_variables
);
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);
} }
else if (option_variables.count("base")) else if (option_variables.count("base"))
{ {
path_iterator = paths.find("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(); std::string base_string = path_iterator->second.string();
path_iterator = paths.find("hsgrdata"); path_iterator = paths.find("hsgrdata");

48
Util/IniFileUtil.h Normal file
View File

@ -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 <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <regex>
#include <string>
// 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<char>(config_stream)),
std::istreambuf_iterator<char>());
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

View File

@ -29,34 +29,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define PROGAM_OPTIONS_H #define PROGAM_OPTIONS_H
#include "GitDescription.h" #include "GitDescription.h"
#include "IniFileUtil.h"
#include "OSRMException.h" #include "OSRMException.h"
#include "SimpleLogger.h" #include "SimpleLogger.h"
#include <osrm/ServerPaths.h> #include <osrm/ServerPaths.h>
#include <boost/any.hpp> #include <boost/any.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <fstream> #include <fstream>
#include <regex>
#include <string> #include <string>
const static unsigned INIT_OK_START_ENGINE = 0; const static unsigned INIT_OK_START_ENGINE = 0;
const static unsigned INIT_OK_DO_NOT_START_ENGINE = 1; const static unsigned INIT_OK_DO_NOT_START_ENGINE = 1;
const static unsigned INIT_FAILED = -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<char>(config_stream)),
std::istreambuf_iterator<char>());
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 // generate boost::program_options object for the routing part
inline unsigned GenerateServerProgramOptions(const int argc, inline unsigned GenerateServerProgramOptions(const int argc,
const char *argv[], const char *argv[],
@ -166,9 +154,8 @@ inline unsigned GenerateServerProgramOptions(const int argc,
!option_variables.count("base")) !option_variables.count("base"))
{ {
SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string(); SimpleLogger().Write() << "Reading options from: " << path_iterator->second.string();
std::string config_str; std::string ini_file_contents = ReadIniFileAndLowerContents(path_iterator->second);
PrepareConfigFile(path_iterator->second, config_str); std::stringstream config_stream(ini_file_contents);
std::stringstream config_stream(config_str);
boost::program_options::store(parse_config_file(config_stream, config_file_options), boost::program_options::store(parse_config_file(config_stream, config_file_options),
option_variables); option_variables);
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);

View File

@ -42,7 +42,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib> #include <cstdlib>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -131,9 +130,8 @@ int main(int argc, char *argv[])
if (boost::filesystem::is_regular_file(config_file_path)) if (boost::filesystem::is_regular_file(config_file_path))
{ {
SimpleLogger().Write() << "Reading options from: " << config_file_path.string(); SimpleLogger().Write() << "Reading options from: " << config_file_path.string();
std::string config_str; std::string ini_file_contents = ReadIniFileAndLowerContents(config_file_path);
PrepareConfigFile(config_file_path.c_str(), config_str); std::stringstream config_stream(ini_file_contents);
std::stringstream config_stream(config_str);
boost::program_options::store(parse_config_file(config_stream, config_file_options), boost::program_options::store(parse_config_file(config_stream, config_file_options),
option_variables); option_variables);
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);