2008-09-18 03:15:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
#include "File Class.h"
|
|
|
|
#include "CriticalSection.h"
|
|
|
|
#include "std string.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
|
|
|
|
{
|
|
|
|
bool operator() (const std::string & a, const std::string & b) const { return _stricmp(a.c_str(),b.c_str()) < 0; }
|
|
|
|
};
|
2013-04-24 07:08:35 +00:00
|
|
|
|
2015-10-25 10:50:28 +00:00
|
|
|
typedef std::string ansi_string;
|
|
|
|
typedef std::map<ansi_string,long> FILELOC;
|
|
|
|
typedef FILELOC::iterator FILELOC_ITR;
|
|
|
|
typedef std::map<ansi_string,ansi_string, insensitive_compare> KeyValueList;
|
2009-12-28 22:22:50 +00:00
|
|
|
|
|
|
|
public:
|
2015-10-25 10:50:28 +00:00
|
|
|
typedef std::map<stdstr,stdstr> KeyValueData;
|
|
|
|
typedef std::vector<stdstr> SectionList;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
protected:
|
2015-10-25 10:50:28 +00:00
|
|
|
CFileBase & m_File;
|
|
|
|
stdstr m_FileName;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
private:
|
2015-10-25 10:50:28 +00:00
|
|
|
ansi_string m_CurrentSection;
|
|
|
|
bool m_CurrentSectionDirty;
|
|
|
|
int m_CurrentSectionFilePos; // Where in the file is the current Section
|
|
|
|
KeyValueList m_CurrentSectionData;
|
|
|
|
|
|
|
|
long m_lastSectionSearch; // When Scanning for a section, what was the last scanned pos
|
|
|
|
|
|
|
|
bool m_ReadOnly;
|
|
|
|
bool m_InstantFlush;
|
|
|
|
const char * m_LineFeed;
|
|
|
|
|
|
|
|
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 );
|
|
|
|
bool MoveToSectionNameData(const char * lpSectionName, bool ChangeCurrentSection);
|
|
|
|
const char * CleanLine ( char * const Line );
|
|
|
|
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);
|
|
|
|
void SaveCurrentSection ( void );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
public:
|
2015-10-25 10:50:28 +00:00
|
|
|
CIniFileBase(CFileBase & FileObject, const char * FileName);
|
|
|
|
virtual ~CIniFileBase(void);
|
|
|
|
|
|
|
|
bool IsEmpty();
|
|
|
|
bool IsFileOpen ( void );
|
|
|
|
bool DeleteSection(const char * lpSectionName);
|
|
|
|
bool GetString ( const char * lpSectionName, const char * lpKeyName, const char * lpDefault, stdstr & Value );
|
|
|
|
stdstr 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 );
|
2009-12-28 22:22:50 +00:00
|
|
|
|
|
|
|
#ifdef _UNICODE
|
2015-10-25 10:50:28 +00:00
|
|
|
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 );
|
2009-12-28 22:22:50 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-10-25 10:50:28 +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 );
|
|
|
|
void SetAutoFlush (bool AutoFlush);
|
|
|
|
void FlushChanges (void);
|
|
|
|
void GetKeyList ( const char * lpSectionName, strlist &List );
|
|
|
|
void GetKeyValueData ( const char * lpSectionName, KeyValueData & List );
|
|
|
|
|
|
|
|
void GetVectorOfSections( SectionList & sections);
|
|
|
|
const stdstr &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:
|
2015-10-25 10:50:28 +00:00
|
|
|
CIniFileT( const char * FileName ) :
|
|
|
|
CIniFileBase(m_FileObject,FileName)
|
|
|
|
{
|
|
|
|
//Try to open file for reading
|
|
|
|
OpenIniFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
CIniFileT( const char * FileName, bool bCreate, bool bReadOnly) :
|
|
|
|
CIniFileBase(m_FileObject,FileName)
|
|
|
|
{
|
|
|
|
if(bReadOnly)
|
|
|
|
{
|
|
|
|
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;
|