2012-06-14 09:06:06 -04:00
|
|
|
// IniFile.cpp: Implementation of the CIniFile class.
|
|
|
|
// Written by: Adam Clauss
|
|
|
|
// Email: cabadam@houston.rr.com
|
|
|
|
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
|
|
|
|
// email suggested changes to me.
|
|
|
|
//
|
|
|
|
// Rewritten by: Shane Hill
|
|
|
|
// Date: 21/08/2001
|
|
|
|
// Email: Shane.Hill@dsto.defence.gov.au
|
|
|
|
// Reason: Remove dependancy on MFC. Code should compile on any
|
|
|
|
// platform.
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*
|
2013-08-05 09:24:23 -04:00
|
|
|
!! MODIFIED BY FAKETRUTH and xoft !!
|
2012-06-14 09:06:06 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
// C++ Includes
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
// C Includes
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
// Local Includes
|
|
|
|
#include "iniFile.h"
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
2013-08-05 09:24:23 -04:00
|
|
|
#define iniEOL endl
|
2012-06-14 09:06:06 -04:00
|
|
|
#else
|
2013-08-05 09:24:23 -04:00
|
|
|
#define iniEOL '\r' << endl
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2013-08-05 09:24:23 -04:00
|
|
|
#define sscanf_s(buffer, stringbuffer, ...) (sscanf(buffer, stringbuffer, __VA_ARGS__))
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
cIniFile::cIniFile(const string & a_Path) :
|
|
|
|
m_IsCaseInsensitive(true)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
Path(a_Path);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::ReadFile(bool a_AllowExampleRedirect)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-08 12:08:29 -04:00
|
|
|
// Normally you would use ifstream, but the SGI CC compiler has
|
|
|
|
// a few bugs with ifstream. So ... fstream used.
|
|
|
|
fstream f;
|
|
|
|
string line;
|
|
|
|
string keyname, valuename, value;
|
|
|
|
string::size_type pLeft, pRight;
|
2013-08-05 09:24:23 -04:00
|
|
|
bool IsFromExampleRedirect = false;
|
|
|
|
|
|
|
|
f.open((FILE_IO_PREFIX + m_Path).c_str(), ios::in);
|
|
|
|
if (f.fail())
|
|
|
|
{
|
|
|
|
f.clear();
|
|
|
|
if (a_AllowExampleRedirect)
|
|
|
|
{
|
|
|
|
// Retry with the .example.ini file instead of .ini:
|
|
|
|
string ExPath(m_Path.substr(0, m_Path.length() - 4));
|
|
|
|
ExPath.append(".example.ini");
|
|
|
|
f.open((FILE_IO_PREFIX + ExPath).c_str(), ios::in);
|
|
|
|
if (f.fail())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IsFromExampleRedirect = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-09-08 12:08:29 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
while (getline(f, line))
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
|
|
|
// To be compatible with Win32, check for existence of '\r'.
|
|
|
|
// Win32 files have the '\r' and Unix files don't at the end of a line.
|
|
|
|
// Note that the '\r' will be written to INI files from
|
|
|
|
// Unix so that the created INI file can be read under Win32
|
|
|
|
// without change.
|
|
|
|
unsigned int lineLength = line.length();
|
|
|
|
if (lineLength == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
if (line[lineLength - 1] == '\r')
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
line = line.substr(0, lineLength - 1);
|
2012-09-08 12:08:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line.length() == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2012-09-08 12:08:29 -04:00
|
|
|
// Check that the user hasn't opened a binary file by checking the first
|
|
|
|
// character of each line!
|
2013-08-05 09:24:23 -04:00
|
|
|
if (!isprint(line[0]))
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
printf("%s: Binary-check failed on char %d\n", __FUNCTION__, line[0]);
|
2012-09-08 12:08:29 -04:00
|
|
|
f.close();
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
if ((pLeft = line.find_first_of(";#[=")) == string::npos)
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
switch (line[pLeft])
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
|
|
|
case '[':
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
((pRight = line.find_last_of("]")) != string::npos) &&
|
|
|
|
(pRight > pLeft)
|
2013-08-05 09:24:23 -04:00
|
|
|
)
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
keyname = line.substr(pLeft + 1, pRight - pLeft - 1);
|
|
|
|
AddKeyName(keyname);
|
2012-09-08 12:08:29 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
valuename = line.substr(0, pLeft);
|
|
|
|
value = line.substr(pLeft + 1);
|
|
|
|
SetValue(keyname, valuename, value);
|
2012-09-08 12:08:29 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
case '#':
|
|
|
|
{
|
|
|
|
if (names.size() == 0)
|
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
HeaderComment(line.substr(pLeft + 1));
|
2012-09-08 12:08:29 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
KeyComment(keyname, line.substr(pLeft + 1));
|
2012-09-08 12:08:29 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // switch (line[pLeft])
|
|
|
|
} // while (getline())
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
f.close();
|
2013-08-05 09:24:23 -04:00
|
|
|
if (names.size() == 0)
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
if (IsFromExampleRedirect)
|
|
|
|
{
|
|
|
|
WriteFile();
|
|
|
|
}
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::WriteFile(void) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
unsigned commentID, keyID, valueID;
|
|
|
|
// Normally you would use ofstream, but the SGI CC compiler has
|
|
|
|
// a few bugs with ofstream. So ... fstream used.
|
|
|
|
fstream f;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
f.open((FILE_IO_PREFIX + m_Path).c_str(), ios::out);
|
|
|
|
if (f.fail())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// Write header comments.
|
|
|
|
for (commentID = 0; commentID < comments.size(); ++commentID)
|
|
|
|
{
|
|
|
|
f << ';' << comments[commentID] << iniEOL;
|
|
|
|
}
|
|
|
|
if (comments.size())
|
|
|
|
{
|
|
|
|
f << iniEOL;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// Write keys and values.
|
|
|
|
for (keyID = 0; keyID < keys.size(); ++keyID)
|
|
|
|
{
|
|
|
|
f << '[' << names[keyID] << ']' << iniEOL;
|
|
|
|
|
|
|
|
// Comments.
|
|
|
|
for (commentID = 0; commentID < keys[keyID].comments.size(); ++commentID)
|
|
|
|
{
|
|
|
|
f << ';' << keys[keyID].comments[commentID] << iniEOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values.
|
|
|
|
for (valueID = 0; valueID < keys[keyID].names.size(); ++valueID)
|
|
|
|
{
|
|
|
|
f << keys[keyID].names[valueID] << '=' << keys[keyID].values[valueID] << iniEOL;
|
|
|
|
}
|
|
|
|
f << iniEOL;
|
|
|
|
}
|
|
|
|
f.close();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
long cIniFile::FindKey(const string & a_KeyName) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
string CaseKeyName = CheckCase(a_KeyName);
|
|
|
|
for (unsigned keyID = 0; keyID < names.size(); ++keyID)
|
|
|
|
{
|
|
|
|
if (CheckCase(names[keyID]) == CaseKeyName)
|
|
|
|
{
|
|
|
|
return long(keyID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noID;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
long cIniFile::FindValue(unsigned const keyID, const string & a_ValueName) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (!keys.size() || (keyID >= keys.size()))
|
|
|
|
{
|
|
|
|
return noID;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
string CaseValueName = CheckCase(a_ValueName);
|
|
|
|
for (unsigned valueID = 0; valueID < keys[keyID].names.size(); ++valueID)
|
|
|
|
{
|
|
|
|
if (CheckCase(keys[keyID].names[valueID]) == CaseValueName)
|
|
|
|
{
|
|
|
|
return long(valueID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noID;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
unsigned cIniFile::AddKeyName(const string & keyname)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
names.resize(names.size() + 1, keyname);
|
|
|
|
keys.resize(keys.size() + 1);
|
|
|
|
return names.size() - 1;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
string cIniFile::KeyName(unsigned const keyID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < names.size())
|
|
|
|
{
|
|
|
|
return names[keyID];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
unsigned cIniFile::NumValues(unsigned const keyID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < keys.size())
|
|
|
|
{
|
|
|
|
return keys[keyID].names.size();
|
|
|
|
}
|
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
unsigned cIniFile::NumValues(const string & keyname)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return keys[keyID].names.size();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
string cIniFile::ValueName(unsigned const keyID, unsigned const valueID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < keys.size() && valueID < keys[keyID].names.size())
|
|
|
|
{
|
|
|
|
return keys[keyID].names[valueID];
|
|
|
|
}
|
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
string cIniFile::ValueName(const string & keyname, unsigned const valueID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return ValueName(keyID, valueID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::SetValue(unsigned const keyID, unsigned const valueID, const string & value)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if ((keyID < keys.size()) && (valueID < keys[keyID].names.size()))
|
|
|
|
{
|
|
|
|
keys[keyID].values[valueID] = value;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::SetValue(const string & keyname, const string & valuename, const string & value, bool const create)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
if (create)
|
|
|
|
{
|
|
|
|
keyID = long(AddKeyName(keyname));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
long valueID = FindValue(unsigned(keyID), valuename);
|
|
|
|
if (valueID == noID)
|
|
|
|
{
|
|
|
|
if (!create)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
keys[keyID].names.resize(keys[keyID].names.size() + 1, valuename);
|
|
|
|
keys[keyID].values.resize(keys[keyID].values.size() + 1, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!create)
|
|
|
|
{
|
|
|
|
keys[keyID].values[valueID] = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keys[keyID].names.resize(keys[keyID].names.size() + 1, valuename);
|
|
|
|
keys[keyID].values.resize(keys[keyID].values.size() + 1, value);
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::SetValueI(const string & keyname, const string & valuename, int const value, bool const create)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%d", value);
|
|
|
|
return SetValue(keyname, valuename, Data, create);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::SetValueF(const string & keyname, const string & valuename, double const value, bool const create)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%f", value);
|
|
|
|
return SetValue(keyname, valuename, Data, create);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::SetValueV(const string & keyname, const string & valuename, char * format, ...)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
AString Data;
|
|
|
|
AppendVPrintf(Data, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return SetValue(keyname, valuename, Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
string cIniFile::GetValue(unsigned const keyID, unsigned const valueID, const string & defValue) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if ((keyID < keys.size()) && (valueID < keys[keyID].names.size()))
|
|
|
|
{
|
|
|
|
return keys[keyID].values[valueID];
|
|
|
|
}
|
|
|
|
return defValue;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
string cIniFile::GetValue(const string & keyname, const string & valuename, const string & defValue) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return defValue;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
long valueID = FindValue(unsigned(keyID), valuename);
|
|
|
|
if (valueID == noID)
|
|
|
|
{
|
|
|
|
return defValue;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
return keys[keyID].values[valueID];
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cIniFile::GetValueI(const string & keyname, const string & valuename, int const defValue) const
|
|
|
|
{
|
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%d", defValue);
|
2013-08-05 09:24:23 -04:00
|
|
|
return atoi(GetValue(keyname, valuename, Data).c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double cIniFile::GetValueF(const string & keyname, const string & valuename, double const defValue) const
|
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%f", defValue);
|
|
|
|
return atof(GetValue(keyname, valuename, Data).c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AString cIniFile::GetValueSet(const AString & keyname, const AString & valuename, const AString & defValue)
|
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
SetValue(keyname, valuename, defValue);
|
|
|
|
return defValue;
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
long valueID = FindValue(unsigned(keyID), valuename);
|
|
|
|
if (valueID == noID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
SetValue(keyname, valuename, defValue);
|
|
|
|
return defValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys[keyID].values[valueID];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double cIniFile::GetValueSetF(const AString & keyname, const AString & valuename, const double defValue)
|
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%f", defValue);
|
|
|
|
return atof(GetValueSet(keyname, valuename, Data).c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cIniFile::GetValueSetI(const AString & keyname, const AString & valuename, const int defValue)
|
|
|
|
{
|
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%d", defValue);
|
2013-08-05 09:24:23 -04:00
|
|
|
return atoi(GetValueSet(keyname, valuename, Data).c_str());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
bool cIniFile::DeleteValueByID(const unsigned keyID, const unsigned valueID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < keys.size() && valueID < keys[keyID].names.size())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// This looks strange, but is neccessary.
|
|
|
|
vector<string>::iterator npos = keys[keyID].names.begin() + valueID;
|
|
|
|
vector<string>::iterator vpos = keys[keyID].values.begin() + valueID;
|
2013-08-05 09:24:23 -04:00
|
|
|
keys[keyID].names.erase(npos, npos + 1);
|
|
|
|
keys[keyID].values.erase(vpos, vpos + 1);
|
2012-06-14 09:06:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::DeleteValue(const string & keyname, const string & valuename)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
long valueID = FindValue(unsigned(keyID), valuename);
|
|
|
|
if (valueID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
return DeleteValueByID(keyID, valueID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
bool cIniFile::DeleteKey(const string & keyname)
|
|
|
|
{
|
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string>::iterator npos = names.begin() + keyID;
|
|
|
|
vector<key>::iterator kpos = keys.begin() + keyID;
|
|
|
|
names.erase(npos, npos + 1);
|
|
|
|
keys.erase(kpos, kpos + 1);
|
|
|
|
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIniFile::Clear(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
names.clear();
|
|
|
|
keys.clear();
|
|
|
|
comments.clear();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIniFile::HeaderComment(const string & comment)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
comments.push_back(comment);
|
|
|
|
// comments.resize(comments.size() + 1, comment);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string cIniFile::HeaderComment(unsigned const commentID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (commentID < comments.size())
|
|
|
|
{
|
|
|
|
return comments[commentID];
|
|
|
|
}
|
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::DeleteHeaderComment(unsigned commentID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (commentID < comments.size())
|
|
|
|
{
|
|
|
|
vector<string>::iterator cpos = comments.begin() + commentID;
|
|
|
|
comments.erase(cpos, cpos + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned cIniFile::NumKeyComments(unsigned const keyID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < keys.size())
|
|
|
|
{
|
|
|
|
return keys[keyID].comments.size();
|
|
|
|
}
|
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned cIniFile::NumKeyComments(const string & keyname) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
return 0;
|
|
|
|
return keys[keyID].comments.size();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::KeyComment(unsigned const keyID, const string & comment)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < keys.size())
|
|
|
|
{
|
|
|
|
keys[keyID].comments.resize(keys[keyID].comments.size() + 1, comment);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::KeyComment(const string & keyname, const string & comment)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return KeyComment(unsigned(keyID), comment);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string cIniFile::KeyComment(unsigned const keyID, unsigned const commentID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if ((keyID < keys.size()) && (commentID < keys[keyID].comments.size()))
|
|
|
|
{
|
|
|
|
return keys[keyID].comments[commentID];
|
|
|
|
}
|
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string cIniFile::KeyComment(const string & keyname, unsigned const commentID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return KeyComment(unsigned(keyID), commentID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::DeleteKeyComment(unsigned const keyID, unsigned const commentID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if ((keyID < keys.size()) && (commentID < keys[keyID].comments.size()))
|
|
|
|
{
|
|
|
|
vector<string>::iterator cpos = keys[keyID].comments.begin() + commentID;
|
|
|
|
keys[keyID].comments.erase(cpos, cpos + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::DeleteKeyComment(const string & keyname, unsigned const commentID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return DeleteKeyComment(unsigned(keyID), commentID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::DeleteKeyComments(unsigned const keyID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID < keys.size())
|
|
|
|
{
|
|
|
|
keys[keyID].comments.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIniFile::DeleteKeyComments(const string & keyname)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
long keyID = FindKey(keyname);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return DeleteKeyComments(unsigned(keyID));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string cIniFile::CheckCase(const string & s) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (!m_IsCaseInsensitive)
|
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
string res(s);
|
|
|
|
size_t len = res.length();
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
res[i] = tolower(res[i]);
|
|
|
|
}
|
|
|
|
return res;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|