zengine/include/ZE_ZConfigFile.h

268 lines
9.5 KiB
C
Raw Normal View History

2002-11-21 05:40:49 +00:00
/*******************************************************************************
2002-12-29 06:50:19 +00:00
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
2002-11-21 05:40:49 +00:00
2002-12-29 06:50:19 +00:00
Licensed under a BSD-style license.
2002-11-21 05:40:49 +00:00
2002-12-29 06:50:19 +00:00
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
2002-11-21 05:40:49 +00:00
*******************************************************************************/
/*!
\file ZE_ZConfigFile.h
\brief Definition file for ZConfigFile.
2003-05-07 20:34:50 +00:00
Definition file for ZConfigFile, an INI-style config file format.<br>
$ id: ZE_ZConfigFile.h,v 1.9 2003/02/10 04:40:16 cozman Exp $
\author James Turk
2002-11-21 05:40:49 +00:00
**/
#ifndef __ze_zconfigfile_h__
#define __ze_zconfigfile_h__
2003-02-10 04:02:38 +00:00
#include "ZE_ZEngine.h"
2002-11-21 05:40:49 +00:00
namespace ZE
{
/*!
\brief ZConfigFile Class for use in ZEngine.
2003-07-11 20:51:44 +00:00
ZConfigFile class for INI-style configuration files for games or applications.
ZConfigFile can have comments using the semicolon (;) or octothorpe (#) characters.
Sections are delimited by [section-name], and variables must start with a letter
and should be in the format <var>variable = data</var>.
2002-11-21 05:40:49 +00:00
**/
2003-02-10 04:02:38 +00:00
class ZConfigFile
2002-11-21 05:40:49 +00:00
{
private:
//Private Types//
/*!
\brief ZConfigFile Variable class.
2003-06-11 00:15:07 +00:00
ZConfigFile class for mapping a variable name to it's value, stored in std::string form (later converted to
bool or int if needed).
**/
2002-11-21 05:40:49 +00:00
class ZCF_Variable
{
public:
//! Variable name.
2003-06-11 00:15:07 +00:00
std::string var;
//! Value associated with variable.
2003-06-11 00:15:07 +00:00
std::string val;
2002-11-21 05:40:49 +00:00
};
/*!
\brief ZConfigFile Section class.
ZConfigFile class for mapping a section name to a list of variables in that section.
**/
2002-11-21 05:40:49 +00:00
class ZCF_Section
{
public:
//! Section name.
2003-06-11 00:15:07 +00:00
std::string section;
//! STL list of variables.
2003-06-11 00:15:07 +00:00
std::list<ZCF_Variable> varList;
2002-11-21 05:40:49 +00:00
};
2003-02-10 04:02:38 +00:00
protected:
2002-11-21 05:40:49 +00:00
//! List of sections of internal type.
2003-06-11 00:15:07 +00:00
std::list<ZCF_Section> rFileLayout;
2002-11-21 05:40:49 +00:00
//! Filename of file currently open.
2003-06-11 00:15:07 +00:00
std::string rFilename;
2002-11-21 05:40:49 +00:00
/*!
2003-06-11 00:15:07 +00:00
\brief Reformat a std::string in a form more suitable to parsing.
2002-11-21 05:40:49 +00:00
2003-06-11 00:15:07 +00:00
Removes whitespace from a std::string and makes all characters lowercase.
\param str The std::string to get a clean version of.
\return Cleaned std::string.
2002-11-21 05:40:49 +00:00
**/
2003-06-11 00:15:07 +00:00
std::string CleanString(std::string str) const;
2002-11-21 05:40:49 +00:00
2002-12-04 23:08:17 +00:00
/*!
2002-12-04 23:06:45 +00:00
\brief Check if a section exists.
Find out if a section currently exists.
\param sec Section to check for.
\return bool, true if section exists in file.
**/
2003-06-11 00:15:07 +00:00
bool Exists(std::string sec) const;
2002-12-04 23:06:45 +00:00
2002-12-04 23:08:17 +00:00
/*!
2002-12-04 23:06:45 +00:00
\brief Check if a variable exists.
Find out if a variable currently exists.
\param sec Section to check in.
\param var Variable to check for.
\return bool, true if section exists in file.
**/
2003-06-11 00:15:07 +00:00
bool Exists(std::string sec, std::string var) const;
2002-12-04 23:06:45 +00:00
/*!
\brief Internal function to set variables.
Set variable to value, called internally only.
\param sec Section for variable.
\param var Variable to set.
\param val Value to set variable to.
**/
2003-06-11 00:15:07 +00:00
void SetVariable(std::string sec, std::string var, std::string val);
2002-12-04 23:06:45 +00:00
/*!
\brief Internal function to get value of a variable.
Get value of variable, called internally only.
\param sec Section for variable.
\param var Variable to get.
\param defVal Value to return if variable doesnt exist.
\return Value of variable.
**/
2003-06-11 00:15:07 +00:00
std::string GetVariable(std::string sec, std::string var, std::string defVal) const;
2002-11-21 05:40:49 +00:00
public:
/*!
\brief Default constructor.
A no-op default constructor.
**/
ZConfigFile();
/*!
\brief Constructor which takes filename.
Constructor takes filename, and calls process on it.
\param filename File to load as ZConfigFile.
**/
2003-06-11 00:15:07 +00:00
ZConfigFile(std::string filename);
2002-11-21 05:40:49 +00:00
/*!
\brief Destructor, flushes file.
Flushes the file, ensures a flush if the file is left open.
**/
2003-02-10 04:40:16 +00:00
virtual ~ZConfigFile();
2002-11-21 05:40:49 +00:00
/*!
\brief Parse a file.
Parses the file, reading the contents into the fileLayout map.
\param filename File to parse and attach this ZDataFile to.
**/
2003-06-11 00:15:07 +00:00
void Process(std::string filename);
2002-11-21 05:40:49 +00:00
2003-02-10 03:19:37 +00:00
/*!
\brief Get value in floating point format from file.
Get the current value of a variable in the file, or defVal if not found in file.
\since 0.8.3
\param section Name of section to seek variable under.
\param var Name of variable to seek value for.
\param defVal Value to return if var does not exist within section.
\return Contents of the variable in floating point format.
**/
2003-06-11 00:15:07 +00:00
float GetFloat(std::string section, std::string var, float defVal) const;
2003-02-10 03:19:37 +00:00
2002-11-21 05:40:49 +00:00
/*!
\brief Get value in integer format from file.
Get the current value of a variable in the file, or defVal if not found in file.
\param section Name of section to seek variable under.
\param var Name of variable to seek value for.
\param defVal Value to return if var does not exist within section.
\return Contents of the variable in integer format.
**/
2003-06-11 00:15:07 +00:00
int GetInt(std::string section, std::string var, int defVal) const;
2002-11-21 05:40:49 +00:00
/*!
\brief Get value in boolean format from file.
Get the current value of a variable in the file, or defVal if not found in file.
(Valid values are "0","1","true" and "false")
\param section Name of section to seek variable under.
\param var Name of variable to seek value for.
\param defVal Value to return if var does not exist within section.
\return Contents of the variable in boolean format.
**/
2003-06-11 00:15:07 +00:00
bool GetBool(std::string section, std::string var, bool defVal) const;
2002-11-21 05:40:49 +00:00
/*!
2003-06-11 00:15:07 +00:00
\brief Get value in std::string format from file.
2002-11-21 05:40:49 +00:00
Get the current value of a variable in the file, or defVal if not found in file.
\param section Name of section to seek variable under.
\param var Name of variable to seek value for.
\param defVal Value to return if var does not exist within section.
2003-06-11 00:15:07 +00:00
\return Contents of the variable in std::string format.
2002-11-21 05:40:49 +00:00
**/
2003-06-11 00:15:07 +00:00
std::string GetString(std::string section, std::string var, std::string defVal) const;
2002-11-21 05:40:49 +00:00
2003-02-10 03:19:37 +00:00
/*!
\brief Set value in floating point format in file.
Set the new value of a variable in the file to val, creating the section and variable
if not already found in file.
\since 0.8.3
\param section Name of section to edit variable under.
\param var Name of variable to set value for.
\param val Floating point value to set variable to in file.
**/
2003-06-11 00:15:07 +00:00
void SetFloat(std::string section, std::string var, float val);
2003-02-10 03:19:37 +00:00
2002-11-21 05:40:49 +00:00
/*!
\brief Set value in integer format in file.
Set the new value of a variable in the file to val, creating the section and variable
if not already found in file.
\param section Name of section to edit variable under.
\param var Name of variable to set value for.
\param val Integer value to set variable to in file.
**/
2003-06-11 00:15:07 +00:00
void SetInt(std::string section, std::string var, int val);
2002-11-21 05:40:49 +00:00
/*!
\brief Set value in boolean format in file.
Set the new value of a variable in the file to val, creating the section and variable
if not already found in file.
\param section Name of section to edit variable under.
\param var Name of variable to set value for.
\param val Boolean value to set variable to in file.
**/
2003-06-11 00:15:07 +00:00
void SetBool(std::string section, std::string var, bool val);
2002-11-21 05:40:49 +00:00
/*!
2003-06-11 00:15:07 +00:00
\brief Set value in std::string format in file.
2002-11-21 05:40:49 +00:00
Set the new value of a variable in the file to val, creating the section and variable
if not already found in file.
\param section Name of section to edit variable under.
\param var Name of variable to set value for.
\param val String value to set variable to in file.
**/
2003-06-11 00:15:07 +00:00
void SetString(std::string section, std::string var, std::string val);
2002-11-21 05:40:49 +00:00
/*!
\brief Write all values to file
Writes all values and sections to file.
**/
void Flush();
/*!
\brief Close the file.
Flush the file and clear the filename.
**/
void Close();
};
}
#endif //__ze_zconfigfile_h__