2008-09-18 03:15:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-01-13 02:21:23 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
#include "CriticalSection.h"
|
2022-10-03 08:04:42 +00:00
|
|
|
#include "File.h"
|
|
|
|
#include "Platform.h"
|
2018-11-19 10:46:58 +00:00
|
|
|
#include <list>
|
2022-10-03 08:04:42 +00:00
|
|
|
#include <map>
|
2020-04-13 02:54:21 +00:00
|
|
|
#include <memory>
|
2022-10-03 08:04:42 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
class CIniFileBase
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2019-04-18 07:27:20 +00:00
|
|
|
public:
|
|
|
|
typedef std::map<std::string, std::string> KeyValueData;
|
2020-10-12 06:21:23 +00:00
|
|
|
typedef std::set<std::string> SectionList;
|
|
|
|
typedef std::list<std::string> strlist;
|
2019-04-18 07:27:20 +00:00
|
|
|
typedef std::pair<const std::string *, const std::string *> KeyValueItem;
|
|
|
|
typedef std::vector<KeyValueItem> KeyValueVector;
|
2022-10-03 08:04:42 +00:00
|
|
|
typedef void (*SortData)(KeyValueVector &);
|
2019-04-18 07:27:20 +00:00
|
|
|
|
|
|
|
CIniFileBase(CFileBase & FileObject, const char * FileName);
|
|
|
|
virtual ~CIniFileBase(void);
|
|
|
|
|
|
|
|
bool IsEmpty();
|
|
|
|
bool IsFileOpen(void);
|
2022-07-11 10:00:25 +00:00
|
|
|
bool IsReadOnly(void);
|
2019-04-18 07:27:20 +00:00
|
|
|
bool DeleteSection(const char * lpSectionName);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
virtual void SaveString(const char * lpSectionName, const char * lpKeyName, const char * lpString);
|
2022-07-11 10:00:25 +00:00
|
|
|
virtual void SaveNumber(const char * lpSectionName, const char * lpKeyName, uint32_t Value);
|
2019-04-18 07:27:20 +00:00
|
|
|
void SetAutoFlush(bool AutoFlush);
|
|
|
|
void FlushChanges(void);
|
|
|
|
bool EntryExists(const char * lpSectionName, const char * lpKeyName);
|
2022-10-03 08:04:42 +00:00
|
|
|
void GetKeyList(const char * lpSectionName, strlist & List);
|
2019-04-18 07:27:20 +00:00
|
|
|
void GetKeyValueData(const char * lpSectionName, KeyValueData & List);
|
|
|
|
void SetCustomSort(SortData SortFunction);
|
|
|
|
|
|
|
|
void GetVectorOfSections(SectionList & sections);
|
2022-10-03 08:04:42 +00:00
|
|
|
const std::string & GetFileName()
|
|
|
|
{
|
|
|
|
return m_FileName;
|
|
|
|
}
|
2019-04-18 07:27:20 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void OpenIniFileReadOnly();
|
|
|
|
void OpenIniFile(bool bCreate = true);
|
|
|
|
void SaveCurrentSection(void);
|
|
|
|
|
2020-04-13 02:54:21 +00:00
|
|
|
std::string FormatStr(const char * strFormat, ...);
|
|
|
|
|
2019-04-18 07:27:20 +00:00
|
|
|
CFileBase & m_File;
|
|
|
|
std::string m_FileName;
|
|
|
|
|
|
|
|
private:
|
2015-10-25 10:50:28 +00:00
|
|
|
struct insensitive_compare
|
|
|
|
{
|
2022-10-03 08:04:42 +00:00
|
|
|
bool operator()(const std::string & a, const std::string & b) const
|
2016-01-13 02:21:23 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-07-11 10:00:25 +00:00
|
|
|
typedef std::map<std::string, long, insensitive_compare> 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;
|
2020-10-12 06:21:23 +00:00
|
|
|
|
2018-03-26 19:39:34 +00:00
|
|
|
std::string m_CurrentSection;
|
|
|
|
bool m_CurrentSectionDirty;
|
2021-05-18 11:51:36 +00:00
|
|
|
int m_CurrentSectionFilePos;
|
2015-10-25 10:50:28 +00:00
|
|
|
KeyValueList m_CurrentSectionData;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
long m_lastSectionSearch;
|
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;
|
2019-04-18 07:27:20 +00:00
|
|
|
SortData m_SortFunction;
|
2015-10-25 10:50:28 +00:00
|
|
|
|
2016-01-12 18:38:10 +00:00
|
|
|
void fInsertSpaces(int Pos, int NoOfSpaces);
|
2022-10-03 08:04:42 +00:00
|
|
|
int GetStringFromFile(char *& String, std::unique_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);
|
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
|
|
|
{
|
2021-03-16 22:49:05 +00:00
|
|
|
// Try to open file for reading
|
2015-10-25 10:50:28 +00:00
|
|
|
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
|
|
|
|
{
|
2021-03-16 22:49:05 +00:00
|
|
|
// Try to open file for reading
|
2015-10-25 10:50:28 +00:00
|
|
|
OpenIniFile(bCreate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual ~CIniFileT(void)
|
|
|
|
{
|
|
|
|
SaveCurrentSection();
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
protected:
|
2022-10-03 08:04:42 +00:00
|
|
|
CFileStorage m_FileObject;
|
2008-09-18 03:15:49 +00:00
|
|
|
};
|
|
|
|
|
2020-12-29 22:44:04 +00:00
|
|
|
typedef CIniFileT<CFile> CIniFile;
|