[Common] Use smart pointer for ini file class

This commit is contained in:
zilmar 2016-11-14 18:10:43 +11:00
parent 981ec94786
commit 36d169c57f
2 changed files with 39 additions and 115 deletions

View File

@ -33,7 +33,7 @@ void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
{ {
stdstr_f SpaceBuffer("%*c", NoOfSpaces, ' '); stdstr_f SpaceBuffer("%*c", NoOfSpaces, ' ');
do do
{ {
SizeToRead = end - Pos; SizeToRead = end - Pos;
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; } if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
@ -57,7 +57,7 @@ void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
int ReadPos = Pos + (NoOfSpaces * -1); int ReadPos = Pos + (NoOfSpaces * -1);
int WritePos = Pos; int WritePos = Pos;
do do
{ {
SizeToRead = end - ReadPos; SizeToRead = end - ReadPos;
if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; } if (SizeToRead > fIS_MvSize) { SizeToRead = fIS_MvSize; }
@ -79,15 +79,15 @@ void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
} }
} }
int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxDataSize, int & DataSize, int & ReadPos) int CIniFileBase::GetStringFromFile(char * & String, AUTO_PTR<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos)
{ {
enum { BufferIncrease = 0x2000 }; enum { BufferIncrease = 0x2000 };
if (MaxDataSize == 0) if (MaxDataSize == 0)
{ {
ReadPos = 0; ReadPos = 0;
MaxDataSize = BufferIncrease; MaxDataSize = BufferIncrease;
Data = new char[MaxDataSize]; Data.reset(new char[MaxDataSize]);
DataSize = m_File.Read(&Data[DataSize], MaxDataSize); DataSize = m_File.Read(&Data.get()[DataSize], MaxDataSize);
} }
for (;;) for (;;)
@ -96,10 +96,10 @@ int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxData
for (count = ReadPos; count < DataSize; count++) for (count = ReadPos; count < DataSize; count++)
{ {
if (Data[count] == '\n') if (Data.get()[count] == '\n')
{ {
int len = (count - ReadPos) + 1; int len = (count - ReadPos) + 1;
String = &Data[ReadPos]; String = &Data.get()[ReadPos];
String[len - 1] = 0; String[len - 1] = 0;
ReadPos = count + 1; ReadPos = count + 1;
return len; return len;
@ -110,7 +110,7 @@ int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxData
{ {
if ((DataSize - ReadPos) > 0) if ((DataSize - ReadPos) > 0)
{ {
memmove(Data, &Data[ReadPos], DataSize - ReadPos); memmove(Data.get(), &Data.get()[ReadPos], DataSize - ReadPos);
} }
DataSize -= ReadPos; DataSize -= ReadPos;
ReadPos = 0; ReadPos = 0;
@ -124,19 +124,18 @@ int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxData
{ {
return -1; return -1;
} }
memcpy(NewBuffer, Data, DataSize); memcpy(NewBuffer, Data.get(), DataSize);
MaxDataSize = NewMaxDataSize; MaxDataSize = NewMaxDataSize;
delete[] Data; Data.reset(NewBuffer);
Data = NewBuffer;
} }
int dwRead = m_File.Read(&Data[DataSize], MaxDataSize - DataSize); int dwRead = m_File.Read(&Data.get()[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.get()[ReadPos];
String[len - 1] = 0; String[len - 1] = 0;
DataSize = 0; DataSize = 0;
ReadPos = 0; ReadPos = 0;
@ -206,12 +205,13 @@ void CIniFileBase::SaveCurrentSection(void)
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin); m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result; int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
char *Input = NULL, *Data = NULL; AUTO_PTR<char> Data;
char *Input = NULL;
//Skip first line as it is the section name //Skip first line as it is the section name
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; }
@ -229,8 +229,6 @@ void CIniFileBase::SaveCurrentSection(void)
} while (result >= 0); } while (result >= 0);
currentLen = EndPos - StartPos; currentLen = EndPos - StartPos;
if (Data) { delete[] Data; Data = NULL; }
if (NeededBufferLen != currentLen) if (NeededBufferLen != currentLen)
{ {
fInsertSpaces(StartPos, NeededBufferLen - currentLen); fInsertSpaces(StartPos, NeededBufferLen - currentLen);
@ -272,7 +270,8 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
m_CurrentSection = ""; m_CurrentSection = "";
} }
char *Input = NULL, *Data = NULL; AUTO_PTR<char> Data;
char *Input = NULL;
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result; int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
FILELOC_ITR iter = m_SectionsPos.find(std::string(lpSectionName)); FILELOC_ITR iter = m_SectionsPos.find(std::string(lpSectionName));
@ -297,7 +296,7 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
pUTF8[1] = 0xbb; pUTF8[1] = 0xbb;
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; }
@ -342,7 +341,7 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
if (bFoundSection && ChangeCurrentSection) if (bFoundSection && ChangeCurrentSection)
{ {
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; }
@ -363,7 +362,6 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
} while (result >= 0); } while (result >= 0);
} }
if (Data) { delete[] Data; Data = NULL; }
return bFoundSection; return bFoundSection;
} }
@ -466,33 +464,33 @@ bool CIniFileBase::DeleteSection(const char * lpSectionName)
stdstr_f strSection("[%s]", lpSectionName); stdstr_f strSection("[%s]", lpSectionName);
if(!m_File.IsOpen()) if (!m_File.IsOpen())
{ {
return false; return false;
} }
m_CurrentSectionFilePos = 0; m_CurrentSectionFilePos = 0;
m_File.Seek(m_CurrentSectionFilePos,CFileBase::begin); m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
size_t dwSize = m_File.GetLength(); size_t dwSize = m_File.GetLength();
if(dwSize == 0) if (dwSize == 0)
{ {
return false; return false;
} }
AUTO_PTR<char> pData(new char[dwSize+1]); AUTO_PTR<char> pData(new char[dwSize + 1]);
if(pData.get() == NULL) if (pData.get() == NULL)
{ {
return false; return false;
} }
uint32_t dwRet = m_File.Read(pData.get(), dwSize); uint32_t dwRet = m_File.Read(pData.get(), dwSize);
if(dwRet == 0 || dwRet < dwSize) if (dwRet == 0 || dwRet < dwSize)
{ {
return false; return false;
} }
pData.get()[dwRet] = 0; pData.get()[dwRet] = 0;
char *pSection = strstr(pData.get(), strSection.c_str()); char *pSection = strstr(pData.get(), strSection.c_str());
if(pSection == NULL) if (pSection == NULL)
{ {
return false; return false;
} }
@ -502,13 +500,13 @@ bool CIniFileBase::DeleteSection(const char * lpSectionName)
std::string strNewData = pData.get(); std::string strNewData = pData.get();
pSection[0] = tmp; pSection[0] = tmp;
char *pEndSection = pSection + strlen(strSection.c_str()), * Data = pData.get(); char *pEndSection = pSection + strlen(strSection.c_str()), *Data = pData.get();
char *pNextSection = NULL; char *pNextSection = NULL;
int result, ReadPos = pEndSection - pData.get(); int result, ReadPos = pEndSection - pData.get();
do do
{ {
char * Input = NULL; char * Input = NULL;
int MaxDataSize = dwSize+1; int MaxDataSize = dwSize + 1;
result = -1; result = -1;
for (int count = ReadPos; count < MaxDataSize; count++) for (int count = ReadPos; count < MaxDataSize; count++)
{ {
@ -527,15 +525,15 @@ bool CIniFileBase::DeleteSection(const char * lpSectionName)
if (strlen(CleanLine((char *)line.c_str())) <= 1) { continue; } if (strlen(CleanLine((char *)line.c_str())) <= 1) { continue; }
if (line[0] != '[') { continue; } if (line[0] != '[') { continue; }
pNextSection = Input; pNextSection = Input;
break; break;
} while (result >= 0); } while (result >= 0);
if(pNextSection) if (pNextSection)
{ {
strNewData += pNextSection; strNewData += pNextSection;
} }
m_File.Seek(m_CurrentSectionFilePos,CFileBase::begin); m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
m_File.Write(strNewData.c_str(), (uint32_t)strlen(strNewData.c_str())); m_File.Write(strNewData.c_str(), (uint32_t)strlen(strNewData.c_str()));
m_File.Flush(); m_File.Flush();
m_File.SetEndOfFile(); m_File.SetEndOfFile();
@ -571,44 +569,6 @@ stdstr CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyNam
return Value; return Value;
} }
#ifdef _UNICODE
bool CIniFileBase::GetString ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, LPCWSTR lpDefault, stdstr & Value )
{
CGuard Guard(m_CS);
std::string strSection;
if (lpSectionName == NULL || wcslen(lpSectionName) == 0)
{
strSection = "default";
}
else
{
stdstr::fromTString(lpSectionName,strSection);
}
if (m_File.IsOpen() && MoveToSectionNameData(strSection.c_str(),true))
{
KeyValueList::iterator iter = m_CurrentSectionData.find(lpKeyName);
if (iter != m_CurrentSectionData.end())
{
stdstr::toTString(iter->second.c_str(),Value);
return true;
}
}
Value = lpDefault;
return false;
}
stdstr CIniFileBase::GetString ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, LPCWSTR lpDefault )
{
stdstr Value;
GetString(lpSectionName,lpKeyName,lpDefault,Value);
return Value;
}
#endif
uint32_t CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize) uint32_t CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, char * lpReturnedString, uint32_t nSize)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
@ -639,30 +599,6 @@ uint32_t CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyN
return (uint32_t)strlen(lpReturnedString); return (uint32_t)strlen(lpReturnedString);
} }
#ifdef _UNICODE
uint32_t CIniFileBase::GetNumber ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, uint32_t nDefault )
{
uint32_t Value;
GetNumber(lpSectionName,lpKeyName,nDefault,Value);
return Value;
}
bool CIniFileBase::GetNumber ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, uint32_t nDefault, uint32_t & Value )
{
std::string strSection;
if (lpSectionName != NULL && wcslen(lpSectionName) > 0)
{
stdstr::fromTString(lpSectionName,strSection);
return GetNumber(strSection.c_str(),lpKeyName.c_str(),nDefault,Value);
}
else
{
return GetNumber(NULL,lpKeyName.c_str(),nDefault,Value);
}
}
#endif
uint32_t CIniFileBase::GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault) uint32_t CIniFileBase::GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault)
{ {
uint32_t Value; uint32_t Value;
@ -844,8 +780,9 @@ void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & Li
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; AUTO_PTR<char> Data;
do char *Input = NULL;
do
{ {
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos); result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
if (result <= 1) { continue; } if (result <= 1) { continue; }
@ -857,7 +794,6 @@ void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & Li
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; }
} }
void CIniFileBase::ClearSectionPosList(long FilePos) void CIniFileBase::ClearSectionPosList(long FilePos)

View File

@ -9,6 +9,7 @@
#include "FileClass.h" #include "FileClass.h"
#include "CriticalSection.h" #include "CriticalSection.h"
#include "StdString.h" #include "StdString.h"
#include "SmartPointer.h"
#include <map> #include <map>
class CIniFileBase class CIniFileBase
@ -49,11 +50,8 @@ private:
CriticalSection m_CS; CriticalSection m_CS;
FILELOC m_SectionsPos; FILELOC m_SectionsPos;
//void AddItemData ( const char * lpKeyName, const char * lpString);
//bool ChangeItemData ( const char * lpKeyName, const char * lpString );
//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, AUTO_PTR<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 * Line); const char * CleanLine(char * Line);
void ClearSectionPosList(long FilePos); void ClearSectionPosList(long FilePos);
@ -76,18 +74,8 @@ public:
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 virtual void SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString);
bool DeleteSection ( LPCWSTR lpSectionName ); virtual void SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value);
bool GetString ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, LPCWSTR lpDefault, stdstr & Value );
stdstr GetString ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, LPCWSTR lpDefault );
uint32_t GetString ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, LPCWSTR lpDefault, LPTSTR lpReturnedString, uint32_t nSize );
uint32_t GetNumber ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, uint32_t nDefault );
bool GetNumber ( LPCWSTR lpSectionName, LPCWSTR lpKeyName, uint32_t nDefault, uint32_t & Value );
#endif
virtual void SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString);
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);
bool EntryExists(const char * lpSectionName, const char * lpKeyName); bool EntryExists(const char * lpSectionName, const char * lpKeyName);