![]() |
advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
|
#include <conf_file.h>
Classes | |
struct | callback_entry_t |
Public Types | |
typedef int | callback_id_t |
typedef std::function< void(pointer_t conf_file, callback_action_t action, std::string const ¶meter_name, std::string const &value)> | callback_t |
typedef std::map< std::string, parameter_value > | parameters_t |
typedef std::shared_ptr< conf_file > | pointer_t |
typedef string_set_t | sections_t |
Public Member Functions | |
callback_id_t | add_callback (callback_t const &c, std::string const ¶meter_name=std::string()) |
Add a callback to detect when changes happen. | |
void | erase_all_parameters () |
Clear the list of all existing parameters from this file. | |
bool | erase_parameter (std::string name) |
Erase the named parameter from this configuration file. | |
bool | exists () const |
Whether an input file was found. | |
int | get_errno () const |
Get the error number opening/reading the configuration file. | |
std::string | get_parameter (std::string name) const |
Get the named parameter. | |
parameters_t | get_parameters () const |
Get a list of parameters. | |
sections_t | get_sections () const |
Get a list of sections. | |
conf_file_setup const & | get_setup () const |
Get the configuration file setup. | |
variables::pointer_t | get_variables () const |
Retrieve the currently attached variables. | |
bool | has_parameter (std::string name) const |
Check whether a parameter is defined. | |
assignment_t | is_assignment_operator (char const *&s, bool skip) const |
Check whether c is an assignment operator. | |
bool | is_comment (char const *s) const |
Check whether the string starts with a comment introducer. | |
void | remove_callback (callback_id_t id) |
Remove a callback. | |
bool | save_configuration (std::string backup_extension=std::string(".bak"), bool replace_backup=false, bool prepend_warning=true, std::string output_filename=std::string()) |
Save the configuration file. | |
int | section_to_variables (std::string const §ion_name, variables::pointer_t var) |
Look for a section to convert in a list of variables. | |
bool | set_parameter (std::string section, std::string name, std::string const &value, assignment_t op=assignment_t::ASSIGNMENT_NONE, std::string const &comment=std::string()) |
Set a parameter. | |
void | set_variables (variables::pointer_t variables) |
Attach a variables object to the configuration file. | |
bool | was_modified () const |
Check whether this configuration file was modified. | |
Static Public Member Functions | |
static pointer_t | get_conf_file (conf_file_setup const &setup) |
Create and read a conf_file. | |
static void | reset_conf_files () |
Forget all the cached configuration files. | |
Private Types | |
typedef std::vector< callback_entry_t > | callback_vector_t |
Private Member Functions | |
conf_file (conf_file_setup const &setup) | |
Initialize and read a configuration file. | |
bool | get_line (std::ifstream &stream, std::string &line) |
Get one line. | |
int | getc (std::ifstream &stream) |
Read one characte from the input stream. | |
void | read_configuration () |
Read a configuration file. | |
void | ungetc (int c) |
Restore one character. | |
void | value_changed (callback_action_t action, std::string const ¶meter_name, std::string const &value) |
Call whenever the value changed so we can handle callbacks. | |
Private Attributes | |
callback_vector_t | f_callbacks = callback_vector_t() |
int | f_errno = 0 |
bool | f_exists = false |
int | f_line = 0 |
bool | f_modified = false |
callback_id_t | f_next_callback_id = 0 |
parameters_t | f_parameters = parameters_t() |
bool | f_reading = false |
sections_t | f_sections = sections_t() |
conf_file_setup const | f_setup |
int | f_unget_char = '\0' |
variables::pointer_t | f_variables = variables::pointer_t() |
Definition at line 182 of file conf_file.h.
Definition at line 194 of file conf_file.h.
typedef std::function<void( pointer_t conf_file , callback_action_t action , std::string const & parameter_name , std::string const & value)> advgetopt::conf_file::callback_t |
Definition at line 193 of file conf_file.h.
|
private |
Definition at line 254 of file conf_file.h.
typedef std::map<std::string, parameter_value> advgetopt::conf_file::parameters_t |
Definition at line 188 of file conf_file.h.
typedef std::shared_ptr<conf_file> advgetopt::conf_file::pointer_t |
Definition at line 186 of file conf_file.h.
Definition at line 187 of file conf_file.h.
|
private |
This constructor initializes this conf_file object and then reads the corresponding configuration file.
Note that you have to use the create_conf_file() function for you to be able to create a configuration file. It is done that way became a file can be read only once. Once loaded, it gets cached until your application quits.
[in] | setup | The configuration file setup. |
Definition at line 1046 of file conf_file.cpp.
References read_configuration().
conf_file::callback_id_t advgetopt::conf_file::add_callback | ( | callback_t const & | c, |
std::string const & | parameter_name = std::string() |
||
) |
This function is used to attach a callback to this configuration file. This is useful if you'd like to know when a change happen to a parameter in this configuration file.
The callbacks get called when:
You can cancel your callback by calling the remove_callback() function with the identifier returned by this function.
To attach another object to your callback, you can either create a callback which is attached to your object and a function member or use std::bind() to attach the object to the function call.
If you specifcy a parameter_name
, the callback is called only if the parameter has that specific name.
[in] | c | The new callback std::function. |
[in] | parameter_name | The parameter name or an empty string. |
Definition at line 1100 of file conf_file.cpp.
References f_callbacks, f_next_callback_id, and advgetopt::get_global_mutex().
void advgetopt::conf_file::erase_all_parameters | ( | ) |
This function goes through the list of parameters it contains and erase each one of them in turn.
The function calls the erase_parameter() function with each one of the parameter still in the list. It is done that way to make sure that the value_changed() function gets called as expected for each value.
Definition at line 1781 of file conf_file.cpp.
References erase_parameter(), and f_parameters.
bool advgetopt::conf_file::erase_parameter | ( | std::string | name | ) |
This function can be used to remove the specified parameter from this configuration file.
If that parameter is not defined in the file, then nothing happens.
[in] | name | The name of the parameter to remove. |
Definition at line 1749 of file conf_file.cpp.
References advgetopt::erased, f_modified, f_parameters, f_reading, and value_changed().
Referenced by erase_all_parameters(), and section_to_variables().
bool advgetopt::conf_file::exists | ( | ) | const |
This function returns true if a file was opened for reading. Whether the file is valid is not marked in this flag.
If you want to know whether an error occurred while reading the file, try the get_errno().
Definition at line 1187 of file conf_file.cpp.
References f_exists, and advgetopt::get_global_mutex().
|
static |
This function creates a new conf_file object unless one with the same filename already exists.
If the configuration file was already loaded, then that pointer gets returned instead of reloading the file. There is currently no API to allow for the removal because another thread or function may have the existing pointer cached and we want all instances of a configuration file to be the same (i.e. if you update the value of a parameter then that new value should be visible by all the users of that configuration file.) Therefore, you can think of a configuration file as a global variable.
[in] | setup | The settings to be used in this configuration file reader. |
Definition at line 809 of file conf_file.cpp.
References advgetopt::conf_file_setup::get_config_url(), advgetopt::conf_file_setup::get_filename(), and advgetopt::get_global_mutex().
Referenced by advgetopt::getopt::parse_options_from_file(), and advgetopt::getopt::process_configuration_file().
int advgetopt::conf_file::get_errno | ( | ) | const |
The class registers the errno value whenever an I/O error happens while handling the configuration file. In most cases the function is expected to return 0.
The ENOENT error should not happen since the setup is going to be marked as invalid when a configuration file does not exist and you should not end up creation a conf_file object when that happens. However, it is expected when you want to make some changes to a few parameters and save them back to file (i.e. the very first time there will be no file under the writable configuration folder.)
Definition at line 1211 of file conf_file.cpp.
References f_errno, and advgetopt::get_global_mutex().
|
private |
This function reads one line. The function takes the line continuation setup in account. So for example a line that ends with a backslash continues on the next line when the line continuation is setup to Unix.
Note that by default comments are also continued. So a backslash in Unix mode continues a comment on the next line.
There is a special case with the semicolon continuation setup. When the line starts as a comment, it will end on the first standalone newline (i.e. a comment does not need to end with a semi-colon.)
[in,out] | in | The input stream. |
[out] | line | Where the line gets saved. |
Definition at line 1889 of file conf_file.cpp.
References f_line, f_setup, advgetopt::conf_file_setup::get_line_continuation(), getc(), is_comment(), advgetopt::iswspace(), advgetopt::line_continuation_fortran, advgetopt::line_continuation_msdos, advgetopt::line_continuation_rfc_822, advgetopt::line_continuation_semicolon, advgetopt::line_continuation_single_line, advgetopt::line_continuation_unix, and ungetc().
Referenced by read_configuration().
std::string advgetopt::conf_file::get_parameter | ( | std::string | name | ) | const |
This function searches for the specified parameter. If that parameter exists, then its value is returned. Note that the value of a parameter may be the empty string.
If the parameter does not exist, the function returns the empty string. To distinguish between an undefined parameter and a parameter set to the empty string, use the has_parameter() function.
[in] | name | The name of the parameter to retrieve. |
Definition at line 1345 of file conf_file.cpp.
References f_parameters, f_variables, and advgetopt::get_global_mutex().
conf_file::parameters_t advgetopt::conf_file::get_parameters | ( | ) | const |
This function returns a copy of the list of parameters defined in this configuration file.
Definition at line 1294 of file conf_file.cpp.
References f_parameters, and advgetopt::get_global_mutex().
Referenced by section_to_variables().
conf_file::sections_t advgetopt::conf_file::get_sections | ( | ) | const |
This function returns a copy of the list of sections defined in this configuration file. In most cases, you should not need this function since you are expected to know what parameters may be defined. There are times though when it can be very practical. For example, the options_config.cpp makes use of it since each section is a parameter which we do not know the name of until we have access to this array of sections.
Definition at line 1269 of file conf_file.cpp.
References f_sections, and advgetopt::get_global_mutex().
conf_file_setup const & advgetopt::conf_file::get_setup | ( | ) | const |
This function returns a copy of the setup used to load this configuration file.
Definition at line 1066 of file conf_file.cpp.
References f_setup.
variables::pointer_t advgetopt::conf_file::get_variables | ( | ) | const |
This function returns the attached variables. This function may return a nullptr.
Definition at line 1246 of file conf_file.cpp.
References f_variables.
|
private |
This function reads one character from the input stream and returns it as an int
.
If there is an ungotten character (i.e. ungetc() was called) then that character is returned.
When the end of the file is reached, this function returns -1.
[in,out] | in | The input stream. |
Definition at line 1825 of file conf_file.cpp.
References f_unget_char.
Referenced by get_line().
bool advgetopt::conf_file::has_parameter | ( | std::string | name | ) | const |
This function checks for the existance of a parameter. It is a good idea to first check for the existance of a parameter since the get_parameter() function may otherwise return an empty string and you cannot know whether that empty string means that the parameter was not defined or it was set to the empty string.
[in] | name | The name of the parameter to check. |
Definition at line 1317 of file conf_file.cpp.
References f_parameters, and advgetopt::get_global_mutex().
assignment_t advgetopt::conf_file::is_assignment_operator | ( | char const *& | s, |
bool | skip | ||
) | const |
This function checks the characters at s
to know whether it matches one of the character(s) allowed as an assignment character.
[in,out] | s | The character(s) to be checked. |
[in] | skip | Whether to position the input pointer s after the assignment character(s). |
s
are considered to represent an assignment character, false otherwise. Definition at line 2251 of file conf_file.cpp.
References advgetopt::ASSIGNMENT_APPEND, advgetopt::ASSIGNMENT_NEW, advgetopt::ASSIGNMENT_NONE, advgetopt::ASSIGNMENT_OPERATOR_COLON, advgetopt::ASSIGNMENT_OPERATOR_EQUAL, advgetopt::ASSIGNMENT_OPERATOR_EXTENDED, advgetopt::ASSIGNMENT_OPERATOR_SPACE, advgetopt::ASSIGNMENT_OPTIONAL, advgetopt::ASSIGNMENT_SET, f_setup, and advgetopt::conf_file_setup::get_assignment_operator().
Referenced by read_configuration().
This function checks whether the s
string starts with a comment.
We support different types of comment introducers. This function checks the flags as defined in the constructor and returns true if the type of character introducer defines a comment.
We currently support:
[in] | s | The string to check for a comment. |
true
if the string represents a comment. Definition at line 2320 of file conf_file.cpp.
References advgetopt::COMMENT_CPP, advgetopt::COMMENT_INI, advgetopt::COMMENT_SHELL, f_setup, and advgetopt::conf_file_setup::get_comment().
Referenced by get_line(), and read_configuration().
|
private |
This function reads a configuration file and saves all the parameters it finds in a map which can later be checked against an option table for validation.
Definition at line 2019 of file conf_file.cpp.
References advgetopt::ASSIGNMENT_NONE, advgetopt::ASSIGNMENT_OPERATOR_SPACE, advgetopt::COMMENT_SAVE, f_errno, f_exists, f_line, f_reading, f_setup, advgetopt::conf_file_setup::get_assignment_operator(), advgetopt::conf_file_setup::get_comment(), advgetopt::conf_file_setup::get_filename(), get_line(), advgetopt::conf_file_setup::get_section_operator(), is_assignment_operator(), is_comment(), advgetopt::iswspace(), advgetopt::SECTION_OPERATOR_BLOCK, advgetopt::SECTION_OPERATOR_INI_FILE, set_parameter(), and advgetopt::unquote().
Referenced by conf_file().
void advgetopt::conf_file::remove_callback | ( | callback_id_t | id | ) |
This function is the opposite of the add_callback(). It removes a callback that you previously added. This is useful if you are interested in hearing about the changing values when set a first time but are not interested at all about future changes.
[in] | id | The id returned by the add_callback() function. |
Definition at line 1121 of file conf_file.cpp.
References f_callbacks, and advgetopt::get_global_mutex().
|
static |
In some rare cases, you may want to get rid of the cached data and re-read all the configuration data from file. In this case, you should delete all your conf_file instances, call this function, and then do the get_conf_file() again.
This function clears the caches. If you keep existing conf_file objects around, they may not match newer instances.
This function is particularly useful when dealing with tests that verify configuration data.
Definition at line 848 of file conf_file.cpp.
References advgetopt::get_global_mutex().
bool advgetopt::conf_file::save_configuration | ( | std::string | backup_extension = std::string(".bak") , |
bool | replace_backup = false , |
||
bool | prepend_warning = true , |
||
std::string | output_filename = std::string() |
||
) |
This function saves the current data from this configuration file to the output file. It overwrites the existing file.
Note that when you load configuration files for the command line, you may load data from many different files. This function only handles the data found in this very file and only that data and whatever modifications you made is included in the output .
If the conf_file is not marked as modified, the function returns immediately with true.
The assignment operator used is the space if allowed, the colon if allowed, otherwise it falls back to the equal operator. At this time, the colon and equal operators are not preceeded or followed by a space (i.e. name=value
).
[in] | backup_extension | If not empty, create a backup with that extension. |
[in] | replace_backup | If true and a backup exists, replace it. |
[in] | prepend_warning | Whether to write a warning at the start of the file. |
[in] | output_filename | The output filename; if empty, fallback to the filename defined in conf_file_setup. |
Definition at line 888 of file conf_file.cpp.
References advgetopt::ASSIGNMENT_OPERATOR_COLON, advgetopt::ASSIGNMENT_OPERATOR_SPACE, f_errno, f_modified, f_parameters, f_setup, advgetopt::conf_file_setup::get_assignment_operator(), advgetopt::conf_file_setup::get_filename(), advgetopt::conf_file_setup::get_name_separator(), and advgetopt::NAME_SEPARATOR_DASHES.
int advgetopt::conf_file::section_to_variables | ( | std::string const & | section_name, |
variables::pointer_t | vars | ||
) |
This function checks for a section named section_name
. If it exists, then it gets converted to a set of variables in vars
and gets removed from the conf_file list of sections.
[in] | section_name | The name of the section to convert to variables. |
[in] | vars | The variables object where the parameters are saved as variables. |
vars
is a null pointer, the number of parameters converted otherwise Definition at line 2364 of file conf_file.cpp.
References erase_parameter(), f_sections, and get_parameters().
bool advgetopt::conf_file::set_parameter | ( | std::string | section, |
std::string | name, | ||
std::string const & | value, | ||
assignment_t | a = assignment_t::ASSIGNMENT_NONE , |
||
std::string const & | comment = std::string() |
||
) |
This function sets a parameter to the specified value.
The name of the value includes the section
names and the name
parameter concatenated with a C++ scope operator (::) in between (unless section
is the empty string in which case no scope operator gets added).
When the name
parameter starts with a scope parameter, the section
parameter is ignored. This allows one to ignore the current section (i.e. the last '[...]' or any '<name> { ... }').
The section
parameter is a list of section names separated by the C++ scope operator (::).
The name
parameter may include C (.) and/or C++ (::) section separators when the configuration file supports those. Internally, those get moved to the section
parameter. That allows us to verify that the number of sections is valid.
This function may be called any number of time. The last value is the one kept. While reading the configuration file, though, a warning is generated when a parameter gets overwritten since this is often the source of a problem.
In the following configuration file:
the variable named var
is set to twice
on exit. A warning will have been generated about the fact that the variable was set twice while reading the configuration file.
The full name of the parameter (i.e. section + name) cannot include any of the following characters:
\
) "</tt> and ‘’‘)
\li comment (’;', '#', '/')
\li assignment operators ('=', ':', '?', '+')
\note
The \p section and \p name parameters have underscores (<tt>_</tt>)
replaced with dashes (<tt>-</tt>) before getting used. The very first
character can be a dash. This allows you to therefore create
a form of internal parameters; i.e. parameters which cannot
appear in a configuration file, an environment variable or on
the command line (where parameter are not allowed to start with
a dash).
\warning
It is important to note that when a \p name includes a C++ scope
operator, the final parameter name looks like it includes a section
name (i.e. the name "a::b", when the C++ section flag is not set,
is accepted as is; so the final parameter name is going to be "a::b" and therefore it will include what looks like a section name.) There should not be any concern about this small glitch though since you do not have to accept any such parameter.
The section/name combo should be dealt with inside this function instead of outside, especially if we are to support all the namespace operators.
[in] | section | The list of sections or an empty string. |
[in] | name | The name of the parameter. |
[in] | value | The value of the parameter. |
[in] | a | The operator used to set this parameter. |
[in] | comment | The comment appearing before value. |
Definition at line 1445 of file conf_file.cpp.
References advgetopt::ASSIGNMENT_APPEND, advgetopt::ASSIGNMENT_NEW, advgetopt::ASSIGNMENT_NONE, advgetopt::ASSIGNMENT_OPTIONAL, advgetopt::ASSIGNMENT_SET, advgetopt::created, f_line, f_modified, f_parameters, f_reading, f_sections, f_setup, advgetopt::conf_file_setup::get_filename(), advgetopt::get_global_mutex(), advgetopt::conf_file_setup::get_section_operator(), advgetopt::conf_file_setup::get_section_to_ignore(), advgetopt::SECTION_OPERATOR_C, advgetopt::SECTION_OPERATOR_CPP, advgetopt::SECTION_OPERATOR_NONE, advgetopt::SECTION_OPERATOR_ONE_SECTION, advgetopt::updated, and value_changed().
Referenced by read_configuration().
void advgetopt::conf_file::set_variables | ( | variables::pointer_t | variables | ) |
The get_parameter() of the configuration file can be transformed to apply user variables to the values of the parameters.
By default, this is not used by the getopt since it loads the values in its tables which then apply the variable when the get_value() is called on the getopt_info objects.
[in] | variables | The variable to attach to this configuration file. |
Definition at line 1233 of file conf_file.cpp.
References f_variables.
This function is used whenever we read one additional character to know whether a certain character followed another. For example, we check for a ‘’\n'whenever we find a
'\r'. However, if the character right after the
'\r'is not a
'\n'` we call this ungetc() function so next time we can re-read that same character.
[in] | c | The character to restore. |
Definition at line 1861 of file conf_file.cpp.
References f_unget_char.
Referenced by get_line().
|
private |
This function is called on a change of the internal values.
The function is used to call the callbacks that were added to this option_info object. The function first copies the existing list of callbacks so you can safely update the list from within a callback.
Definition at line 1151 of file conf_file.cpp.
References f_callbacks, and advgetopt::get_global_mutex().
Referenced by erase_parameter(), and set_parameter().
bool advgetopt::conf_file::was_modified | ( | ) | const |
This function returns the value of the f_modified flag which is true if any value was createed, updated, or erased from the configuration file since after it was loaded.
This tells you whether you should call the save() function, assuming you want to keep such changes.
Definition at line 1801 of file conf_file.cpp.
References f_modified.
|
private |
Definition at line 279 of file conf_file.h.
Referenced by add_callback(), remove_callback(), and value_changed().
|
private |
Definition at line 271 of file conf_file.h.
Referenced by get_errno(), read_configuration(), and save_configuration().
Definition at line 273 of file conf_file.h.
Referenced by exists(), and read_configuration().
|
private |
Definition at line 270 of file conf_file.h.
Referenced by get_line(), read_configuration(), and set_parameter().
Definition at line 275 of file conf_file.h.
Referenced by erase_parameter(), save_configuration(), set_parameter(), and was_modified().
|
private |
Definition at line 280 of file conf_file.h.
Referenced by add_callback().
|
private |
Definition at line 278 of file conf_file.h.
Referenced by erase_all_parameters(), erase_parameter(), get_parameter(), get_parameters(), has_parameter(), save_configuration(), and set_parameter().
Definition at line 272 of file conf_file.h.
Referenced by erase_parameter(), read_configuration(), and set_parameter().
|
private |
Definition at line 276 of file conf_file.h.
Referenced by get_sections(), section_to_variables(), and set_parameter().
|
private |
Definition at line 267 of file conf_file.h.
Referenced by get_line(), get_setup(), is_assignment_operator(), is_comment(), read_configuration(), save_configuration(), and set_parameter().
|
private |
Definition at line 269 of file conf_file.h.
|
private |
Definition at line 277 of file conf_file.h.
Referenced by get_parameter(), get_variables(), and set_variables().
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.