sl@0
|
1 |
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// INIFILE.CPP
|
sl@0
|
15 |
// Defines the class CIniFile for accessing ini file.
|
sl@0
|
16 |
// is used by the test harness in t_Usbman_dummyCC.mmp
|
sl@0
|
17 |
//
|
sl@0
|
18 |
//
|
sl@0
|
19 |
|
sl@0
|
20 |
/**
|
sl@0
|
21 |
@file
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
#include <f32file.h>
|
sl@0
|
25 |
#include "LegacyParser.h"
|
sl@0
|
26 |
|
sl@0
|
27 |
const TUint KTokenSize = 32;
|
sl@0
|
28 |
_LIT(KDefaultIniFileDir,"\\");
|
sl@0
|
29 |
|
sl@0
|
30 |
void CIniFile::Panic(TIniPanic aPanic)
|
sl@0
|
31 |
{
|
sl@0
|
32 |
_LIT(KIniData,"CIniFile");
|
sl@0
|
33 |
User::Panic(KIniData,aPanic);
|
sl@0
|
34 |
}
|
sl@0
|
35 |
|
sl@0
|
36 |
CIniFile::CIniFile()
|
sl@0
|
37 |
: iPtr(NULL,0)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
CIniFile::~CIniFile()
|
sl@0
|
42 |
{
|
sl@0
|
43 |
delete (TText*)iPtr.Ptr();
|
sl@0
|
44 |
delete iToken;
|
sl@0
|
45 |
delete iName;
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
CIniFile* CIniFile::NewL(const TDesC& aName)
|
sl@0
|
49 |
/**
|
sl@0
|
50 |
* Factory function for CIniFile.
|
sl@0
|
51 |
*
|
sl@0
|
52 |
* @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
|
sl@0
|
53 |
*/
|
sl@0
|
54 |
{
|
sl@0
|
55 |
CIniFile* self = new(ELeave) CIniFile;
|
sl@0
|
56 |
CleanupStack::PushL(self);
|
sl@0
|
57 |
self->ConstructL(aName, KDefaultIniFileDir);
|
sl@0
|
58 |
CleanupStack::Pop(self);
|
sl@0
|
59 |
return self;
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
CIniFile* CIniFile::NewL(const TDesC& aName, const TDesC& aPath)
|
sl@0
|
63 |
/**
|
sl@0
|
64 |
* Factory function for CIniFile that allows the user to specify both filename
|
sl@0
|
65 |
* and path
|
sl@0
|
66 |
*
|
sl@0
|
67 |
* @param aName The name of the ini file to be used, e.g. "GPRSBTT.INI".
|
sl@0
|
68 |
* @param aPath The location of the file e.g. "\\system\\data\\".
|
sl@0
|
69 |
*/
|
sl@0
|
70 |
{
|
sl@0
|
71 |
CIniFile* self = new(ELeave) CIniFile;
|
sl@0
|
72 |
CleanupStack::PushL(self);
|
sl@0
|
73 |
self->ConstructL(aName, aPath);
|
sl@0
|
74 |
CleanupStack::Pop(self);
|
sl@0
|
75 |
return self;
|
sl@0
|
76 |
}
|
sl@0
|
77 |
|
sl@0
|
78 |
void CIniFile::ConstructL(const TDesC& aName, const TDesC& aPath)
|
sl@0
|
79 |
/**
|
sl@0
|
80 |
* Allocate a buffer and Read file's contents into iPtr
|
sl@0
|
81 |
*
|
sl@0
|
82 |
* @param aName is the name of the ini file to be used, e.g. "REFTSY.INI"
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
{
|
sl@0
|
85 |
iToken = HBufC::NewL(KTokenSize+2); // 2 extra chars for []
|
sl@0
|
86 |
|
sl@0
|
87 |
RFs fs;
|
sl@0
|
88 |
User::LeaveIfError(fs.Connect());
|
sl@0
|
89 |
CleanupClosePushL(fs);
|
sl@0
|
90 |
|
sl@0
|
91 |
TFindFile ff(fs);
|
sl@0
|
92 |
|
sl@0
|
93 |
#ifdef BUILD_FOR_JETSTREAM
|
sl@0
|
94 |
// Jetstream
|
sl@0
|
95 |
// For security reasons ini files are in the private directory for the
|
sl@0
|
96 |
// comms-infras/rootserver (C32exe) process
|
sl@0
|
97 |
TBuf<KMaxPath> privatePath;
|
sl@0
|
98 |
User::LeaveIfError(fs.PrivatePath(privatePath));
|
sl@0
|
99 |
User::LeaveIfError(ff.FindByDir(aName, privatePath));
|
sl@0
|
100 |
#else
|
sl@0
|
101 |
|
sl@0
|
102 |
User::LeaveIfError(ff.FindByDir(aName, aPath));
|
sl@0
|
103 |
|
sl@0
|
104 |
#endif
|
sl@0
|
105 |
|
sl@0
|
106 |
iName=ff.File().AllocL();
|
sl@0
|
107 |
|
sl@0
|
108 |
RFile file;
|
sl@0
|
109 |
TInt size;
|
sl@0
|
110 |
User::LeaveIfError(file.Open(fs,*iName,EFileStreamText|EFileRead|EFileShareReadersOnly));
|
sl@0
|
111 |
CleanupClosePushL(file);
|
sl@0
|
112 |
|
sl@0
|
113 |
User::LeaveIfError(file.Size(size));
|
sl@0
|
114 |
|
sl@0
|
115 |
|
sl@0
|
116 |
TText* data = REINTERPRET_CAST(TText*, User::AllocL(size));
|
sl@0
|
117 |
iPtr.Set(data, size/sizeof(TText), size/sizeof(TText));
|
sl@0
|
118 |
TPtr8 dest(REINTERPRET_CAST(TUint8*,data), 0, size);
|
sl@0
|
119 |
User::LeaveIfError(file.Read(dest));
|
sl@0
|
120 |
|
sl@0
|
121 |
TUint8* ptr = REINTERPRET_CAST(TUint8*,data);
|
sl@0
|
122 |
|
sl@0
|
123 |
//
|
sl@0
|
124 |
// This is orderred as FEFF assuming the processor is Little Endian
|
sl@0
|
125 |
// The data in the file is FFFE. PRR 28/9/98
|
sl@0
|
126 |
//
|
sl@0
|
127 |
if(size>= STATIC_CAST(TInt,sizeof(TText)) && iPtr[0]==0xFEFF)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText));
|
sl@0
|
130 |
iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1);
|
sl@0
|
131 |
}
|
sl@0
|
132 |
else if(size)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
TText* newdata = REINTERPRET_CAST(TText*,
|
sl@0
|
135 |
User::AllocL(size*sizeof(TText)));
|
sl@0
|
136 |
iPtr.Set(newdata, size, size);
|
sl@0
|
137 |
TInt i;
|
sl@0
|
138 |
for(i=0 ; i<size ; ++i)
|
sl@0
|
139 |
iPtr[i]=ptr[i];
|
sl@0
|
140 |
delete data;
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
CleanupStack::PopAndDestroy(); // file
|
sl@0
|
144 |
CleanupStack::PopAndDestroy(); // fs
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
TBool CIniFile::FindVar(const TDesC &aSection,
|
sl@0
|
148 |
const TDesC &aVarName,
|
sl@0
|
149 |
TPtrC &aResult)
|
sl@0
|
150 |
//
|
sl@0
|
151 |
// Find a variable's value given a section name and a var name
|
sl@0
|
152 |
//
|
sl@0
|
153 |
{
|
sl@0
|
154 |
__ASSERT_DEBUG(aSection.Length()<=(TInt)KTokenSize,Panic(ESectionNameTooBig));
|
sl@0
|
155 |
__ASSERT_DEBUG(aVarName.Length()<=(TInt)KTokenSize,Panic(EVarNameTooBig));
|
sl@0
|
156 |
|
sl@0
|
157 |
TPtr sectionToken=iToken->Des();
|
sl@0
|
158 |
_LIT(KSectionTokenString,"[%S]");
|
sl@0
|
159 |
sectionToken.Format(KSectionTokenString,&aSection);
|
sl@0
|
160 |
TInt sectionStart=iPtr.Find(sectionToken);
|
sl@0
|
161 |
TInt ret = ETrue;
|
sl@0
|
162 |
if (sectionStart==KErrNotFound)
|
sl@0
|
163 |
ret = EFalse;
|
sl@0
|
164 |
else
|
sl@0
|
165 |
{
|
sl@0
|
166 |
TPtrC section=iPtr.Mid(sectionStart);
|
sl@0
|
167 |
sectionStart+=section.Find(TPtrC(_S("]")));
|
sl@0
|
168 |
if (sectionStart==KErrNotFound)
|
sl@0
|
169 |
ret = EFalse;
|
sl@0
|
170 |
else
|
sl@0
|
171 |
{
|
sl@0
|
172 |
sectionStart++;
|
sl@0
|
173 |
section.Set(iPtr.Mid(sectionStart));
|
sl@0
|
174 |
|
sl@0
|
175 |
TInt sectionEnd=section.Find(TPtrC(_S("[")));
|
sl@0
|
176 |
if (sectionEnd==KErrNotFound)
|
sl@0
|
177 |
sectionEnd=iPtr.Length()-sectionStart;
|
sl@0
|
178 |
else
|
sl@0
|
179 |
sectionEnd--;
|
sl@0
|
180 |
section.Set(iPtr.Mid(sectionStart,sectionEnd));
|
sl@0
|
181 |
TPtr varToken=iToken->Des();
|
sl@0
|
182 |
_LIT(KVarTokenString,"%S=");
|
sl@0
|
183 |
varToken.Format(KVarTokenString,&aVarName);
|
sl@0
|
184 |
TInt pos=section.Find(varToken);
|
sl@0
|
185 |
if (pos==KErrNotFound)
|
sl@0
|
186 |
ret = EFalse;
|
sl@0
|
187 |
else
|
sl@0
|
188 |
{
|
sl@0
|
189 |
// 'lex' points at the start of the data
|
sl@0
|
190 |
TPtrC lex(section.Mid(pos));
|
sl@0
|
191 |
TInt startpos = lex.Locate(TChar('='));
|
sl@0
|
192 |
startpos++; // startpos points immediately after the =.
|
sl@0
|
193 |
while ( TChar(lex[startpos]).IsSpace() )
|
sl@0
|
194 |
startpos++; // skip to start of data
|
sl@0
|
195 |
TInt endpos = lex.Locate(TChar('\n')); // assumes \n is after =.
|
sl@0
|
196 |
if ( endpos == KErrNotFound ) // may not be \n on last line
|
sl@0
|
197 |
endpos = section.Length()-1;
|
sl@0
|
198 |
aResult.Set(lex.Mid(startpos).Ptr(),endpos-startpos-1);
|
sl@0
|
199 |
}
|
sl@0
|
200 |
}
|
sl@0
|
201 |
}
|
sl@0
|
202 |
|
sl@0
|
203 |
return ret;
|
sl@0
|
204 |
}
|
sl@0
|
205 |
|
sl@0
|
206 |
TBool CIniFile::FindVar(const TDesC &aSection,const TDesC &aVarName,
|
sl@0
|
207 |
TInt &aResult)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
TInt ret = EFalse;
|
sl@0
|
210 |
TPtrC ptr(NULL,0);
|
sl@0
|
211 |
if (FindVar(aSection,aVarName,ptr))
|
sl@0
|
212 |
{
|
sl@0
|
213 |
TLex lex(ptr);
|
sl@0
|
214 |
if (lex.Val(aResult)==KErrNone)
|
sl@0
|
215 |
ret = ETrue;
|
sl@0
|
216 |
}
|
sl@0
|
217 |
|
sl@0
|
218 |
return ret;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
|
sl@0
|
221 |
//
|
sl@0
|
222 |
// End of file
|