sl@0: // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // INIFILE.CPP sl@0: // Defines the class CIniFile for accessing ini file. sl@0: // is used by the test harness in t_Usbman_dummyCC.mmp sl@0: // sl@0: // sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include sl@0: #include "LegacyParser.h" sl@0: sl@0: const TUint KTokenSize = 32; sl@0: _LIT(KDefaultIniFileDir,"\\"); sl@0: sl@0: void CIniFile::Panic(TIniPanic aPanic) sl@0: { sl@0: _LIT(KIniData,"CIniFile"); sl@0: User::Panic(KIniData,aPanic); sl@0: } sl@0: sl@0: CIniFile::CIniFile() sl@0: : iPtr(NULL,0) sl@0: { sl@0: } sl@0: sl@0: CIniFile::~CIniFile() sl@0: { sl@0: delete (TText*)iPtr.Ptr(); sl@0: delete iToken; sl@0: delete iName; sl@0: } sl@0: sl@0: CIniFile* CIniFile::NewL(const TDesC& aName) sl@0: /** sl@0: * Factory function for CIniFile. sl@0: * sl@0: * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI". sl@0: */ sl@0: { sl@0: CIniFile* self = new(ELeave) CIniFile; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aName, KDefaultIniFileDir); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CIniFile* CIniFile::NewL(const TDesC& aName, const TDesC& aPath) sl@0: /** sl@0: * Factory function for CIniFile that allows the user to specify both filename sl@0: * and path sl@0: * sl@0: * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI". sl@0: * @param aPath The location of the file e.g. "\\system\\data\\". sl@0: */ sl@0: { sl@0: CIniFile* self = new(ELeave) CIniFile; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aName, aPath); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: void CIniFile::ConstructL(const TDesC& aName, const TDesC& aPath) sl@0: /** sl@0: * Allocate a buffer and Read file's contents into iPtr sl@0: * sl@0: * @param aName is the name of the ini file to be used, e.g. "REFTSY.INI" sl@0: */ sl@0: { sl@0: iToken = HBufC::NewL(KTokenSize+2); // 2 extra chars for [] sl@0: sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: sl@0: TFindFile ff(fs); sl@0: sl@0: #ifdef BUILD_FOR_JETSTREAM sl@0: // Jetstream sl@0: // For security reasons ini files are in the private directory for the sl@0: // comms-infras/rootserver (C32exe) process sl@0: TBuf privatePath; sl@0: User::LeaveIfError(fs.PrivatePath(privatePath)); sl@0: User::LeaveIfError(ff.FindByDir(aName, privatePath)); sl@0: #else sl@0: sl@0: User::LeaveIfError(ff.FindByDir(aName, aPath)); sl@0: sl@0: #endif sl@0: sl@0: iName=ff.File().AllocL(); sl@0: sl@0: RFile file; sl@0: TInt size; sl@0: User::LeaveIfError(file.Open(fs,*iName,EFileStreamText|EFileRead|EFileShareReadersOnly)); sl@0: CleanupClosePushL(file); sl@0: sl@0: User::LeaveIfError(file.Size(size)); sl@0: sl@0: sl@0: TText* data = REINTERPRET_CAST(TText*, User::AllocL(size)); sl@0: iPtr.Set(data, size/sizeof(TText), size/sizeof(TText)); sl@0: TPtr8 dest(REINTERPRET_CAST(TUint8*,data), 0, size); sl@0: User::LeaveIfError(file.Read(dest)); sl@0: sl@0: TUint8* ptr = REINTERPRET_CAST(TUint8*,data); sl@0: sl@0: // sl@0: // This is orderred as FEFF assuming the processor is Little Endian sl@0: // The data in the file is FFFE. PRR 28/9/98 sl@0: // sl@0: if(size>= STATIC_CAST(TInt,sizeof(TText)) && iPtr[0]==0xFEFF) sl@0: { sl@0: Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText)); sl@0: iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1); sl@0: } sl@0: else if(size) sl@0: { sl@0: TText* newdata = REINTERPRET_CAST(TText*, sl@0: User::AllocL(size*sizeof(TText))); sl@0: iPtr.Set(newdata, size, size); sl@0: TInt i; sl@0: for(i=0 ; iDes(); sl@0: _LIT(KSectionTokenString,"[%S]"); sl@0: sectionToken.Format(KSectionTokenString,&aSection); sl@0: TInt sectionStart=iPtr.Find(sectionToken); sl@0: TInt ret = ETrue; sl@0: if (sectionStart==KErrNotFound) sl@0: ret = EFalse; sl@0: else sl@0: { sl@0: TPtrC section=iPtr.Mid(sectionStart); sl@0: sectionStart+=section.Find(TPtrC(_S("]"))); sl@0: if (sectionStart==KErrNotFound) sl@0: ret = EFalse; sl@0: else sl@0: { sl@0: sectionStart++; sl@0: section.Set(iPtr.Mid(sectionStart)); sl@0: sl@0: TInt sectionEnd=section.Find(TPtrC(_S("["))); sl@0: if (sectionEnd==KErrNotFound) sl@0: sectionEnd=iPtr.Length()-sectionStart; sl@0: else sl@0: sectionEnd--; sl@0: section.Set(iPtr.Mid(sectionStart,sectionEnd)); sl@0: TPtr varToken=iToken->Des(); sl@0: _LIT(KVarTokenString,"%S="); sl@0: varToken.Format(KVarTokenString,&aVarName); sl@0: TInt pos=section.Find(varToken); sl@0: if (pos==KErrNotFound) sl@0: ret = EFalse; sl@0: else sl@0: { sl@0: // 'lex' points at the start of the data sl@0: TPtrC lex(section.Mid(pos)); sl@0: TInt startpos = lex.Locate(TChar('=')); sl@0: startpos++; // startpos points immediately after the =. sl@0: while ( TChar(lex[startpos]).IsSpace() ) sl@0: startpos++; // skip to start of data sl@0: TInt endpos = lex.Locate(TChar('\n')); // assumes \n is after =. sl@0: if ( endpos == KErrNotFound ) // may not be \n on last line sl@0: endpos = section.Length()-1; sl@0: aResult.Set(lex.Mid(startpos).Ptr(),endpos-startpos-1); sl@0: } sl@0: } sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: TBool CIniFile::FindVar(const TDesC &aSection,const TDesC &aVarName, sl@0: TInt &aResult) sl@0: { sl@0: TInt ret = EFalse; sl@0: TPtrC ptr(NULL,0); sl@0: if (FindVar(aSection,aVarName,ptr)) sl@0: { sl@0: TLex lex(ptr); sl@0: if (lex.Val(aResult)==KErrNone) sl@0: ret = ETrue; sl@0: } sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: // sl@0: // End of file