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-10-25 05:15:44 -04:00
|
|
|
cIniFile::cIniFile(void) :
|
2013-08-05 09:24:23 -04:00
|
|
|
m_IsCaseInsensitive(true)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:15:44 -04:00
|
|
|
bool cIniFile::ReadFile(const AString & a_FileName, 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;
|
2013-10-25 05:38:14 -04:00
|
|
|
AString line;
|
|
|
|
AString keyname, valuename, value;
|
|
|
|
AString::size_type pLeft, pRight;
|
2013-08-05 09:24:23 -04:00
|
|
|
bool IsFromExampleRedirect = false;
|
|
|
|
|
2013-10-25 05:15:44 -04:00
|
|
|
f.open((FILE_IO_PREFIX + a_FileName).c_str(), ios::in);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (f.fail())
|
|
|
|
{
|
|
|
|
f.clear();
|
|
|
|
if (a_AllowExampleRedirect)
|
|
|
|
{
|
|
|
|
// Retry with the .example.ini file instead of .ini:
|
2013-10-25 05:38:14 -04:00
|
|
|
AString ExPath(a_FileName.substr(0, a_FileName.length() - 4));
|
2013-08-05 09:24:23 -04:00
|
|
|
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.
|
2013-10-25 05:38:14 -04:00
|
|
|
size_t lineLength = line.length();
|
2012-09-08 12:08:29 -04:00
|
|
|
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-10-25 05:38:14 -04:00
|
|
|
if ((pLeft = line.find_first_of(";#[=")) == AString::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 (
|
2013-10-25 05:38:14 -04:00
|
|
|
((pRight = line.find_last_of("]")) != AString::npos) &&
|
2012-09-08 12:08:29 -04:00
|
|
|
(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);
|
2014-01-11 14:10:50 -05:00
|
|
|
AddValue(keyname, valuename, value);
|
2012-09-08 12:08:29 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
case '#':
|
|
|
|
{
|
|
|
|
if (names.size() == 0)
|
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
AddHeaderComment(line.substr(pLeft + 1));
|
2012-09-08 12:08:29 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
AddKeyComment(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)
|
|
|
|
{
|
2013-10-25 05:15:44 -04:00
|
|
|
WriteFile(FILE_IO_PREFIX + a_FileName);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:15:44 -04:00
|
|
|
bool cIniFile::WriteFile(const AString & a_FileName) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
// 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-10-25 05:15:44 -04:00
|
|
|
f.open((FILE_IO_PREFIX + a_FileName).c_str(), ios::out);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (f.fail())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// Write header comments.
|
2013-10-25 05:38:14 -04:00
|
|
|
size_t NumComments = comments.size();
|
|
|
|
for (size_t commentID = 0; commentID < NumComments; ++commentID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
f << ';' << comments[commentID] << iniEOL;
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
if (NumComments > 0)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
f << iniEOL;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// Write keys and values.
|
2013-10-25 05:38:14 -04:00
|
|
|
for (size_t keyID = 0; keyID < keys.size(); ++keyID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
f << '[' << names[keyID] << ']' << iniEOL;
|
|
|
|
|
|
|
|
// Comments.
|
2013-10-25 05:38:14 -04:00
|
|
|
for (size_t commentID = 0; commentID < keys[keyID].comments.size(); ++commentID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
f << ';' << keys[keyID].comments[commentID] << iniEOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values.
|
2013-10-25 05:38:14 -04:00
|
|
|
for (size_t valueID = 0; valueID < keys[keyID].names.size(); ++valueID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
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-10-25 05:38:14 -04:00
|
|
|
int cIniFile::FindKey(const AString & a_KeyName) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
AString CaseKeyName = CheckCase(a_KeyName);
|
|
|
|
for (size_t keyID = 0; keyID < names.size(); ++keyID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
if (CheckCase(names[keyID]) == CaseKeyName)
|
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
return keyID;
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return noID;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::FindValue(const int keyID, const AString & a_ValueName) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (!keys.size() || (keyID >= (int)keys.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return noID;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString CaseValueName = CheckCase(a_ValueName);
|
|
|
|
for (size_t valueID = 0; valueID < keys[keyID].names.size(); ++valueID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
if (CheckCase(keys[keyID].names[valueID]) == CaseValueName)
|
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
return int(valueID);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return noID;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::AddKeyName(const AString & 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-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetKeyName(const int keyID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (keyID < (int)names.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return names[keyID];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::GetNumValues(const int keyID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (keyID < (int)keys.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
return (int)keys[keyID].names.size();
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::GetNumValues(const AString & keyname) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
return (int)keys[keyID].names.size();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetValueName(const int keyID, const int valueID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if ((keyID < (int)keys.size()) && (valueID < (int)keys[keyID].names.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return keys[keyID].names[valueID];
|
|
|
|
}
|
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetValueName(const AString & keyname, const int valueID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
return GetValueName(keyID, valueID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
void cIniFile::AddValue(const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value)
|
|
|
|
{
|
|
|
|
int keyID = FindKey(a_KeyName);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
keyID = int(AddKeyName(a_KeyName));
|
|
|
|
}
|
|
|
|
|
|
|
|
keys[keyID].names.push_back(a_ValueName);
|
|
|
|
keys[keyID].values.push_back(a_Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIniFile::AddValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value)
|
|
|
|
{
|
|
|
|
AddValue(a_KeyName, a_ValueName, Printf("%d", a_Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cIniFile::AddValueF(const AString & a_KeyName, const AString & a_ValueName, const double a_Value)
|
|
|
|
{
|
|
|
|
AddValue(a_KeyName, a_ValueName, Printf("%f", a_Value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::SetValue(const int keyID, const int valueID, const AString & value)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
if (((size_t)keyID >= keys.size()) || ((size_t)valueID >= keys[keyID].names.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
return false;
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
2014-01-11 10:44:28 -05:00
|
|
|
keys[keyID].values[valueID] = value;
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
bool cIniFile::SetValue(const AString & a_KeyName, const AString & a_ValueName, const AString & a_Value, const bool a_CreateIfNotExists)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
int keyID = FindKey(a_KeyName);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
if (!a_CreateIfNotExists)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-11 10:44:28 -05:00
|
|
|
keyID = AddKeyName(a_KeyName);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
int valueID = FindValue(keyID, a_ValueName);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (valueID == noID)
|
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
if (!a_CreateIfNotExists)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-11 10:44:28 -05:00
|
|
|
keys[keyID].names.push_back(a_ValueName);
|
|
|
|
keys[keyID].values.push_back(a_Value);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
keys[keyID].values[valueID] = a_Value;
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
bool cIniFile::SetValueI(const AString & a_KeyName, const AString & a_ValueName, const int a_Value, const bool a_CreateIfNotExists)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
return SetValue(a_KeyName, a_ValueName, Printf("%d", a_Value), a_CreateIfNotExists);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
bool cIniFile::SetValueF(const AString & a_KeyName, const AString & a_ValueName, double const a_Value, const bool a_CreateIfNotExists)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-11 10:44:28 -05:00
|
|
|
return SetValue(a_KeyName, a_ValueName, Printf("%f", a_Value), a_CreateIfNotExists);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-11 10:44:28 -05:00
|
|
|
bool cIniFile::SetValueV(const AString & a_KeyName, const AString & a_ValueName, const char * a_Format, ...)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-16 02:34:10 -05:00
|
|
|
va_list args;
|
2014-01-11 10:44:28 -05:00
|
|
|
va_start(args, a_Format);
|
2013-08-05 09:24:23 -04:00
|
|
|
AString Data;
|
2014-01-16 02:34:10 -05:00
|
|
|
AppendVPrintf(Data, a_Format, args);
|
2013-08-05 09:24:23 -04:00
|
|
|
va_end(args);
|
2014-01-11 10:44:28 -05:00
|
|
|
return SetValue(a_KeyName, a_ValueName, Data);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetValue(const int keyID, const int valueID, const AString & defValue) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if ((keyID < (int)keys.size()) && (valueID < (int)keys[keyID].names.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return keys[keyID].values[valueID];
|
|
|
|
}
|
|
|
|
return defValue;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetValue(const AString & keyname, const AString & valuename, const AString & defValue) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return defValue;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int valueID = FindValue(int(keyID), valuename);
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::GetValueI(const AString & keyname, const AString & valuename, const int defValue) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
double cIniFile::GetValueF(const AString & keyname, const AString & valuename, double const defValue) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
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-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
SetValue(keyname, valuename, defValue);
|
|
|
|
return defValue;
|
|
|
|
}
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int valueID = FindValue(int(keyID), valuename);
|
2013-08-05 09:24:23 -04:00
|
|
|
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-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteValueByID(const int keyID, const int valueID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if ((keyID < (int)keys.size()) && (valueID < (int)keys[keyID].names.size()))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// This looks strange, but is neccessary.
|
2013-10-25 05:38:14 -04:00
|
|
|
vector<AString>::iterator npos = keys[keyID].names.begin() + valueID;
|
|
|
|
vector<AString>::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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteValue(const AString & keyname, const AString & valuename)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int valueID = FindValue(int(keyID), valuename);
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteKey(const AString & keyname)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
vector<AString>::iterator npos = names.begin() + keyID;
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
void cIniFile::AddHeaderComment(const AString & 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetHeaderComment(const int commentID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (commentID < (int)comments.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return comments[commentID];
|
|
|
|
}
|
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteHeaderComment(int commentID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (commentID < (int)comments.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
vector<AString>::iterator cpos = comments.begin() + commentID;
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::GetNumKeyComments(const int keyID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (keyID < (int)keys.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return keys[keyID].comments.size();
|
|
|
|
}
|
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
int cIniFile::GetNumKeyComments(const AString & keyname) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
2013-10-25 05:38:14 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
return 0;
|
2013-10-25 05:38:14 -04:00
|
|
|
}
|
|
|
|
return (int)keys[keyID].comments.size();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::AddKeyComment(const int keyID, const AString & comment)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (keyID < (int)keys.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::AddKeyComment(const AString & keyname, const AString & comment)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
return AddKeyComment(keyID, comment);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetKeyComment(const int keyID, const int commentID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if ((keyID < (int)keys.size()) && (commentID < (int)keys[keyID].comments.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
return keys[keyID].comments[commentID];
|
|
|
|
}
|
|
|
|
return "";
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::GetKeyComment(const AString & keyname, const int commentID) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
return GetKeyComment(int(keyID), commentID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteKeyComment(const int keyID, const int commentID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if ((keyID < (int)keys.size()) && (commentID < (int)keys[keyID].comments.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
vector<AString>::iterator cpos = keys[keyID].comments.begin() + commentID;
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteKeyComment(const AString & keyname, const int commentID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
return DeleteKeyComment(int(keyID), commentID);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteKeyComments(const int keyID)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
if (keyID < (int)keys.size())
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
|
|
|
keys[keyID].comments.clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
bool cIniFile::DeleteKeyComments(const AString & keyname)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-25 05:38:14 -04:00
|
|
|
int keyID = FindKey(keyname);
|
2013-08-05 09:24:23 -04:00
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
return DeleteKeyComments(int(keyID));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-25 05:38:14 -04:00
|
|
|
AString cIniFile::CheckCase(const AString & s) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-05 09:24:23 -04:00
|
|
|
if (!m_IsCaseInsensitive)
|
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
2013-10-25 05:38:14 -04:00
|
|
|
AString res(s);
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|