123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- Config Files
- ============
- The configuration file functions are a simple implementation of the INI
- file format, with the addition of default values.
- .. code:: cpp
- #include <util/config-file.h>
- .. type:: config_t
- Config File Functions
- ---------------------
- .. function:: config_t *config_create(const char *file)
- Creates a new configuration object and associates it with the
- specified file name.
- :param file: Path to the new configuration file
- :return: A new configuration file object
- ----------------------
- .. function:: int config_open(config_t **config, const char *file, enum config_open_type open_type)
- Opens a configuration file.
- :param config: Pointer that receives a pointer to a new configuration
- file object (if successful)
- :param file: Path to the configuration file
- :param open_type: Can be one of the following values:
- - CONFIG_OPEN_EXISTING - Fail if the file doesn't
- exist.
- - CONFIG_OPEN_ALWAYS - Try to open the file. If
- the file doesn't exist, create it.
- :return: Can return the following values:
- - CONFIG_SUCCESS - Successful
- - CONFIG_FILENOTFOUND - File not found
- - CONFIG_ERROR - Generic error
- ----------------------
- .. function:: int config_open_string(config_t **config, const char *str)
- Opens configuration data via a string rather than a file.
- :param config: Pointer that receives a pointer to a new configuration
- file object (if successful)
- :param str: Configuration string
- :return: Can return the following values:
- - CONFIG_SUCCESS - Successful
- - CONFIG_FILENOTFOUND - File not found
- - CONFIG_ERROR - Generic error
- ----------------------
- .. function:: int config_save(config_t *config)
- Saves configuration data to a file (if associated with a file).
- :param config: Configuration object
- :return: Can return the following values:
- - CONFIG_SUCCESS - Successful
- - CONFIG_FILENOTFOUND - File not found
- - CONFIG_ERROR - Generic error
- ----------------------
- .. function:: int config_save_safe(config_t *config, const char *temp_ext, const char *backup_ext)
- Saves configuration data and minimizes overwrite corruption risk.
- Saves the file with the file name
- :param config: Configuration object
- :param temp_ext: Temporary extension for the new file
- :param backup_ext: Backup extension for the old file. Can be *NULL*
- if no backup is desired.
- :return: Can return the following values:
- - CONFIG_SUCCESS - Successful
- - CONFIG_FILENOTFOUND - File not found
- - CONFIG_ERROR - Generic error
- ----------------------
- .. function:: void config_close(config_t *config)
- Closes the configuration object.
- :param config: Configuration object
- ----------------------
- .. function:: size_t config_num_sections(config_t *config)
- Returns the number of sections.
- :param config: Configuration object
- :return: Number of configuration sections
- ----------------------
- .. function:: const char *config_get_section(config_t *config, size_t idx)
- Returns a section name based upon its index.
- :param config: Configuration object
- :param idx: Index of the section
- :return: The section's name
- Set/Get Functions
- -----------------
- .. function:: void config_set_string(config_t *config, const char *section, const char *name, const char *value)
- Sets a string value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The string value
- ----------------------
- .. function:: void config_set_int(config_t *config, const char *section, const char *name, int64_t value)
- Sets an integer value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The integer value
- ----------------------
- .. function:: void config_set_uint(config_t *config, const char *section, const char *name, uint64_t value)
- Sets an unsigned integer value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The unsigned integer value
- ----------------------
- .. function:: void config_set_bool(config_t *config, const char *section, const char *name, bool value)
- Sets a boolean value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The boolean value
- ----------------------
- .. function:: void config_set_double(config_t *config, const char *section, const char *name, double value)
- Sets a floating point value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The floating point value
- ----------------------
- .. function:: const char *config_get_string(config_t *config, const char *section, const char *name)
- Gets a string value. If the value is not set, it will use the
- default value. If there is no default value, it will return *NULL*.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The string value
- ----------------------
- .. function:: int64_t config_get_int(config_t *config, const char *section, const char *name)
- Gets an integer value. If the value is not set, it will use the
- default value. If there is no default value, it will return 0.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The integer value
- ----------------------
- .. function:: uint64_t config_get_uint(config_t *config, const char *section, const char *name)
- Gets an unsigned integer value. If the value is not set, it will use
- the default value. If there is no default value, it will return 0.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The unsigned integer value
- ----------------------
- .. function:: bool config_get_bool(config_t *config, const char *section, const char *name)
- Gets a boolean value. If the value is not set, it will use the
- default value. If there is no default value, it will return false.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The boolean value
- ----------------------
- .. function:: double config_get_double(config_t *config, const char *section, const char *name)
- Gets a floating point value. If the value is not set, it will use
- the default value. If there is no default value, it will return 0.0.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The floating point value
- ----------------------
- .. function:: bool config_remove_value(config_t *config, const char *section, const char *name)
- Removes a value. Does not remove the default value if any.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- Default Value Functions
- -----------------------
- .. function:: int config_open_defaults(config_t *config, const char *file)
- Opens a file and uses it for default values.
- :param config: Configuration object
- :param file: The file to open for default values
- ----------------------
- .. function:: void config_set_default_string(config_t *config, const char *section, const char *name, const char *value)
- Sets a default string value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The string value
- ----------------------
- .. function:: void config_set_default_int(config_t *config, const char *section, const char *name, int64_t value)
- Sets a default integer value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The integer value
- ----------------------
- .. function:: void config_set_default_uint(config_t *config, const char *section, const char *name, uint64_t value)
- Sets a default unsigned integer value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The unsigned integer value
- ----------------------
- .. function:: void config_set_default_bool(config_t *config, const char *section, const char *name, bool value)
- Sets a default boolean value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The boolean value
- ----------------------
- .. function:: void config_set_default_double(config_t *config, const char *section, const char *name, double value)
- Sets a default floating point value.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :param value: The floating point value
- ----------------------
- .. function:: const char *config_get_default_string(config_t *config, const char *section, const char *name)
- Gets a default string value. If there is no default value, it will return *NULL*.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The default string value
- ----------------------
- .. function:: int64_t config_get_default_int(config_t *config, const char *section, const char *name)
- Gets a default integer value. If there is no default value, it will return 0.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The integer value
- ----------------------
- .. function:: uint64_t config_get_default_uint(config_t *config, const char *section, const char *name)
- Gets a default unsigned integer value. If there is no default value, it will return 0.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The unsigned integer value
- ----------------------
- .. function:: bool config_get_default_bool(config_t *config, const char *section, const char *name)
- Gets a default boolean value. If there is no default value, it will return false.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The boolean value
- ----------------------
- .. function:: double config_get_default_double(config_t *config, const char *section, const char *name)
- Gets a default floating point value. If there is no default value, it will return 0.0.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: The floating point value
- -----------------------
- .. function:: bool config_has_user_value(config_t *config, const char *section, const char *name)
- Returns whether a value is user-set (true) or default/none (false).
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: Whether a user value exists
- -----------------------
- .. function:: bool config_has_default_value(config_t *config, const char *section, const char *name)
- Returns whether a value has a default set.
- :param config: Configuration object
- :param section: The section of the value
- :param name: The value name
- :return: Whether a user value exists
|