|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Class for processing parameter-file. |
|
16 * @internalComponent |
|
17 * @released |
|
18 * |
|
19 */ |
|
20 |
|
21 |
|
22 |
|
23 #include "parameterfileprocessor.h" |
|
24 |
|
25 |
|
26 /** |
|
27 Constructor of CParameterFileProcessor class |
|
28 |
|
29 @param aParamFileName parameter-file name |
|
30 |
|
31 @internalComponent |
|
32 @released |
|
33 */ |
|
34 CParameterFileProcessor::CParameterFileProcessor(String aParamFileName): |
|
35 iParamFileName(aParamFileName),iNoOfArguments(0), |
|
36 iParamFileArgs(NULL) |
|
37 { |
|
38 } |
|
39 |
|
40 |
|
41 /** |
|
42 Function to open parameter-file |
|
43 |
|
44 @internalComponent |
|
45 @released |
|
46 |
|
47 @return True/False depending on the status of parameter-file open statement. |
|
48 */ |
|
49 bool CParameterFileProcessor::OpenFile() |
|
50 { |
|
51 |
|
52 iParamFile.open(iParamFileName.data(),std::ios::binary); |
|
53 if (iParamFile.is_open()) |
|
54 return true; |
|
55 else |
|
56 { |
|
57 std::cout<<"Error: Couldn't open parameter-file for reading:"<<iParamFileName.c_str()<<"\n"; |
|
58 return false; |
|
59 } |
|
60 } |
|
61 |
|
62 |
|
63 /** |
|
64 Function to parse the parameter-file |
|
65 |
|
66 @internalComponent |
|
67 @released |
|
68 |
|
69 @return True/False depending on the status of parameter-file parsing. |
|
70 */ |
|
71 bool CParameterFileProcessor::ParameterFileProcessor() |
|
72 { |
|
73 if(OpenFile()) |
|
74 { |
|
75 while(!iParamFile.eof()) |
|
76 { |
|
77 // Read the parameter-file line by line. |
|
78 String line; |
|
79 std::getline(iParamFile,line); |
|
80 |
|
81 // Read the line till the occurence of character ';' or EOL. |
|
82 unsigned int pos = line.find_first_of(";\r\n"); |
|
83 if (pos != std::string::npos) |
|
84 line = line.substr(0,pos); |
|
85 |
|
86 // Split the line if multiple parameters are provided |
|
87 // in a single line. |
|
88 if(!SplitLine(line)) |
|
89 return false; |
|
90 } |
|
91 |
|
92 if (SetNoOfArguments() == false) |
|
93 { |
|
94 return false; |
|
95 } |
|
96 |
|
97 SetParameters(); |
|
98 |
|
99 // Close parameter-file |
|
100 CloseFile(); |
|
101 |
|
102 return true; |
|
103 } |
|
104 else |
|
105 return false; |
|
106 } |
|
107 |
|
108 |
|
109 /** |
|
110 Function to split line of paramfile-file |
|
111 |
|
112 @param aLine parameter-file line |
|
113 |
|
114 @internalComponent |
|
115 @released |
|
116 |
|
117 @return True/False depending on the status of spliting. |
|
118 */ |
|
119 bool CParameterFileProcessor::SplitLine(String& aLine) |
|
120 { |
|
121 unsigned int startPos=0; |
|
122 unsigned int endPos=0; |
|
123 |
|
124 // Segregate parameters based on white-space or tabs. |
|
125 startPos= aLine.find_first_not_of(" \t",endPos); |
|
126 while(startPos != std::string::npos) |
|
127 { |
|
128 endPos= aLine.find_first_of(" \t",startPos); |
|
129 String paramStr= aLine.substr(startPos,endPos-startPos); |
|
130 |
|
131 unsigned int position= aLine.find_first_of("\"",startPos); |
|
132 |
|
133 // If the parameter contains double quotes('"') then also include the spaces(if provided) |
|
134 // within the quotes. |
|
135 if((position!=std::string::npos) && position<=endPos) |
|
136 { |
|
137 endPos= aLine.find_first_of("\"",position+1); |
|
138 if(endPos!= std::string::npos) |
|
139 { |
|
140 endPos= aLine.find_first_of(" \t",endPos+1); |
|
141 if(endPos != std::string::npos) |
|
142 { |
|
143 paramStr= aLine.substr(startPos,endPos-startPos); |
|
144 } |
|
145 |
|
146 // Remove '"' from parameter |
|
147 for(unsigned int count =0;count<paramStr.size();count++) |
|
148 { |
|
149 if (paramStr.at(count) == '"') |
|
150 { |
|
151 paramStr.erase(count,count+1); |
|
152 } |
|
153 } |
|
154 } |
|
155 // Generate error message if enclosing quotes are not found. |
|
156 else |
|
157 { |
|
158 std::cout<<"Error while parsing parameter-file"<<iParamFileName.c_str()<<". Closing \"\"\" not found\n"; |
|
159 return false; |
|
160 } |
|
161 } |
|
162 |
|
163 iParameters.push_back(paramStr); |
|
164 startPos= aLine.find_first_not_of(" \t",endPos); |
|
165 } |
|
166 return true; |
|
167 } |
|
168 |
|
169 |
|
170 /** |
|
171 Function to set number of parameters read. |
|
172 |
|
173 @return false if no parameters are specified in parameter-file, else true |
|
174 @internalComponent |
|
175 @released |
|
176 */ |
|
177 bool CParameterFileProcessor::SetNoOfArguments() |
|
178 { |
|
179 unsigned int noOfArguements = iParameters.size(); |
|
180 if (!noOfArguements) |
|
181 { |
|
182 std::cout<<"Warning: No parameters specified in paramer-file:"<<iParamFileName.data()<<"\n"; |
|
183 return false; |
|
184 } |
|
185 iNoOfArguments = noOfArguements+1; |
|
186 return true; |
|
187 } |
|
188 |
|
189 |
|
190 /** |
|
191 Function to set the value of parameters read in the |
|
192 form of 2D char array. |
|
193 |
|
194 @internalComponent |
|
195 @released |
|
196 */ |
|
197 void CParameterFileProcessor::SetParameters() |
|
198 { |
|
199 // Store the parameters read in a 2D array of characters |
|
200 unsigned int paramSize = iParameters.size(); |
|
201 iParamFileArgs=new char*[paramSize+1]; |
|
202 |
|
203 for (unsigned int count=1; count<=paramSize; count++) |
|
204 { |
|
205 String param = iParameters.at(count-1); |
|
206 *(iParamFileArgs+count) = new char[param.size()+1]; |
|
207 strcpy(*(iParamFileArgs+count),param.c_str()); |
|
208 } |
|
209 } |
|
210 |
|
211 |
|
212 /** |
|
213 Function to close parameter-file |
|
214 |
|
215 @internalComponent |
|
216 @released |
|
217 */ |
|
218 void CParameterFileProcessor::CloseFile() |
|
219 { |
|
220 iParamFile.close(); |
|
221 } |
|
222 |
|
223 |
|
224 /** |
|
225 Function to return number of parameters read from parameter-file |
|
226 |
|
227 @internalComponent |
|
228 @released |
|
229 |
|
230 @return iNoOfArguments - Number of parameters read from parameter-file |
|
231 */ |
|
232 unsigned int CParameterFileProcessor::GetNoOfArguments() const |
|
233 { |
|
234 return iNoOfArguments; |
|
235 } |
|
236 |
|
237 |
|
238 /** |
|
239 Function to return parameters read from parameter-file |
|
240 |
|
241 @internalComponent |
|
242 @released |
|
243 |
|
244 @return iParamFileArgs - Parameters read from parameter-file |
|
245 */ |
|
246 char** CParameterFileProcessor::GetParameters() const |
|
247 { |
|
248 return iParamFileArgs; |
|
249 } |
|
250 |
|
251 |
|
252 /** |
|
253 Destructor of CParameterFileProcessor class |
|
254 |
|
255 @internalComponent |
|
256 @released |
|
257 */ |
|
258 CParameterFileProcessor::~CParameterFileProcessor() |
|
259 { |
|
260 for (unsigned int count=1;count<iNoOfArguments;count++) |
|
261 delete[] *(iParamFileArgs+count); |
|
262 } |