📄 ini.cpp
字号:
// Copyright 1997-98 (c) Iuri Apollonio
// given freely to www.codeguru.com
// Ini.cpp: implementation of the COwnerIni class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Ini.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COwnerIni::COwnerIni()
{
csLineEnd = _T("\r\n");
}
COwnerIni::~COwnerIni()
{
Clear();
}
void COwnerIni::Clear()
{
csList.RemoveAll();
}
bool COwnerIni::Read(const TCHAR * cFileName)
{
Clear();
TCHAR buf[MAX_PATH];
WIN32_FIND_DATA d;
HANDLE hHandle = NULL;
if (INVALID_HANDLE_VALUE
== (hHandle = FindFirstFile(cFileName,&d)))
{
FindClose ( hHandle );
return false;
}
FindClose ( hHandle );
#ifdef _UNICODE
CFileHelper file(cFileName,CFile::modeCreate
| CFile::modeReadWrite | CFile::modeNoTruncate);
while (file.GetPosition () < file.GetLength ())
{
unsigned char buffChar[MAX_PATH];
file.SvrGetLine (buffChar,MAX_PATH -1);
MultiByteToWideChar( CP_ACP, 0, (char*)buffChar, -1, buf, MAX_PATH );
CString cs(buf);
TRIM(cs);
if ( !cs.IsEmpty() )
csList.Add(cs);
}
#else
ifstream ifs(cFileName);
while (ifs.good())
{
ifs.getline(buf, MAX_PATH - 1);
CString cs(buf);
TRIM(cs);
if ( !cs.IsEmpty() )
csList.Add(cs);
}
#endif
return true;
}
bool COwnerIni::Write(const TCHAR * cFileName)
{
ASSERT(cFileName);
#ifdef _UNICODE
WIN32_FIND_DATA d;
HANDLE hHandle = NULL;
if (INVALID_HANDLE_VALUE
== (hHandle = FindFirstFile(cFileName,&d)))
{
FindClose ( hHandle );
try
{
CFile file(cFileName, CFile::modeCreate );
}
catch( CException* pException )
{
pException ->Delete();
return false;
}
}
FindClose ( hHandle );
CFileHelper file(cFileName,
CFile::modeCreate | CFile::modeReadWrite|CFile::modeNoTruncate);
int t, max = csList.GetSize();
char buff[MAX_PATH * 10];
for (t = 0; t < max; t++)
{
DWORD dwSize = WideCharToMultiByte(CP_ACP,
0,(LPCTSTR)csList[t],-1,(char*)buff,MAX_PATH * 10,NULL,NULL);
file.Write (buff,dwSize);
char buff2[2];
buff2[0] = _T('\r');
buff2[1] = _T('\n');
file.Write (buff2,2);
}
#else
ofstream ofs(cFileName);
int t, max = csList.GetSize();
for (t = 0; t < max; t++)
{
ofs << csList[t] << _T("\n");
}
#endif
return true;
}
int COwnerIni::FindSection(const TCHAR * cSection)
{
int t, max = csList.GetSize();
CString csSection;
csSection.Format(_T("[%s]"), cSection);
for (t = 0; t < max; t++)
{
if (csList[t] == csSection)
{
return t;
}
}
return -1;
}
int COwnerIni::InsertSection(const TCHAR * cSection)
{
ASSERT(cSection);
if (!cSection) return -1;
int idx = FindSection(cSection);
if (idx < 0)
{
CString csSection;
csSection.Format(_T("[%s]"), cSection);
idx = csList.Add(csSection);
}
return idx;
}
int COwnerIni::FindItem(const int iSection,
const TCHAR * cItem, CString &csVal)
{
ASSERT(iSection >= 0);
ASSERT(cItem);
int max = csList.GetSize(), t;
CString csItem(cItem), csLook;
for (t = iSection; t < max; t++)
{
if (!IsSection(t))
{
csLook = csList[t];
int iPos = csLook.Find(_T("="));
if (iPos < 1) continue;
CString sTest = csLook.Left(iPos);
TRIM(sTest);
if (sTest.CompareNoCase (cItem) == 0)
{
CString s = csLook.Right(csLook.GetLength() - iPos - 1);
TRIM(s);
csVal = s;
return t;
}
}
else return -1;
}
return -1;
}
int COwnerIni::FindMultiItem(const int iSection,
const TCHAR * cItem, CString &csVal)
{
ASSERT(iSection >= 0);
ASSERT(cItem);
int max = csList.GetSize(), t, i;
CString csItem(cItem), csLook;
csItem += _T(" = \"");
for (t = iSection; t < max; t++)
{
if (!IsSection(t))
{
csLook = csList[t];
if (csLook == csItem)
{
csVal = _T("");
for (i = t + 1; i < max; i++)
{
csLook = csList[i];
if (csLook == _T('\"') || IsSection(i))
{
i = max;
}
else
{
if (csVal != _T("")) csVal += csLineEnd;
csVal += csLook;
}
}
return t;
}
}
else
return -1;
}
return -1;
}
bool COwnerIni::IsSection(const int iSection)
{
ASSERT(iSection >= 0 && iSection < csList.GetSize());
if (iSection >= 0 && iSection < csList.GetSize())
{
CString csItem = csList[iSection];
if (csItem.GetLength() > 2 &&
csItem.Left(1) == _T('[') &&
csItem.Right(1) == _T(']')) return true;
}
return false;
}
bool COwnerIni::RemoveSection(const TCHAR * cSection)
{
int idx = FindSection(cSection);
if (idx >= 0)
{
for (;;)
{
csList.RemoveAt(idx);
if (idx >= csList.GetSize())
return true;
if (IsSection(idx))
return true;
}
}
return true;
}
void COwnerIni::RemoveMultiLineItem(const int idx)
{
int max = csList.GetSize(), t;
CString csLook;
for (t = idx; t < max; t++)
{
if (!IsSection(t))
{
csLook = csList[t];
if (csLook == _T('\"'))
{
csList.RemoveAt(t);
return;
}
csList.RemoveAt(t);
}
else return;
}
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const bool bVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %s"),
cItem, bVal ? _T("true") : _T("false"));
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const COLORREF crVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %d"), cItem, (DWORD) crVal);
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const TCHAR * cVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %s"), cItem, cVal);
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const double dbVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %f"), cItem, dbVal);
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const float fVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %f"), cItem, fVal);
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const long lVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %d"), cItem, lVal);
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetValue(const TCHAR * cSection,
const TCHAR * cItem, const int iVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %d"), cItem, iVal);
if (iIdx >= 0)
csList.SetAt(iIdx, csVal);
else
csList.InsertAt(idx+1, csVal);
return true;
}
return false;
}
bool COwnerIni::SetMultiValue(const TCHAR * cSection,
const TCHAR * cItem, const TCHAR * cVal)
{
int idx = InsertSection(cSection);
if (idx >= 0)
{
CString csVal;
int iIdx = FindItem(idx+1, cItem, csVal);
csVal.Format(_T("%s = %s"), cItem, cVal);
TCHAR * c = csVal.LockBuffer();
int i = csVal.Find(_T('\r'));
while (i >= 0)
{
UINT t = _T('
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -