project64/Source/Common/IniFile.cpp

908 lines
26 KiB
C++
Raw Normal View History

2021-04-14 05:34:15 +00:00
#include "IniFile.h"
#include "StdString.h"
#include <stdlib.h>
#include <stdarg.h>
CIniFileBase::CIniFileBase(CFileBase & FileObject, const char * FileName) :
m_lastSectionSearch(0),
m_CurrentSectionFilePos(0),
m_LineFeed("\r\n"),
m_ReadOnly(true),
m_InstantFlush(true),
m_File(FileObject),
m_FileName(FileName),
m_CurrentSectionDirty(false),
2021-04-12 11:35:39 +00:00
m_SortFunction(nullptr)
{
}
CIniFileBase::~CIniFileBase(void)
{
2015-10-25 10:50:28 +00:00
SaveCurrentSection();
}
2016-01-12 18:38:10 +00:00
void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
{
2016-01-12 18:38:10 +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);
end = (long)m_File.GetPosition();
2015-10-25 10:50:28 +00:00
if (NoOfSpaces > 0)
{
std::string SpaceBuffer = FormatStr("%*c", NoOfSpaces, ' ');
2015-10-25 10:50:28 +00:00
do
{
2015-10-25 10:50:28 +00:00
SizeToRead = end - Pos;
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
if (SizeToRead > 0)
{
2016-01-12 18:38:10 +00:00
m_File.Seek(SizeToRead * -1, CFileBase::current);
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;
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);
WritePos = Pos;
2015-10-25 10:50:28 +00:00
do
{
2015-10-25 10:50:28 +00:00
SizeToRead = end - ReadPos;
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);
std::string SpaceBuffer = FormatStr("%*c", (NoOfSpaces * -1), ' ');
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
}
}
int CIniFileBase::GetStringFromFile(char * & String, std::unique_ptr<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos)
{
2015-10-25 10:50:28 +00:00
enum { BufferIncrease = 0x2000 };
if (MaxDataSize == 0)
{
ReadPos = 0;
MaxDataSize = BufferIncrease;
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
{
if (Data.get()[count] == '\n')
2015-10-25 10:50:28 +00:00
{
int len = (count - ReadPos) + 1;
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
{
memmove(Data.get(), &Data.get()[ReadPos], DataSize - ReadPos);
2015-10-25 10:50:28 +00:00
}
DataSize -= ReadPos;
ReadPos = 0;
}
else
{
// 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;
}
memcpy(NewBuffer, Data.get(), DataSize);
2015-10-25 10:50:28 +00:00
MaxDataSize = NewMaxDataSize;
Data.reset(NewBuffer);
2015-10-25 10:50:28 +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;
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;
}
}
2016-01-12 18:38:10 +00:00
void CIniFileBase::SaveCurrentSection(void)
{
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)
{
// 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;
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()));
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
{
// Increase/decrease space needed
2015-10-25 10:50:28 +00:00
int NeededBufferLen = 0;
{
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;
std::unique_ptr<char> Data;
2021-04-12 11:35:39 +00:00
char *Input = nullptr;
2015-10-25 10:50:28 +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;
do
{
2016-01-12 18:38:10 +00:00
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
2015-10-25 10:50:28 +00:00
if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1 || Input[0] != '[')
{
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);
}
// Set pointer to beginning of the start POS
2015-10-25 10:50:28 +00:00
m_File.Seek(StartPos, CFileBase::begin);
}
{
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)
{
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
{
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
2015-10-25 10:50:28 +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();
}
bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection)
{
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 = "";
}
std::unique_ptr<char> Data;
2021-04-12 11:35:39 +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;
uint8_t pUTF8[3];
2015-10-25 10:50:28 +00:00
pUTF8[0] = 0xef;
pUTF8[1] = 0xbb;
pUTF8[2] = 0xbf;
do
{
2016-01-12 18:38:10 +00:00
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
2015-10-25 10:50:28 +00:00
if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1) { continue; }
// 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;
}
if (CurrentSection[0] != '[') { continue; }
int lineEndPos = (int)strlen(CurrentSection) - 1;
if (CurrentSection[lineEndPos] != ']') { continue; }
// Take off the ']' from the end of the string
2015-10-25 10:50:28 +00:00
CurrentSection[lineEndPos] = 0;
CurrentSection += 1;
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);
}
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();
do
{
2016-01-12 18:38:10 +00:00
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
2015-10-25 10:50:28 +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, '=');
2021-04-12 11:35:39 +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;
}
const char * CIniFileBase::CleanLine(char * Line)
{
2015-10-25 10:50:28 +00:00
char * Pos = Line;
// 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;
}
}
}
// 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
{
if (Line[count] != ' ' && Line[count] != '\r') { break; }
Line[count] = 0;
}
return Line;
}
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
}
}
void CIniFileBase::OpenIniFile(bool bCreate)
{
// 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);
}
bool CIniFileBase::IsEmpty()
{
2015-10-25 10:50:28 +00:00
if (m_File.GetLength() == 0)
return true;
return false;
}
2016-01-12 18:38:10 +00:00
bool CIniFileBase::IsFileOpen(void)
2015-10-25 10:50:28 +00:00
{
return m_File.IsOpen();
}
bool CIniFileBase::IsReadOnly(void)
{
return m_ReadOnly;
}
bool CIniFileBase::DeleteSection(const char * lpSectionName)
{
CGuard Guard(m_CS);
2015-10-25 10:50:28 +00:00
if (!m_File.IsOpen()) { return false; }
SaveCurrentSection();
if (!MoveToSectionNameData(lpSectionName, true))
2015-10-25 10:50:28 +00:00
{
return false;
}
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
long DeleteSectionStart = (long)(m_CurrentSectionFilePos - (strlen(lpSectionName) + strlen(m_LineFeed) + 2));
long NextSectionStart = -1;
2015-10-25 10:50:28 +00:00
{
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, NextLine = 0, result;
std::unique_ptr <char> Data;
2021-04-12 11:35:39 +00:00
char *Input = nullptr;
do
{
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1) { continue; }
if (Input[0] != '[')
{
NextLine = (int)((m_File.GetPosition() - DataSize) + ReadPos);
continue;
}
NextSectionStart = NextLine != 0 ? NextLine : (long)((m_File.GetPosition() - DataSize) + ReadPos);
break;
} while (result >= 0);
}
2015-10-25 10:50:28 +00:00
if (NextSectionStart != -1)
2015-10-25 10:50:28 +00:00
{
m_File.Seek(0, CFileBase::end);
long end = (long)m_File.GetPosition();
long ReadPos = NextSectionStart;
long WritePos = DeleteSectionStart;
2015-10-25 10:50:28 +00:00
enum { fIS_MvSize = 0x2000 };
unsigned char Data[fIS_MvSize + 1];
int SizeToRead;
do
{
SizeToRead = end - ReadPos;
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
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
{
m_File.Seek(DeleteSectionStart, CFileBase::begin);
m_File.Flush();
m_File.SetEndOfFile();
2015-10-25 10:50:28 +00:00
}
m_File.Flush();
ClearSectionPosList(0);
m_CurrentSection = "";
m_CurrentSectionData.clear();
m_CurrentSectionFilePos = -1;
2015-10-25 10:50:28 +00:00
return true;
}
bool CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, std::string & Value)
{
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;
}
std::string CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault)
{
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;
}
uint32_t CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize)
{
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())
{
strncpy(lpReturnedString, iter->second.c_str(), nSize - 1);
2015-10-25 10:50:28 +00:00
lpReturnedString[nSize - 1] = 0;
return (uint32_t)strlen(lpReturnedString);
2015-10-25 10:50:28 +00:00
}
}
strncpy(lpReturnedString, lpDefault, nSize - 1);
2015-10-25 10:50:28 +00:00
lpReturnedString[nSize - 1] = 0;
return (uint32_t)strlen(lpReturnedString);
}
uint32_t CIniFileBase::GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault)
{
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;
}
bool CIniFileBase::GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value)
{
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;
}
void CIniFileBase::SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString)
{
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();
}
}
void CIniFileBase::SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value)
{
if (m_ReadOnly)
{
return;
}
// Translate the string to an ASCII version and save as text
SaveString(lpSectionName, lpKeyName, FormatStr("%d", Value).c_str());
}
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)
{
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)
{
2015-10-25 10:50:28 +00:00
CGuard Guard(m_CS);
SaveCurrentSection();
}
2016-01-12 18:38:10 +00:00
void CIniFileBase::SetAutoFlush(bool AutoFlush)
{
2015-10-25 10:50:28 +00:00
m_InstantFlush = AutoFlush;
if (AutoFlush)
{
FlushChanges();
}
}
void CIniFileBase::GetKeyList(const char * lpSectionName, strlist &List)
{
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);
}
}
}
void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & List)
{
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;
}
2016-01-12 18:38:10 +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;
std::unique_ptr<char> Data;
2021-04-12 11:35:39 +00:00
char *Input = nullptr;
do
{
2016-01-12 18:38:10 +00:00
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
2015-10-25 10:50:28 +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, '=');
2021-04-12 11:35:39 +00:00
if (Pos == nullptr) { continue; }
2015-10-25 10:50:28 +00:00
Pos[0] = 0;
List.insert(KeyValueData::value_type(stdstr(Input).Trim(), &Pos[1]));
2015-10-25 10:50:28 +00:00
} while (result >= 0);
}
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)
{
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;
}
}
2016-01-12 18:38:10 +00:00
void CIniFileBase::GetVectorOfSections(SectionList & sections)
{
2015-10-25 10:50:28 +00:00
sections.clear();
CGuard Guard(m_CS);
if (!m_File.IsOpen())
{
return;
}
{
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++)
{
sections.insert(iter->first);
2015-10-25 10:50:28 +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)
{
vsprintf(buffer, strFormat, args);
FormatedStr = buffer;
}
va_end(args);
return FormatedStr;
}