[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

@ -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 };
if (MaxDataSize == 0)
{
ReadPos = 0;
MaxDataSize = BufferIncrease;
Data = new char[MaxDataSize];
DataSize = m_File.Read(&Data[DataSize], MaxDataSize);
Data.reset(new char[MaxDataSize]);
DataSize = m_File.Read(&Data.get()[DataSize], MaxDataSize);
}
for (;;)
@ -96,10 +96,10 @@ int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxData
for (count = ReadPos; count < DataSize; count++)
{
if (Data[count] == '\n')
if (Data.get()[count] == '\n')
{
int len = (count - ReadPos) + 1;
String = &Data[ReadPos];
String = &Data.get()[ReadPos];
String[len - 1] = 0;
ReadPos = count + 1;
return len;
@ -110,7 +110,7 @@ int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxData
{
if ((DataSize - ReadPos) > 0)
{
memmove(Data, &Data[ReadPos], DataSize - ReadPos);
memmove(Data.get(), &Data.get()[ReadPos], DataSize - ReadPos);
}
DataSize -= ReadPos;
ReadPos = 0;
@ -124,19 +124,18 @@ int CIniFileBase::GetStringFromFile(char * & String, char * &Data, int & MaxData
{
return -1;
}
memcpy(NewBuffer, Data, DataSize);
memcpy(NewBuffer, Data.get(), DataSize);
MaxDataSize = NewMaxDataSize;
delete[] Data;
Data = NewBuffer;
Data.reset(NewBuffer);
}
int dwRead = m_File.Read(&Data[DataSize], MaxDataSize - DataSize);
int dwRead = m_File.Read(&Data.get()[DataSize], MaxDataSize - DataSize);
if (dwRead == 0)
{
if (DataSize > 0)
{
int len = DataSize + 1;
String = &Data[ReadPos];
String = &Data.get()[ReadPos];
String[len - 1] = 0;
DataSize = 0;
ReadPos = 0;
@ -206,7 +205,8 @@ void CIniFileBase::SaveCurrentSection(void)
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
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
int StartPos = m_CurrentSectionFilePos;
@ -229,8 +229,6 @@ void CIniFileBase::SaveCurrentSection(void)
} while (result >= 0);
currentLen = EndPos - StartPos;
if (Data) { delete[] Data; Data = NULL; }
if (NeededBufferLen != currentLen)
{
fInsertSpaces(StartPos, NeededBufferLen - currentLen);
@ -272,7 +270,8 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
m_CurrentSection = "";
}
char *Input = NULL, *Data = NULL;
AUTO_PTR<char> Data;
char *Input = NULL;
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
FILELOC_ITR iter = m_SectionsPos.find(std::string(lpSectionName));
@ -363,7 +362,6 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
} while (result >= 0);
}
if (Data) { delete[] Data; Data = NULL; }
return bFoundSection;
}
@ -571,44 +569,6 @@ stdstr CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyNam
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)
{
CGuard Guard(m_CS);
@ -639,30 +599,6 @@ uint32_t CIniFileBase::GetString(const char * lpSectionName, const char * lpKeyN
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 Value;
@ -844,7 +780,8 @@ void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & Li
if (!MoveToSectionNameData(strSection.c_str(), false)) { return; }
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
char *Input = NULL, *Data = NULL;
AUTO_PTR<char> Data;
char *Input = NULL;
do
{
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
@ -857,7 +794,6 @@ void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & Li
List.insert(KeyValueData::value_type(Input, &Pos[1]));
} while (result >= 0);
if (Data) { delete[] Data; Data = NULL; }
}
void CIniFileBase::ClearSectionPosList(long FilePos)

View File

@ -9,6 +9,7 @@
#include "FileClass.h"
#include "CriticalSection.h"
#include "StdString.h"
#include "SmartPointer.h"
#include <map>
class CIniFileBase
@ -49,11 +50,8 @@ private:
CriticalSection m_CS;
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);
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);
const char * CleanLine(char * Line);
void ClearSectionPosList(long FilePos);
@ -76,16 +74,6 @@ public:
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);
#ifdef _UNICODE
bool DeleteSection ( LPCWSTR lpSectionName );
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);