2012-06-14 09:06:06 -04:00
|
|
|
// IniFile.cpp: Implementation of the CIniFile class.
|
|
|
|
// Written by: Adam Clauss
|
|
|
|
// Email: cabadam@houston.rr.com
|
2015-05-09 03:25:09 -04:00
|
|
|
// You may use this class / code as you wish in your programs. Feel free to distribute it, and
|
2012-06-14 09:06:06 -04:00
|
|
|
// email suggested changes to me.
|
|
|
|
//
|
|
|
|
// Rewritten by: Shane Hill
|
2015-05-09 03:25:09 -04:00
|
|
|
// Date: 2001-08-21
|
2012-06-14 09:06:06 -04:00
|
|
|
// Email: Shane.Hill@dsto.defence.gov.au
|
|
|
|
// Reason: Remove dependancy on MFC. Code should compile on any
|
|
|
|
// platform.
|
2014-10-27 17:34:02 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
/*
|
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
|
2014-10-23 09:15:10 -04:00
|
|
|
#include "IniFile.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
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
|
|
|
{
|
2015-05-14 10:47:51 -04:00
|
|
|
|
|
|
|
m_Filename = a_FileName;
|
|
|
|
|
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;
|
2015-05-14 10:47:51 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-10-27 17:34:02 -04:00
|
|
|
bool IsFirstLine = true;
|
2014-02-16 09:25:32 -05:00
|
|
|
|
|
|
|
while (getline(f, line))
|
2014-02-15 19:56:36 -05:00
|
|
|
{
|
2014-02-16 09:25:32 -05: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.
|
2012-09-08 12:08:29 -04:00
|
|
|
|
2014-02-16 09:25:32 -05:00
|
|
|
// Removes UTF-8 Byte Order Markers (BOM) if, present.
|
|
|
|
if (IsFirstLine)
|
2012-09-08 12:08:29 -04:00
|
|
|
{
|
2014-02-16 09:25:32 -05:00
|
|
|
RemoveBom(line);
|
|
|
|
IsFirstLine = false;
|
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
2014-02-16 09:25:32 -05:00
|
|
|
size_t lineLength = line.length();
|
|
|
|
if (lineLength == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line[lineLength - 1] == '\r')
|
|
|
|
{
|
|
|
|
line = line.substr(0, lineLength - 1);
|
|
|
|
}
|
2014-02-15 19:56:36 -05:00
|
|
|
|
2014-02-16 09:25:32 -05:00
|
|
|
if (line.length() == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-08 12:08:29 -04:00
|
|
|
|
2014-02-16 09:25:32 -05:00
|
|
|
// Check that the user hasn't opened a binary file by checking the first
|
|
|
|
// character of each line!
|
|
|
|
if (!isprint(line[0]))
|
|
|
|
{
|
|
|
|
printf("%s: Binary-check failed on char %d\n", __FUNCTION__, line[0]);
|
|
|
|
f.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((pLeft = line.find_first_of(";#[=")) == AString::npos)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-08 12:08:29 -04:00
|
|
|
|
2014-02-16 09:25:32 -05:00
|
|
|
switch (line[pLeft])
|
|
|
|
{
|
2014-02-17 08:45:31 -05:00
|
|
|
case '[':
|
|
|
|
{
|
2014-02-17 08:46:41 -05:00
|
|
|
if (
|
|
|
|
((pRight = line.find_last_of("]")) != AString::npos) &&
|
|
|
|
(pRight > pLeft)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
keyname = line.substr(pLeft + 1, pRight - pLeft - 1);
|
|
|
|
AddKeyName(keyname);
|
|
|
|
}
|
|
|
|
break;
|
2014-02-17 08:45:31 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-02-17 08:45:31 -05:00
|
|
|
case '=':
|
|
|
|
{
|
2014-02-17 08:46:41 -05:00
|
|
|
valuename = line.substr(0, pLeft);
|
2016-02-05 12:55:16 -05:00
|
|
|
value = TrimString(line.substr(pLeft + 1));
|
2014-02-17 08:46:41 -05:00
|
|
|
AddValue(keyname, valuename, value);
|
|
|
|
break;
|
2014-02-17 08:45:31 -05:00
|
|
|
}
|
2014-02-15 19:56:36 -05:00
|
|
|
|
2014-02-17 08:45:31 -05:00
|
|
|
case ';':
|
|
|
|
case '#':
|
|
|
|
{
|
2014-04-17 13:50:25 -04:00
|
|
|
if (names.empty())
|
2014-02-17 08:46:41 -05:00
|
|
|
{
|
|
|
|
AddHeaderComment(line.substr(pLeft + 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddKeyComment(keyname, line.substr(pLeft + 1));
|
|
|
|
}
|
|
|
|
break;
|
2014-02-17 08:45:31 -05:00
|
|
|
}
|
2014-02-16 09:25:32 -05:00
|
|
|
} // switch (line[pLeft])
|
2014-02-17 08:51:36 -05:00
|
|
|
} // while (getline())
|
2014-02-16 09:25:32 -05:00
|
|
|
|
|
|
|
f.close();
|
2014-04-17 13:50:25 -04:00
|
|
|
if (keys.empty() && names.empty() && comments.empty())
|
2014-02-16 09:25:32 -05:00
|
|
|
{
|
2014-04-17 13:50:25 -04:00
|
|
|
// File be empty or unreadable, equivalent to nonexistant
|
2014-02-16 09:25:32 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsFromExampleRedirect)
|
|
|
|
{
|
|
|
|
WriteFile(FILE_IO_PREFIX + a_FileName);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
2014-02-16 09:25:32 -05: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;
|
2016-02-05 12:55:16 -05:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// 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;
|
|
|
|
}
|
2016-02-05 12:55:16 -05:00
|
|
|
|
2013-08-05 09:24:23 -04:00
|
|
|
// 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)
|
|
|
|
{
|
2015-05-24 07:56:56 -04:00
|
|
|
return static_cast<int>(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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (!keys.size() || (keyID >= static_cast<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);
|
2015-07-29 11:04:03 -04:00
|
|
|
for (size_t valueID = 0; valueID < keys[static_cast<size_t>(keyID)].names.size(); ++valueID)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (CheckCase(keys[static_cast<size_t>(keyID)].names[valueID]) == CaseValueName)
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
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);
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<int>(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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (keyID < static_cast<int>(names.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return names[static_cast<size_t>(keyID)];
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (keyID < static_cast<int>(keys.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<int>(keys[static_cast<size_t>(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;
|
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<int>(keys[static_cast<size_t>(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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if ((keyID < static_cast<int>(keys.size())) && (valueID < static_cast<int>(keys[static_cast<size_t>(keyID)].names.size())))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return keys[static_cast<size_t>(keyID)].names[static_cast<size_t>(valueID)];
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
keys[static_cast<size_t>(keyID)].names.push_back(a_ValueName);
|
|
|
|
keys[static_cast<size_t>(keyID)].values.push_back(a_Value);
|
2014-01-11 10:44:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if ((static_cast<size_t>(keyID) >= keys.size()) || (static_cast<size_t>(valueID) >= keys[static_cast<size_t>(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
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
keys[static_cast<size_t>(keyID)].values[static_cast<size_t>(valueID)] = value;
|
2014-01-11 10:44:28 -05:00
|
|
|
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;
|
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
keys[static_cast<size_t>(keyID)].names.push_back(a_ValueName);
|
|
|
|
keys[static_cast<size_t>(keyID)].values.push_back(a_Value);
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
keys[static_cast<size_t>(keyID)].values[static_cast<size_t>(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-06-05 12:58:29 -04:00
|
|
|
bool cIniFile::SetValueI(const AString & a_Keyname, const AString & a_ValueName, const Int64 a_Value, const bool a_CreateIfNotExists)
|
|
|
|
{
|
2014-06-06 19:10:58 -04:00
|
|
|
return SetValue(a_Keyname, a_ValueName, Printf("%lld", a_Value), a_CreateIfNotExists);
|
2014-06-05 12:58:29 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if ((keyID < static_cast<int>(keys.size())) && (valueID < static_cast<int>(keys[static_cast<size_t>(keyID)].names.size())))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return keys[static_cast<size_t>(keyID)].values[static_cast<size_t>(valueID)];
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
return keys[static_cast<size_t>(keyID)].values[static_cast<size_t>(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;
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
return keys[static_cast<size_t>(keyID)].values[static_cast<size_t>(valueID)];
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-05 12:58:29 -04:00
|
|
|
Int64 cIniFile::GetValueSetI(const AString & keyname, const AString & valuename, const Int64 defValue)
|
|
|
|
{
|
|
|
|
AString Data;
|
|
|
|
Printf(Data, "%lld", defValue);
|
2014-06-06 19:18:58 -04:00
|
|
|
AString resultstring = GetValueSet(keyname, valuename, Data);
|
2014-07-21 17:49:06 -04:00
|
|
|
Int64 result = defValue;
|
2014-06-10 15:43:27 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
sscanf_s(resultstring.c_str(), "%lld", &result);
|
|
|
|
#else
|
2014-06-06 19:10:58 -04:00
|
|
|
sscanf(resultstring.c_str(), "%lld", &result);
|
2014-06-10 15:43:27 -04:00
|
|
|
#endif
|
2014-06-06 19:10:58 -04:00
|
|
|
return result;
|
2014-06-05 12:58:29 -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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if ((keyID < static_cast<int>(keys.size())) && (valueID < static_cast<int>(keys[static_cast<size_t>(keyID)].names.size())))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// This looks strange, but is neccessary.
|
2015-07-29 11:04:03 -04:00
|
|
|
vector<AString>::iterator npos = keys[static_cast<size_t>(keyID)].names.begin() + valueID;
|
|
|
|
vector<AString>::iterator vpos = keys[static_cast<size_t>(keyID)].values.begin() + valueID;
|
|
|
|
keys[static_cast<size_t>(keyID)].names.erase(npos, npos + 1);
|
|
|
|
keys[static_cast<size_t>(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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
bool cIniFile::HasValue(const AString & a_KeyName, const AString & a_ValueName) const
|
2014-09-03 17:00:16 -04:00
|
|
|
{
|
|
|
|
// Find the key:
|
|
|
|
int keyID = FindKey(a_KeyName);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 12:55:16 -05:00
|
|
|
|
2014-09-03 17:00:16 -04:00
|
|
|
// Find the value:
|
|
|
|
int valueID = FindValue(keyID, a_ValueName);
|
|
|
|
return (valueID != noID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (commentID < static_cast<int>(comments.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return comments[static_cast<size_t>(commentID)];
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (commentID < static_cast<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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (keyID < static_cast<int>(keys.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<int>(keys[static_cast<size_t>(keyID)].comments.size());
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<int>(keys[static_cast<size_t>(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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (keyID < static_cast<int>(keys.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
keys[static_cast<size_t>(keyID)].comments.resize(keys[static_cast<size_t>(keyID)].comments.size() + 1, comment);
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if ((keyID < static_cast<int>(keys.size())) && (commentID < static_cast<int>(keys[static_cast<size_t>(keyID)].comments.size())))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return keys[static_cast<size_t>(keyID)].comments[static_cast<size_t>(commentID)];
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if ((keyID < static_cast<int>(keys.size())) && (commentID < static_cast<int>(keys[static_cast<size_t>(keyID)].comments.size())))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
vector<AString>::iterator cpos = keys[static_cast<size_t>(keyID)].comments.begin() + commentID;
|
|
|
|
keys[static_cast<size_t>(keyID)].comments.erase(cpos, cpos + 1);
|
2013-08-05 09:24:23 -04:00
|
|
|
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
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
if (keyID < static_cast<int>(keys.size()))
|
2013-08-05 09:24:23 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
keys[static_cast<size_t>(keyID)].comments.clear();
|
2013-08-05 09:24:23 -04:00
|
|
|
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;
|
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
return DeleteKeyComments(static_cast<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++)
|
|
|
|
{
|
2015-05-19 14:32:10 -04:00
|
|
|
res[i] = static_cast<char>(tolower(res[i]));
|
2013-08-05 09:24:23 -04:00
|
|
|
}
|
|
|
|
return res;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-08-05 09:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-15 19:56:36 -05:00
|
|
|
|
|
|
|
void cIniFile::RemoveBom(AString & a_line) const
|
|
|
|
{
|
2014-10-27 17:34:02 -04:00
|
|
|
// The BOM sequence for UTF-8 is 0xEF, 0xBB, 0xBF
|
2014-02-16 09:25:32 -05:00
|
|
|
static unsigned const char BOM[] = { 0xEF, 0xBB, 0xBF };
|
|
|
|
|
|
|
|
// The BOM sequence, if present, is always th e first three characters of the input.
|
|
|
|
const AString ref = a_line.substr(0, 3);
|
2014-02-15 19:56:36 -05:00
|
|
|
|
2014-02-16 09:25:32 -05:00
|
|
|
// If any of the first three chars do not match, return and do nothing.
|
2015-07-29 11:04:03 -04:00
|
|
|
for (size_t i = 0; i < 3; ++i)
|
2014-02-15 19:56:36 -05:00
|
|
|
{
|
2014-02-16 09:25:32 -05:00
|
|
|
if (static_cast<unsigned char>(ref[i]) != BOM[i])
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-02-15 19:56:36 -05:00
|
|
|
}
|
2014-02-16 09:25:32 -05:00
|
|
|
|
|
|
|
// First three characters match; erase them.
|
|
|
|
a_line.erase(0, 3);
|
2014-02-15 19:56:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 03:45:07 -05:00
|
|
|
|
2015-05-14 10:47:51 -04:00
|
|
|
bool cIniFile::KeyExists(AString a_keyname) const
|
|
|
|
{
|
|
|
|
return FindKey(a_keyname) != noID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<AString, AString>> cIniFile::GetValues(AString a_keyName)
|
|
|
|
{
|
|
|
|
std::vector<std::pair<AString, AString>> ret;
|
|
|
|
int keyID = FindKey(a_keyName);
|
|
|
|
if (keyID == noID)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
for (size_t valueID = 0; valueID < keys[static_cast<size_t>(keyID)].names.size(); ++valueID)
|
2015-05-14 10:47:51 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
ret.emplace_back(keys[static_cast<size_t>(keyID)].names[valueID], keys[static_cast<size_t>(keyID)].values[valueID]);
|
2015-05-14 10:47:51 -04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-24 03:45:07 -05:00
|
|
|
AStringVector ReadUpgradeIniPorts(
|
2015-05-14 10:47:51 -04:00
|
|
|
cSettingsRepositoryInterface & a_Settings,
|
2015-01-24 03:45:07 -05:00
|
|
|
const AString & a_KeyName,
|
|
|
|
const AString & a_PortsValueName,
|
|
|
|
const AString & a_OldIPv4ValueName,
|
|
|
|
const AString & a_OldIPv6ValueName,
|
|
|
|
const AString & a_DefaultValue
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Read the regular value, but don't use the default (in order to detect missing value for upgrade):
|
2015-05-18 11:04:27 -04:00
|
|
|
|
|
|
|
AStringVector Ports;
|
|
|
|
|
|
|
|
for (auto pair : a_Settings.GetValues(a_KeyName))
|
|
|
|
{
|
|
|
|
if (pair.first != a_PortsValueName)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AStringVector temp = StringSplitAndTrim(pair.second, ";,");
|
|
|
|
Ports.insert(Ports.end(), temp.begin(), temp.end());
|
|
|
|
}
|
2015-01-24 03:45:07 -05:00
|
|
|
|
|
|
|
if (Ports.empty())
|
|
|
|
{
|
|
|
|
// Historically there were two separate entries for IPv4 and IPv6, merge them and migrate:
|
2015-05-14 10:47:51 -04:00
|
|
|
AString Ports4 = a_Settings.GetValue(a_KeyName, a_OldIPv4ValueName, a_DefaultValue);
|
|
|
|
AString Ports6 = a_Settings.GetValue(a_KeyName, a_OldIPv6ValueName);
|
2015-01-24 03:45:07 -05:00
|
|
|
Ports = MergeStringVectors(StringSplitAndTrim(Ports4, ";,"), StringSplitAndTrim(Ports6, ";,"));
|
2015-05-14 10:47:51 -04:00
|
|
|
a_Settings.DeleteValue(a_KeyName, a_OldIPv4ValueName);
|
|
|
|
a_Settings.DeleteValue(a_KeyName, a_OldIPv6ValueName);
|
2015-01-24 03:45:07 -05:00
|
|
|
|
|
|
|
// If those weren't present or were empty, use the default:"
|
|
|
|
if (Ports.empty())
|
|
|
|
{
|
|
|
|
Ports = StringSplitAndTrim(a_DefaultValue, ";,");
|
|
|
|
}
|
2015-05-14 10:47:51 -04:00
|
|
|
a_Settings.SetValue(a_KeyName, a_PortsValueName, StringsConcat(Ports, ','));
|
2015-01-24 03:45:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ports;
|
|
|
|
}
|
2016-04-14 05:40:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
|