2021-04-14 05:34:15 +00:00
|
|
|
#include "IniFile.h"
|
2022-07-11 10:00:25 +00:00
|
|
|
#include "StdString.h"
|
2020-04-13 02:54:21 +00:00
|
|
|
#include <stdarg.h>
|
2022-10-03 08:04:42 +00:00
|
|
|
#include <stdlib.h>
|
2020-04-13 02:54:21 +00:00
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
CIniFileBase::CIniFileBase(CFileBase & FileObject, const char * FileName) :
|
2016-04-28 07:22:59 +00:00
|
|
|
m_lastSectionSearch(0),
|
|
|
|
m_CurrentSectionFilePos(0),
|
|
|
|
m_LineFeed("\r\n"),
|
|
|
|
m_ReadOnly(true),
|
|
|
|
m_InstantFlush(true),
|
|
|
|
m_File(FileObject),
|
|
|
|
m_FileName(FileName),
|
2019-04-18 07:27:20 +00:00
|
|
|
m_CurrentSectionDirty(false),
|
2021-04-12 11:35:39 +00:00
|
|
|
m_SortFunction(nullptr)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CIniFileBase::~CIniFileBase(void)
|
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
SaveCurrentSection();
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
fIS_MvSize = 0x2000
|
|
|
|
};
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
unsigned char Data[fIS_MvSize + 1];
|
|
|
|
int SizeToRead, result;
|
|
|
|
long end, WritePos;
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(0, CFileBase::end);
|
2022-07-11 10:00:25 +00:00
|
|
|
end = (long)m_File.GetPosition();
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
if (NoOfSpaces > 0)
|
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
std::string SpaceBuffer = FormatStr("%*c", NoOfSpaces, ' ');
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-11-14 07:10:43 +00:00
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
SizeToRead = end - Pos;
|
2022-10-03 08:04:42 +00:00
|
|
|
if (SizeToRead > fIS_MvSize)
|
|
|
|
{
|
|
|
|
SizeToRead = fIS_MvSize;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
if (SizeToRead > 0)
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(SizeToRead * -1, CFileBase::current);
|
2022-07-11 10:00:25 +00:00
|
|
|
WritePos = (long)m_File.GetPosition();
|
2016-01-12 18:38:10 +00:00
|
|
|
memset(Data, 0, sizeof(Data));
|
|
|
|
result = m_File.Read(Data, SizeToRead);
|
|
|
|
m_File.Seek(WritePos, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
end = WritePos;
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
m_File.Write(SpaceBuffer.c_str(), (uint32_t)SpaceBuffer.length());
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Write(Data, result);
|
|
|
|
m_File.Seek(WritePos, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
} while (SizeToRead > 0);
|
|
|
|
}
|
|
|
|
if (NoOfSpaces < 0)
|
|
|
|
{
|
|
|
|
int ReadPos = Pos + (NoOfSpaces * -1);
|
2018-11-18 01:06:02 +00:00
|
|
|
WritePos = Pos;
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-11-14 07:10:43 +00:00
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
SizeToRead = end - ReadPos;
|
2022-10-03 08:04:42 +00:00
|
|
|
if (SizeToRead > fIS_MvSize)
|
|
|
|
{
|
|
|
|
SizeToRead = fIS_MvSize;
|
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(ReadPos, CFileBase::begin);
|
|
|
|
m_File.Read(Data, SizeToRead);
|
|
|
|
m_File.Seek(WritePos, CFileBase::begin);
|
|
|
|
m_File.Write(Data, SizeToRead);
|
2015-10-25 10:50:28 +00:00
|
|
|
ReadPos += SizeToRead;
|
|
|
|
WritePos += SizeToRead;
|
|
|
|
} while (SizeToRead > 0);
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(WritePos, CFileBase::begin);
|
2020-04-13 02:54:21 +00:00
|
|
|
std::string SpaceBuffer = FormatStr("%*c", (NoOfSpaces * -1), ' ');
|
2016-01-12 18:46:50 +00:00
|
|
|
m_File.Write(SpaceBuffer.c_str(), (uint32_t)SpaceBuffer.length());
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(WritePos, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
m_File.SetEndOfFile();
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(0, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
int CIniFileBase::GetStringFromFile(char *& String, std::unique_ptr<char> & Data, int & MaxDataSize, int & DataSize, int & ReadPos)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
BufferIncrease = 0x2000
|
|
|
|
};
|
2015-10-25 10:50:28 +00:00
|
|
|
if (MaxDataSize == 0)
|
|
|
|
{
|
|
|
|
ReadPos = 0;
|
|
|
|
MaxDataSize = BufferIncrease;
|
2016-11-14 07:10:43 +00:00
|
|
|
Data.reset(new char[MaxDataSize]);
|
|
|
|
DataSize = m_File.Read(&Data.get()[DataSize], MaxDataSize);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
for (count = ReadPos; count < DataSize; count++)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-11-14 07:10:43 +00:00
|
|
|
if (Data.get()[count] == '\n')
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
int len = (count - ReadPos) + 1;
|
2016-11-14 07:10:43 +00:00
|
|
|
String = &Data.get()[ReadPos];
|
2016-01-12 18:38:10 +00:00
|
|
|
String[len - 1] = 0;
|
2015-10-25 10:50:28 +00:00
|
|
|
ReadPos = count + 1;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ReadPos != 0)
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if ((DataSize - ReadPos) > 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-11-14 07:10:43 +00:00
|
|
|
memmove(Data.get(), &Data.get()[ReadPos], DataSize - ReadPos);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
DataSize -= ReadPos;
|
|
|
|
ReadPos = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 23:11:25 +00:00
|
|
|
// Increase buffer size
|
2015-10-25 10:50:28 +00:00
|
|
|
int NewMaxDataSize = MaxDataSize + BufferIncrease;
|
|
|
|
char * NewBuffer = new char[NewMaxDataSize];
|
2021-04-12 11:35:39 +00:00
|
|
|
if (NewBuffer == nullptr)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-14 07:10:43 +00:00
|
|
|
memcpy(NewBuffer, Data.get(), DataSize);
|
2015-10-25 10:50:28 +00:00
|
|
|
MaxDataSize = NewMaxDataSize;
|
2016-11-14 07:10:43 +00:00
|
|
|
Data.reset(NewBuffer);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 07:10:43 +00:00
|
|
|
int dwRead = m_File.Read(&Data.get()[DataSize], MaxDataSize - DataSize);
|
2015-10-25 10:50:28 +00:00
|
|
|
if (dwRead == 0)
|
|
|
|
{
|
|
|
|
if (DataSize > 0)
|
|
|
|
{
|
|
|
|
int len = DataSize + 1;
|
2016-11-14 07:10:43 +00:00
|
|
|
String = &Data.get()[ReadPos];
|
2016-01-12 18:38:10 +00:00
|
|
|
String[len - 1] = 0;
|
2015-10-25 10:50:28 +00:00
|
|
|
DataSize = 0;
|
|
|
|
ReadPos = 0;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
DataSize += dwRead;
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void CIniFileBase::SaveCurrentSection(void)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2022-07-11 10:00:25 +00:00
|
|
|
if (!m_CurrentSectionDirty || m_ReadOnly)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_CurrentSectionDirty = false;
|
|
|
|
if (m_CurrentSection.length() == 0)
|
|
|
|
{
|
|
|
|
m_CurrentSection = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
int lineFeedLen = (int)strlen(m_LineFeed);
|
|
|
|
|
|
|
|
if (m_CurrentSectionFilePos == -1)
|
|
|
|
{
|
2021-03-16 23:11:25 +00:00
|
|
|
// Section has not been added yet
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(0, CFileBase::end);
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
int len = (int)m_CurrentSection.length() + (lineFeedLen * 2) + 5;
|
2020-04-13 02:54:21 +00:00
|
|
|
std::unique_ptr<char> SectionName(new char[len]);
|
2015-10-25 10:50:28 +00:00
|
|
|
if (m_File.GetLength() < (int)strlen(m_LineFeed))
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
sprintf(SectionName.get(), "[%s]%s", m_CurrentSection.c_str(), m_LineFeed);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
sprintf(SectionName.get(), "%s[%s]%s", m_LineFeed, m_CurrentSection.c_str(), m_LineFeed);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Write(SectionName.get(), (int)strlen(SectionName.get()));
|
2022-07-11 10:00:25 +00:00
|
|
|
m_CurrentSectionFilePos = (long)m_File.GetPosition();
|
2016-01-12 18:38:10 +00:00
|
|
|
m_SectionsPos.insert(FILELOC::value_type(m_CurrentSection, m_CurrentSectionFilePos));
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-16 23:11:25 +00:00
|
|
|
// Increase/decrease space needed
|
2015-10-25 10:50:28 +00:00
|
|
|
int NeededBufferLen = 0;
|
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
std::unique_ptr<char> LineData;
|
2015-10-25 10:50:28 +00:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
|
|
|
|
{
|
|
|
|
int newLen = (int)iter->first.length() + (int)iter->second.length() + lineFeedLen + 5;
|
|
|
|
if (newLen > len)
|
|
|
|
{
|
|
|
|
LineData.reset(new char[newLen]);
|
|
|
|
len = newLen;
|
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
sprintf(LineData.get(), "%s=%s%s", iter->first.c_str(), iter->second.c_str(), m_LineFeed);
|
2015-10-25 10:50:28 +00:00
|
|
|
NeededBufferLen += (int)strlen(LineData.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int currentLen = 0;
|
|
|
|
|
|
|
|
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
|
|
|
|
|
|
|
|
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
|
2020-04-13 02:54:21 +00:00
|
|
|
std::unique_ptr<char> Data;
|
2022-10-03 08:04:42 +00:00
|
|
|
char * Input = nullptr;
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2021-03-16 23:11:25 +00:00
|
|
|
// Skip first line as it is the section name
|
2015-10-25 10:50:28 +00:00
|
|
|
int StartPos = m_CurrentSectionFilePos;
|
|
|
|
int EndPos = StartPos;
|
2016-11-14 07:10:43 +00:00
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
|
2022-10-03 08:04:42 +00:00
|
|
|
if (result <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
if (strlen(CleanLine(Input)) <= 1 || Input[0] != '[')
|
|
|
|
{
|
2022-07-11 10:00:25 +00:00
|
|
|
EndPos = (long)((m_File.GetPosition() - DataSize) + ReadPos);
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Input[0] == '[')
|
|
|
|
{
|
|
|
|
NeededBufferLen += lineFeedLen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} while (result >= 0);
|
|
|
|
currentLen = EndPos - StartPos;
|
|
|
|
|
|
|
|
if (NeededBufferLen != currentLen)
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
fInsertSpaces(StartPos, NeededBufferLen - currentLen);
|
2015-10-25 10:50:28 +00:00
|
|
|
m_File.Flush();
|
|
|
|
ClearSectionPosList(StartPos);
|
|
|
|
}
|
2021-03-16 23:11:25 +00:00
|
|
|
// Set pointer to beginning of the start POS
|
2015-10-25 10:50:28 +00:00
|
|
|
m_File.Seek(StartPos, CFileBase::begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
std::unique_ptr<char> LineData;
|
2015-10-25 10:50:28 +00:00
|
|
|
int len = 0;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (m_SortFunction != nullptr)
|
2019-04-18 07:27:20 +00:00
|
|
|
{
|
|
|
|
KeyValueVector data;
|
|
|
|
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
|
|
|
|
{
|
|
|
|
data.push_back(KeyValueItem(&iter->first, &iter->second));
|
|
|
|
}
|
|
|
|
m_SortFunction(data);
|
|
|
|
for (size_t i = 0, n = data.size(); i < n; i++)
|
|
|
|
{
|
|
|
|
KeyValueItem & item = data[i];
|
|
|
|
int newLen = (int)(item.first->length()) + (int)item.second->length() + lineFeedLen + 5;
|
|
|
|
if (newLen > len)
|
|
|
|
{
|
|
|
|
LineData.reset(new char[newLen]);
|
|
|
|
len = newLen;
|
|
|
|
}
|
|
|
|
sprintf(LineData.get(), "%s=%s%s", item.first->c_str(), item.second->c_str(), m_LineFeed);
|
|
|
|
m_File.Write(LineData.get(), (int)strlen(LineData.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2019-04-18 07:27:20 +00:00
|
|
|
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2019-04-18 07:27:20 +00:00
|
|
|
int newLen = (int)iter->first.length() + (int)iter->second.length() + lineFeedLen + 5;
|
|
|
|
if (newLen > len)
|
|
|
|
{
|
|
|
|
LineData.reset(new char[newLen]);
|
|
|
|
len = newLen;
|
|
|
|
}
|
|
|
|
sprintf(LineData.get(), "%s=%s%s", iter->first.c_str(), iter->second.c_str(), m_LineFeed);
|
|
|
|
m_File.Write(LineData.get(), (int)strlen(LineData.get()));
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_File.Flush();
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if (strcmp(lpSectionName, m_CurrentSection.c_str()) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ChangeCurrentSection)
|
|
|
|
{
|
|
|
|
SaveCurrentSection();
|
|
|
|
m_CurrentSection = "";
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:54:21 +00:00
|
|
|
std::unique_ptr<char> Data;
|
2022-10-03 08:04:42 +00:00
|
|
|
char * Input = nullptr;
|
2015-10-25 10:50:28 +00:00
|
|
|
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
|
|
|
|
|
|
|
|
FILELOC_ITR iter = m_SectionsPos.find(std::string(lpSectionName));
|
|
|
|
bool bFoundSection = false;
|
|
|
|
if (iter != m_SectionsPos.end())
|
|
|
|
{
|
|
|
|
if (ChangeCurrentSection)
|
|
|
|
{
|
|
|
|
m_CurrentSection = iter->first;
|
|
|
|
m_CurrentSectionFilePos = iter->second;
|
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(iter->second, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
bFoundSection = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_File.Seek(m_lastSectionSearch, CFileBase::begin);
|
|
|
|
|
|
|
|
//long Fpos;
|
2016-01-12 18:46:50 +00:00
|
|
|
uint8_t pUTF8[3];
|
2015-10-25 10:50:28 +00:00
|
|
|
pUTF8[0] = 0xef;
|
|
|
|
pUTF8[1] = 0xbb;
|
|
|
|
pUTF8[2] = 0xbf;
|
|
|
|
|
2016-11-14 07:10:43 +00:00
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
|
2022-10-03 08:04:42 +00:00
|
|
|
if (result <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strlen(CleanLine(Input)) <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2021-03-16 23:11:25 +00:00
|
|
|
// We only care about sections
|
2015-10-25 10:50:28 +00:00
|
|
|
char * CurrentSection = Input;
|
|
|
|
|
|
|
|
if (m_lastSectionSearch == 0 && !memcmp(CurrentSection, pUTF8, 3))
|
|
|
|
{
|
|
|
|
CurrentSection += 3;
|
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
if (CurrentSection[0] != '[')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
int lineEndPos = (int)strlen(CurrentSection) - 1;
|
2022-10-03 08:04:42 +00:00
|
|
|
if (CurrentSection[lineEndPos] != ']')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-16 23:11:25 +00:00
|
|
|
// Take off the ']' from the end of the string
|
2015-10-25 10:50:28 +00:00
|
|
|
CurrentSection[lineEndPos] = 0;
|
|
|
|
CurrentSection += 1;
|
2022-07-11 10:00:25 +00:00
|
|
|
m_lastSectionSearch = (long)((m_File.GetPosition() - DataSize) + ReadPos);
|
2016-01-12 18:38:10 +00:00
|
|
|
m_SectionsPos.insert(FILELOC::value_type(CurrentSection, m_lastSectionSearch));
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
if (_stricmp(lpSectionName, CurrentSection) != 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChangeCurrentSection)
|
|
|
|
{
|
|
|
|
m_CurrentSection = lpSectionName;
|
|
|
|
m_CurrentSectionFilePos = m_lastSectionSearch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(m_lastSectionSearch, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
bFoundSection = true;
|
|
|
|
break;
|
|
|
|
} while (result >= 0);
|
|
|
|
}
|
|
|
|
|
2018-11-18 01:06:02 +00:00
|
|
|
if (!bFoundSection && strcmp(lpSectionName, "default") == 0)
|
|
|
|
{
|
|
|
|
m_SectionsPos.insert(FILELOC::value_type(lpSectionName, 0));
|
|
|
|
if (ChangeCurrentSection)
|
|
|
|
{
|
|
|
|
m_CurrentSection = lpSectionName;
|
|
|
|
m_CurrentSectionFilePos = 0;
|
|
|
|
}
|
|
|
|
m_File.Seek(m_lastSectionSearch, CFileBase::begin);
|
|
|
|
bFoundSection = true;
|
|
|
|
}
|
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
if (bFoundSection && ChangeCurrentSection)
|
|
|
|
{
|
|
|
|
m_CurrentSectionData.clear();
|
2016-11-14 07:10:43 +00:00
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
|
2022-10-03 08:04:42 +00:00
|
|
|
if (result <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strlen(CleanLine(Input)) <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Input[0] == '[')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
char * Pos = strchr(Input, '=');
|
2022-10-03 08:04:42 +00:00
|
|
|
if (Pos == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
char * Value = &Pos[1];
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
char * Pos1 = Pos - 1;
|
2015-10-25 10:50:28 +00:00
|
|
|
while (((*Pos1 == ' ') || (*Pos1 == '\t')) && (Pos1 > Input))
|
|
|
|
{
|
|
|
|
Pos1--;
|
|
|
|
}
|
|
|
|
Pos1[1] = 0;
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
m_CurrentSectionData.insert(KeyValueList::value_type(Input, Value));
|
2015-10-25 10:50:28 +00:00
|
|
|
} while (result >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bFoundSection;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 11:14:29 +00:00
|
|
|
const char * CIniFileBase::CleanLine(char * Line)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
char * Pos = Line;
|
|
|
|
|
2021-03-16 23:11:25 +00:00
|
|
|
// Remove any comment from the line
|
2021-04-12 11:35:39 +00:00
|
|
|
while (Pos != nullptr)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
Pos = strchr(Pos, '/');
|
2021-04-12 11:35:39 +00:00
|
|
|
if (Pos != nullptr)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
if (Pos[1] == '/')
|
|
|
|
{
|
|
|
|
if (Pos > Line)
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
char * Pos_1 = Pos - 1;
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
if (Pos_1[0] != ':')
|
|
|
|
{
|
|
|
|
Pos[0] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Pos += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Pos[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Pos += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 23:11:25 +00:00
|
|
|
// Strip any spaces or line feeds from the end of the line
|
2016-01-12 18:38:10 +00:00
|
|
|
for (int count = (int)strlen(&Line[0]) - 1; count >= 0; count--)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
if (Line[count] != ' ' && Line[count] != '\r')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
Line[count] = 0;
|
|
|
|
}
|
|
|
|
return Line;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CIniFileBase::OpenIniFileReadOnly()
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if (m_File.Open(m_FileName.c_str(), CFileBase::modeRead))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
m_ReadOnly = true;
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(0, CFileBase::begin);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CIniFileBase::OpenIniFile(bool bCreate)
|
|
|
|
{
|
2021-03-16 23:11:25 +00:00
|
|
|
// Open for reading/writing
|
2015-10-25 10:50:28 +00:00
|
|
|
m_ReadOnly = false;
|
2016-01-12 18:38:10 +00:00
|
|
|
if (!m_File.Open(m_FileName.c_str(), CFileBase::modeReadWrite | CFileBase::shareDenyWrite))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if (!m_File.Open(m_FileName.c_str(), CFileBase::modeRead))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if (bCreate)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if (!m_File.Open(m_FileName.c_str(), CFileBase::modeReadWrite | CFileBase::modeCreate | CFileBase::shareDenyWrite))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ReadOnly = true;
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
m_File.Seek(0, CFileBase::begin);
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CIniFileBase::IsEmpty()
|
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
if (m_File.GetLength() == 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
bool CIniFileBase::IsFileOpen(void)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
return m_File.IsOpen();
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
bool CIniFileBase::IsReadOnly(void)
|
|
|
|
{
|
|
|
|
return m_ReadOnly;
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
bool CIniFileBase::DeleteSection(const char * lpSectionName)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
CGuard Guard(m_CS);
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
if (!m_File.IsOpen())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-12 06:21:23 +00:00
|
|
|
|
2020-04-13 02:54:21 +00:00
|
|
|
SaveCurrentSection();
|
|
|
|
if (!MoveToSectionNameData(lpSectionName, true))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-09-29 11:14:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-14 07:10:43 +00:00
|
|
|
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
|
2022-07-11 10:00:25 +00:00
|
|
|
long DeleteSectionStart = (long)(m_CurrentSectionFilePos - (strlen(lpSectionName) + strlen(m_LineFeed) + 2));
|
2020-04-13 02:54:21 +00:00
|
|
|
long NextSectionStart = -1;
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, NextLine = 0, result;
|
2022-10-03 08:04:42 +00:00
|
|
|
std::unique_ptr<char> Data;
|
|
|
|
char * Input = nullptr;
|
2020-04-13 02:54:21 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
|
2022-10-03 08:04:42 +00:00
|
|
|
if (result <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strlen(CleanLine(Input)) <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-29 11:14:29 +00:00
|
|
|
|
2020-10-12 06:21:23 +00:00
|
|
|
if (Input[0] != '[')
|
|
|
|
{
|
2022-07-11 10:00:25 +00:00
|
|
|
NextLine = (int)((m_File.GetPosition() - DataSize) + ReadPos);
|
2020-10-12 06:21:23 +00:00
|
|
|
continue;
|
2020-04-13 02:54:21 +00:00
|
|
|
}
|
2022-07-11 10:00:25 +00:00
|
|
|
NextSectionStart = NextLine != 0 ? NextLine : (long)((m_File.GetPosition() - DataSize) + ReadPos);
|
2020-04-13 02:54:21 +00:00
|
|
|
break;
|
|
|
|
} while (result >= 0);
|
2016-09-29 11:14:29 +00:00
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2020-04-13 02:54:21 +00:00
|
|
|
if (NextSectionStart != -1)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
m_File.Seek(0, CFileBase::end);
|
2022-07-11 10:00:25 +00:00
|
|
|
long end = (long)m_File.GetPosition();
|
2020-04-13 02:54:21 +00:00
|
|
|
long ReadPos = NextSectionStart;
|
|
|
|
long WritePos = DeleteSectionStart;
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
fIS_MvSize = 0x2000
|
|
|
|
};
|
2020-04-13 02:54:21 +00:00
|
|
|
unsigned char Data[fIS_MvSize + 1];
|
|
|
|
int SizeToRead;
|
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
SizeToRead = end - ReadPos;
|
2022-10-03 08:04:42 +00:00
|
|
|
if (SizeToRead > fIS_MvSize)
|
|
|
|
{
|
|
|
|
SizeToRead = fIS_MvSize;
|
|
|
|
}
|
2020-04-13 02:54:21 +00:00
|
|
|
m_File.Seek(ReadPos, CFileBase::begin);
|
|
|
|
m_File.Read(Data, SizeToRead);
|
|
|
|
m_File.Seek(WritePos, CFileBase::begin);
|
|
|
|
m_File.Write(Data, SizeToRead);
|
|
|
|
ReadPos += SizeToRead;
|
|
|
|
WritePos += SizeToRead;
|
|
|
|
} while (SizeToRead > 0);
|
|
|
|
m_File.Seek(DeleteSectionStart + (end - NextSectionStart), CFileBase::begin);
|
|
|
|
m_File.Flush();
|
|
|
|
m_File.SetEndOfFile();
|
|
|
|
}
|
|
|
|
else
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
m_File.Seek(DeleteSectionStart, CFileBase::begin);
|
|
|
|
m_File.Flush();
|
|
|
|
m_File.SetEndOfFile();
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
m_File.Flush();
|
2020-04-13 02:54:21 +00:00
|
|
|
ClearSectionPosList(0);
|
|
|
|
m_CurrentSection = "";
|
|
|
|
m_CurrentSectionData.clear();
|
|
|
|
m_CurrentSectionFilePos = -1;
|
2015-10-25 10:50:28 +00:00
|
|
|
return true;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:39:34 +00:00
|
|
|
bool CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, std::string & Value)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
lpSectionName = "default";
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName, true))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
|
|
|
|
if (iter != m_CurrentSectionData.end())
|
|
|
|
{
|
|
|
|
Value = iter->second.c_str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value = lpDefault;
|
|
|
|
return false;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:39:34 +00:00
|
|
|
std::string CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2018-03-26 19:39:34 +00:00
|
|
|
std::string Value;
|
2016-01-12 18:38:10 +00:00
|
|
|
GetString(lpSectionName, lpKeyName, lpDefault, Value);
|
2015-10-25 10:50:28 +00:00
|
|
|
return Value;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
uint32_t CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
|
|
|
|
std::string strSection;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
strSection = "default";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strSection = lpSectionName;
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
if (m_File.IsOpen() && MoveToSectionNameData(strSection.c_str(), true))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
|
|
|
|
if (iter != m_CurrentSectionData.end())
|
|
|
|
{
|
2016-01-12 18:46:50 +00:00
|
|
|
strncpy(lpReturnedString, iter->second.c_str(), nSize - 1);
|
2015-10-25 10:50:28 +00:00
|
|
|
lpReturnedString[nSize - 1] = 0;
|
2016-01-12 18:46:50 +00:00
|
|
|
return (uint32_t)strlen(lpReturnedString);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 18:46:50 +00:00
|
|
|
strncpy(lpReturnedString, lpDefault, nSize - 1);
|
2015-10-25 10:50:28 +00:00
|
|
|
lpReturnedString[nSize - 1] = 0;
|
2016-01-12 18:46:50 +00:00
|
|
|
return (uint32_t)strlen(lpReturnedString);
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
uint32_t CIniFileBase::GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
uint32_t Value;
|
2016-01-12 18:38:10 +00:00
|
|
|
GetNumber(lpSectionName, lpKeyName, nDefault, Value);
|
2015-10-25 10:50:28 +00:00
|
|
|
return Value;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
bool CIniFileBase::GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
lpSectionName = "default";
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName, true))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
|
|
|
|
if (iter != m_CurrentSectionData.end())
|
|
|
|
{
|
|
|
|
Value = 0;
|
|
|
|
sscanf(iter->second.c_str(), "%u", &Value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value = nDefault;
|
|
|
|
return false;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
void CIniFileBase::SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2022-07-11 10:00:25 +00:00
|
|
|
if (m_ReadOnly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
if (!m_File.IsOpen())
|
|
|
|
{
|
|
|
|
if (lpString)
|
|
|
|
{
|
|
|
|
OpenIniFile();
|
|
|
|
}
|
|
|
|
if (!m_File.IsOpen())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string strSection;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
strSection = "default";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strSection = lpSectionName;
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
if (!MoveToSectionNameData(strSection.c_str(), true))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
m_CurrentSection = strSection;
|
|
|
|
m_CurrentSectionData.clear();
|
|
|
|
m_CurrentSectionFilePos = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
|
|
|
|
if (iter != m_CurrentSectionData.end())
|
|
|
|
{
|
|
|
|
if (lpString)
|
|
|
|
{
|
|
|
|
if (iter->second != lpString)
|
|
|
|
{
|
|
|
|
iter->second = lpString;
|
|
|
|
m_CurrentSectionDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_CurrentSectionData.erase(iter);
|
|
|
|
m_CurrentSectionDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lpString)
|
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
m_CurrentSectionData.insert(KeyValueList::value_type(lpKeyName, lpString));
|
2015-10-25 10:50:28 +00:00
|
|
|
m_CurrentSectionDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_InstantFlush)
|
|
|
|
{
|
|
|
|
SaveCurrentSection();
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
void CIniFileBase::SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2022-07-11 10:00:25 +00:00
|
|
|
if (m_ReadOnly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-03-16 23:11:25 +00:00
|
|
|
// Translate the string to an ASCII version and save as text
|
2020-04-13 02:54:21 +00:00
|
|
|
SaveString(lpSectionName, lpKeyName, FormatStr("%d", Value).c_str());
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 07:22:59 +00:00
|
|
|
bool CIniFileBase::EntryExists(const char * lpSectionName, const char * lpKeyName)
|
|
|
|
{
|
|
|
|
CGuard Guard(m_CS);
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2016-04-28 07:22:59 +00:00
|
|
|
{
|
|
|
|
lpSectionName = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName, true))
|
|
|
|
{
|
|
|
|
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
|
|
|
|
if (iter != m_CurrentSectionData.end())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void CIniFileBase::FlushChanges(void)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
SaveCurrentSection();
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void CIniFileBase::SetAutoFlush(bool AutoFlush)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
m_InstantFlush = AutoFlush;
|
|
|
|
if (AutoFlush)
|
|
|
|
{
|
|
|
|
FlushChanges();
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
void CIniFileBase::GetKeyList(const char * lpSectionName, strlist & List)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
List.clear();
|
|
|
|
|
|
|
|
CGuard Guard(m_CS);
|
|
|
|
if (!m_File.IsOpen())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
lpSectionName = "default";
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
if (MoveToSectionNameData(lpSectionName, true))
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
|
|
|
|
{
|
|
|
|
List.push_back(iter->first);
|
|
|
|
}
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:46:50 +00:00
|
|
|
void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & List)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
if (!m_File.IsOpen())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string strSection;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (lpSectionName == nullptr || strlen(lpSectionName) == 0)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
strSection = "default";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strSection = lpSectionName;
|
|
|
|
}
|
|
|
|
|
2022-10-03 08:04:42 +00:00
|
|
|
if (!MoveToSectionNameData(strSection.c_str(), false))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
|
|
|
|
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
|
2022-07-11 10:00:25 +00:00
|
|
|
std::unique_ptr<char> Data;
|
2022-10-03 08:04:42 +00:00
|
|
|
char * Input = nullptr;
|
2016-11-14 07:10:43 +00:00
|
|
|
do
|
2016-09-29 11:14:29 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
|
2022-10-03 08:04:42 +00:00
|
|
|
if (result <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strlen(CleanLine(Input)) <= 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Input[0] == '[')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-01-12 18:38:10 +00:00
|
|
|
char * Pos = strchr(Input, '=');
|
2022-10-03 08:04:42 +00:00
|
|
|
if (Pos == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
Pos[0] = 0;
|
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
List.insert(KeyValueData::value_type(stdstr(Input).Trim(), &Pos[1]));
|
2015-10-25 10:50:28 +00:00
|
|
|
} while (result >= 0);
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 07:27:20 +00:00
|
|
|
void CIniFileBase::SetCustomSort(SortData SortFunction)
|
|
|
|
{
|
|
|
|
CGuard Guard(m_CS);
|
|
|
|
m_SortFunction = SortFunction;
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void CIniFileBase::ClearSectionPosList(long FilePos)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
if (FilePos <= 0)
|
|
|
|
{
|
|
|
|
m_SectionsPos.clear();
|
|
|
|
m_lastSectionSearch = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILELOC::iterator iter = m_SectionsPos.begin();
|
|
|
|
while (iter != m_SectionsPos.end())
|
|
|
|
{
|
|
|
|
FILELOC::iterator CurrentIter = iter;
|
2016-01-12 18:38:10 +00:00
|
|
|
iter++;
|
2015-10-25 10:50:28 +00:00
|
|
|
long TestFilePos = CurrentIter->second;
|
|
|
|
if (TestFilePos > FilePos)
|
|
|
|
{
|
|
|
|
m_SectionsPos.erase(CurrentIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_lastSectionSearch = FilePos;
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void CIniFileBase::GetVectorOfSections(SectionList & sections)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
sections.clear();
|
|
|
|
|
|
|
|
CGuard Guard(m_CS);
|
|
|
|
if (!m_File.IsOpen())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-04-13 02:54:21 +00:00
|
|
|
std::string DoesNotExist = FormatStr("DoesNotExist%d%d%d", rand(), rand(), rand());
|
2016-01-12 18:38:10 +00:00
|
|
|
MoveToSectionNameData(DoesNotExist.c_str(), false);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (FILELOC::const_iterator iter = m_SectionsPos.begin(); iter != m_SectionsPos.end(); iter++)
|
|
|
|
{
|
2020-10-12 06:21:23 +00:00
|
|
|
sections.insert(iter->first);
|
2015-10-25 10:50:28 +00:00
|
|
|
}
|
2020-04-13 02:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CIniFileBase::FormatStr(const char * strFormat, ...)
|
|
|
|
{
|
|
|
|
std::string FormatedStr;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, strFormat);
|
|
|
|
|
|
|
|
size_t nlen = _vscprintf(strFormat, args) + 1;
|
|
|
|
char * buffer = (char *)alloca(nlen * sizeof(char));
|
|
|
|
buffer[nlen - 1] = 0;
|
2021-04-12 11:35:39 +00:00
|
|
|
if (buffer != nullptr)
|
2020-04-13 02:54:21 +00:00
|
|
|
{
|
|
|
|
vsprintf(buffer, strFormat, args);
|
|
|
|
FormatedStr = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
return FormatedStr;
|
|
|
|
}
|