Update contrib.
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
15 // Defines the class CIniFile for accessing ini file.
16 // is used by the test harness in t_Usbman_dummyCC.mmp
25 #include "LegacyParser.h"
27 const TUint KTokenSize = 32;
28 _LIT(KDefaultIniFileDir,"\\");
30 void CIniFile::Panic(TIniPanic aPanic)
32 _LIT(KIniData,"CIniFile");
33 User::Panic(KIniData,aPanic);
43 delete (TText*)iPtr.Ptr();
48 CIniFile* CIniFile::NewL(const TDesC& aName)
50 * Factory function for CIniFile.
52 * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
55 CIniFile* self = new(ELeave) CIniFile;
56 CleanupStack::PushL(self);
57 self->ConstructL(aName, KDefaultIniFileDir);
58 CleanupStack::Pop(self);
62 CIniFile* CIniFile::NewL(const TDesC& aName, const TDesC& aPath)
64 * Factory function for CIniFile that allows the user to specify both filename
67 * @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
68 * @param aPath The location of the file e.g. "\\system\\data\\".
71 CIniFile* self = new(ELeave) CIniFile;
72 CleanupStack::PushL(self);
73 self->ConstructL(aName, aPath);
74 CleanupStack::Pop(self);
78 void CIniFile::ConstructL(const TDesC& aName, const TDesC& aPath)
80 * Allocate a buffer and Read file's contents into iPtr
82 * @param aName is the name of the ini file to be used, e.g. "REFTSY.INI"
85 iToken = HBufC::NewL(KTokenSize+2); // 2 extra chars for []
88 User::LeaveIfError(fs.Connect());
89 CleanupClosePushL(fs);
93 #ifdef BUILD_FOR_JETSTREAM
95 // For security reasons ini files are in the private directory for the
96 // comms-infras/rootserver (C32exe) process
97 TBuf<KMaxPath> privatePath;
98 User::LeaveIfError(fs.PrivatePath(privatePath));
99 User::LeaveIfError(ff.FindByDir(aName, privatePath));
102 User::LeaveIfError(ff.FindByDir(aName, aPath));
106 iName=ff.File().AllocL();
110 User::LeaveIfError(file.Open(fs,*iName,EFileStreamText|EFileRead|EFileShareReadersOnly));
111 CleanupClosePushL(file);
113 User::LeaveIfError(file.Size(size));
116 TText* data = REINTERPRET_CAST(TText*, User::AllocL(size));
117 iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
118 TPtr8 dest(REINTERPRET_CAST(TUint8*,data), 0, size);
119 User::LeaveIfError(file.Read(dest));
121 TUint8* ptr = REINTERPRET_CAST(TUint8*,data);
124 // This is orderred as FEFF assuming the processor is Little Endian
125 // The data in the file is FFFE. PRR 28/9/98
127 if(size>= STATIC_CAST(TInt,sizeof(TText)) && iPtr[0]==0xFEFF)
129 Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
130 iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
134 TText* newdata = REINTERPRET_CAST(TText*,
135 User::AllocL(size*sizeof(TText)));
136 iPtr.Set(newdata, size, size);
138 for(i=0 ; i<size ; ++i)
143 CleanupStack::PopAndDestroy(); // file
144 CleanupStack::PopAndDestroy(); // fs
147 TBool CIniFile::FindVar(const TDesC &aSection,
148 const TDesC &aVarName,
151 // Find a variable's value given a section name and a var name
154 __ASSERT_DEBUG(aSection.Length()<=(TInt)KTokenSize,Panic(ESectionNameTooBig));
155 __ASSERT_DEBUG(aVarName.Length()<=(TInt)KTokenSize,Panic(EVarNameTooBig));
157 TPtr sectionToken=iToken->Des();
158 _LIT(KSectionTokenString,"[%S]");
159 sectionToken.Format(KSectionTokenString,&aSection);
160 TInt sectionStart=iPtr.Find(sectionToken);
162 if (sectionStart==KErrNotFound)
166 TPtrC section=iPtr.Mid(sectionStart);
167 sectionStart+=section.Find(TPtrC(_S("]")));
168 if (sectionStart==KErrNotFound)
173 section.Set(iPtr.Mid(sectionStart));
175 TInt sectionEnd=section.Find(TPtrC(_S("[")));
176 if (sectionEnd==KErrNotFound)
177 sectionEnd=iPtr.Length()-sectionStart;
180 section.Set(iPtr.Mid(sectionStart,sectionEnd));
181 TPtr varToken=iToken->Des();
182 _LIT(KVarTokenString,"%S=");
183 varToken.Format(KVarTokenString,&aVarName);
184 TInt pos=section.Find(varToken);
185 if (pos==KErrNotFound)
189 // 'lex' points at the start of the data
190 TPtrC lex(section.Mid(pos));
191 TInt startpos = lex.Locate(TChar('='));
192 startpos++; // startpos points immediately after the =.
193 while ( TChar(lex[startpos]).IsSpace() )
194 startpos++; // skip to start of data
195 TInt endpos = lex.Locate(TChar('\n')); // assumes \n is after =.
196 if ( endpos == KErrNotFound ) // may not be \n on last line
197 endpos = section.Length()-1;
198 aResult.Set(lex.Mid(startpos).Ptr(),endpos-startpos-1);
206 TBool CIniFile::FindVar(const TDesC &aSection,const TDesC &aVarName,
211 if (FindVar(aSection,aVarName,ptr))
214 if (lex.Val(aResult)==KErrNone)