migrate ProgramOptions to C++11

This commit is contained in:
Dennis Luxen 2014-05-07 12:05:51 +02:00
parent c5a3937c80
commit c09e897dab

View File

@ -37,10 +37,9 @@ 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/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/regex.hpp>
#include <boost/unordered_map.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;
@ -48,118 +47,81 @@ 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 // support old capitalized option names by down-casing them with a regex replace
inline void PrepareConfigFile( inline void PrepareConfigFile(const boost::filesystem::path &path, std::string &output)
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::ifstream config_stream( path.string().c_str() ); std::istreambuf_iterator<char>());
std::string input_str( std::regex regex("^([^=]*)"); // match from start of line to '='
(std::istreambuf_iterator<char>(config_stream)), std::string format("\\L$1\\E"); // replace with downcased substring
std::istreambuf_iterator<char>() output = std::regex_replace(input_str, regex, format);
);
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 unsigned GenerateServerProgramOptions( inline unsigned GenerateServerProgramOptions(const int argc,
const int argc, const char *argv[],
const char * argv[], ServerPaths &paths,
ServerPaths & paths, std::string &ip_address,
std::string & ip_address, int &ip_port,
int & ip_port, int &requested_num_threads,
int & requested_num_threads, bool &use_shared_memory,
bool & use_shared_memory, bool &trial)
bool & trial {
) {
// 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")
("help,h", "Show this help message")
(
"config,c", "config,c",
boost::program_options::value<boost::filesystem::path>( boost::program_options::value<boost::filesystem::path>(&paths["config"])
&paths["config"] ->default_value("server.ini"),
)->default_value("server.ini"), "Path to a configuration file")(
"Path to a configuration file"
)
(
"trial", "trial",
boost::program_options::value<bool>(&trial)->implicit_value(true), boost::program_options::value<bool>(&trial)->implicit_value(true),
"Quit after initialization" "Quit after initialization");
);
// 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" ".hsgr file")("nodesdata",
)
(
"nodesdata",
boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]), boost::program_options::value<boost::filesystem::path>(&paths["nodesdata"]),
".nodes file" ".nodes file")(
)
(
"edgesdata", "edgesdata",
boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]), boost::program_options::value<boost::filesystem::path>(&paths["edgesdata"]),
".edges file") ".edges file")("geometry",
(
"geometry",
boost::program_options::value<boost::filesystem::path>(&paths["geometries"]), boost::program_options::value<boost::filesystem::path>(&paths["geometries"]),
".geometry file") ".geometry file")(
(
"ramindex", "ramindex",
boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]), boost::program_options::value<boost::filesystem::path>(&paths["ramindex"]),
".ramIndex file") ".ramIndex file")(
(
"fileindex", "fileindex",
boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]), boost::program_options::value<boost::filesystem::path>(&paths["fileindex"]),
"File index file") "File index file")(
(
"namesdata", "namesdata",
boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]), boost::program_options::value<boost::filesystem::path>(&paths["namesdata"]),
".names file") ".names file")("timestamp",
(
"timestamp",
boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]), boost::program_options::value<boost::filesystem::path>(&paths["timestamp"]),
".timestamp file") ".timestamp file")(
(
"ip,i", "ip,i",
boost::program_options::value<std::string>(&ip_address)->default_value("0.0.0.0"), boost::program_options::value<std::string>(&ip_address)->default_value("0.0.0.0"),
"IP address" "IP address")(
) "port,p", boost::program_options::value<int>(&ip_port)->default_value(5000), "TCP/IP port")(
(
"port,p",
boost::program_options::value<int>(&ip_port)->default_value(5000),
"TCP/IP port"
)
(
"threads,t", "threads,t",
boost::program_options::value<int>(&requested_num_threads)->default_value(8), boost::program_options::value<int>(&requested_num_threads)->default_value(8),
"Number of threads to use" "Number of threads to use")(
)
(
"sharedmemory,s", "sharedmemory,s",
boost::program_options::value<bool>(&use_shared_memory)->implicit_value(true), boost::program_options::value<bool>(&use_shared_memory)->implicit_value(true),
"Load data from shared memory" "Load data from shared memory");
);
// 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;
@ -173,23 +135,25 @@ inline unsigned GenerateServerProgramOptions(
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]) + " <base.osrm> [<options>]" boost::filesystem::basename(argv[0]) + " <base.osrm> [<options>]");
);
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)
boost::program_options::command_line_parser(argc, argv).options(cmdline_options).positional(positional_options).run(), .options(cmdline_options)
option_variables .positional(positional_options)
); .run(),
option_variables);
if(option_variables.count("version")) { if (option_variables.count("version"))
{
SimpleLogger().Write() << g_GIT_DESCRIPTION; SimpleLogger().Write() << g_GIT_DESCRIPTION;
return INIT_OK_DO_NOT_START_ENGINE; return INIT_OK_DO_NOT_START_ENGINE;
} }
if(option_variables.count("help")) { if (option_variables.count("help"))
{
SimpleLogger().Write() << visible_options; SimpleLogger().Write() << visible_options;
return INIT_OK_DO_NOT_START_ENGINE; return INIT_OK_DO_NOT_START_ENGINE;
} }
@ -198,112 +162,110 @@ inline unsigned GenerateServerProgramOptions(
// 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 config_str;
PrepareConfigFile( path_iterator->second, config_str ); PrepareConfigFile(path_iterator->second, config_str);
std::stringstream config_stream( config_str ); std::stringstream config_stream(config_str);
boost::program_options::store( boost::program_options::store(parse_config_file(config_stream, config_file_options),
parse_config_file(config_stream, config_file_options), option_variables);
option_variables
);
boost::program_options::notify(option_variables); boost::program_options::notify(option_variables);
} }
if( 1 > requested_num_threads ) { if (1 > requested_num_threads)
{
throw OSRMException("Number of threads must be a positive number"); throw OSRMException("Number of threads must be a positive number");
} }
if( !use_shared_memory && option_variables.count("base") ) { if (!use_shared_memory && 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");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".hsgr"; path_iterator->second = base_string + ".hsgr";
} else { }
else
{
throw OSRMException(base_string + ".hsgr not found"); throw OSRMException(base_string + ".hsgr not found");
} }
path_iterator = paths.find("nodesdata"); path_iterator = paths.find("nodesdata");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".nodes"; path_iterator->second = base_string + ".nodes";
} else { }
else
{
throw OSRMException(base_string + ".nodes not found"); throw OSRMException(base_string + ".nodes not found");
} }
path_iterator = paths.find("edgesdata"); path_iterator = paths.find("edgesdata");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".edges"; path_iterator->second = base_string + ".edges";
} else { }
else
{
throw OSRMException(base_string + ".edges not found"); throw OSRMException(base_string + ".edges not found");
} }
path_iterator = paths.find("geometries"); path_iterator = paths.find("geometries");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".geometry"; path_iterator->second = base_string + ".geometry";
} else { }
else
{
throw OSRMException(base_string + ".geometry not found"); throw OSRMException(base_string + ".geometry not found");
} }
path_iterator = paths.find("ramindex"); path_iterator = paths.find("ramindex");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".ramIndex"; path_iterator->second = base_string + ".ramIndex";
} else { }
else
{
throw OSRMException(base_string + ".ramIndex not found"); throw OSRMException(base_string + ".ramIndex not found");
} }
path_iterator = paths.find("fileindex"); path_iterator = paths.find("fileindex");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".fileIndex"; path_iterator->second = base_string + ".fileIndex";
} else { }
else
{
throw OSRMException(base_string + ".fileIndex not found"); throw OSRMException(base_string + ".fileIndex not found");
} }
path_iterator = paths.find("namesdata"); path_iterator = paths.find("namesdata");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".names"; path_iterator->second = base_string + ".names";
} else { }
else
{
throw OSRMException(base_string + ".namesIndex not found"); throw OSRMException(base_string + ".namesIndex not found");
} }
path_iterator = paths.find("timestamp"); path_iterator = paths.find("timestamp");
if( if (path_iterator != paths.end() &&
path_iterator != paths.end() && !boost::filesystem::is_regular_file(path_iterator->second))
!boost::filesystem::is_regular_file(path_iterator->second) {
) {
path_iterator->second = base_string + ".timestamp"; path_iterator->second = base_string + ".timestamp";
} }