37#include <cppthread/log.h>
48#include <snapdev/poison.h>
89 ,
char * argv[])
const
102 int const mode(R_OK | (writable ? W_OK : 0));
105 if(access(r.c_str(), mode) == 0)
107 existing_files.push_back(r);
111 return existing_files;
131 if(std::find(names.begin(), names.end(), add) == names.end())
133 names.push_back(add);
161 ,
char * argv[])
const
180 std::size_t
const max(
size(
"config-dir"));
181 directories.reserve(max);
182 for(std::size_t idx(0); idx < max; ++idx)
184 directories.push_back(
get_string(
"config-dir", idx));
193 if(directories.empty())
197 std::vector<char *> sub_argv;
198 sub_argv.resize(args.size() + 2);
200 for(std::size_t idx(0); idx < args.size(); ++idx)
202 sub_argv[idx + 1] =
const_cast<char *
>(args[idx].c_str());
204 sub_argv[args.size() + 1] =
nullptr;
214 *configuration_directories !=
nullptr;
215 ++configuration_directories)
217 directories.push_back(*configuration_directories);
221 if(directories.empty())
227 std::string directory_name(
"/usr/share/advgetopt/options/");
228 directory_name += name;
229 directories.push_back(directory_name);
231 directory_name =
"/usr/share/";
232 directory_name += name;
233 directory_name +=
"/options";
234 directories.push_back(directory_name);
236 directory_name =
"/etc/";
237 directory_name += name;
238 directories.push_back(directory_name);
244 for(
auto directory : directories)
246 if(!directory.empty())
248 std::string
const full_filename(directory + (
"/" + filename));
250 if(user_filename == full_filename)
261 if(!with_project_name.empty())
263 for(
auto const & n : with_project_name)
295 ,
bool writable)
const
305 *configuration_files !=
nullptr;
306 ++configuration_files)
308 char const * filename(*configuration_files);
309 if(*filename ==
'\0')
315 if(user_filename == filename)
326 if(!with_project_name.empty())
328 for(
auto const & n : with_project_name)
377 char const * found(
nullptr);
379 *configuration_files !=
nullptr;
380 ++configuration_files)
382 char const * filename(*configuration_files);
383 if(*filename ==
'\0')
390 if(filename[0] ==
'~'
391 && (filename[1] ==
'/' || filename[1] ==
'\0'))
423 std::string directory;
427 *configuration_directories !=
nullptr;
428 ++configuration_directories)
430 char const * dir(*configuration_directories);
437 && (dir[1] ==
'/' || dir[1] ==
'\0'))
448 if(directory.empty())
453 directory =
"/etc/" + name;
456 if(directory.back() !=
'/')
472 std::string filename(
"/etc/");
488 return std::string();
505 throw getopt_logic_error(
"we just checked both of those names and at least one was valid.");
537 for(
int idx(1); idx < argc; ++idx)
539 if(strcmp(argv[idx],
"--config-dir") == 0)
541 for(++idx; idx < argc; ++idx)
543 if(argv[idx][0] ==
'-')
548 result.push_back(argv[idx]);
551 else if(strncmp(argv[idx],
"--config-dir=", 13) == 0)
553 result.push_back(argv[idx] + 13);
609 for(
auto f : filenames)
648 conf_setup = std::make_shared<conf_file_setup>(filename);
652 conf_setup = std::make_shared<conf_file_setup>(
656 if(!conf_setup->is_valid())
669 conf->section_to_variables(
675 if(!sections.empty())
679 if(configuration_sections ==
nullptr)
681 configuration_sections = std::make_shared<option_info>(name);
682 configuration_sections->add_flag(
689 cppthread::log << cppthread::log_level_t::error
692 <<
"\" must have GETOPT_FLAG_MULTIPLE set."
696 for(
auto s : sections)
698 if(!configuration_sections->has_value(s))
700 configuration_sections->add_value(
708 for(
auto const & param : conf->get_parameters())
716 || param.first.length() == 1)
718 cppthread::log << cppthread::log_level_t::error
719 <<
"unknown option \""
721 <<
"\" found in configuration file \""
724 << param.second.get_line()
733 opt = std::make_shared<option_info>(param.first);
741 opt->set_default(param.second);
753 cppthread::log << cppthread::log_level_t::error
756 <<
"\" is not supported in configuration files (found in \""
764 std::string value(param.second.get_value());
765 switch(param.second.get_assignment_operator())
774 if(opt->is_defined())
788 value = opt->get_value() + value;
793 if(opt->is_defined())
797 cppthread::log << cppthread::log_level_t::error
800 <<
"\" found in configuration file \""
803 << param.second.get_line()
804 <<
" uses the := operator but the value is already defined."
Definitions of the advanced getopt class.
std::shared_ptr< conf_file_setup > pointer_t
std::shared_ptr< conf_file > pointer_t
static pointer_t get_conf_file(conf_file_setup const &setup)
Create and read a conf_file.
std::string get_string(std::string const &name, int idx=0, bool raw=false) const
Get the content of an option as a string.
string_list_t get_configuration_filenames(bool exists, bool writable, int argc=0, char *argv[]=nullptr) const
Generate a list of configuration filenames.
std::string get_group_or_project_name() const
Retrieve the group or project name.
std::size_t size(std::string const &name) const
Retrieve the number of arguments.
options_environment f_options_environment
void add_option_from_string(option_info::pointer_t opt, std::string const &value, std::string const &filename, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Add an option with a value defined in a string.
void get_managed_configuration_filenames(string_list_t &names, bool writable, int argc, char *argv[]) const
Generate the list of managed configuration filenames.
void process_configuration_file(std::string const &filename)
Parse one specific configuration file and process the results.
void parse_configuration_files(int argc=0, char *argv[]=nullptr)
This function checks for arguments in configuration files.
void get_direct_configuration_filenames(string_list_t &names, bool writable) const
Define the list of direct configuration filenames.
option_info::map_by_name_t f_options_by_name
bool is_defined(std::string const &name) const
Check whether a parameter is defined.
option_info::pointer_t get_option(std::string const &name, bool exact_option=false) const
Retrieve an option by name.
static string_list_t find_config_dir(int argc, char *argv[])
Search for the "--config-dir" option in a set of arguments.
std::string f_program_fullname
static void add_configuration_filename(string_list_t &names, std::string const &add)
Add one configuration filename to our list.
std::string f_environment_variable
bool has_flag(flag_t flag) const
Check whether an environment flag is set or not.
static string_list_t split_environment(std::string const &environment)
Transform a string in an array of arguments.
std::string get_output_filename() const
Determine the best suited file for updates.
variables::pointer_t f_variables
std::shared_ptr< option_info > pointer_t
static void set_configuration_filename(std::string const &filename)
Save the filename of the current configuration file.
Declaration of the conf_file class used to read a configuration file.
Definitions of the advanced getopt exceptions.
The advgetopt environment to parse command line options.
string_list_t insert_group_name(std::string const &filename, char const *group_name, char const *project_name, bool add_default_on_empty)
Insert the group (or project) name in the filename.
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_DYNAMIC_PARAMETERS
static constexpr flag_t GETOPT_FLAG_CONFIGURATION_FILE
std::string option_with_underscores(std::string const &s)
Converts an option back to using underscores.
static constexpr flag_t GETOPT_FLAG_DYNAMIC
static constexpr flag_t GETOPT_FLAG_MULTIPLE
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS
std::vector< std::string > string_list_t
std::string handle_user_directory(std::string const &filename)
Replace a starting ~/... with the contents of the $HOME variable.
std::string default_group_name(std::string const &filename, char const *group_name, char const *project_name, int priority)
Generate the default filename (the ".../50-...")
constexpr char const CONFIGURATION_SECTIONS[]
char const *const * f_configuration_directories
conf_file_setup const * f_config_setup
char const * f_section_variables_name
char const * f_configuration_filename
char const * f_project_name
char const *const * f_configuration_files
char const * f_group_name