In every program I have written, I end up using some sort of INI file to keep settings from one run to the next. Instead of implementing it separately in each program, I finally got around to writing this class,
CIniFile |
After you create your
CIniFile |
SetPath(CString |
To read the INI file data into the class, call
ReadFile() |
To retrieve data from the class, use
GetValue |
//returns value of keyname/valuename as CString CString GetValue(CString keyname, CString valuename); //returns value of keyname/valuename as int int GetValueI(CString keyname, CString valuename); //returns value of keyname/valuename as double double GetValueF(CString keyname, CString valuename); |
To set data values in the class, call
SetValue |
bool SetValue(CString key, CString valuename, CString value, bool create = 1); bool SetValueI(CString key, CString valuename, int value, bool create = 1); bool SetValueF(CString key, CString valuename, double value, bool create = 1); |
Use the optional parameter create as false if you do not want it to create the key/value if it doesn’t already exist.
SetValue |
To delete a value from the class, call
DeleteValue(CString keyname, CString |
To delete an entire key from the class, call
DeleteKey(CString keyname) |
To remove all data stored in the class, call
Reset() |
Other useful functions are
GetNumKeys() |
GetNumValues(CString keyname) |
Finally, to write all your stored data to the specified INI file, call
WriteFile() |
Thats it! It is simple.
Here is a complete listing of the functions included in the class:
//default constructor CIniFile(); //constructor, can specify pathname here instead of //using SetPath later CIniFile(CString inipath); //default destructor virtual ~CIniFile(); //sets path of ini file to read and write from void SetPath(CString newpath); //reads ini file specified using CIniFile::SetPath() //returns true if successful, false otherwise bool ReadFile(); //writes data stored in class to ini file void WriteFile(); //deletes all stored ini data void Reset(); //returns number of keys currently in the ini int GetNumKeys(); //returns number of values stored for specified key int GetNumValues(CString keyname); //gets value of [keyname] valuename = //overloaded to return CString, int, and double, //returns, or 0 if key/value not found. Sets error //member to show problem CString GetValue(CString keyname, CString valuename); int GetValueI(CString keyname, CString valuename); double GetValueF(CString keyname, CString valuename); //sets value of [keyname] valuename =. //specify the optional paramter as false (0) if you do not //want it to create the key if it doesn't exist. Returns true //if data entered, false otherwise overloaded to accept CString, //int, and double bool SetValue(CString key, CString valuename, CString value, bool create = 1); bool SetValueI(CString key, CString valuename, int value, bool create = 1); bool SetValueF(CString key, CString valuename, double value, bool create = 1); //deletes specified value //returns true if value existed and deleted, false otherwise bool DeleteValue(CString keyname, CString valuename); //deletes specified key and all values contained within //returns true if key existed and deleted, false otherwise bool DeleteKey(CString keyname); |
Email comments to pandcc3@comwerx.net