Changeset 19385


Ignore:
Timestamp:
Nov 12, 2018, 9:32:33 PM (7 months ago)
Author:
tbretz
Message:
Implemneted to set an independent prefix path in fact++.rc or from the commandline which is used to read the default configuration files. Improved output of reading files. Do not accept an invalid user specified priority file. Return a prefixed filename if filename does not contain a path.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Configuration.cc

    r19363 r19385  
    331331#include <iostream>
    332332#include <iomanip>
    333 
    334 #include <boost/filesystem.hpp>
    335333
    336334#ifdef HAVE_SQL
     
    492490        ;
    493491
    494     po::options_description def_config;
    495     def_config.add_options()
    496         ("default",  var<string>(fName+string(".rc")), "Default configuration file.")
    497         ;
    498 
    499492    po::options_description config("Configuration options");
    500493    config.add_options()
     
    502495        ("database",    var<string>(), "Database link as in\n\t[user[:password]@]server.com[:port]/database[?compress=0|1]\nOverwrites options from the default configuration file.")
    503496        ("no-database",                "Suppress any access to the database even if a database URL was set.")
     497        ("default",     var<string>(fName+string(".rc")), "Default configuration file.")
     498        ("prefix",      var<string>(), "Path to default configuration file.")
    504499        ;
    505500
    506501    fOptionsCommandline[kVisible].add(generic);
    507502    fOptionsCommandline[kVisible].add(config);
    508     fOptionsCommandline[kVisible].add(def_config);
    509503    fOptionsConfigfile[kVisible].add(config);
    510504}
     
    981975
    982976    fPriorityFile = "";
     977    fPrefixPath   = "";
    983978    fDefaultFile  = "";
    984979    fDatabase     = "";
     
    10541049    const string globalfile = (path.parent_path()/boost::filesystem::path("fact++.rc")).string();
    10551050
    1056     cerr << "Reading global  options from '" << globalfile << "'." << endl;
     1051
     1052    errno = 0;
    10571053
    10581054    ifstream gfile(globalfile.c_str());
    1059     // ===> FIXME: Proper handling of missing file or wrong file name
     1055    cerr << "Reading global  options from '" << globalfile << "' [" << strerror(errno) << "]" << endl;
    10601056    const po::parsed_options parsed_globalfile =
    10611057        !gfile ?
     
    10681064#endif
    10691065
     1066    po::store(parsed_globalfile, getfiles);
     1067
     1068    // Get default prefix path from command line
     1069    if (getfiles.count("prefix"))
     1070        fPrefixPath = getfiles["prefix"].as<string>();
     1071
    10701072    // Get default file from command line
    10711073    if (getfiles.count("default"))
    1072     {
    10731074        fDefaultFile = getfiles["default"].as<string>();
    1074         cerr << "Reading default options from '" << fDefaultFile << "'." << endl;
    1075     }
     1075
     1076    const string default_file = (boost::filesystem::path(fPrefixPath)/boost::filesystem::path(fDefaultFile)).string();
    10761077
    10771078    const bool checkf    = !getfiles.count("dont-check-files") && !getfiles.count("dont-check");
     
    10791080    //const bool exists    = boost::filesystem::exists(fDefaultFile);
    10801081
    1081     ifstream indef(fDefaultFile.c_str());
    1082     // ===> FIXME: Proper handling of missing file or wrong file name
     1082    errno = 0;
     1083
     1084    ifstream indef(default_file);
     1085    if (!fDefaultFile.empty())
     1086        cerr << "Reading default options from '" << default_file << "' [" << strerror(errno) << "]" << endl;
    10831087    const po::parsed_options parsed_defaultfile =
    10841088        !indef && defaulted ?
     
    11141118    {
    11151119        fPriorityFile = getfiles["config"].as<string>();
    1116         cerr << "Reading config options from '" << fPriorityFile << "'." << endl;
     1120        cerr << "Reading config  options from '" << fPriorityFile << "'." << endl;
    11171121    }
    11181122
    11191123    ifstream inpri(fPriorityFile.c_str());
     1124    if (!fPriorityFile.empty() && !inpri)
     1125        throw runtime_error("Reading '"+fPriorityFile+"' failed: "+strerror(errno));
     1126
    11201127    // ===> FIXME: Proper handling of missing file or wrong file name
    11211128    const po::parsed_options parsed_priorityfile =
  • trunk/FACT++/src/Configuration.h

    r18997 r19385  
    44#include <iostream>
    55#include <boost/program_options.hpp>
     6#include <boost/filesystem.hpp>
    67
    78namespace po = boost::program_options;
     
    3637
    3738    std::string fPriorityFile;  /// File name of the priority configuration file (overwrites option from the databse)
     39    std::string fPrefixPath;    /// Path to the default configuration file
    3840    std::string fDefaultFile;   /// File name of the default configuration file (usually {program}.rc)
    3941    std::string fDatabase;      /// URL for database connection (see Configuration::parse_database)
     
    204206    }
    205207
     208    const std::string GetPrefixedString(const std::string &var)
     209    {
     210        const boost::filesystem::path ff(Get<std::string>(var));
     211        const boost::filesystem::path pp(fPrefixPath);
     212        return (ff.has_parent_path() ? ff : pp/ff).string();
     213    }
     214
    206215/*
    207216    template<class T>
     
    234243*/
    235244    const std::string &GetName() const { return fName; }
     245    const boost::filesystem::path GetPrefixPath() const { return fPrefixPath; }
    236246};
    237247
Note: See TracChangeset for help on using the changeset viewer.