PGE API 0.4
PR00F's Game Engine full documentation
Loading...
Searching...
No Matches
PGEcfgFile Class Reference

PR00F's Game Engine configuration file handler base class. More...

Detailed Description

PR00F's Game Engine configuration file handler base class.

This class can load configuration variable data from simple ascii text files and save to file as well. Further logic is added by derived classes.

Definition at line 29 of file PGEcfgFile.h.

#include <PGEcfgFile.h>

+ Inheritance diagram for PGEcfgFile:

Public Member Functions

 PGEcfgFile (bool bRequireAllAcceptedVarsDefineRequirement, bool bCaseSensitiveVars)
 
virtual ~PGEcfgFile ()
 
CConsole & getConsole () const
 Returns access to console preset with logger module name as this class.
 
std::map< std::string, PGEcfgVariable > & getVars ()
 
const std::map< std::string, PGEcfgVariable > & getVars () const
 
bool load (const char *fname)
 Loads variables from the given config file.
 
bool save (const char *fname="") const
 Saves variables to the given config file.
 
const std::string & getFilename () const
 
const std::string & getPathToFile () const
 
bool getAllAcceptedVarsDefineRequirement () const
 
bool getCaseSensitiveVars () const
 
const std::set< std::string > & getAcceptedVars () const
 
bool setAcceptedVars (const std::set< std::string > &newAcceptedVars)
 
std::vector< std::string > & getTemplate ()
 Returns the lines of the template generated from the loaded config file.
 
const std::vector< std::string > & getTemplate () const
 Returns the lines of the template generated from the loaded config file.
 
 PGEcfgFile (const PGEcfgFile &other)
 
PGEcfgFileoperator= (const PGEcfgFile &other)
 

Static Public Member Functions

static const char * getLoggerModuleName ()
 Returns the logger module name of this class.
 

Protected Member Functions

virtual bool validateOnLoad (std::ifstream &) const
 Validate the file being processed by load().
 
virtual bool validateOnSave (std::ofstream &) const
 Validate the file being processed by save().
 

Static Protected Member Functions

static bool lineIsValueAssignment (const std::string &sTrimmedLine, bool bCaseSensitiveVars, std::string &sVar, std::string &sValue, bool &bParseError)
 

Protected Attributes

std::set< std::string > m_acceptedVars
 
std::map< std::string, PGEcfgVariablem_vars
 

Private Member Functions

void lineHandleAssignment (const std::string &sVar, const std::string &sValue, const char *fname, std::set< std::string > &m_missingVars, bool &bParseError)
 

Static Private Member Functions

static bool lineIsComment (const std::string &sTrimmedLine)
 
static bool lineShouldBeIgnored (const std::string &sTrimmedLine)
 

Private Attributes

bool m_bRequireAllAcceptedVarsDefineRequirement
 
bool m_bCaseSensitiveVars
 
std::string m_sFilename
 
std::string m_sPathToFile
 
std::vector< std::string > m_vTemplateLines
 

Constructor & Destructor Documentation

◆ PGEcfgFile() [1/2]

PGEcfgFile::PGEcfgFile ( bool bRequireAllAcceptedVarsDefineRequirement,
bool bCaseSensitiveVars )
Parameters
bRequireAllAcceptedVarsDefineRequirementIf true, all variables that are specified as accepted variables must be defined in the config file.
bCaseSensitiveVarsIf true, all variables will be treated case sensitive, e.g. "Name =" will be different to "name =" or "NAME =". Note: it is your responsibility to define all accepted variables in their lowercafe form if you set case sensitivity to false!

Definition at line 27 of file PGEcfgFile.cpp.

◆ ~PGEcfgFile()

PGEcfgFile::~PGEcfgFile ( )
virtual

Definition at line 35 of file PGEcfgFile.cpp.

◆ PGEcfgFile() [2/2]

PGEcfgFile::PGEcfgFile ( const PGEcfgFile & other)
inline

Definition at line 65 of file PGEcfgFile.h.

Member Function Documentation

◆ getAcceptedVars()

const std::set< std::string > & PGEcfgFile::getAcceptedVars ( ) const

Definition at line 324 of file PGEcfgFile.cpp.

◆ getAllAcceptedVarsDefineRequirement()

bool PGEcfgFile::getAllAcceptedVarsDefineRequirement ( ) const

Definition at line 314 of file PGEcfgFile.cpp.

◆ getCaseSensitiveVars()

bool PGEcfgFile::getCaseSensitiveVars ( ) const

Definition at line 319 of file PGEcfgFile.cpp.

◆ getConsole()

CConsole & PGEcfgFile::getConsole ( ) const

Returns access to console preset with logger module name as this class.

Definition at line 39 of file PGEcfgFile.cpp.

◆ getFilename()

const std::string & PGEcfgFile::getFilename ( ) const

Definition at line 304 of file PGEcfgFile.cpp.

◆ getLoggerModuleName()

const char * PGEcfgFile::getLoggerModuleName ( )
static

Returns the logger module name of this class.

Definition at line 44 of file PGEcfgFile.cpp.

◆ getPathToFile()

const std::string & PGEcfgFile::getPathToFile ( ) const

Definition at line 309 of file PGEcfgFile.cpp.

◆ getTemplate() [1/2]

std::vector< std::string > & PGEcfgFile::getTemplate ( )

Returns the lines of the template generated from the loaded config file.

This template is built up automatically in case of a successful load() but you can also modify it if needed. This template is used when writing the configuration back to file: it defines which CVARs in what order should be written to the file, and also the comments unrelated to CVARS.

Returns
The config file template.

Definition at line 350 of file PGEcfgFile.cpp.

◆ getTemplate() [2/2]

const std::vector< std::string > & PGEcfgFile::getTemplate ( ) const

Returns the lines of the template generated from the loaded config file.

This template is built up automatically in case of a successful load() but you can also modify it if needed. This template is used when writing the configuration back to file: it defines which CVARs in what order should be written to the file, and also the comments unrelated to CVARS.

Returns
The config file template.

Definition at line 364 of file PGEcfgFile.cpp.

◆ getVars() [1/2]

std::map< std::string, PGEcfgVariable > & PGEcfgFile::getVars ( )

Definition at line 49 of file PGEcfgFile.cpp.

◆ getVars() [2/2]

const std::map< std::string, PGEcfgVariable > & PGEcfgFile::getVars ( ) const

Definition at line 54 of file PGEcfgFile.cpp.

◆ lineHandleAssignment()

void PGEcfgFile::lineHandleAssignment ( const std::string & sVar,
const std::string & sValue,
const char * fname,
std::set< std::string > & m_missingVars,
bool & bParseError )
private

Definition at line 486 of file PGEcfgFile.cpp.

◆ lineIsComment()

bool PGEcfgFile::lineIsComment ( const std::string & sTrimmedLine)
staticprivate

Definition at line 476 of file PGEcfgFile.cpp.

◆ lineIsValueAssignment()

bool PGEcfgFile::lineIsValueAssignment ( const std::string & sTrimmedLine,
bool bCaseSensitiveVars,
std::string & sVar,
std::string & sValue,
bool & bParseError )
staticprotected

Definition at line 373 of file PGEcfgFile.cpp.

◆ lineShouldBeIgnored()

bool PGEcfgFile::lineShouldBeIgnored ( const std::string & sTrimmedLine)
staticprivate

Definition at line 481 of file PGEcfgFile.cpp.

◆ load()

bool PGEcfgFile::load ( const char * fname)

Loads variables from the given config file.

Remember: CVAR names are automatically converted to lowercase if you did not request case-sensitive variables in the constructor. It also fills the template vector of strings which can be accessed by getTemplate().

Returns
True on success, false otherwise.

Definition at line 66 of file PGEcfgFile.cpp.

◆ operator=()

PGEcfgFile & PGEcfgFile::operator= ( const PGEcfgFile & other)
inline

Definition at line 73 of file PGEcfgFile.h.

◆ save()

bool PGEcfgFile::save ( const char * fname = "") const

Saves variables to the given config file.

It uses the template vector of strings which can be accessed by getTemplate().

Parameters
fnameThe filename of the file to write to. If left empty, getFilename() is used to determine the file name.
Returns
True on success, false otherwise.

Definition at line 196 of file PGEcfgFile.cpp.

◆ setAcceptedVars()

bool PGEcfgFile::setAcceptedVars ( const std::set< std::string > & newAcceptedVars)

Definition at line 329 of file PGEcfgFile.cpp.

◆ validateOnLoad()

bool PGEcfgFile::validateOnLoad ( std::ifstream & ) const
protectedvirtual

Validate the file being processed by load().

Derived class can override this to provide a validating function, e.g. special way to decide if the file being loaded actually has valid format. This is invoked by load() before trying to parse the file. The stream must be already opened prior to calling this function. This function must not open/close the given file stream. Upon success, load() will continue reading the file from the point until this function has read the file.

Returns
False to indicate that load() should not process the file and fail. True if load() should continue processing the file.

Reimplemented in PGEcfgProfiles.

Definition at line 450 of file PGEcfgFile.cpp.

◆ validateOnSave()

bool PGEcfgFile::validateOnSave ( std::ofstream & ) const
protectedvirtual

Validate the file being processed by save().

Derived class can override this to provide a validating function which makes the file compatible with the optionally overridden validateOnLoad() function. For example, if validateOnLoad() checks for a special header, then validateOnSave() is responsible for writing that special header into the file. This is invoked by save() when trying to write the file. The stream must be already opened prior to calling this function. This function must not open/close the given file stream. Upon success, save() will continue writing the file from the point this function has written the file.

Returns
False to indicate that save() should not continue writing the file and fail. True if save() should continue writing the file.

Reimplemented in PGEcfgProfiles.

Definition at line 467 of file PGEcfgFile.cpp.

Member Data Documentation

◆ m_acceptedVars

std::set<std::string> PGEcfgFile::m_acceptedVars
protected

Definition at line 85 of file PGEcfgFile.h.

◆ m_bCaseSensitiveVars

bool PGEcfgFile::m_bCaseSensitiveVars
private

Definition at line 97 of file PGEcfgFile.h.

◆ m_bRequireAllAcceptedVarsDefineRequirement

bool PGEcfgFile::m_bRequireAllAcceptedVarsDefineRequirement
private

Definition at line 96 of file PGEcfgFile.h.

◆ m_sFilename

std::string PGEcfgFile::m_sFilename
private

Definition at line 99 of file PGEcfgFile.h.

◆ m_sPathToFile

std::string PGEcfgFile::m_sPathToFile
private

Definition at line 100 of file PGEcfgFile.h.

◆ m_vars

std::map<std::string, PGEcfgVariable> PGEcfgFile::m_vars
protected

Definition at line 86 of file PGEcfgFile.h.

◆ m_vTemplateLines

std::vector<std::string> PGEcfgFile::m_vTemplateLines
private

Definition at line 101 of file PGEcfgFile.h.


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