Code: Select all
/*
** File: TA3D_Config.h
** Notes:
** Cire: This legacy class should manage to allow one to store and retreive
** variables. I have added support for float, double, int(32)
** bool and std::string to its class, It works something similar to a map
** and a vector allowing access to its keys via either int or name
** by overloading the [] operator.
**
** WARNINGS: This class does NOT protect string data that contains '\n' within its
** confines. Should string data contain such a thing its possible that it
** could corrupt the remaing of the file from being read correctly.
**
** TODO: This probably should have strong checks in some areas that may prevent
** some functions from throwing an exception when there are no keys.
*/
#pragma once
namespace TA3D
{
namespace UTILS
{
class cConfig
{
// Each variable we store is placed in a key (m_key),
// however for the most part a key is kept private to our class
// with the exception that operators will return a pointer to it.
typedef struct m_Key
{
String KeyName;
String KeyValue;
uint8 KeyValueType;
m_Key()
{
KeyName = String( "" );
KeyValue = String( "" );
KeyValueType = 0; // uses TA3D::VARS:CONSTS:TA3D_VAR_TYPES
}
};
private:
// a few typedefs to make life easier.
typedef std::vector< m_Key * > KeyList;
typedef KeyList::iterator KeyItor;
String m_sConfigFile; // config file is set via constructor
KeyList m_Keys; // vector of our variables.
bool m_Dirty; // will be true if we changed any data, useful
// so that we can save if data is dirty.
// private functions, do NOT expose these to public or
// something bad could happen.
m_Key *GetKey( const String &KeyName );
m_Key *GetOrCreateKey( const String &KeyName );
public:
// constructor
cConfig( const String &FileName );
// deconstructor.
~cConfig();
// returns the number of keys we have.
uint32 NumKeys() { return (uint32)m_Keys.size(); }
// Erases a key.
bool EraseKey( const String &name );
// operator overloading for subscripting
m_Key *operator [](int i);
// this allows subscripting like a map, but unlike a map
// it will not create a key if it don't exists.
m_Key *operator []( const std::string &Name);
// various function to set a value of a key.
void SetKey( const String &name, const real32 v );
void SetKey( const String &name, const real64 v );
void SetKey( const String &name, const sint32 v );
void SetKey( const String &name, const bool v );
void SetKey( const String &name, const String &v );
// various functions to pull data from a key. Note that you can
// pull data from a key in any fasion you want, thus a key with
// an inital setting value of 'float' can be pulled as 'int' or
// 'string', ect.
real32 KeyValueFloat( const String &name );
real64 KeyValueDouble( const String &name );
sint32 KeyValueInt( const String &name );
bool KeyValueBool( const String &name );
String KeyValueString( const String &name );
// functions to save/read config files.
bool SaveConfigFile();
bool ReadConfigFile();
}; // class cConfig
} // namespace UTILS
} // namespace TA3D
Code: Select all
enum TA3D_VAR_TYPE
{
TA3D_VAR_TYPE_UNKNOWN = 0,
TA3D_VAR_TYPE_FLOAT = 1,
TA3D_VAR_TYPE_DOUBLE = 2,
TA3D_VAR_TYPE_INT = 3,
TA3D_VAR_TYPE_BOOL = 4,
TA3D_VAR_TYPE_STRING = 5
};
I Included the above file into TA3D_Namespace.h file, and set a external variable in VARS namespace to g_Config which is a pointer to the above class, so that we have a global configuration class.
It requres some changes to 'ta3d.cpp' and 'menu.cpp'. which I will post later.
++Cire.