VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
G::MapFile Class Reference

A class for reading and editing key=value files, supporting the creation of backup files, variable expansion, character-set encoding and logging. More...

#include <gmapfile.h>

Public Member Functions

 MapFile ()
 Constructor for an empty map.
 
 MapFile (const G::StringMap &map)
 Constructor that reads from a string map.
 
 MapFile (const OptionMap &map, const std::string &yes=std::string())
 Constructor that reads from an option value map, typically parsed out from a command-line. More...
 
 MapFile (const G::Path &, bool utf8=false)
 Constructor that reads from a file.
 
 MapFile (std::istream &, bool utf8=false)
 Constructor that reads from a stream.
 
const G::StringArraykeys () const
 Returns a reference to the ordered list of keys.
 
void add (const std::string &key, const std::string &value)
 Adds or updates a single item in the map.
 
void writeItem (std::ostream &, const std::string &key, bool utf8=false) const
 Writes a single item from this map to the stream.
 
void editInto (const G::Path &path, bool make_backup=true, bool allow_read_error=false, bool allow_write_error=false, bool utf8=false) const
 Edits an existing file so that its contents reflect this map.
 
bool contains (const std::string &key) const
 Returns true if the map contains the given key.
 
G::Path pathValue (const std::string &key) const
 Returns a mandatory path value from the map. Throws if it does not exist.
 
G::Path pathValue (const std::string &key, const G::Path &default_) const
 Returns a path value from the map.
 
G::Path expandedPathValue (const std::string &key) const
 Returns a mandatory path value from the map with expand(). Throws if it does not exist.
 
G::Path expandedPathValue (const std::string &key, const G::Path &default_) const
 Returns a path value from the map with expand().
 
unsigned int numericValue (const std::string &key, unsigned int default_) const
 Returns a numeric value from the map.
 
std::string value (const std::string &key, const std::string &default_=std::string()) const
 Returns a string value from the map.
 
std::string value (const std::string &key, const char *default_) const
 Returns a string value from the map.
 
bool booleanValue (const std::string &key, bool default_) const
 Returns a boolean value from the map.
 
void remove (const std::string &key)
 Removes a value (if it exists).
 
const G::StringMapmap () const
 Returns a reference to the internal map.
 
void log () const
 Logs the contents.
 
std::string expand (const std::string &value) const
 Does one-pass variable substitution for the given string. More...
 

Static Public Member Functions

static void check (const G::Path &, bool utf8=false)
 Throws if the file is invalid. More...
 
static void writeItem (std::ostream &, const std::string &key, const std::string &value, bool utf8=false)
 Writes an arbitrary item to the stream.
 

Detailed Description

A class for reading and editing key=value files, supporting the creation of backup files, variable expansion, character-set encoding and logging.

See Also
G::OptionValue

Definition at line 43 of file gmapfile.h.

Constructor & Destructor Documentation

G::MapFile::MapFile ( const OptionMap map,
const std::string &  yes = std::string() 
)
explicit

Constructor that reads from an option value map, typically parsed out from a command-line.

Unvalued 'true' options in the option value map are loaded into this mapfile object with a value given by the 'yes' parameter, whereas unvalued 'false' options are not loaded. Multi-valued options are loaded as a comma-separated list.

Definition at line 65 of file gmapfile.cpp.

Member Function Documentation

void G::MapFile::check ( const G::Path path,
bool  utf8 = false 
)
static

Throws if the file is invalid.

This is equivalent to constructing a temporary MapFile object, but it specifically does not do any logging.

Definition at line 129 of file gmapfile.cpp.

std::string G::MapFile::expand ( const std::string &  value) const

Does one-pass variable substitution for the given string.

Sub-strings like "%xyz%" are replaced by 'value("xyz")' and "%%" is replaced by "%". If there are no appropriate value in the map then the sub-string is left alone (so "%xyz%" remains as "%xyz%" if there is no "xyz" map item).

Definition at line 321 of file gmapfile.cpp.


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