2008-09-18 03:15:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-01-13 02:21:23 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
/* for POSIX method away from Win32 _stricmp--see "Platform.h" */
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2016-01-12 18:46:50 +00:00
|
|
|
#include "Platform.h"
|
2016-01-13 02:21:23 +00:00
|
|
|
|
2015-12-04 06:49:31 +00:00
|
|
|
#include "FileClass.h"
|
2009-12-28 22:22:50 +00:00
|
|
|
#include "CriticalSection.h"
|
2015-12-04 06:49:31 +00:00
|
|
|
#include "StdString.h"
|
2016-11-14 07:10:43 +00:00
|
|
|
#include "SmartPointer.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
#include <map>
|
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
class CIniFileBase
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2015-10-25 10:50:28 +00:00
|
|
|
struct insensitive_compare
|
|
|
|
{
|
2016-01-13 02:21:23 +00:00
|
|
|
bool operator() (const std::string & a, const std::string & b) const
|
|
|
|
{
|
|
|
|
return _stricmp(a.c_str(), b.c_str()) < 0;
|
|
|
|
}
|
2015-10-25 10:50:28 +00:00
|
|
|
};
|
2013-04-24 07:08:35 +00:00
|
|
|
|
2018-03-26 19:39:34 +00:00
|
|
|
typedef std::map<std::string, long> FILELOC;
|
2015-10-25 10:50:28 +00:00
|
|
|
typedef FILELOC::iterator FILELOC_ITR;
|
2018-03-26 19:39:34 +00:00
|
|
|
typedef std::map<std::string, std::string, insensitive_compare> KeyValueList;
|
2009-12-28 22:22:50 +00:00
|
|
|
|
|
|
|
public:
|
2018-11-18 01:06:02 +00:00
|
|
|
typedef std::map<std::string, std::string> KeyValueData;
|
|
|
|
typedef std::vector<std::string> SectionList;
|
|
|
|
typedef std::list<std::string> strlist;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
protected:
|
2018-03-26 19:39:34 +00:00
|
|
|
CFileBase & m_File;
|
|
|
|
std::string m_FileName;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
private:
|
2018-03-26 19:39:34 +00:00
|
|
|
std::string m_CurrentSection;
|
|
|
|
bool m_CurrentSectionDirty;
|
|
|
|
int m_CurrentSectionFilePos; // Where in the file is the current Section
|
2015-10-25 10:50:28 +00:00
|
|
|
KeyValueList m_CurrentSectionData;
|
|
|
|
|
2018-03-26 19:39:34 +00:00
|
|
|
long m_lastSectionSearch; // When Scanning for a section, what was the last scanned pos
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2018-03-26 19:39:34 +00:00
|
|
|
bool m_ReadOnly;
|
|
|
|
bool m_InstantFlush;
|
2015-10-25 10:50:28 +00:00
|
|
|
const char * m_LineFeed;
|
|
|
|
|
|
|
|
CriticalSection m_CS;
|
|
|
|
FILELOC m_SectionsPos;
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void fInsertSpaces(int Pos, int NoOfSpaces);
|
2018-03-26 19:39:34 +00:00
|
|
|
int GetStringFromFile(char * & String, AUTO_PTR<char> &Data, int & MaxDataSize, int & DataSize, int & ReadPos);
|
2016-01-12 18:38:10 +00:00
|
|
|
bool MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection);
|
2016-09-29 11:14:29 +00:00
|
|
|
const char * CleanLine(char * Line);
|
2016-01-12 18:38:10 +00:00
|
|
|
void ClearSectionPosList(long FilePos);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
protected:
|
2015-10-25 10:50:28 +00:00
|
|
|
void OpenIniFileReadOnly();
|
|
|
|
void OpenIniFile(bool bCreate = true);
|
2016-01-12 18:38:10 +00:00
|
|
|
void SaveCurrentSection(void);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
public:
|
2016-01-12 18:38:10 +00:00
|
|
|
CIniFileBase(CFileBase & FileObject, const char * FileName);
|
2015-10-25 10:50:28 +00:00
|
|
|
virtual ~CIniFileBase(void);
|
|
|
|
|
|
|
|
bool IsEmpty();
|
2016-01-12 18:38:10 +00:00
|
|
|
bool IsFileOpen(void);
|
|
|
|
bool DeleteSection(const char * lpSectionName);
|
2018-03-26 19:39:34 +00:00
|
|
|
bool GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault, std::string & Value);
|
|
|
|
std::string GetString(const char * lpSectionName, const char * lpKeyName, const char * lpDefault);
|
2016-01-12 18:38:10 +00:00
|
|
|
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);
|
|
|
|
bool GetNumber(const char * lpSectionName, const char * lpKeyName, uint32_t nDefault, uint32_t & Value);
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-11-14 07:10:43 +00:00
|
|
|
virtual void SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString);
|
|
|
|
virtual void SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value);
|
2016-01-12 18:38:10 +00:00
|
|
|
void SetAutoFlush(bool AutoFlush);
|
|
|
|
void FlushChanges(void);
|
2016-04-28 07:22:59 +00:00
|
|
|
bool EntryExists(const char * lpSectionName, const char * lpKeyName);
|
2016-01-12 18:38:10 +00:00
|
|
|
void GetKeyList(const char * lpSectionName, strlist &List);
|
|
|
|
void GetKeyValueData(const char * lpSectionName, KeyValueData & List);
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void GetVectorOfSections(SectionList & sections);
|
2018-03-26 19:39:34 +00:00
|
|
|
const std::string &GetFileName() { return m_FileName; }
|
2009-12-28 22:22:50 +00:00
|
|
|
};
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
template <class CFileStorage>
|
|
|
|
class CIniFileT :
|
2015-10-25 10:50:28 +00:00
|
|
|
public CIniFileBase
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
public:
|
2016-01-12 18:38:10 +00:00
|
|
|
CIniFileT(const char * FileName) :
|
|
|
|
CIniFileBase(m_FileObject, FileName)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
//Try to open file for reading
|
|
|
|
OpenIniFile();
|
|
|
|
}
|
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
CIniFileT(const char * FileName, bool bCreate, bool bReadOnly) :
|
|
|
|
CIniFileBase(m_FileObject, FileName)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
2016-01-12 18:38:10 +00:00
|
|
|
if (bReadOnly)
|
2015-10-25 10:50:28 +00:00
|
|
|
{
|
|
|
|
OpenIniFileReadOnly();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Try to open file for reading
|
|
|
|
OpenIniFile(bCreate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual ~CIniFileT(void)
|
|
|
|
{
|
|
|
|
SaveCurrentSection();
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
protected:
|
2015-10-25 10:50:28 +00:00
|
|
|
CFileStorage m_FileObject;
|
2008-09-18 03:15:49 +00:00
|
|
|
};
|
|
|
|
|
2015-01-31 19:27:27 +00:00
|
|
|
typedef CIniFileT<CFile> CIniFile;
|