feisty meow concerns codebase  2.140
configuration::ini_parser Class Reference

Parses strings in the fairly well-known INI file format. More...

#include <ini_parser.h>

Inheritance diagram for configuration::ini_parser:
Collaboration diagram for configuration::ini_parser:

Public Member Functions

 ini_parser (const basis::astring &to_parse, treatment_of_defaults behavior=RETURN_ONLY)
 constructs an ini_parser by parsing entries out of "to_parse". More...
 
 ~ini_parser ()
 
void reset (const basis::astring &to_parse)
 drops any existing information and processes the string "to_parse". More...
 
void add (const basis::astring &to_parse)
 merges items parsed from "to_parse" into the current set. More...
 
bool well_formed () const
 returns true if the ini file's contents were in the format expected. More...
 
bool restate (basis::astring &new_ini, bool add_spaces=false)
 stores a cleaned version of the internal state into "new_ini". More...
 
void merge_section (const basis::astring &section_name, const structures::string_table &to_merge)
 merges the table "to_merge" into the "section_name". More...
 
- Public Member Functions inherited from configuration::table_configurator
 table_configurator (treatment_of_defaults behavior=AUTO_STORE)
 Constructor just needs to know what to do for missing items. More...
 
 table_configurator (const table_configurator &to_copy)
 
virtual ~table_configurator ()
 
table_configuratoroperator= (const table_configurator &to_copy)
 
 DEFINE_CLASS_NAME ("table_configurator")
 
virtual void sections (structures::string_array &list)
 retrieves the section names into "list". More...
 
void reset ()
 
virtual bool get (const basis::astring &section, const basis::astring &entry, basis::astring &found)
 implements the configurator retrieval function. More...
 
virtual bool put (const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)
 implements the configurator storage function. More...
 
virtual bool section_exists (const basis::astring &section)
 true if the "section" is presently in the table config. More...
 
virtual bool delete_section (const basis::astring &section)
 removes the entire "section" specified. More...
 
virtual bool delete_entry (const basis::astring &section, const basis::astring &entry)
 removes the entry specified by the "section" and "entry" name. More...
 
virtual bool get_section (const basis::astring &section, structures::string_table &info)
 reads the entire table held under "section" into a table called "info". More...
 
virtual bool put_section (const basis::astring &section, const structures::string_table &info)
 writes a table called "info" into the "section" held here. More...
 
- Public Member Functions inherited from configuration::configurator
 configurator (treatment_of_defaults behavior=RETURN_ONLY)
 
virtual ~configurator ()
 
treatment_of_defaults behavior () const
 observes the behavior chosen for the load() function. More...
 
void behavior (treatment_of_defaults new_behavior)
 modifies the behavior of the load() function. More...
 
bool store (const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)
 a synonym for put. More...
 
basis::astring load (const basis::astring &section, const basis::astring &entry, const basis::astring &default_value)
 a synonym for get that implements the auto-store behavior. More...
 
bool store (const basis::astring &section, const basis::astring &entry, int value)
 stores an integer value from the configuration store. More...
 
int load (const basis::astring &section, const basis::astring &entry, int def_value)
 loads an integer value from the configuration store. More...
 
void section_set (structures::string_set &list)
 similar to above, but stores section names into a set. More...
 
virtual bool delete_entry (const basis::astring &formal(section), const basis::astring &formal(entry))
 eliminates the entry specified by the "section" and "entry" name. More...
 
virtual bool delete_section (const basis::astring &formal(section))
 whacks the entire "section" specified. More...
 
virtual bool get_section (const basis::astring &formal(section), structures::string_table &formal(found))
 retrieves an entire "section", if supported by the derived object. More...
 
virtual bool put_section (const basis::astring &formal(section), const structures::string_table &formal(to_store))
 stores an entire "section" from the table in "to_store", if supported. More...
 

Additional Inherited Members

- Public Types inherited from configuration::configurator
enum  treatment_of_defaults { AUTO_STORE , RETURN_ONLY }
 

Detailed Description

Parses strings in the fairly well-known INI file format.

Description of INI file format:

The format expected in this parser for initialization files allows for three types of entries. These are section headers, comments and value definitions. Section headers define the start of a list of value definitions. A section header is a name in brackets, like [startup]. A comment is a string of text that will be ignored. Comments are preceded by an octothorpe ('#') or a semicolon (';'). The parser will keep comments roughly in the same places they were found in the string that was parsed. A comment is allowed to follow a section header on the same line. Value definitions are a pair of strings separated by an equality operator ('='). The left side of the value definition is referred to here as the variable's name while the right side is referred to as the variable's value. Note that any line which is not a comment or a section header is considered implicitly to be a value definition, even if it does not contain an equals operator. This is required for parsing certain INI files that don't follow the established format. Such lines will have an empty string for their value. White space (either tab characters or space characters) are allowed before and after any of these constructs. Spaces may also exist before and after the equals operator of a value definition, but once the value starts, any white space is considered part of the value. Trailing white space is not considered part of a variable name, but white space between the characters before the equals operator is signficant. Any number of carriage returns can separate the lines of the INI file.

Here is an example of a valid INI file:

# Initialization file for Frootnik Corp.
[common] ; all of our programs use these.
magnification=1
text_color=puce
font=atavata
;;; see font/color document in readme.txt.
[sourceburger] ; sourceburger application specific settings
crashnow = 0
crashlater = 1
get clue=0
windowtitle = Source Burger 5000
danger will robinson
virtual bool get(const basis::astring &section, const basis::astring &entry, basis::astring &found)
implements the configurator retrieval function.
Implements an application lock to ensure only one is running at once.

Definition at line 73 of file ini_parser.h.

Constructor & Destructor Documentation

◆ ini_parser()

configuration::ini_parser::ini_parser ( const basis::astring to_parse,
treatment_of_defaults  behavior = RETURN_ONLY 
)

constructs an ini_parser by parsing entries out of "to_parse".

after construction, the success of parsing can be checked using well_formed().

Definition at line 51 of file ini_parser.cpp.

References configuration::table_configurator::reset().

◆ ~ini_parser()

configuration::ini_parser::~ini_parser ( )

Definition at line 59 of file ini_parser.cpp.

References basis::WHACK().

Member Function Documentation

◆ add()

void configuration::ini_parser::add ( const basis::astring to_parse)

merges items parsed from "to_parse" into the current set.

processes the string "to_parse" as in the reset() method but adds any new sections found to our configuration. if sections are found with the same names, then the values found in "to_parse" override the ones already listed.

Definition at line 118 of file ini_parser.cpp.

References is_eol(), basis::astring::length(), LOG, merge_section(), configuration::variable_tokenizer::parse(), basis::astring::reset(), configuration::variable_tokenizer::set_comment_chars(), basis::astring::substring(), configuration::variable_tokenizer::table(), configuration::variable_tokenizer::text_form(), and basis::astring::zap().

Referenced by reset().

◆ merge_section()

void configuration::ini_parser::merge_section ( const basis::astring section_name,
const structures::string_table to_merge 
)

merges the table "to_merge" into the "section_name".

any new values from "to_merge" that are not found in the section with "section_name" in "this" object are added and any existing values will be replaced.

Definition at line 162 of file ini_parser.cpp.

References structures::symbol_table< contents >::name(), configuration::table_configurator::put(), configuration::table_configurator::put_section(), configuration::table_configurator::section_exists(), and structures::symbol_table< contents >::symbols().

Referenced by add().

◆ reset()

void configuration::ini_parser::reset ( const basis::astring to_parse)

drops any existing information and processes the string "to_parse".

Definition at line 110 of file ini_parser.cpp.

References add(), basis::astring::reset(), and configuration::table_configurator::reset().

◆ restate()

bool configuration::ini_parser::restate ( basis::astring new_ini,
bool  add_spaces = false 
)

stores a cleaned version of the internal state into "new_ini".

if "add_spaces" is true, then the entries will be in the form of 'x = y' rather than 'x=y'.

Definition at line 220 of file ini_parser.cpp.

References structures::string_table::add_spaces(), configuration::table_configurator::get_section(), basis::array< contents >::length(), configuration::table_configurator::sections(), and structures::string_table::text_form().

◆ well_formed()

bool configuration::ini_parser::well_formed ( ) const
inline

returns true if the ini file's contents were in the format expected.

Definition at line 94 of file ini_parser.h.


The documentation for this class was generated from the following files: