#ifndef _CFILE_H
#define _CFILE_H
#include <f32file.h>
#include <badesca.h>
#include <bautils.h>
#include <utf.h>
class CFile
{
public:
static CFile *NewL(const TDesC &afileName);
static CFile *NewLC(const TDesC &afileName);
CFile(const TDesC &afileName);
~CFile();
public:
void ConstructL(TUint aShareMode);
TInt Read(TDes8 &aDes)const;
TInt Write(const TDesC8 &aDes);
TInt Seek(TSeek aMode, TInt &aPos) const;
TInt GetFileSize() const;
void DestroyL();
public:
static void GetDirFiles(const TDesC &aPath, CDesCArray& aFiles);
static void GetLinesFromFile(const TDesC& aFilename,CDesCArray& aLines);
static void GetLinesFromFile(const TDesC& aFilename,CDesC8Array& aLines);
protected:
TBuf<128> iFileName;
TInt iFileSize;
RFs iFs;
RFile iFile;
};
#endif
#include "CFile.h"
CFile *CFile::NewL(const TDesC &afileName)
{
CFile *pfile = CFile::NewLC(afileName);
CleanupStack::Pop(pfile);
return pfile;
}
CFile *CFile::NewLC(const TDesC &afileName)
{
CFile *pfile = new (ELeave) CFile(afileName);
CleanupStack::PushL(pfile);
return pfile;
}
CFile::CFile(const TDesC &afileName)
{
iFileName.Copy(afileName);
iFileSize = 0;
}
CFile::~CFile()
{
DestroyL();
}
void CFile::ConstructL(TUint aShareMode)
{
User::LeaveIfError(iFs.Connect());
TInt err = iFile.Open(iFs,iFileName,aShareMode);
if(err == KErrNotFound)
User::LeaveIfError(iFile.Create(iFs,iFileName,aShareMode));
iFile.Size(iFileSize);
}
TInt CFile::Read(TDes8 &aDes)const
{
return iFile.Read(aDes);
}
TInt CFile::Write(const TDesC8 &aDes)
{
return iFile.Write(aDes);
}
TInt CFile::Seek(TSeek aMode, TInt &aPos) const
{
return iFile.Seek(aMode, aPos);
}
TInt CFile::GetFileSize() const
{
return iFileSize;
}
void CFile::DestroyL()
{
iFile.Close();
iFs.Close();
}
void CFile::GetDirFiles(const TDesC &aPath, CDesCArray& aFiles)
{
RFs fs;
User::LeaveIfError(fs.Connect());
if( BaflUtils::FolderExists(fs,aPath) ){
CDir* dir=NULL;
User::LeaveIfError( fs.GetDir(aPath, KEntryAttNormal | KEntryAttMatchMask, ESortByName, dir) );
TInt ii = dir->Count();
for(TInt i = 0; i < ii; i++)
{
TEntry aEntry=(*dir)[i];
TBuf<256> filePath(aPath);
filePath.Append(aEntry.iName);
aFiles.AppendL(filePath);
}
}
fs.Close();
}
void CFile::GetLinesFromFile(const TDesC& aFilename,CDesCArray& aLines)
{
RFs fs;
User::LeaveIfError(fs.Connect() );
if( BaflUtils::FileExists(fs,aFilename) ){
RFile file;
TUint shareMode=EFileRead | EFileWrite ;
file.Open( fs, aFilename,shareMode ) ;
TInt fileSize=0;
file.Size(fileSize); //取得文件大小
CnvUtfConverter *cutf=new (ELeave) CnvUtfConverter;
HBufC8 *bufc = HBufC8::NewL(fileSize+1);
file.Read( (TDes8&) (bufc->Des()) ) ;
TInt res;
TPtrC8 iCursor(bufc->Des());
TBuf<256> buf16;
while( ( res = iCursor.FindF(_L8("\r\n")) ) >= 0 ) {
res = iCursor.FindF(_L8("\r\n"));
TPtrC8 result = iCursor.Left(res);
iCursor.Set(iCursor.Right(iCursor.Length() - (res +2)));
buf16.Zero();
buf16=cutf->ConvertToUnicodeFromUtf8L(result)->Des();
aLines.AppendL(buf16);
}
if(iCursor.Size()>0) {
buf16.Zero();
buf16=cutf->ConvertToUnicodeFromUtf8L(iCursor)->Des();
aLines.AppendL(buf16);
}
delete cutf;
delete bufc;
file.Close();
}
fs.Close();
}
void CFile::GetLinesFromFile(const TDesC& aFilename,CDesC8Array& aLines)
{
RFs fs;
User::LeaveIfError(fs.Connect() );
if( BaflUtils::FileExists(fs,aFilename) ){
RFile file;
TUint shareMode=EFileRead | EFileWrite ;
file.Open( fs, aFilename,shareMode ) ;
TInt fileSize=0;
file.Size(fileSize);
CnvUtfConverter *cutf=new (ELeave) CnvUtfConverter;
HBufC8 *bufc = HBufC8::NewL(fileSize+1);
file.Read( (TDes8&) (bufc->Des()) );
TInt res;
TPtrC8 iCursor(bufc->Des());
while( ( res = iCursor.FindF(_L8("\r\n")) ) >= 0 ){ //有可能位置是从0开始的 如果有问题.再改成 >0
TPtrC8 result = iCursor.Left(res);
iCursor.Set(iCursor.Right(iCursor.Length() - (res +2)));
aLines.AppendL(result);
}
if(iCursor.Size()>0) {
aLines.AppendL(iCursor);
}
delete cutf;
delete bufc;
file.Close();
}
fs.Close();
}
#ifndef _CINIFILE_H_
#define _CINIFILE_H_
#include <e32base.h> //CArrayPtrFlat
#include "CFile.h"
typedef struct tagINIElement
{
TBuf8<40> _Section;
TBuf8<40> _Key;
TBuf8<512> _Value;
}IniElement;
class CIniFile :public CFile
{
public:
static CIniFile* NewL(const TDesC &aFileName);
static CIniFile* NewLC(const TDesC &aFileName);
CIniFile(const TDesC &aFileName);
~CIniFile();
void SetFileName(const TDesC &aFileName);
TInt OpenIni();
TInt ReadValue(const TDesC8 &aSection, const TDesC8 &aKey, TDes8 &aValue );
TInt WriteValue(const TDesC8 &aSection, const TDesC8 &aKey, const TDesC8 &aValue );
void WriteIni();
void CloseIni();
void DestroyL();
private:
CArrayPtrFlat<IniElement> *pIniElements;
};
#endif
#include "CIniFile.h"
CIniFile* CIniFile::NewL(const TDesC &aFileName)
{
CIniFile* self = CIniFile::NewLC(aFileName);
CleanupStack::Pop(self);
return self;
}
CIniFile* CIniFile::NewLC(const TDesC &aFileName)
{
CIniFile *self = new (ELeave)CIniFile(aFileName);
CleanupStack::PushL(self);
return self;
}
CIniFile::CIniFile(const TDesC &aFileName)
:CFile(aFileName)
{
pIniElements = NULL;
}
CIniFile::~CIniFile()
{
DestroyL();
}
TInt CIniFile::OpenIni()
{
ConstructL(EFileRead | EFileWrite);
HBufC8 *bufc = HBufC8::NewL(iFileSize + 1);
Read( (TDes8&)(bufc->Des()) ); //读出所有文件内容
TPtrC8 iCursor(bufc->Des()); //指向文件内容的指针
TInt iPos = 0;
pIniElements = new(ELeave) CArrayPtrFlat<IniElement>(20);
TBuf8<40> szCurSection(_L8(""));
TBuf8<40> szSection(_L8(""));
while( ( iPos = iCursor.FindF(_L8("\r\n")) ) >= 0 ) {
iPos = iCursor.FindF(_L8("\r\n"));
TPtrC8 result = iCursor.Left(iPos); //取出一行内容
HBufC8 *pTmp = HBufC8::NewL(result.Length());
pTmp->Des().Copy(result);
pTmp->Des().Trim();
if((pTmp->Des().Length() > 0) && pTmp->Des()[0] == '[') //查找section
{
pTmp->Des().Delete(0, 1);
if( pTmp->Des()[pTmp->Des().Length() -1 ] == ']')
pTmp->Des().Delete(pTmp->Des().Length()-1, 1);
szSection.Copy(pTmp->Des());
if (szCurSection != szSection)
szCurSection = szSection;
} else if( KErrNotFound != pTmp->Des().Find(_L8("=")) ) //key=value
{
if (szCurSection.Compare(_L8("")) != 0)
{
IniElement *element = new(ELeave) IniElement;
element->_Section.Copy(szCurSection) ;
TPtrC8 ptrKey(pTmp->Des());
TInt iKeyPos=0;
iKeyPos = ptrKey.FindF(_L8("="));
TPtrC8 ptrTmpKey = ptrKey.Left(iKeyPos);
element->_Key.Copy(ptrTmpKey);
element->_Key.Trim();
ptrKey.Set( ptrKey.Right(ptrKey.Length() - iKeyPos -1) );
element->_Value.Copy(ptrKey);
element->_Value.Trim();
pIniElements->AppendL(element);
}
} else //注释,,空行等无用数据
{
if (szCurSection.Compare(_L8("")) ==0 ) //没有section的注释空行..在文件开头
{
IniElement *element = new(ELeave) IniElement;
element->_Section.Copy(_L8("###"));
element->_Key.Copy(_L8("##"));
element->_Value.Copy(pTmp->Des());
pIniElements->AppendL(element);
} else //section中的注释空行
{
IniElement *element = new(ELeave) IniElement;
element->_Section.Copy(szCurSection);
element->_Key.Copy(_L8("##"));
element->_Value.Copy(pTmp->Des());
pIniElements->AppendL(element);
}
}
delete pTmp;
iCursor.Set( iCursor.Right(iCursor.Length() - (iPos +2)) );//
}
delete bufc;
CloseIni(); //关闭文件句柄
return 1;
}
TInt CIniFile::ReadValue(const TDesC8 &aSection, const TDesC8 &aKey, TDes8 &aValue )
{
for(int i=0; i<pIniElements->Count(); i++)
{
if( (pIniElements->At(i)->_Section.Compare( (TDesC8&)aSection ) ==0 ) &&
(pIniElements->At(i)->_Key.Compare( (TDesC8&)aKey ) ==0 ) )
{
aValue.Copy(pIniElements->At(i)->_Value);
aValue.ZeroTerminate();
return 1;
}
}
return 0;
}
TInt CIniFile::WriteValue(const TDesC8 &aSection, const TDesC8 &aKey, const TDesC8 &aValue )
{
struct IniElement *element = new (ELeave) IniElement;
CleanupDeletePushL(element);
element->_Section.Copy(aSection);
element->_Key.Copy(aKey);
element->_Value.Copy(aValue);
struct IniElement *ine = new (ELeave) IniElement;
CleanupDeletePushL(ine);
TInt b_KeyFind = 0;
for(int i = 0; i<pIniElements->Count(); i++)
{
ine = pIniElements->At(i);
if( (ine->_Section.Compare( element->_Section ) ==0 ) &&
(ine->_Key.Compare(element->_Key ) ==0 ))
{
ine->_Value.Copy(element->_Value);
b_KeyFind = 1;
break;
}
}
if(0 == b_KeyFind)
{
TInt b_secFind = 0;
for(int i=0; i<pIniElements->Count(); i++)
{
if( (pIniElements->At(i)->_Section.Compare( (TDesC8&)aSection ) ==0) &&
(pIniElements->At(i)->_Key.Compare( _L8("##")) != 0) )
{
pIniElements->InsertL(i+1, element);
b_secFind = 1;
break;
}
}
if(b_secFind != 1) //如果是新增的section
pIniElements->AppendL(element);
}
WriteIni();
CloseIni(); //关闭文件句柄
CleanupStack::PopAndDestroy(2);
return 1;
}
void CIniFile::WriteIni()
{
ConstructL(EFileWrite) ;
TInt pos = 0;
Seek(ESeekStart,pos);
TBuf8<40> szCurSection(_L8(""));
if(pIniElements->Count()>0)
{
szCurSection.Copy(pIniElements->At(0)->_Section);
if(szCurSection.Compare( _L8("###")) != 0)
{
Write( _L8("[") );
Write( pIniElements->At(0)->_Section );
Write( _L8("]") );
Write( _L8("\r\n"));
}
IniElement *inie = new(ELeave) IniElement;
CleanupDeletePushL(inie);
for(int i=0; i<pIniElements->Count(); i++)
{
inie = pIniElements->At(i);
if(inie->_Section.Compare(szCurSection) == 0)
{
if( (inie->_Key.Compare(_L8("##")) == 0 ) &&
(inie->_Value.Compare(_L8("")) == 0 ))
{
Write( _L8("\r\n"));
} else if( (inie->_Key.Compare(_L8("##")) == 0 ) &&
(inie->_Value.Compare(_L8("")) != 0 ))
{
Write(inie->_Value);
Write( _L8("\r\n"));
} else if( inie->_Key.Compare(_L8("") )!= 0)
{
Write(inie->_Key);
Write( _L8(" = "));
Write(inie->_Value);
Write( _L8("\r\n"));
}
} else
{
szCurSection.Copy(inie->_Section);
if(szCurSection.Compare( _L8("###")) != 0)
{
Write( _L8("[") );
Write( inie->_Section );
Write( _L8("]") );
Write( _L8("\r\n"));
}
if( (inie->_Key.Compare(_L8("##")) == 0 ) &&
(inie->_Value.Compare(_L8("")) == 0 ))
{
Write( _L8("\r\n"));
} else if( (inie->_Key.Compare(_L8("##")) == 0 ) &&
(inie->_Value.Compare(_L8("")) != 0 ))
{
Write(inie->_Value);
Write( _L8("\r\n"));
} else if( inie->_Key.Compare(_L8("")) != 0)
{
Write(inie->_Key);
Write( _L8(" = "));
Write(inie->_Value);
Write( _L8("\r\n"));
}
}
}
CleanupStack::PopAndDestroy();
}
}
void CIniFile::CloseIni()
{
CFile::DestroyL();
}
void CIniFile::DestroyL()
{
for(int i = 0; i < pIniElements->Count(); i++)
delete pIniElements->At(i);
delete pIniElements;
}
void CIniFile::SetFileName(const TDesC &aFileName)
{
DestroyL();
CloseIni();
iFileName.Copy(aFileName);
iFileSize = 0;
}
调用方式:
TBuf<128> path;
path.ZeroTerminate();
path.Format(_L("c:\\cbm.ini"));
CIniFile *ini = CIniFile::NewL(path);
ini->OpenIni();
TBuf8<256> sChannels;
sChannels.ZeroTerminate();
ini->ReadValue(_L8("cbm"), _L8("channels"), sChannels);
ini->ReadValue(_L8("cbm"), _L8("te"), sChannels);
TBuf8<4> flag;
flag.ZeroTerminate();
flag.Format(_L8("23"));
ini->WriteValue(_L8("channels"), _L8("flag"), flag);
path.Format(_L("c:\\TDRConfig.ini"));
ini->SetFileName(path);
ini->OpenIni();
ini->ReadValue(_L8("123"), _L8("Indo"), sChannels);
ini->WriteValue(_L8("channels"), _L8("flag"), flag);
ini->CloseIni();
delete ini;