📄 iniex.cpp
字号:
// IniEx.cpp: implementation of the CIniEx class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IniEx.h"
#include "malloc.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//GrowSize for Dynmiz Section Allocation
CIniEx::CIniEx(int GrowSize/*=4*/)
{
m_GrowSize=GrowSize;
m_SectionNo=0;
m_writeWhenChange=FALSE;
m_makeBackup=FALSE;
m_NoCaseSensitive=TRUE;
m_Changed=FALSE;
m_Keys=NULL;
m_Values=NULL;
}
CIniEx::~CIniEx()
{
if (m_writeWhenChange)
WriteFile(m_makeBackup);
ResetContent();
}
BOOL CIniEx::OpenAtExeDirectory(LPCSTR pFileName,
BOOL writeWhenChange,/*=TRUE*/
BOOL createIfNotExist/*=TRUE*/,
BOOL noCaseSensitive /*=TRUE*/,
BOOL makeBackup /*=FALSE*/)
{
CString filePath;
//if it's a dll argv will be NULL and it may cause memory leak
#ifndef _USRDLL
CString tmpFilePath;
int nPlace=0;
tmpFilePath=__argv[0];
nPlace=tmpFilePath.ReverseFind('\\');
if (nPlace!=-1)
{
filePath=tmpFilePath.Left(nPlace);
}
else
{
char curDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,curDir);
filePath=curDir;
}
#else
//it must be safe for dll's
char curDir[MAX_PATH];
GetCurrentDirectory(MAX_PATH,curDir);
filePath=curDir;
#endif
filePath+="\\";
filePath+=pFileName;
return Open(filePath,writeWhenChange,createIfNotExist,noCaseSensitive,makeBackup);
}
BOOL CIniEx::Open(LPCSTR pFileName,
BOOL writeWhenChange,/*=TRUE*/
BOOL createIfNotExist/*=TRUE*/,
BOOL noCaseSensitive /*=TRUE*/,
BOOL makeBackup /*=FALSE*/)
{
CFileException e;
BOOL bRet;
CString Line;
CString sectionStr;
int nPlace;
UINT mode=CFile::modeReadWrite;
//if it's second ini file for this instance
//we have to save it and delete member variables contents
if (!m_FileName.IsEmpty())
{
WriteFile();
ResetContent();
}
m_NoCaseSensitive=noCaseSensitive;
m_writeWhenChange=writeWhenChange;
m_makeBackup=makeBackup;
CString tmpKey;
CString tmpValue;
if (createIfNotExist)
mode= mode | CFile::modeCreate | CFile::modeNoTruncate;
try
{
CStdioFile file;
bRet=file.Open( pFileName, mode, &e );
if( !bRet )
{
return FALSE;
}
m_FileName=pFileName;
//Grow the arrays as GrowSize(which given constructor)
m_Keys=(CStringArray **)malloc( m_GrowSize * sizeof(CStringArray *) );
m_Values=(CStringArray **)malloc( m_GrowSize * sizeof(CStringArray *) );
for (int i=0;i<m_GrowSize;i++)
{
m_Keys[m_SectionNo+i]=NULL;
m_Values[m_SectionNo+i]=NULL;
}
//1.th section for sectionless ini files
m_Keys[m_SectionNo]=new CStringArray;
m_Values[m_SectionNo]=new CStringArray;
file.SeekToBegin();
while (TRUE)
{
//Read one line from given ini file
bRet=file.ReadString(Line);
if (!bRet) break;
//if line's first character = '['
//and last character = ']' it's section
if (Line.Left(1)=="[" && Line.Right(1)=="]")
{
m_SectionNo++;
GrowIfNecessary();
m_Keys[m_SectionNo]=new CStringArray;
m_Values[m_SectionNo]=new CStringArray;
sectionStr=Line.Mid(1,Line.GetLength()-2);
continue;
}
nPlace=Line.Find("=");
if (nPlace==-1)
{
tmpKey=Line;
tmpValue="";
}
else
{
tmpKey=Line.Left(nPlace);
tmpValue=Line.Mid(nPlace+1);
}
m_Keys[m_SectionNo]->Add(tmpKey);
m_Values[m_SectionNo]->Add(tmpValue);
m_Sections.SetAtGrow(m_SectionNo,sectionStr);
}
file.Close();
}
catch (CFileException *e)
{
m_ErrStr=e->m_cause;
}
return TRUE;
}
CString CIniEx::GetValue(CString Key)
{
return GetValue("",Key);
}
//if Section Name="" -> looking up key for witout section
CString CIniEx::GetValue(CString Section,CString Key,CString DefaultValue/*=""*/)
{
int nIndex=LookupSection(&Section);
if (nIndex==-1) return DefaultValue;
int nRet;
CString retStr;
for (int i=m_Keys[nIndex]->GetUpperBound();i>=0;i--)
{
nRet=CompareStrings(&m_Keys[nIndex]->GetAt(i),&Key);
if (nRet==0)
{
retStr=m_Values[nIndex]->GetAt(i);
int nPlace=retStr.ReverseFind(';');
if (nPlace!=-1) retStr.Delete(nPlace,retStr.GetLength()-nPlace);
return retStr;
}
}
return DefaultValue;
}
//returns index of key for given section
//if no result returns -1
int CIniEx::LookupKey(int nSectionIndex,CString *Key)
{
ASSERT(nSectionIndex<=m_SectionNo);
int nRet;
for (int i=m_Keys[nSectionIndex]->GetUpperBound();i>=0;i--)
{
nRet=CompareStrings(&m_Keys[nSectionIndex]->GetAt(i),Key);
if (nRet==0) return i;
}
return -1;
}
//return given sections index in array
int CIniEx::LookupSection(CString *Section)
{
int nRet;
for (int i=0;i<m_Sections.GetSize();i++)
{
nRet=CompareStrings(&m_Sections.GetAt(i),Section);
if (nRet==0) return i;
}
return -1;
}
//Sets for Key=Value for without section
void CIniEx::SetValue(CString Key,CString Value)
{
SetValue("",Key,Value);
}
//writes Key=value given section
void CIniEx::SetValue(CString Section,CString Key,CString Value)
{
//file opened?
ASSERT(!m_FileName.IsEmpty());
//if given key already existing, overwrite it
int nIndex=LookupSection(&Section);
int nKeyIndex;
if (nIndex==-1)
{
//if key not exist grow arrays (if necessary)
m_Changed=TRUE;
m_SectionNo++;
GrowIfNecessary();
m_Keys[m_SectionNo]=new CStringArray;
m_Values[m_SectionNo]=new CStringArray;
nIndex=m_SectionNo;
m_Sections.SetAtGrow(m_SectionNo,Section);
}
//looking up keys for section
nKeyIndex=LookupKey(nIndex,&Key);
//if key exist -> overwrite it
//if not add to end of array
if (nKeyIndex!=-1) //Bulundu ise;
{
//Var olan value'nun 鼁erine yaz齦齳or Fakat ayn齭
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -