📄 ini2.cpp
字号:
#include "stdafx.h"
#include "Ini2.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// If the IniFilename contains no path,
// the module-directory will be add to the FileName,
// to avoid storing in the windows-directory
void CIni::AddModulPath(CString& strFileName,BOOL bModulPath /*= TRUE*/)
{
TCHAR drive[_MAX_DRIVE];
TCHAR dir[_MAX_DIR];
TCHAR fname[_MAX_FNAME];
TCHAR ext[_MAX_EXT];
_tsplitpath( strFileName, drive, dir, fname, ext );
if( ! drive[0] )
{
//PathCanonicalize(..) doesn't work with for all Plattforms !
CString strModule;
if( bModulPath )
{
GetModuleFileName(NULL,strModule.GetBuffer(MAX_INI_BUFFER),MAX_INI_BUFFER);
strModule.ReleaseBuffer();
}
else
{
GetCurrentDirectory(MAX_INI_BUFFER,strModule.GetBuffer(MAX_INI_BUFFER));
strModule.ReleaseBuffer();
// fix by "cpp@world-online.no"
strModule.TrimRight(_T('\\'));
strModule.TrimRight(_T('/'));
strModule += _T("\\");
}
strModule.ReleaseBuffer();
_tsplitpath( strModule, drive, dir, fname, ext );
strModule = drive;
strModule+= dir;
strModule+= strFileName;
strFileName = strModule;
}
}
CString CIni::GetDefaultSection()
{
return AfxGetAppName();
}
CString CIni::GetDefaultIniFile(BOOL bModulPath /*= TRUE*/)
{
TCHAR drive[_MAX_DRIVE];
TCHAR dir[_MAX_DIR];
TCHAR fname[_MAX_FNAME];
TCHAR ext[_MAX_EXT];
CString strTemp;
CString strApplName;
GetModuleFileName(NULL,strTemp.GetBuffer(MAX_INI_BUFFER),MAX_INI_BUFFER);
strTemp.ReleaseBuffer();
_tsplitpath( strTemp, drive, dir, fname, ext );
strTemp = fname; //"ApplName"
strTemp += _T(".ini"); //"ApplName.ini"
if( bModulPath )
{
strApplName = drive;
strApplName += dir;
strApplName += strTemp;
}
else
{
GetCurrentDirectory(MAX_INI_BUFFER,strApplName.GetBuffer(MAX_INI_BUFFER));
strApplName.ReleaseBuffer();
strApplName.TrimRight(_T('\\'));
strApplName.TrimRight(_T('/'));
strApplName += _T("\\");
strApplName += strTemp;
}
return strApplName;
}
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
// Creates/Use file : "Drive:\ApplPath\ApplName.ini"
CIni::CIni(BOOL bModulPath /*= TRUE*/):
m_bModulPath(bModulPath)
{
m_strFileName = GetDefaultIniFile(m_bModulPath);
m_strSection = GetDefaultSection();
}
CIni::CIni(CIni const& Ini, BOOL bModulPath /*= TRUE*/):
m_strFileName(Ini.m_strFileName),
m_strSection(Ini.m_strSection),
m_bModulPath(Ini.m_bModulPath)
{
if(m_strFileName.IsEmpty())
m_strFileName = GetDefaultIniFile(m_bModulPath);
AddModulPath(m_strFileName,m_bModulPath);
if(m_strSection.IsEmpty())
m_strSection = GetDefaultSection();
}
CIni::CIni(CString const& strFileName, BOOL bModulPath /*= TRUE*/):
m_strFileName(strFileName),
m_bModulPath(bModulPath)
{
if(m_strFileName.IsEmpty())
m_strFileName = GetDefaultIniFile(m_bModulPath);
AddModulPath(m_strFileName,bModulPath);
m_strSection = GetDefaultSection();
}
CIni::CIni(CString const& strFileName, CString const& strSection, BOOL bModulPath /*= TRUE*/):
m_strFileName(strFileName),
m_strSection(strSection),
m_bModulPath(bModulPath)
{
if(m_strFileName.IsEmpty())
m_strFileName = GetDefaultIniFile(m_bModulPath);
AddModulPath(m_strFileName,bModulPath);
if(m_strSection.IsEmpty())
m_strSection = GetDefaultSection();
}
CIni::~CIni()
{
}
//////////////////////////////////////////////////////////////////////
// Zugriff auf Quelle/Ziel von IO-Operationen
//////////////////////////////////////////////////////////////////////
void CIni::SetFileName(const CString& strFileName)
{
m_strFileName = strFileName;
AddModulPath(m_strFileName);
}
void CIni::SetSection(const CString& strSection)
{
m_strSection = strSection;
}
const CString& CIni::GetFileName() const
{
return m_strFileName;
}
const CString& CIni::GetSection() const
{
return m_strSection;
}
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
void CIni::Init( LPCTSTR strFileName, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
if(strFileName != NULL)
m_strFileName = strFileName;
}
CString CIni::GetString(LPCTSTR strEntry, LPCTSTR strDefault/*=NULL*/, LPCTSTR strSection/* = NULL*/)
{
if(strDefault == NULL)
return CString(GetLPCSTR(strEntry,strSection,_T("")));
else
return CString(GetLPCSTR(strEntry,strSection,strDefault));
}
double CIni::GetDouble(LPCTSTR strEntry, double fDefault/* = 0.0*/, LPCTSTR strSection/* = NULL*/)
{
TCHAR strDefault[MAX_PATH];
_sntprintf(strDefault, ARRSIZE(strDefault), _T("%g"), fDefault);
GetLPCSTR(strEntry,strSection,strDefault);
return _tstof(m_chBuffer);
}
float CIni::GetFloat(LPCTSTR strEntry,float fDefault/* = 0.0*/, LPCTSTR strSection/* = NULL*/)
{
TCHAR strDefault[MAX_PATH];
_sntprintf(strDefault, ARRSIZE(strDefault), _T("%g"), fDefault);
GetLPCSTR(strEntry,strSection,strDefault);
return (float)_tstof(m_chBuffer);
}
int CIni::GetInt(LPCTSTR strEntry,int nDefault/* = 0*/,LPCTSTR strSection/* = NULL*/)
{
TCHAR strDefault[MAX_PATH];
_sntprintf(strDefault, ARRSIZE(strDefault), _T("%d"), nDefault);
GetLPCSTR(strEntry,strSection,strDefault);
return _tstoi(m_chBuffer);
}
ULONGLONG CIni::GetUInt64(LPCTSTR strEntry,ULONGLONG nDefault/* = 0*/,LPCTSTR strSection/* = NULL*/)
{
TCHAR strDefault[MAX_PATH];
_sntprintf(strDefault, ARRSIZE(strDefault), _T("%I64u"), nDefault);
GetLPCSTR(strEntry,strSection,strDefault);
ULONGLONG nResult;
if (_stscanf(m_chBuffer, _T("%I64u"), &nResult) != 1)
return 0;
return nResult;
}
WORD CIni::GetWORD(LPCTSTR strEntry,WORD nDefault/* = 0*/,LPCTSTR strSection/* = NULL*/)
{
TCHAR strDefault[MAX_PATH];
_sntprintf(strDefault, ARRSIZE(strDefault), _T("%u"), nDefault);
GetLPCSTR(strEntry,strSection,strDefault);
return (WORD)_tstoi(m_chBuffer);
}
BOOL CIni::GetBool(LPCTSTR strEntry,BOOL bDefault/* = FALSE*/,LPCTSTR strSection/* = NULL*/)
{
TCHAR strDefault[MAX_PATH];
_sntprintf(strDefault, ARRSIZE(strDefault), _T("%d"), bDefault);
GetLPCSTR(strEntry,strSection,strDefault);
return ( _tstoi(m_chBuffer) != 0 );
}
CPoint CIni::GetPoint(LPCTSTR strEntry, CPoint ptDefault, LPCTSTR strSection)
{
CPoint ptReturn=ptDefault;
CString strDefault;
strDefault.Format(_T("(%d,%d)"),ptDefault.x, ptDefault.y);
CString strPoint = GetString(strEntry,strDefault);
_stscanf(strPoint,_T("(%d,%d)"), &ptReturn.x, &ptReturn.y);
return ptReturn;
}
CRect CIni::GetRect(LPCTSTR strEntry, CRect rectDefault, LPCTSTR strSection)
{
CRect rectReturn=rectDefault;
CString strDefault;
//old version :strDefault.Format("(%d,%d,%d,%d)",rectDefault.top,rectDefault.left,rectDefault.bottom,rectDefault.right);
strDefault.Format(_T("%d,%d,%d,%d"),rectDefault.left,rectDefault.top,rectDefault.right,rectDefault.bottom);
CString strRect = GetString(strEntry,strDefault);
//new Version found
if( 4==_stscanf(strRect,_T("%d,%d,%d,%d"),&rectDefault.left,&rectDefault.top,&rectDefault.right,&rectDefault.bottom))
return rectReturn;
//old Version found
_stscanf(strRect,_T("(%d,%d,%d,%d)"), &rectReturn.top,&rectReturn.left,&rectReturn.bottom,&rectReturn.right);
return rectReturn;
}
COLORREF CIni::GetColRef(LPCTSTR strEntry, COLORREF crDefault, LPCTSTR strSection)
{
int temp[3]={ GetRValue(crDefault),
GetGValue(crDefault),
GetBValue(crDefault)};
CString strDefault;
strDefault.Format(_T("RGB(%hd,%hd,%hd)"),temp[0],temp[1],temp[2]);
CString strColRef = GetString(strEntry,strDefault);
_stscanf(strColRef,_T("RGB(%d,%d,%d)"), temp, temp+1, temp+2);
return RGB(temp[0],temp[1],temp[2]);
}
void CIni::WriteString(LPCTSTR strEntry, LPCTSTR str, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
WritePrivateProfileString(m_strSection,strEntry,str,m_strFileName);
}
void CIni::WriteDouble(LPCTSTR strEntry,double f, LPCTSTR strSection/*= NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
TCHAR strBuffer[MAX_PATH];
_sntprintf(strBuffer, ARRSIZE(strBuffer), _T("%g"), f);
WritePrivateProfileString(m_strSection,strEntry,strBuffer,m_strFileName);
}
void CIni::WriteFloat(LPCTSTR strEntry,float f, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
TCHAR strBuffer[MAX_PATH];
_sntprintf(strBuffer, ARRSIZE(strBuffer), _T("%g"), f);
WritePrivateProfileString(m_strSection,strEntry,strBuffer,m_strFileName);
}
void CIni::WriteInt(LPCTSTR strEntry,int n, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
TCHAR strBuffer[MAX_PATH];
_itot(n, strBuffer, 10);
WritePrivateProfileString(m_strSection,strEntry,strBuffer,m_strFileName);
}
void CIni::WriteUInt64(LPCTSTR strEntry,ULONGLONG n, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
TCHAR strBuffer[MAX_PATH];
_ui64tot(n, strBuffer, 10);
WritePrivateProfileString(m_strSection,strEntry,strBuffer,m_strFileName);
}
void CIni::WriteWORD(LPCTSTR strEntry,WORD n, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
TCHAR strBuffer[MAX_PATH];
_ultot(n, strBuffer, 10);
WritePrivateProfileString(m_strSection,strEntry,strBuffer,m_strFileName);
}
void CIni::WriteBool(LPCTSTR strEntry,BOOL b, LPCTSTR strSection/* = NULL*/)
{
if(strSection != NULL)
m_strSection = strSection;
TCHAR strBuffer[MAX_PATH];
_sntprintf(strBuffer, ARRSIZE(strBuffer), _T("%d"), b);
WritePrivateProfileString(m_strSection, strEntry, strBuffer, m_strFileName);
}
void CIni::WritePoint(LPCTSTR strEntry,CPoint pt, LPCTSTR strSection)
{
if(strSection != NULL)
m_strSection = strSection;
CString strBuffer;
strBuffer.Format(_T("(%d,%d)"),pt.x,pt.y);
Write(m_strFileName,m_strSection,strEntry,strBuffer);
}
void CIni::WriteRect(LPCTSTR strEntry,CRect rect, LPCTSTR strSection)
{
if(strSection != NULL)
m_strSection = strSection;
CString strBuffer;
strBuffer.Format(_T("(%d,%d,%d,%d)"),rect.top,rect.left,rect.bottom,rect.right);
Write(m_strFileName,m_strSection,strEntry,strBuffer);
}
void CIni::WriteColRef(LPCTSTR strEntry,COLORREF cr, LPCTSTR strSection)
{
if(strSection != NULL)
m_strSection = strSection;
CString strBuffer;
strBuffer.Format(_T("RGB(%d,%d,%d)"),GetRValue(cr), GetGValue(cr), GetBValue(cr));
Write(m_strFileName,m_strSection,strEntry,strBuffer);
}
TCHAR* CIni::GetLPCSTR(LPCTSTR strEntry, LPCTSTR strSection, LPCTSTR strDefault)
{
// evtl Section neu setzen
if(strSection != NULL)
m_strSection = strSection;
CString temp;
if(strDefault == NULL)
temp = Read(m_strFileName,m_strSection,strEntry,CString());
else
temp = Read(m_strFileName,m_strSection,strEntry,strDefault);
return (TCHAR*)memcpy(m_chBuffer,(LPCTSTR)temp,(temp.GetLength() + 1)*sizeof(TCHAR));// '+1' damit die Null am Ende mit kopiert wird
}
void CIni::SerGetString( BOOL bGet,CString & str,LPCTSTR strEntry,LPCTSTR strSection,LPCTSTR strDefault)
{
if(bGet)
str = GetString(strEntry,strDefault/*=NULL*/,strSection/* = NULL*/);
else
WriteString(strEntry,str, strSection/* = NULL*/);
}
void CIni::SerGetDouble( BOOL bGet,double& f, LPCTSTR strEntry,LPCTSTR strSection/* = NULL*/,double fDefault/* = 0.0*/)
{
if(bGet)
f = GetDouble(strEntry,fDefault/*=NULL*/,strSection/* = NULL*/);
else
WriteDouble(strEntry,f, strSection/* = NULL*/);
}
void CIni::SerGetFloat( BOOL bGet,float & f, LPCTSTR strEntry, LPCTSTR strSection/* = NULL*/,float fDefault/* = 0.0*/)
{
if(bGet)
f = GetFloat(strEntry,fDefault/*=NULL*/,strSection/* = NULL*/);
else
WriteFloat(strEntry,f, strSection/* = NULL*/);
}
void CIni::SerGetInt( BOOL bGet,int & n, LPCTSTR strEntry,LPCTSTR strSection/* = NULL*/,int nDefault/* = 0*/)
{
if(bGet)
n = GetInt(strEntry,nDefault/*=NULL*/,strSection/* = NULL*/);
else
WriteInt(strEntry,n, strSection/* = NULL*/);
}
void CIni::SerGetDWORD( BOOL bGet,DWORD & n, LPCTSTR strEntry,LPCTSTR strSection/* = NULL*/,DWORD nDefault/* = 0*/)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -