|
1 // Copyright (c) 2003-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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 */ |
|
19 |
|
20 |
|
21 #include "CMtfTestActionUtilsConfigFileParser.h" |
|
22 |
|
23 #include <f32file.h> |
|
24 |
|
25 const TInt KFileBufferSize = 1024; |
|
26 _LIT8(KComment,"*#*"); |
|
27 |
|
28 |
|
29 CMtfTestActionUtilsConfigFileParser* CMtfTestActionUtilsConfigFileParser::NewL(const TDesC& aFileName) |
|
30 { |
|
31 CMtfTestActionUtilsConfigFileParser* self = new (ELeave) CMtfTestActionUtilsConfigFileParser(); |
|
32 CleanupStack::PushL(self); |
|
33 self->ConstructL(aFileName); |
|
34 CleanupStack::Pop(); |
|
35 return self; |
|
36 } |
|
37 |
|
38 |
|
39 CMtfTestActionUtilsConfigFileParser::CMtfTestActionUtilsConfigFileParser() |
|
40 { |
|
41 } |
|
42 |
|
43 |
|
44 CMtfTestActionUtilsConfigFileParser::~CMtfTestActionUtilsConfigFileParser() |
|
45 { |
|
46 iName.ResetAndDestroy(); |
|
47 iContent.ResetAndDestroy(); |
|
48 iContent8.ResetAndDestroy(); |
|
49 } |
|
50 |
|
51 |
|
52 void CMtfTestActionUtilsConfigFileParser::ConstructL(const TDesC& aFileName) |
|
53 { |
|
54 RFs fs; |
|
55 fs.Connect(); |
|
56 |
|
57 RFile file; |
|
58 User::LeaveIfError(file.Open(fs,aFileName,EFileRead)); |
|
59 |
|
60 TInt eof = EFalse; |
|
61 TInt fileOffset = 0; |
|
62 TBuf8<KFileBufferSize> fileBuffer; |
|
63 |
|
64 while (!eof) |
|
65 { |
|
66 fileBuffer.SetLength(0); |
|
67 User::LeaveIfError(file.Read(fileOffset,fileBuffer,KFileBufferSize)); |
|
68 TInt read = fileBuffer.Length(); |
|
69 |
|
70 if (read < KFileBufferSize) |
|
71 { |
|
72 fileBuffer.Append('\n'); |
|
73 eof = ETrue; |
|
74 } |
|
75 |
|
76 TInt lineOverflow = fileBuffer.Locate('\n'); |
|
77 if ((lineOverflow == KErrNotFound) && (read == KFileBufferSize)) |
|
78 User::Leave(KErrOverflow); |
|
79 |
|
80 TInt eol = EFalse; |
|
81 while (!eol) |
|
82 { |
|
83 TInt lineFeedLocation = fileBuffer.Locate('\n'); |
|
84 if (lineFeedLocation == KErrNotFound) |
|
85 eol = ETrue; |
|
86 else |
|
87 { |
|
88 fileOffset += lineFeedLocation + 1; |
|
89 TInt lineLength; |
|
90 if ((lineFeedLocation != 0) && (fileBuffer[lineFeedLocation-1] == '\r')) |
|
91 lineLength = lineFeedLocation-1; |
|
92 else |
|
93 lineLength = lineFeedLocation; |
|
94 TPtrC8 line = fileBuffer.Left(lineLength); |
|
95 TInt commentLocation = line.Match(KComment); |
|
96 if (commentLocation != KErrNotFound) |
|
97 { |
|
98 TPtrC8 skipComment = line.Left(commentLocation); |
|
99 line.Set(skipComment); |
|
100 } |
|
101 TInt seperatorLocation = line.Locate('='); |
|
102 if (seperatorLocation != KErrNotFound) |
|
103 if ((seperatorLocation == 0) || (seperatorLocation == line.Length()-1)) |
|
104 seperatorLocation = KErrNotFound; |
|
105 if (seperatorLocation != KErrNotFound) |
|
106 { |
|
107 TPtrC8 namePtr = line.Left(seperatorLocation); |
|
108 HBufC8* nameBuf8 = HBufC8::NewL(namePtr.Length()); |
|
109 CleanupStack::PushL(nameBuf8); |
|
110 TPtr8 name8 = nameBuf8->Des(); |
|
111 name8.Copy(namePtr); |
|
112 name8.Trim(); |
|
113 HBufC* nameBuf16 = HBufC::NewL(namePtr.Length()); |
|
114 TPtr name16 = nameBuf16->Des(); |
|
115 name16.Copy(name8); |
|
116 iName.Append(nameBuf16); |
|
117 CleanupStack::PopAndDestroy(nameBuf8); |
|
118 |
|
119 TPtrC8 contentPtr = line.Mid(seperatorLocation+1); |
|
120 HBufC8* contentBuf8 = HBufC8::NewL(contentPtr.Length()); |
|
121 CleanupStack::PushL(contentBuf8); |
|
122 TPtr8 content8 = contentBuf8->Des(); |
|
123 content8.Copy(contentPtr); |
|
124 content8.Trim(); |
|
125 HBufC* contentBuf16 = HBufC::NewL(contentPtr.Length()); |
|
126 TPtr content16 = contentBuf16->Des(); |
|
127 content16.Copy(content8); |
|
128 iContent.Append(contentBuf16); |
|
129 iContent8.Append(contentBuf8); |
|
130 CleanupStack::Pop(contentBuf8); |
|
131 } |
|
132 TPtrC8 theRest = fileBuffer.Mid(lineFeedLocation+1); |
|
133 fileBuffer.Copy(theRest); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 fs.Close(); |
|
139 } |
|
140 |
|
141 |
|
142 TInt CMtfTestActionUtilsConfigFileParser::GetFieldAsInteger(const TDesC& aFieldName,TInt& aValue) |
|
143 { |
|
144 TInt count = iName.Count(); |
|
145 |
|
146 for (TInt i=0; i<count; i++) |
|
147 { |
|
148 if (iName[i]->Compare(aFieldName) == 0) |
|
149 { |
|
150 TPtrC content = iContent[i]->Des(); |
|
151 TLex lex(content); |
|
152 lex.Val(aValue); |
|
153 return (KErrNone); |
|
154 } |
|
155 } |
|
156 |
|
157 return (KErrNotFound); |
|
158 } |
|
159 |
|
160 |
|
161 TInt CMtfTestActionUtilsConfigFileParser::GetFieldAsString(const TDesC& aFieldName,TPtrC& aString) |
|
162 { |
|
163 TInt count = iName.Count(); |
|
164 |
|
165 for (TInt i=0; i<count; i++) |
|
166 { |
|
167 if (iName[i]->Compare(aFieldName) == 0) |
|
168 { |
|
169 aString.Set(*iContent[i]); |
|
170 return (KErrNone); |
|
171 } |
|
172 } |
|
173 |
|
174 return (KErrNotFound); |
|
175 } |
|
176 |
|
177 |
|
178 TInt CMtfTestActionUtilsConfigFileParser::GetFieldAsString8(const TDesC& aFieldName,TPtrC8& aString) |
|
179 { |
|
180 TInt count = iName.Count(); |
|
181 |
|
182 for (TInt i=0; i<count; i++) |
|
183 { |
|
184 if (iName[i]->Compare(aFieldName) == 0) |
|
185 { |
|
186 aString.Set(*iContent8[i]); |
|
187 return (KErrNone); |
|
188 } |
|
189 } |
|
190 |
|
191 return (KErrNotFound); |
|
192 } |
|
193 |
|
194 |