[Project64] Cleanup IniFileClass.cpp

This commit is contained in:
zilmar 2016-01-13 05:38:10 +11:00
parent 0a45420b4b
commit ba77c128ac
2 changed files with 139 additions and 139 deletions

View File

@ -3,14 +3,14 @@
#include <TChar.H> #include <TChar.H>
CIniFileBase::CIniFileBase(CFileBase & FileObject, LPCTSTR FileName) : CIniFileBase::CIniFileBase(CFileBase & FileObject, LPCTSTR FileName) :
m_lastSectionSearch(0), m_lastSectionSearch(0),
m_CurrentSectionFilePos(0), m_CurrentSectionFilePos(0),
m_LineFeed("\r\n"), m_LineFeed("\r\n"),
m_ReadOnly(true), m_ReadOnly(true),
m_InstantFlush(true), m_InstantFlush(true),
m_File(FileObject), m_File(FileObject),
m_FileName(FileName), m_FileName(FileName),
m_CurrentSectionDirty(false) m_CurrentSectionDirty(false)
{ {
} }
@ -19,36 +19,36 @@ CIniFileBase::~CIniFileBase(void)
SaveCurrentSection(); SaveCurrentSection();
} }
void CIniFileBase::fInsertSpaces ( int Pos, int NoOfSpaces ) void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
{ {
enum { fIS_MvSize = 0x2000 }; enum { fIS_MvSize = 0x2000 };
unsigned char Data[fIS_MvSize + 1]; unsigned char Data[fIS_MvSize + 1];
int SizeToRead, result; int SizeToRead, result;
long end, WritePos; long end, WritePos;
m_File.Seek(0,CFileBase::end); m_File.Seek(0, CFileBase::end);
end = m_File.GetPosition(); end = m_File.GetPosition();
if (NoOfSpaces > 0) if (NoOfSpaces > 0)
{ {
stdstr_f SpaceBuffer(_T("%*c"),NoOfSpaces,' '); stdstr_f SpaceBuffer(_T("%*c"), NoOfSpaces, ' ');
do { do {
SizeToRead = end - Pos; SizeToRead = end - Pos;
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; } if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
if (SizeToRead > 0) if (SizeToRead > 0)
{ {
m_File.Seek(SizeToRead * -1,CFileBase::current); m_File.Seek(SizeToRead * -1, CFileBase::current);
WritePos = m_File.GetPosition(); WritePos = m_File.GetPosition();
memset(Data,0,sizeof(Data)); memset(Data, 0, sizeof(Data));
result = m_File.Read(Data,SizeToRead); result = m_File.Read(Data, SizeToRead);
m_File.Seek(WritePos,CFileBase::begin); m_File.Seek(WritePos, CFileBase::begin);
end = WritePos; end = WritePos;
m_File.Write(SpaceBuffer.c_str(),(ULONG)SpaceBuffer.length()); m_File.Write(SpaceBuffer.c_str(), (ULONG)SpaceBuffer.length());
m_File.Write(Data,result); m_File.Write(Data, result);
m_File.Seek(WritePos,CFileBase::begin); m_File.Seek(WritePos, CFileBase::begin);
} }
} while (SizeToRead > 0); } while (SizeToRead > 0);
} }
@ -60,25 +60,25 @@ void CIniFileBase::fInsertSpaces ( int Pos, int NoOfSpaces )
do { do {
SizeToRead = end - ReadPos; SizeToRead = end - ReadPos;
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; } if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
m_File.Seek(ReadPos,CFileBase::begin); m_File.Seek(ReadPos, CFileBase::begin);
m_File.Read(Data,SizeToRead); m_File.Read(Data, SizeToRead);
m_File.Seek(WritePos,CFileBase::begin); m_File.Seek(WritePos, CFileBase::begin);
m_File.Write(Data,SizeToRead); m_File.Write(Data, SizeToRead);
ReadPos += SizeToRead; ReadPos += SizeToRead;
WritePos += SizeToRead; WritePos += SizeToRead;
} while (SizeToRead > 0); } while (SizeToRead > 0);
m_File.Seek(WritePos,CFileBase::begin); m_File.Seek(WritePos, CFileBase::begin);
stdstr_f SpaceBuffer(_T("%*c"),(NoOfSpaces * -1),' '); stdstr_f SpaceBuffer(_T("%*c"), (NoOfSpaces * -1), ' ');
m_File.Write(SpaceBuffer.c_str(),(ULONG)SpaceBuffer.length()); m_File.Write(SpaceBuffer.c_str(), (ULONG)SpaceBuffer.length());
m_File.Seek(WritePos,CFileBase::begin); m_File.Seek(WritePos, CFileBase::begin);
m_File.SetEndOfFile(); m_File.SetEndOfFile();
m_File.Seek(0,CFileBase::begin); m_File.Seek(0, CFileBase::begin);
} }
} }
int CIniFileBase::GetStringFromFile ( char * & String, char * &Data, int & MaxDataSize, int & DataSize, int & ReadPos ) int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxDataSize, int & DataSize, int & ReadPos)
{ {
enum { BufferIncrease = 0x2000 }; enum { BufferIncrease = 0x2000 };
if (MaxDataSize == 0) if (MaxDataSize == 0)
@ -86,20 +86,20 @@ int CIniFileBase::GetStringFromFile ( char * & String, char * &Data, int & MaxDa
ReadPos = 0; ReadPos = 0;
MaxDataSize = BufferIncrease; MaxDataSize = BufferIncrease;
Data = new char[MaxDataSize]; Data = new char[MaxDataSize];
DataSize = m_File.Read(&Data[DataSize],MaxDataSize); DataSize = m_File.Read(&Data[DataSize], MaxDataSize);
} }
for (;;) for (;;)
{ {
int count; int count;
for (count = ReadPos; count < DataSize; count ++) for (count = ReadPos; count < DataSize; count++)
{ {
if (Data[count] == '\n') if (Data[count] == '\n')
{ {
int len = (count - ReadPos) + 1; int len = (count - ReadPos) + 1;
String = &Data[ReadPos]; String = &Data[ReadPos];
String[len-1] = 0; String[len - 1] = 0;
ReadPos = count + 1; ReadPos = count + 1;
return len; return len;
} }
@ -107,9 +107,9 @@ int CIniFileBase::GetStringFromFile ( char * & String, char * &Data, int & MaxDa
if (ReadPos != 0) if (ReadPos != 0)
{ {
if ((DataSize - ReadPos) > 0 ) if ((DataSize - ReadPos) > 0)
{ {
memmove(Data,&Data[ReadPos],DataSize - ReadPos); memmove(Data, &Data[ReadPos], DataSize - ReadPos);
} }
DataSize -= ReadPos; DataSize -= ReadPos;
ReadPos = 0; ReadPos = 0;
@ -123,20 +123,20 @@ int CIniFileBase::GetStringFromFile ( char * & String, char * &Data, int & MaxDa
{ {
return -1; return -1;
} }
memcpy(NewBuffer,Data,DataSize); memcpy(NewBuffer, Data, DataSize);
MaxDataSize = NewMaxDataSize; MaxDataSize = NewMaxDataSize;
delete [] Data; delete[] Data;
Data = NewBuffer; Data = NewBuffer;
} }
int dwRead = m_File.Read(&Data[DataSize],MaxDataSize - DataSize); int dwRead = m_File.Read(&Data[DataSize], MaxDataSize - DataSize);
if (dwRead == 0) if (dwRead == 0)
{ {
if (DataSize > 0) if (DataSize > 0)
{ {
int len = DataSize + 1; int len = DataSize + 1;
String = &Data[ReadPos]; String = &Data[ReadPos];
String[len-1] = 0; String[len - 1] = 0;
DataSize = 0; DataSize = 0;
ReadPos = 0; ReadPos = 0;
return len; return len;
@ -147,7 +147,7 @@ int CIniFileBase::GetStringFromFile ( char * & String, char * &Data, int & MaxDa
} }
} }
void CIniFileBase::SaveCurrentSection ( void ) void CIniFileBase::SaveCurrentSection(void)
{ {
if (!m_CurrentSectionDirty) if (!m_CurrentSectionDirty)
{ {
@ -164,21 +164,21 @@ void CIniFileBase::SaveCurrentSection ( void )
if (m_CurrentSectionFilePos == -1) if (m_CurrentSectionFilePos == -1)
{ {
//Section has not been added yet //Section has not been added yet
m_File.Seek(0,CFileBase::end); m_File.Seek(0, CFileBase::end);
int len = (int)m_CurrentSection.length() + (lineFeedLen * 2) + 5; int len = (int)m_CurrentSection.length() + (lineFeedLen * 2) + 5;
AUTO_PTR<char> SectionName(new char[len]); AUTO_PTR<char> SectionName(new char[len]);
if (m_File.GetLength() < (int)strlen(m_LineFeed)) if (m_File.GetLength() < (int)strlen(m_LineFeed))
{ {
sprintf(SectionName.get(),"[%s]%s",m_CurrentSection.c_str(),m_LineFeed); sprintf(SectionName.get(), "[%s]%s", m_CurrentSection.c_str(), m_LineFeed);
} }
else else
{ {
sprintf(SectionName.get(),"%s[%s]%s",m_LineFeed,m_CurrentSection.c_str(),m_LineFeed); sprintf(SectionName.get(), "%s[%s]%s", m_LineFeed, m_CurrentSection.c_str(), m_LineFeed);
} }
m_File.Write(SectionName.get(),(int)strlen(SectionName.get())); m_File.Write(SectionName.get(), (int)strlen(SectionName.get()));
m_CurrentSectionFilePos = m_File.GetPosition(); m_CurrentSectionFilePos = m_File.GetPosition();
m_SectionsPos.insert(FILELOC::value_type(m_CurrentSection,m_CurrentSectionFilePos)); m_SectionsPos.insert(FILELOC::value_type(m_CurrentSection, m_CurrentSectionFilePos));
} }
else else
{ {
@ -196,7 +196,7 @@ void CIniFileBase::SaveCurrentSection ( void )
LineData.reset(new char[newLen]); LineData.reset(new char[newLen]);
len = newLen; len = newLen;
} }
sprintf(LineData.get(),"%s=%s%s",iter->first.c_str(),iter->second.c_str(),m_LineFeed); sprintf(LineData.get(), "%s=%s%s", iter->first.c_str(), iter->second.c_str(), m_LineFeed);
NeededBufferLen += (int)strlen(LineData.get()); NeededBufferLen += (int)strlen(LineData.get());
} }
} }
@ -211,7 +211,7 @@ void CIniFileBase::SaveCurrentSection ( void )
int StartPos = m_CurrentSectionFilePos; int StartPos = m_CurrentSectionFilePos;
int EndPos = StartPos; int EndPos = StartPos;
do { do {
result = GetStringFromFile(Input,Data,MaxDataSize,DataSize,ReadPos); result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
if (result <= 1) { continue; } if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1 || Input[0] != '[') if (strlen(CleanLine(Input)) <= 1 || Input[0] != '[')
{ {
@ -227,11 +227,11 @@ void CIniFileBase::SaveCurrentSection ( void )
} while (result >= 0); } while (result >= 0);
currentLen = EndPos - StartPos; currentLen = EndPos - StartPos;
if (Data) { delete [] Data; Data = NULL; } if (Data) { delete[] Data; Data = NULL; }
if (NeededBufferLen != currentLen) if (NeededBufferLen != currentLen)
{ {
fInsertSpaces(StartPos,NeededBufferLen - currentLen); fInsertSpaces(StartPos, NeededBufferLen - currentLen);
m_File.Flush(); m_File.Flush();
ClearSectionPosList(StartPos); ClearSectionPosList(StartPos);
} }
@ -251,16 +251,16 @@ void CIniFileBase::SaveCurrentSection ( void )
LineData.reset(new char[newLen]); LineData.reset(new char[newLen]);
len = newLen; len = newLen;
} }
sprintf(LineData.get(),"%s=%s%s",iter->first.c_str(),iter->second.c_str(),m_LineFeed); 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())); m_File.Write(LineData.get(), (int)strlen(LineData.get()));
} }
} }
m_File.Flush(); m_File.Flush();
} }
bool CIniFileBase::MoveToSectionNameData ( LPCSTR lpSectionName, bool ChangeCurrentSection ) bool CIniFileBase::MoveToSectionNameData(LPCSTR lpSectionName, bool ChangeCurrentSection)
{ {
if (strcmp(lpSectionName,m_CurrentSection.c_str()) == 0) if (strcmp(lpSectionName, m_CurrentSection.c_str()) == 0)
{ {
return true; return true;
} }
@ -282,7 +282,7 @@ bool CIniFileBase::MoveToSectionNameData ( LPCSTR lpSectionName, bool ChangeCurr
m_CurrentSection = iter->first; m_CurrentSection = iter->first;
m_CurrentSectionFilePos = iter->second; m_CurrentSectionFilePos = iter->second;
} }
m_File.Seek(iter->second,CFileBase::begin); m_File.Seek(iter->second, CFileBase::begin);
bFoundSection = true; bFoundSection = true;
} }
else else
@ -296,7 +296,7 @@ bool CIniFileBase::MoveToSectionNameData ( LPCSTR lpSectionName, bool ChangeCurr
pUTF8[2] = 0xbf; pUTF8[2] = 0xbf;
do { do {
result = GetStringFromFile(Input,Data,MaxDataSize,DataSize,ReadPos); result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
if (result <= 1) { continue; } if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1) { continue; } if (strlen(CleanLine(Input)) <= 1) { continue; }
@ -315,9 +315,9 @@ bool CIniFileBase::MoveToSectionNameData ( LPCSTR lpSectionName, bool ChangeCurr
CurrentSection[lineEndPos] = 0; CurrentSection[lineEndPos] = 0;
CurrentSection += 1; CurrentSection += 1;
m_lastSectionSearch = (m_File.GetPosition() - DataSize) + ReadPos; m_lastSectionSearch = (m_File.GetPosition() - DataSize) + ReadPos;
m_SectionsPos.insert(FILELOC::value_type(CurrentSection,m_lastSectionSearch)); m_SectionsPos.insert(FILELOC::value_type(CurrentSection, m_lastSectionSearch));
if (_stricmp(lpSectionName,CurrentSection) != 0) if (_stricmp(lpSectionName, CurrentSection) != 0)
{ {
continue; continue;
} }
@ -329,7 +329,7 @@ bool CIniFileBase::MoveToSectionNameData ( LPCSTR lpSectionName, bool ChangeCurr
} }
else else
{ {
m_File.Seek(m_lastSectionSearch,CFileBase::begin); m_File.Seek(m_lastSectionSearch, CFileBase::begin);
} }
bFoundSection = true; bFoundSection = true;
break; break;
@ -340,44 +340,44 @@ bool CIniFileBase::MoveToSectionNameData ( LPCSTR lpSectionName, bool ChangeCurr
{ {
m_CurrentSectionData.clear(); m_CurrentSectionData.clear();
do { do {
result = GetStringFromFile(Input,Data,MaxDataSize,DataSize,ReadPos); result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
if (result <= 1) { continue; } if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1) { continue; } if (strlen(CleanLine(Input)) <= 1) { continue; }
if (Input[0] == '[') { break; } if (Input[0] == '[') { break; }
char * Pos = strchr(Input,'='); char * Pos = strchr(Input, '=');
if (Pos == NULL) { continue; } if (Pos == NULL) { continue; }
char * Value = &Pos[1]; char * Value = &Pos[1];
char * Pos1 = Pos-1; char * Pos1 = Pos - 1;
while (((*Pos1 == ' ') || (*Pos1 == '\t')) && (Pos1 > Input)) while (((*Pos1 == ' ') || (*Pos1 == '\t')) && (Pos1 > Input))
{ {
Pos1--; Pos1--;
} }
Pos1[1] = 0; Pos1[1] = 0;
m_CurrentSectionData.insert(KeyValueList::value_type(Input,Value)); m_CurrentSectionData.insert(KeyValueList::value_type(Input, Value));
} while (result >= 0); } while (result >= 0);
} }
if (Data) { delete [] Data; Data = NULL; } if (Data) { delete[] Data; Data = NULL; }
return bFoundSection; return bFoundSection;
} }
const char * CIniFileBase::CleanLine ( char * const Line ) const char * CIniFileBase::CleanLine(char * const Line)
{ {
char * Pos = Line; char * Pos = Line;
//Remove any comment from the line //Remove any comment from the line
while (Pos != NULL) while (Pos != NULL)
{ {
Pos = strchr(Pos,'/'); Pos = strchr(Pos, '/');
if (Pos != NULL) if (Pos != NULL)
{ {
if (Pos[1] == '/') if (Pos[1] == '/')
{ {
if (Pos > Line) if (Pos > Line)
{ {
char * Pos_1 = Pos-1; char * Pos_1 = Pos - 1;
if (Pos_1[0] != ':') if (Pos_1[0] != ':')
{ {
@ -399,7 +399,7 @@ const char * CIniFileBase::CleanLine ( char * const Line )
} }
//strip any spaces or line feeds from the end of the line //strip any spaces or line feeds from the end of the line
for (int count = (int)strlen(&Line[0]) - 1; count >= 0; count --) for (int count = (int)strlen(&Line[0]) - 1; count >= 0; count--)
{ {
if (Line[count] != ' ' && Line[count] != '\r') { break; } if (Line[count] != ' ' && Line[count] != '\r') { break; }
Line[count] = 0; Line[count] = 0;
@ -409,10 +409,10 @@ const char * CIniFileBase::CleanLine ( char * const Line )
void CIniFileBase::OpenIniFileReadOnly() void CIniFileBase::OpenIniFileReadOnly()
{ {
if (m_File.Open(m_FileName.c_str(),CFileBase::modeRead)) if (m_File.Open(m_FileName.c_str(), CFileBase::modeRead))
{ {
m_ReadOnly = true; m_ReadOnly = true;
m_File.Seek(0,CFileBase::begin); m_File.Seek(0, CFileBase::begin);
} }
} }
@ -420,13 +420,13 @@ void CIniFileBase::OpenIniFile(bool bCreate)
{ {
//Open for reading/Writing //Open for reading/Writing
m_ReadOnly = false; m_ReadOnly = false;
if (!m_File.Open(m_FileName.c_str(),CFileBase::modeReadWrite | CFileBase::shareDenyWrite)) if (!m_File.Open(m_FileName.c_str(), CFileBase::modeReadWrite | CFileBase::shareDenyWrite))
{ {
if (!m_File.Open(m_FileName.c_str(),CFileBase::modeRead)) if (!m_File.Open(m_FileName.c_str(), CFileBase::modeRead))
{ {
if(bCreate) if (bCreate)
{ {
if (!m_File.Open(m_FileName.c_str(),CFileBase::modeReadWrite | CFileBase::modeCreate | CFileBase::shareDenyWrite)) if (!m_File.Open(m_FileName.c_str(), CFileBase::modeReadWrite | CFileBase::modeCreate | CFileBase::shareDenyWrite))
{ {
return; return;
} }
@ -437,7 +437,7 @@ void CIniFileBase::OpenIniFile(bool bCreate)
m_ReadOnly = true; m_ReadOnly = true;
} }
} }
m_File.Seek(0,CFileBase::begin); m_File.Seek(0, CFileBase::begin);
} }
bool CIniFileBase::IsEmpty() bool CIniFileBase::IsEmpty()
@ -447,14 +447,14 @@ bool CIniFileBase::IsEmpty()
return false; return false;
} }
bool CIniFileBase::IsFileOpen ( void ) bool CIniFileBase::IsFileOpen(void)
{ {
return m_File.IsOpen(); return m_File.IsOpen();
} }
bool CIniFileBase::DeleteSection ( LPCTSTR lpSectionName ) bool CIniFileBase::DeleteSection(LPCTSTR lpSectionName)
{ {
stdstr_f strSection("[%s]",lpSectionName); stdstr_f strSection("[%s]", lpSectionName);
/*if(m_File.IsOpen()) /*if(m_File.IsOpen())
{ {
@ -516,7 +516,7 @@ bool CIniFileBase::DeleteSection ( LPCTSTR lpSectionName )
return true; return true;
} }
bool CIniFileBase::GetString ( LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault, stdstr & Value ) bool CIniFileBase::GetString(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault, stdstr & Value)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
@ -525,7 +525,7 @@ bool CIniFileBase::GetString ( LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lp
lpSectionName = "default"; lpSectionName = "default";
} }
if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName,true)) if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName, true))
{ {
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName); KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
if (iter != m_CurrentSectionData.end()) if (iter != m_CurrentSectionData.end())
@ -538,10 +538,10 @@ bool CIniFileBase::GetString ( LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lp
return false; return false;
} }
stdstr CIniFileBase::GetString ( LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault ) stdstr CIniFileBase::GetString(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault)
{ {
stdstr Value; stdstr Value;
GetString(lpSectionName,lpKeyName,lpDefault,Value); GetString(lpSectionName, lpKeyName, lpDefault, Value);
return Value; return Value;
} }
@ -583,7 +583,7 @@ stdstr CIniFileBase::GetString ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, LPCW
#endif #endif
uint32_t CIniFileBase::GetString ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, uint32_t nSize ) uint32_t CIniFileBase::GetString(LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, uint32_t nSize)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
@ -598,17 +598,17 @@ uint32_t CIniFileBase::GetString ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LP
strSection = lpSectionName; strSection = lpSectionName;
} }
if (m_File.IsOpen() && MoveToSectionNameData(strSection.c_str(),true)) if (m_File.IsOpen() && MoveToSectionNameData(strSection.c_str(), true))
{ {
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName); KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
if (iter != m_CurrentSectionData.end()) if (iter != m_CurrentSectionData.end())
{ {
_tcsncpy(lpReturnedString,iter->second.c_str(),nSize - 1); _tcsncpy(lpReturnedString, iter->second.c_str(), nSize - 1);
lpReturnedString[nSize - 1] = 0; lpReturnedString[nSize - 1] = 0;
return (ULONG)_tcslen(lpReturnedString); return (ULONG)_tcslen(lpReturnedString);
} }
} }
_tcsncpy(lpReturnedString,lpDefault,nSize - 1); _tcsncpy(lpReturnedString, lpDefault, nSize - 1);
lpReturnedString[nSize - 1] = 0; lpReturnedString[nSize - 1] = 0;
return (ULONG)_tcslen(lpReturnedString); return (ULONG)_tcslen(lpReturnedString);
} }
@ -637,14 +637,14 @@ bool CIniFileBase::GetNumber ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, ULONG n
} }
#endif #endif
uint32_t CIniFileBase::GetNumber ( LPCSTR lpSectionName, LPCSTR lpKeyName, uint32_t nDefault ) uint32_t CIniFileBase::GetNumber(LPCSTR lpSectionName, LPCSTR lpKeyName, uint32_t nDefault)
{ {
uint32_t Value; uint32_t Value;
GetNumber(lpSectionName,lpKeyName,nDefault,Value); GetNumber(lpSectionName, lpKeyName, nDefault, Value);
return Value; return Value;
} }
bool CIniFileBase::GetNumber ( LPCSTR lpSectionName, LPCSTR lpKeyName, uint32_t nDefault, uint32_t & Value ) bool CIniFileBase::GetNumber(LPCSTR lpSectionName, LPCSTR lpKeyName, uint32_t nDefault, uint32_t & Value)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
@ -653,7 +653,7 @@ bool CIniFileBase::GetNumber ( LPCSTR lpSectionName, LPCSTR lpKeyName, uint32_t
lpSectionName = "default"; lpSectionName = "default";
} }
if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName,true)) if (m_File.IsOpen() && MoveToSectionNameData(lpSectionName, true))
{ {
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName); KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
if (iter != m_CurrentSectionData.end()) if (iter != m_CurrentSectionData.end())
@ -667,7 +667,7 @@ bool CIniFileBase::GetNumber ( LPCSTR lpSectionName, LPCSTR lpKeyName, uint32_t
return false; return false;
} }
void CIniFileBase::SaveString ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString ) void CIniFileBase::SaveString(LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
if (!m_File.IsOpen()) if (!m_File.IsOpen())
@ -692,7 +692,7 @@ void CIniFileBase::SaveString ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTS
strSection = lpSectionName; strSection = lpSectionName;
} }
if (!MoveToSectionNameData(strSection.c_str(),true)) if (!MoveToSectionNameData(strSection.c_str(), true))
{ {
m_CurrentSection = strSection; m_CurrentSection = strSection;
m_CurrentSectionData.clear(); m_CurrentSectionData.clear();
@ -720,7 +720,7 @@ void CIniFileBase::SaveString ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTS
{ {
if (lpString) if (lpString)
{ {
m_CurrentSectionData.insert(KeyValueList::value_type(lpKeyName,lpString)); m_CurrentSectionData.insert(KeyValueList::value_type(lpKeyName, lpString));
m_CurrentSectionDirty = true; m_CurrentSectionDirty = true;
} }
} }
@ -731,19 +731,19 @@ void CIniFileBase::SaveString ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTS
} }
} }
void CIniFileBase::SaveNumber ( LPCTSTR lpSectionName, LPCTSTR lpKeyName, uint32_t Value ) void CIniFileBase::SaveNumber(LPCTSTR lpSectionName, LPCTSTR lpKeyName, uint32_t Value)
{ {
//translate the string to an ascii version and save as text //translate the string to an ascii version and save as text
SaveString(lpSectionName,lpKeyName,stdstr_f(_T("%d"),Value).c_str()); SaveString(lpSectionName, lpKeyName, stdstr_f(_T("%d"), Value).c_str());
} }
void CIniFileBase::FlushChanges (void) void CIniFileBase::FlushChanges(void)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
SaveCurrentSection(); SaveCurrentSection();
} }
void CIniFileBase::SetAutoFlush (bool AutoFlush) void CIniFileBase::SetAutoFlush(bool AutoFlush)
{ {
m_InstantFlush = AutoFlush; m_InstantFlush = AutoFlush;
if (AutoFlush) if (AutoFlush)
@ -752,7 +752,7 @@ void CIniFileBase::SetAutoFlush (bool AutoFlush)
} }
} }
void CIniFileBase::GetKeyList ( LPCTSTR lpSectionName, strlist &List ) void CIniFileBase::GetKeyList(LPCTSTR lpSectionName, strlist &List)
{ {
List.clear(); List.clear();
@ -767,7 +767,7 @@ void CIniFileBase::GetKeyList ( LPCTSTR lpSectionName, strlist &List )
lpSectionName = "default"; lpSectionName = "default";
} }
if (MoveToSectionNameData(lpSectionName,true)) if (MoveToSectionNameData(lpSectionName, true))
{ {
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++) for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
{ {
@ -776,7 +776,7 @@ void CIniFileBase::GetKeyList ( LPCTSTR lpSectionName, strlist &List )
} }
} }
void CIniFileBase::GetKeyValueData ( LPCTSTR lpSectionName, KeyValueData & List ) void CIniFileBase::GetKeyValueData(LPCTSTR lpSectionName, KeyValueData & List)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
if (!m_File.IsOpen()) if (!m_File.IsOpen())
@ -795,25 +795,25 @@ void CIniFileBase::GetKeyValueData ( LPCTSTR lpSectionName, KeyValueData & List
strSection = lpSectionName; strSection = lpSectionName;
} }
if (!MoveToSectionNameData(strSection.c_str(),false)) { return; } if (!MoveToSectionNameData(strSection.c_str(), false)) { return; }
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result; int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
char *Input = NULL, *Data = NULL; char *Input = NULL, *Data = NULL;
do { do {
result = GetStringFromFile(Input,Data,MaxDataSize,DataSize,ReadPos); result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
if (result <= 1) { continue; } if (result <= 1) { continue; }
if (strlen(CleanLine(Input)) <= 1) { continue; } if (strlen(CleanLine(Input)) <= 1) { continue; }
if (Input[0] == '[') { break; } if (Input[0] == '[') { break; }
char * Pos = strchr(Input,'='); char * Pos = strchr(Input, '=');
if (Pos == NULL) { continue; } if (Pos == NULL) { continue; }
Pos[0] = 0; Pos[0] = 0;
List.insert(KeyValueData::value_type(Input,&Pos[1])); List.insert(KeyValueData::value_type(Input, &Pos[1]));
} while (result >= 0); } while (result >= 0);
if (Data) { delete [] Data; Data = NULL; } if (Data) { delete[] Data; Data = NULL; }
} }
void CIniFileBase::ClearSectionPosList( long FilePos ) void CIniFileBase::ClearSectionPosList(long FilePos)
{ {
if (FilePos <= 0) if (FilePos <= 0)
{ {
@ -826,7 +826,7 @@ void CIniFileBase::ClearSectionPosList( long FilePos )
while (iter != m_SectionsPos.end()) while (iter != m_SectionsPos.end())
{ {
FILELOC::iterator CurrentIter = iter; FILELOC::iterator CurrentIter = iter;
iter ++; iter++;
long TestFilePos = CurrentIter->second; long TestFilePos = CurrentIter->second;
if (TestFilePos > FilePos) if (TestFilePos > FilePos)
{ {
@ -837,7 +837,7 @@ void CIniFileBase::ClearSectionPosList( long FilePos )
} }
} }
void CIniFileBase::GetVectorOfSections( SectionList & sections) void CIniFileBase::GetVectorOfSections(SectionList & sections)
{ {
sections.clear(); sections.clear();
@ -848,8 +848,8 @@ void CIniFileBase::GetVectorOfSections( SectionList & sections)
} }
{ {
stdstr_f DoesNotExist(_T("DoesNotExist%d%d%d"),rand(),rand(),rand()); stdstr_f DoesNotExist(_T("DoesNotExist%d%d%d"), rand(), rand(), rand());
MoveToSectionNameData(DoesNotExist.c_str(),false); MoveToSectionNameData(DoesNotExist.c_str(), false);
} }
for (FILELOC::const_iterator iter = m_SectionsPos.begin(); iter != m_SectionsPos.end(); iter++) for (FILELOC::const_iterator iter = m_SectionsPos.begin(); iter != m_SectionsPos.end(); iter++)

View File

@ -9,16 +9,16 @@ class CIniFileBase
{ {
struct insensitive_compare struct insensitive_compare
{ {
bool operator() (const std::string & a, const std::string & b) const { return _stricmp(a.c_str(),b.c_str()) < 0; } bool operator() (const std::string & a, const std::string & b) const { return _stricmp(a.c_str(), b.c_str()) < 0; }
}; };
typedef std::string ansi_string; typedef std::string ansi_string;
typedef std::map<ansi_string,long> FILELOC; typedef std::map<ansi_string, long> FILELOC;
typedef FILELOC::iterator FILELOC_ITR; typedef FILELOC::iterator FILELOC_ITR;
typedef std::map<ansi_string,ansi_string, insensitive_compare> KeyValueList; typedef std::map<ansi_string, ansi_string, insensitive_compare> KeyValueList;
public: public:
typedef std::map<stdstr,stdstr> KeyValueData; typedef std::map<stdstr, stdstr> KeyValueData;
typedef std::vector<stdstr> SectionList; typedef std::vector<stdstr> SectionList;
protected: protected:
@ -43,29 +43,29 @@ private:
//void AddItemData ( const char * lpKeyName, const char * lpString); //void AddItemData ( const char * lpKeyName, const char * lpString);
//bool ChangeItemData ( const char * lpKeyName, const char * lpString ); //bool ChangeItemData ( const char * lpKeyName, const char * lpString );
//void DeleteItem ( const char * lpKeyName ); //void DeleteItem ( const char * lpKeyName );
void fInsertSpaces ( int Pos, int NoOfSpaces ); void fInsertSpaces(int Pos, int NoOfSpaces);
int GetStringFromFile ( char * & String, char * &Data, int & MaxDataSize, int & DataSize, int & ReadPos ); int GetStringFromFile(char * & String, char * &Data, int & MaxDataSize, int & DataSize, int & ReadPos);
bool MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection); bool MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection);
const char * CleanLine ( char * const Line ); const char * CleanLine(char * const Line);
void ClearSectionPosList( long FilePos ); void ClearSectionPosList(long FilePos);
protected: protected:
void OpenIniFileReadOnly(); void OpenIniFileReadOnly();
void OpenIniFile(bool bCreate = true); void OpenIniFile(bool bCreate = true);
void SaveCurrentSection ( void ); void SaveCurrentSection(void);
public: public:
CIniFileBase(CFileBase & FileObject, const char * FileName); CIniFileBase(CFileBase & FileObject, const char * FileName);
virtual ~CIniFileBase(void); virtual ~CIniFileBase(void);
bool IsEmpty(); bool IsEmpty();
bool IsFileOpen ( void ); bool IsFileOpen(void);
bool DeleteSection(const char * lpSectionName); bool DeleteSection(const char * lpSectionName);
bool GetString ( const char * lpSectionName, const char * lpKeyName, const char * lpDefault, stdstr & Value ); bool GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, stdstr & Value);
stdstr GetString ( const char * lpSectionName, const char * lpKeyName, const char * lpDefault ); stdstr GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault);
uint32_t GetString ( const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize ); uint32_t GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize);
uint32_t GetNumber ( const char * lpSectionName, const char * lpKeyName, uint32_t nDefault ); uint32_t GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault);
bool GetNumber ( const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value ); bool GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value);
#ifdef _UNICODE #ifdef _UNICODE
bool DeleteSection ( LPCWSTR lpSectionName ); bool DeleteSection ( LPCWSTR lpSectionName );
@ -77,15 +77,15 @@ public:
#endif #endif
virtual void SaveString ( const char * lpSectionName, const char * lpKeyName, const char * lpString ); virtual void SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString);
virtual void SaveNumber ( const char * lpSectionName, const char * lpKeyName, uint32_t Value ); virtual void SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value);
void SetAutoFlush (bool AutoFlush); void SetAutoFlush(bool AutoFlush);
void FlushChanges (void); void FlushChanges(void);
void GetKeyList ( const char * lpSectionName, strlist &List ); void GetKeyList(const char * lpSectionName, strlist &List);
void GetKeyValueData ( const char * lpSectionName, KeyValueData & List ); void GetKeyValueData(const char * lpSectionName, KeyValueData & List);
void GetVectorOfSections( SectionList & sections); void GetVectorOfSections(SectionList & sections);
const stdstr &GetFileName() {return m_FileName;} const stdstr &GetFileName() { return m_FileName; }
}; };
template <class CFileStorage> template <class CFileStorage>
@ -93,17 +93,17 @@ class CIniFileT :
public CIniFileBase public CIniFileBase
{ {
public: public:
CIniFileT( const char * FileName ) : CIniFileT(const char * FileName) :
CIniFileBase(m_FileObject,FileName) CIniFileBase(m_FileObject, FileName)
{ {
//Try to open file for reading //Try to open file for reading
OpenIniFile(); OpenIniFile();
} }
CIniFileT( const char * FileName, bool bCreate, bool bReadOnly) : CIniFileT(const char * FileName, bool bCreate, bool bReadOnly) :
CIniFileBase(m_FileObject,FileName) CIniFileBase(m_FileObject, FileName)
{ {
if(bReadOnly) if (bReadOnly)
{ {
OpenIniFileReadOnly(); OpenIniFileReadOnly();
} }