Skip to content

Commit

Permalink
Add check for absolute path of config file (#1276)
Browse files Browse the repository at this point in the history
Co-authored-by: Kip Hamiltons <[email protected]>
  • Loading branch information
KipHamiltons and youngest-person-alive authored May 28, 2020
1 parent 9883568 commit a3b38f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/utils/configfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const OptionId kConfigFileId{
"parameter per line, e.g.:\n--weights=/path/to/weights",
'c'};
const char* kDefaultConfigFile = "lc0.config";
const char* kDefaultConfigFileParam = "<default>";
} // namespace

std::vector<std::string> ConfigFile::arguments_;
Expand All @@ -55,7 +56,7 @@ void ConfigFile::PopulateOptions(OptionsParser* options) {
// ProcessAllFlags() that should be called only once, and needs the config file.
std::string ConfigFile::ProcessConfigFlag(
const std::vector<std::string>& args) {
std::string filename = kDefaultConfigFile;
std::string filename = kDefaultConfigFileParam;
for (auto iter = args.begin(), end = args.end(); iter != end; ++iter) {
std::string param = *iter;

Expand All @@ -81,28 +82,33 @@ std::string ConfigFile::ProcessConfigFlag(
bool ConfigFile::Init(OptionsParser* options) {
arguments_.clear();

// Get the relative path from the config file parameter.
// Get the path from the config file parameter.
std::string filename = ProcessConfigFlag(CommandLine::Arguments());

// If filename is an empty string then return true. This is to override
// If filename is an empty string then return true. This is to override
// loading the default configuration file.
if (filename == "") return true;

filename = CommandLine::BinaryDirectory() + "/" + filename;

// Parses the file into the arguments_ vector.
if (!ParseFile(filename, options)) return false;

return true;
}

bool ConfigFile::ParseFile(const std::string& filename,
bool ConfigFile::ParseFile(std::string& filename,
OptionsParser* options) {
std::ifstream input(filename);

// Check to see if we are using the default config file or not.
OptionsDict dict = options->GetOptionsDict();
const bool using_default_config = dict.IsDefault<std::string>(kConfigFileId);
const bool using_default_config =
filename == std::string(kDefaultConfigFileParam);

// If no logfile was set on the command line, then the default is
// to check in the binary directory.
if (using_default_config) {
filename = CommandLine::BinaryDirectory() + '/' + kDefaultConfigFile;
}

std::ifstream input(filename);

if (!input.is_open()) {
// It is okay if we cannot open the default file since it is normal
Expand Down
3 changes: 2 additions & 1 deletion src/utils/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class ConfigFile {

private:
// Parses the config file into the arguments_ vector.
static bool ParseFile(const std::string& filename, OptionsParser* options);
static bool ParseFile(std::string& filename, OptionsParser* options);

// Returns the absolute path to the config file argument given.
static std::string ProcessConfigFlag(const std::vector<std::string>& args);

static std::vector<std::string> arguments_;
Expand Down

0 comments on commit a3b38f9

Please sign in to comment.