Common: Update delete section in IniFileClass
This commit is contained in:
parent
56fde2db97
commit
cb0472fc90
|
@ -1,5 +1,7 @@
|
|||
#include "stdafx.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
CIniFileBase::CIniFileBase(CFileBase & FileObject, const char * FileName) :
|
||||
m_lastSectionSearch(0),
|
||||
|
@ -32,7 +34,7 @@ void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
|
|||
|
||||
if (NoOfSpaces > 0)
|
||||
{
|
||||
stdstr_f SpaceBuffer("%*c", NoOfSpaces, ' ');
|
||||
std::string SpaceBuffer = FormatStr("%*c", NoOfSpaces, ' ');
|
||||
|
||||
do
|
||||
{
|
||||
|
@ -71,7 +73,7 @@ void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
|
|||
} while (SizeToRead > 0);
|
||||
|
||||
m_File.Seek(WritePos, CFileBase::begin);
|
||||
stdstr_f SpaceBuffer("%*c", (NoOfSpaces * -1), ' ');
|
||||
std::string SpaceBuffer = FormatStr("%*c", (NoOfSpaces * -1), ' ');
|
||||
m_File.Write(SpaceBuffer.c_str(), (uint32_t)SpaceBuffer.length());
|
||||
|
||||
m_File.Seek(WritePos, CFileBase::begin);
|
||||
|
@ -80,7 +82,7 @@ void CIniFileBase::fInsertSpaces(int Pos, int NoOfSpaces)
|
|||
}
|
||||
}
|
||||
|
||||
int CIniFileBase::GetStringFromFile(char * & String, AUTO_PTR<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos)
|
||||
int CIniFileBase::GetStringFromFile(char * & String, std::unique_ptr<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos)
|
||||
{
|
||||
enum { BufferIncrease = 0x2000 };
|
||||
if (MaxDataSize == 0)
|
||||
|
@ -168,7 +170,7 @@ void CIniFileBase::SaveCurrentSection(void)
|
|||
m_File.Seek(0, CFileBase::end);
|
||||
|
||||
int len = (int)m_CurrentSection.length() + (lineFeedLen * 2) + 5;
|
||||
AUTO_PTR<char> SectionName(new char[len]);
|
||||
std::unique_ptr<char> SectionName(new char[len]);
|
||||
if (m_File.GetLength() < (int)strlen(m_LineFeed))
|
||||
{
|
||||
sprintf(SectionName.get(), "[%s]%s", m_CurrentSection.c_str(), m_LineFeed);
|
||||
|
@ -186,7 +188,7 @@ void CIniFileBase::SaveCurrentSection(void)
|
|||
//increase/decrease space needed
|
||||
int NeededBufferLen = 0;
|
||||
{
|
||||
AUTO_PTR<char> LineData(NULL);
|
||||
std::unique_ptr<char> LineData;
|
||||
int len = 0;
|
||||
|
||||
for (KeyValueList::iterator iter = m_CurrentSectionData.begin(); iter != m_CurrentSectionData.end(); iter++)
|
||||
|
@ -206,7 +208,7 @@ void CIniFileBase::SaveCurrentSection(void)
|
|||
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
|
||||
|
||||
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
|
||||
AUTO_PTR<char> Data;
|
||||
std::unique_ptr<char> Data;
|
||||
char *Input = NULL;
|
||||
|
||||
//Skip first line as it is the section name
|
||||
|
@ -241,7 +243,7 @@ void CIniFileBase::SaveCurrentSection(void)
|
|||
}
|
||||
|
||||
{
|
||||
AUTO_PTR<char> LineData(NULL);
|
||||
std::unique_ptr<char> LineData;
|
||||
int len = 0;
|
||||
|
||||
if (m_SortFunction != NULL)
|
||||
|
@ -295,7 +297,7 @@ bool CIniFileBase::MoveToSectionNameData(const char * lpSectionName, bool Change
|
|||
m_CurrentSection = "";
|
||||
}
|
||||
|
||||
AUTO_PTR<char> Data;
|
||||
std::unique_ptr<char> Data;
|
||||
char *Input = NULL;
|
||||
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
|
||||
|
||||
|
@ -493,87 +495,75 @@ bool CIniFileBase::IsFileOpen(void)
|
|||
|
||||
bool CIniFileBase::DeleteSection(const char * lpSectionName)
|
||||
{
|
||||
CGuard Guard(m_CS);
|
||||
|
||||
if (!m_File.IsOpen()) { return false; }
|
||||
|
||||
SaveCurrentSection();
|
||||
if (!MoveToSectionNameData(lpSectionName, true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
|
||||
long DeleteSectionStart = m_CurrentSectionFilePos - (strlen(lpSectionName) + strlen(m_LineFeed) + 2);
|
||||
long NextSectionStart = -1;
|
||||
|
||||
{
|
||||
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, NextLine = 0, result;
|
||||
std::unique_ptr <char> Data;
|
||||
char *Input = NULL;
|
||||
do
|
||||
{
|
||||
result = GetStringFromFile(Input, Data, MaxDataSize, DataSize, ReadPos);
|
||||
if (result <= 1) { continue; }
|
||||
if (strlen(CleanLine(Input)) <= 1) { continue; }
|
||||
|
||||
if (Input[0] != '[')
|
||||
{
|
||||
NextLine = (m_File.GetPosition() - DataSize) + ReadPos;
|
||||
continue;
|
||||
}
|
||||
NextSectionStart = NextLine != 0 ? NextLine : (m_File.GetPosition() - DataSize) + ReadPos;
|
||||
break;
|
||||
} while (result >= 0);
|
||||
}
|
||||
|
||||
if (NextSectionStart != -1)
|
||||
{
|
||||
m_File.Seek(0, CFileBase::end);
|
||||
long end = m_File.GetPosition();
|
||||
long ReadPos = NextSectionStart;
|
||||
long WritePos = DeleteSectionStart;
|
||||
|
||||
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
|
||||
{
|
||||
m_File.Seek(DeleteSectionStart, CFileBase::begin);
|
||||
m_File.Flush();
|
||||
m_File.SetEndOfFile();
|
||||
}
|
||||
m_File.Flush();
|
||||
ClearSectionPosList(0);
|
||||
m_CurrentSection = "";
|
||||
m_CurrentSectionData.clear();
|
||||
m_CurrentSectionFilePos = -1;
|
||||
|
||||
stdstr_f strSection("[%s]", lpSectionName);
|
||||
|
||||
if (!m_File.IsOpen())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_CurrentSectionFilePos = 0;
|
||||
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
|
||||
|
||||
size_t dwSize = m_File.GetLength();
|
||||
if (dwSize == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
AUTO_PTR<char> pData(new char[dwSize + 1]);
|
||||
if (pData.get() == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
uint32_t dwRet = m_File.Read(pData.get(), (uint32_t)dwSize);
|
||||
if (dwRet == 0 || dwRet < dwSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
pData.get()[dwRet] = 0;
|
||||
|
||||
char *pSection = strstr(pData.get(), strSection.c_str());
|
||||
if (pSection == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
char tmp = pSection[0];
|
||||
pSection[0] = 0;
|
||||
|
||||
std::string strNewData = pData.get();
|
||||
pSection[0] = tmp;
|
||||
|
||||
char *pEndSection = pSection + strlen(strSection.c_str()), *Data = pData.get();
|
||||
char *pNextSection = NULL;
|
||||
int result, ReadPos = (int)(pEndSection - pData.get());
|
||||
do
|
||||
{
|
||||
char * Input = NULL;
|
||||
int MaxDataSize = (int)(dwSize + 1);
|
||||
result = -1;
|
||||
for (int count = ReadPos; count < MaxDataSize; count++)
|
||||
{
|
||||
if (Data[count] != '\n')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
int len = (count - ReadPos) + 1;
|
||||
Input = &Data[ReadPos];
|
||||
ReadPos = count + 1;
|
||||
result = len;
|
||||
break;
|
||||
}
|
||||
if (result <= 1) { continue; }
|
||||
std::string line(Input, result);
|
||||
if (strlen(CleanLine((char *)line.c_str())) <= 1) { continue; }
|
||||
if (line[0] != '[') { continue; }
|
||||
pNextSection = Input;
|
||||
break;
|
||||
} while (result >= 0);
|
||||
|
||||
if (pNextSection)
|
||||
{
|
||||
strNewData += pNextSection;
|
||||
}
|
||||
|
||||
m_File.Seek(m_CurrentSectionFilePos, CFileBase::begin);
|
||||
m_File.Write(strNewData.c_str(), (uint32_t)strlen(strNewData.c_str()));
|
||||
m_File.Flush();
|
||||
m_File.SetEndOfFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -730,10 +720,10 @@ void CIniFileBase::SaveString(const char * lpSectionName, const char * lpKeyNam
|
|||
}
|
||||
}
|
||||
|
||||
void CIniFileBase::SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value)
|
||||
void CIniFileBase::SaveNumber(const char * lpSectionName, const char * lpKeyName, int32_t Value)
|
||||
{
|
||||
//translate the string to an ascii version and save as text
|
||||
SaveString(lpSectionName, lpKeyName, stdstr_f("%d", Value).c_str());
|
||||
SaveString(lpSectionName, lpKeyName, FormatStr("%d", Value).c_str());
|
||||
}
|
||||
|
||||
bool CIniFileBase::EntryExists(const char * lpSectionName, const char * lpKeyName)
|
||||
|
@ -817,7 +807,7 @@ void CIniFileBase::GetKeyValueData(const char * lpSectionName, KeyValueData & Li
|
|||
if (!MoveToSectionNameData(strSection.c_str(), false)) { return; }
|
||||
|
||||
int MaxDataSize = 0, DataSize = 0, ReadPos = 0, result;
|
||||
AUTO_PTR<char> Data;
|
||||
std::unique_ptr <char> Data;
|
||||
char *Input = NULL;
|
||||
do
|
||||
{
|
||||
|
@ -874,7 +864,7 @@ void CIniFileBase::GetVectorOfSections(SectionList & sections)
|
|||
}
|
||||
|
||||
{
|
||||
stdstr_f DoesNotExist("DoesNotExist%d%d%d", rand(), rand(), rand());
|
||||
std::string DoesNotExist = FormatStr("DoesNotExist%d%d%d", rand(), rand(), rand());
|
||||
MoveToSectionNameData(DoesNotExist.c_str(), false);
|
||||
}
|
||||
|
||||
|
@ -882,4 +872,23 @@ void CIniFileBase::GetVectorOfSections(SectionList & sections)
|
|||
{
|
||||
sections.push_back(iter->first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (buffer != NULL)
|
||||
{
|
||||
vsprintf(buffer, strFormat, args);
|
||||
FormatedStr = buffer;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
return FormatedStr;
|
||||
}
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef _WIN32
|
||||
/* for POSIX method away from Win32 _stricmp--see "Platform.h" */
|
||||
#include <strings.h>
|
||||
#endif
|
||||
|
||||
#include "FileClass.h"
|
||||
#include "CriticalSection.h"
|
||||
#include "SmartPointer.h"
|
||||
#include "Platform.h"
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
|
||||
class CIniFileBase
|
||||
{
|
||||
|
@ -37,7 +35,7 @@ public:
|
|||
bool GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value);
|
||||
|
||||
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, int32_t Value);
|
||||
void SetAutoFlush(bool AutoFlush);
|
||||
void FlushChanges(void);
|
||||
bool EntryExists(const char * lpSectionName, const char * lpKeyName);
|
||||
|
@ -53,6 +51,8 @@ protected:
|
|||
void OpenIniFile(bool bCreate = true);
|
||||
void SaveCurrentSection(void);
|
||||
|
||||
std::string FormatStr(const char * strFormat, ...);
|
||||
|
||||
CFileBase & m_File;
|
||||
std::string m_FileName;
|
||||
|
||||
|
@ -85,7 +85,7 @@ private:
|
|||
SortData m_SortFunction;
|
||||
|
||||
void fInsertSpaces(int Pos, int NoOfSpaces);
|
||||
int GetStringFromFile(char * & String, AUTO_PTR<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos);
|
||||
int GetStringFromFile(char * & String, std::unique_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);
|
||||
|
|
Loading…
Reference in New Issue