diff --git a/src/ParamFile.cpp b/src/ParamFile.cpp index fb2548eec..b53706e86 100644 --- a/src/ParamFile.cpp +++ b/src/ParamFile.cpp @@ -90,14 +90,10 @@ void ParamReader::extract_or_exit(std::string const& param, T& output) } template -bool ParamReader::extract_multiple(std::string const& param, T* output, std::size_t N, T default_value) +bool ParamReader::extract_multiple_no_default(std::string const& param, T* output, std::size_t N) { if (!exists(param)) - { - std::cerr << "Using default value: " << default_value << std::endl; - std::fill_n(output, N, default_value); return false; - } std::istringstream iss(m_param_value_map[param]); for (auto i = 0; i < N; i++) @@ -105,8 +101,7 @@ bool ParamReader::extract_multiple(std::string const& param, T* output, std::siz if (!raw_extract(iss, output[i])) { std::cerr << "ERROR: Got " << i << " out of " << N << " parameters for " - << param << ".\nUsing default value: " << default_value << std::endl; - std::fill_n(output, N, default_value); + << param << std::endl; return false; } } @@ -114,50 +109,31 @@ bool ParamReader::extract_multiple(std::string const& param, T* output, std::siz } template -bool ParamReader::cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value) +void ParamReader::extract_multiple_or_exit(std::string const& param, T* output, std::size_t N) { - if (conditional) - return extract_multiple(param, output, N, default_value); - std::fill_n(output, N, default_value); - return true; + if (extract_multiple_no_default(param, output, N) == false) + std::exit(1); } template -bool ParamReader::extract_multiple_no_default(std::string const& param, T* output, std::size_t N) +bool ParamReader::extract_multiple(std::string const& param, T* output, std::size_t N, T default_value) { - if (!exists(param)) + if (extract_multiple_no_default(param, output, N) == false) { + std::cerr << "Using default value: " << default_value << std::endl; + std::fill_n(output, N, default_value); return false; } - - std::istringstream iss(m_param_value_map[param]); - for (auto i = 0; i < N; i++) - { - if (!raw_extract(iss, output[i])) - { - std::cerr << "ERROR: Got " << i << " out of " << N << " parameters for " - << param << std::endl; - return false; - } - } return true; } template -void ParamReader::extract_multiple_or_exit(std::string const& param, T* output, std::size_t N) +bool ParamReader::cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value) { - if (!exists(param)) - std::exit(1); - - std::istringstream iss(m_param_value_map[param]); - for (auto i = 0; i < N; i++) - { - if (!raw_extract(iss, output[i])) - { - std::cerr << "ERROR: Got " << i << " out of " << N << " parameters for " << param << std::endl; - std::exit(1); - } - } + if (conditional) + return extract_multiple(param, output, N, default_value); + std::fill_n(output, N, default_value); + return true; } void ParamReader::extract_multiple_strings_or_exit(std::string const& param, std::vector& output, std::size_t N) @@ -175,7 +151,7 @@ void ParamReader::extract_multiple_strings_or_exit(std::string const& param, std std::cerr << "ERROR: Only got " << i << " out of " << N << " parameters for " << param << std::endl; std::exit(1); } - output.emplace_back(token); + output.push_back(std::move(token)); } } @@ -262,15 +238,18 @@ void ParamReader::parse_param_file(std::string const& param_file) // try to read the value of this parameter into a string std::string value_line; std::string tmp_line; + // read every line after the parameter name until there's no more or a break while (std::getline(param_stream, tmp_line)) { trim(tmp_line); + // break if the next line is empty or doesn't start with a letter, number, or '-' character if (tmp_line.empty() || !(std::isalnum(tmp_line[0]) || tmp_line[0] == '-')) break; if (!value_line.empty()) value_line.push_back('\n'); value_line.append(tmp_line); } + // if there is no value found for the parameter, print an error and continue if (value_line.empty()) { std::cerr << "ERROR: Value is missing for parameter: " << param_line << std::endl; diff --git a/src/ParamFile.hpp b/src/ParamFile.hpp index 49f87b189..f860976ac 100644 --- a/src/ParamFile.hpp +++ b/src/ParamFile.hpp @@ -25,54 +25,54 @@ class ParamReader { * Each parameter-value pair can be extracted out by one of the extract_*() * functions by passing a storage variable of the type expected. * - * Note: The parameter file names passed into this function can be empty. + * @note: The parameter file names passed into this function can be empty. */ ParamReader(std::string const& param_file, std::string const& preparam_file, std::string const& admin_file); /** - * Extract a single parameter value or assign the default to `output`. - * - * @note: The parameter value is checked using the numeric limits of the - * output variable's type. + * Extract a single numeric value or assign the default to `output`. */ template bool extract(std::string const& param, T& output, T default_value); /** - * Extract and assign a single parameter value to `output` or exit the program. - * - * @note: The parameter value is checked using the numeric limits of the - * output variable's type. + * Extract and assign a single numeric value to `output` or exit the program. */ template void extract_or_exit(std::string const& param, T& output); /** - * Extract multiple parameter values or assign the default to `N` values in `output`. - * - * @note: The parameter values are checked using the numeric limits of the - * output variable's type. + * Extract multiple numeric values to `N` numbers in `output`. */ template - bool extract_multiple(std::string const& param, T* output, std::size_t N, T default_value); + bool extract_multiple_no_default(std::string const& param, T* output, std::size_t N); + /** + * Extract and assign multiple numeric values to `N` numbers in `output` or exit the program. + */ template - bool cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value); + void extract_multiple_or_exit(std::string const& param, T* output, std::size_t N); + /** + * Extract multiple numeric values or assign the default to `N` numbers in `output`. + */ template - bool extract_multiple_no_default(std::string const& param, T* output, std::size_t N); + bool extract_multiple(std::string const& param, T* output, std::size_t N, T default_value); /** - * Extract and assign multiple parameter values to `N` values in `output` or exit the program. - * - * @note: The parameter values are checked using the numeric limits of the - * output variable's type. + * Conditionally extract multiple numeric values or assign the default to `N` numbers in `output`. */ template - void extract_multiple_or_exit(std::string const& param, T* output, std::size_t N); + bool cond_extract_multiple(bool conditional, std::string const& param, T* output, std::size_t N, T default_value); + /** + * Extract exactly `N` strings and add them to `output` or exit the program. + */ void extract_multiple_strings_or_exit(std::string const& param, std::vector& output, std::size_t N); + /** + * Extract a matrix of strings with `num_cols` columns per row and add them to `output` or exit the program. + */ void extract_string_matrix_or_exit(std::string const& param, std::vector>& output, std::size_t num_cols); private: