This is a class designed to read and write INI configuration files.
I can't promise that the specification will end up being followed to the letter, but I'll try. At this time, though the order of keys and sections will remain the same, the whitespace, comment, and line structure of an externally created ini file may not remain in tact if that file is updated and rewritten using this include.
This constructor just takes an optional filename so you can load an INI file directly on object creation if you want. Note though that doing it this way makes it more difficult to instantly tell if there was a problem do to the lack of a return value, so you must then evaluate ini::get_error_line() == 0 to verify a successful load.
ini(string filename = "");
Deletes all keys from the given section without deleting the section itself.
bool ini::clear_section(string section);
bool: true if the section was successfully cleared, false if it doesn't exist.
Creates a new INI section with the given name.
bool ini::create_section(string section_name);
bool: true if the section was successfully created, false otherwise.
Delete a key from a given section.
bool ini::delete_key(string section, string key);
bool: true if the key was successfully deleted, false and sets an error if the key you want to delete doesn't exist or if the key name is invalid.
Delete the given section.
bool ini::delete_section(string section);
bool: true if the section was successfully deleted, false otherwise.
Dump all loaded data into a string, such as what's used by the save function, or so that you can encrypt it, pack it or such things.
string ini::dump(bool indent = false);
string: the entire INI data as a string.
Fetch a boolean value from the INI data given a section and key.
bool ini::get_bool(string section, string key, bool default_value = false);
bool: the value at the particular key if found, the default value if not.
All getters will use this format, and if one returns a default value (blank string, an int that equals 0, a boolean that equals false etc), and if you want to know whether the key actually existed, use the error checking system.
Fetch a double from the INI data given a section and key.
double ini::get_double(string section, string key, double default_value = 0.0);
double: the value at the particular key if found, the default value if not.
All getters will use this format, and if one returns a default value (blank string, an int that equals 0, a boolean that equals false etc), and if you want to know whether the key actually existed, use the error checking system.
Return the line the last error took place on if applicable. This does not clear the error information, since one may wish to get the line number and the text which are in 2 different functions. So make sure to call this function before ini::get_error_text() if the line number is something you're interested in retrieving.
int ini::get_error_line();
int: the line number of the last error, if any. A return value of -1 means that this error is not associated with a line number, and 0 means there is no error in the first place.
Returns the last error message, almost always used if an INI file fails to load and you want to know why. This function also clears the error, so to figure out the line, call ini::get_error_line() before calling this.
string ini::get_error_text();
string: the last error message, if any.
Fetch a string from the INI data given a section and key.
string ini::get_string(string section, string key, string default_value = "");
string: the value at the particular key if found, the default value if not.
All getters will use this format, and if one returns a default value (blank string, an int that equals 0, a boolean that equals false etc), and if you want to know whether the key actually existed, use the error checking system.
Determine if the INI object has no data in it.
bool ini::is_empty();
bool: true if there is no data loaded into this ini object, false otherwise.
Determine if a particular key exists in the INI data.
bool ini::key_exists(string section, string key);
bool: true if the specified key exists, false otherwise.
An error will be accessible from the error system if the given section doesn't exist.
List all key names in a given section.
string[]@ ini::list_keys(string section);
string[]@: a handle to an array containing all the keys. An empty array means that the section is either blank or doesn't exist, the latter being able to be checked with the error system.
List all section names that exist.
string[]@ list_sections(bool include_blank_section = false);
string[]@: a handle to an array containing all the key names.
Returns all section names containing a wildcard identifier. This way if searching through a file containing many normal sections and a few wildcard sections, it is possible to query only the wildcards for faster lookup.
string[]@ ini::list_wildcard_sections();
string[]@: a handle to an array containing all the wildcard sections.
Load an INI file.
bool ini::load(string filename, bool robust = true);
bool: true if the ini data was successfully loaded, false otherwise.
This function loads INI data stored as a string, doing it this way insures that ini data can come from any source, such as an encrypted string if need be.
bool ini::load_string(string data, string filename = "*");
bool: true if the data was successfully loaded, false otherwise.
Input data is expected to have CRLF line endings.
Resets all variables to default. You can call this yourself and it is also called by loading functions to clear data from partially successful loads upon error.
`void ini::reset(bool make_blank_section = true);
ini::load_string() function creates that section itself.Save everything currently loaded to a file.
bool ini::save(string filename, bool indent = false);
bool: true if the data was successfully saved, false otherwise.
This function is similar to ini::save(), but it first performs a temporary backup of any existing data, then restores that backup if the saving fails. This is slower and should only be used when necessary, but should insure 0 data loss.
bool ini::save_robust(string filename, bool indent = false);
bool: true if the data was successfully saved, false otherwise.
Determine if a particular section exists in the INI data.
bool ini::section_exists(string section);`
bool: true if the section exists, false if not.
Set a boolean value in the INI data given a section name, a key and a value.
bool ini::set_bool(string section, string key, bool value);
bool: true if the value was successfully written, false otherwise.
All of the other setters use this format. If the key exists already, the value, of course, will be updated.
Set a double in the INI data given a section name, a key and a value.
bool ini::set_double(string section, string key, double value);
bool: true if the double was successfully written, false otherwise.
All of the other setters use this format. If the key exists already, the value, of course, will be updated.
Set a string in the INI data given a section name, a key and a value.
bool ini::set_string(string section, string key, string value);
bool: true if the string was successfully written, false otherwise.
All of the other setters use this format. If the key exists already, the value, of course, will be updated.
Contains the filename of the currently loaded INI data.
string loaded_filename;