|
1 /* |
|
2 * Copyright (c) 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 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "options.h" |
|
20 #include "../common/exception.h" |
|
21 |
|
22 #include <fstream.h> |
|
23 #include <vector> |
|
24 #include <string> |
|
25 #include <algorithm> |
|
26 |
|
27 std::string COptions::iCurrentVersion = "Registry Database Generator Version 1, 0, 1"; |
|
28 |
|
29 std::string COptions::iDescription = "A utility for generating and updating the Software Component Registry database which stores " |
|
30 "information for all installed software components."; |
|
31 |
|
32 std::string COptions::iCopyright = "Copyright (c) 2008 Symbian Software Ltd. All rights reserved."; |
|
33 |
|
34 std::string COptions::iUsage = |
|
35 "Usage : scrtool [-h|-?] [-v] [-l] [-w] -c or -d [DB name] -f [Environment XML file] -p [Pre-provision XML file]" |
|
36 |
|
37 "\n-h|-? Show help usage." |
|
38 |
|
39 "\n-v Show tool version." |
|
40 |
|
41 "\n-c [mandatory if -d not specified]" |
|
42 "\n\tCreate or regenerate database. This option would always create a new database." |
|
43 "\n\tIf a database with the same name already exists it would be overwritten.\n" |
|
44 |
|
45 "-d [mandatory if -c not specified]" |
|
46 "\n\tName of an already existing SCR database." |
|
47 "\n\tThis option assumes existence of a database and updates it with" |
|
48 "\n\tadditional software environment or pre-provisioning information." |
|
49 |
|
50 "\n-f\tXML file which contains software environment information." |
|
51 |
|
52 "\n-p\tXML file which contains pre-provision information." |
|
53 |
|
54 "\n-l\tName of the log file, Default: scrtool.log\n" |
|
55 |
|
56 "\n-w\tLevel of logging information [ERROR,WARN,INFO,ALL], Default: ALL.\n"; |
|
57 |
|
58 std::string COptions::iGeneralInfo = |
|
59 "Tool can be used in the following ways: \n" |
|
60 "1. [-c and -f]: Create database with environment information.\n" |
|
61 "2. [-c and -f]: Replace an existing database with new environment information.\n" |
|
62 "3. [-d and -f]: Add new environment information to an existing database as specified in input XML files.\n" |
|
63 "4. [-d and -p]: Add pre-provision details to an already existing database.\n" |
|
64 "5. [-c,-f and -p]: Create database, populate environments and update the pre-provision information.\n" |
|
65 "Name of the database ( either -c or -d ) and at least one XML file (-f) with environment update" |
|
66 "\nshould be specified."; |
|
67 |
|
68 |
|
69 COptions::COptions(int aParamsCount, char** aParams) |
|
70 :iDbExist(false), |
|
71 iLoggingEnabled(false), |
|
72 iLogLevel(CLogger::ELogAll), |
|
73 iLogFileName("scrtool.log"), |
|
74 iPreProvisionUpdate(false) |
|
75 { |
|
76 ValidateOptions(aParamsCount, aParams); |
|
77 } |
|
78 |
|
79 COptions::~COptions() |
|
80 { |
|
81 } |
|
82 |
|
83 void COptions::ValidateOptions(int aParamsCount, char** aParams) |
|
84 { |
|
85 bool envFileNameOptionExists = false; |
|
86 bool createDbOptionExists= false; |
|
87 bool preProvFileExists = false; |
|
88 |
|
89 int count = 0; |
|
90 while(++count < aParamsCount) |
|
91 { |
|
92 // each option should have a corresponding data present |
|
93 if(*aParams[count] != '-' ) |
|
94 { |
|
95 DisplayUsage(); |
|
96 throw CException("Invalid argument passed.",ExceptionCodes::EInvalidArgument); |
|
97 } |
|
98 |
|
99 const char* ch = aParams[count]+1; |
|
100 |
|
101 switch(*ch) |
|
102 { |
|
103 case 'h': |
|
104 case '?': |
|
105 DisplayDetailedUsage(); |
|
106 exit(0); |
|
107 break; |
|
108 case 'v': |
|
109 DisplayVersion(); |
|
110 exit(0); |
|
111 break; |
|
112 case 'c': |
|
113 CheckParamExists(aParamsCount,++count); |
|
114 iScrDbName = aParams[count]; |
|
115 createDbOptionExists = true; |
|
116 break; |
|
117 case 'd': |
|
118 CheckParamExists(aParamsCount,++count); |
|
119 iScrDbName = aParams[count]; |
|
120 iDbExist = true; |
|
121 break; |
|
122 case 'f': |
|
123 { |
|
124 CheckParamExists(aParamsCount,count+1); |
|
125 // get the XML file names |
|
126 char * xmlFileName; |
|
127 do |
|
128 { |
|
129 xmlFileName = aParams[++count]; |
|
130 if(IsFileAbsent(xmlFileName)) |
|
131 { |
|
132 DisplayUsage(); |
|
133 std::string errMsg(xmlFileName); |
|
134 errMsg += " not present"; |
|
135 throw CException(errMsg,ExceptionCodes::EFileNotPresent); |
|
136 } |
|
137 iXmlFileNames.push_back(xmlFileName); |
|
138 } while( count+1 < aParamsCount && *aParams[count+1] != '-' ); |
|
139 envFileNameOptionExists = true; |
|
140 break; |
|
141 } |
|
142 |
|
143 case 'p': |
|
144 { |
|
145 CheckParamExists(aParamsCount,++count); |
|
146 // get the XML file names |
|
147 char * xmlFileName = aParams[count]; |
|
148 |
|
149 if(IsFileAbsent(xmlFileName)) |
|
150 { |
|
151 DisplayUsage(); |
|
152 std::string errMsg(xmlFileName); |
|
153 errMsg += " not present"; |
|
154 throw CException(errMsg,ExceptionCodes::EFileNotPresent); |
|
155 } |
|
156 iPreProvisionFileName = xmlFileName; |
|
157 iPreProvisionUpdate = true; |
|
158 break; |
|
159 } |
|
160 case 'l': |
|
161 CheckParamExists(aParamsCount,++count); |
|
162 iLogFileName = aParams[count]; |
|
163 break; |
|
164 case 'w': |
|
165 CheckParamExists(aParamsCount,++count); |
|
166 SetLogLevel(aParams[count]); |
|
167 break; |
|
168 default: |
|
169 { |
|
170 DisplayUsage(); |
|
171 throw CException("Invalid argument passed.",ExceptionCodes::EInvalidArgument); |
|
172 } |
|
173 } // end of switch |
|
174 } // end of while |
|
175 |
|
176 // if any of these 3 options are not satisfied then |
|
177 // mandatory option has not been specified. |
|
178 if( // existing db name and pre-provision file |
|
179 !((iDbExist && iPreProvisionUpdate) |
|
180 // existing db and env file |
|
181 || (envFileNameOptionExists && iDbExist) |
|
182 // create new db and env file |
|
183 || (createDbOptionExists && envFileNameOptionExists))) |
|
184 { |
|
185 DisplayUsage(); |
|
186 throw CException("Mandatory option not specified",ExceptionCodes::EMandatoryOption); |
|
187 } |
|
188 if(createDbOptionExists && iDbExist) |
|
189 { |
|
190 DisplayUsage(); |
|
191 throw CException("-c and -d cannot be specified at the same time.",ExceptionCodes::EMutuallyExclusive); |
|
192 } |
|
193 if(createDbOptionExists) |
|
194 { |
|
195 remove(iScrDbName.c_str()); |
|
196 } |
|
197 } |
|
198 |
|
199 |
|
200 void COptions::CheckParamExists(int aParamsCount, int aCurrentCount) |
|
201 { |
|
202 if(aCurrentCount >= aParamsCount) |
|
203 { |
|
204 DisplayUsage(); |
|
205 throw CException("Invalid argument passed.",ExceptionCodes::EInvalidArgument); |
|
206 } |
|
207 } |
|
208 |
|
209 bool COptions::IsFileAbsent(const char* aFileName) |
|
210 { |
|
211 std::ifstream fileStream(aFileName,ios::in); |
|
212 if(!fileStream) |
|
213 return true; |
|
214 fileStream.close(); |
|
215 return false; |
|
216 } |
|
217 |
|
218 void COptions::DisplayDetailedUsage() |
|
219 { |
|
220 std::cout << iDescription << std::endl; |
|
221 std::cout << iCopyright << std::endl; |
|
222 DisplayUsage(); |
|
223 std::cout << iGeneralInfo << std::endl; |
|
224 } |
|
225 |
|
226 void COptions::DisplayUsage() |
|
227 { |
|
228 DisplayVersion(); |
|
229 std::cout << iUsage << std::endl; |
|
230 } |
|
231 |
|
232 void COptions::DisplayVersion() |
|
233 { |
|
234 std::cout << iCurrentVersion << std::endl; |
|
235 } |
|
236 |
|
237 void COptions::SetLogLevel(const char* aLogLevel) |
|
238 { |
|
239 std::string logLevel(aLogLevel); |
|
240 std::transform(logLevel.begin(), logLevel.end(), logLevel.begin(), ::toupper); |
|
241 |
|
242 if(logLevel == "ERROR") |
|
243 { |
|
244 iLogLevel = CLogger::ELogError; |
|
245 } |
|
246 else if(logLevel == "WARN") |
|
247 { |
|
248 iLogLevel = CLogger::ELogWarning; |
|
249 } |
|
250 else if(logLevel == "INFO") |
|
251 { |
|
252 iLogLevel = static_cast<CLogger::TLogLevel>(CLogger::ELogEnter | CLogger::ELogExit | CLogger::ELog); |
|
253 } |
|
254 else if(logLevel == "ALL") |
|
255 { |
|
256 iLogLevel = CLogger::ELogAll; |
|
257 } |
|
258 else |
|
259 { |
|
260 throw CException("Invalid log level specified",ExceptionCodes::EInvalidArgument); |
|
261 } |
|
262 } |
|
263 |
|
264 std::string COptions::GetLogFileName() |
|
265 { |
|
266 return iLogFileName; |
|
267 } |
|
268 |
|
269 CLogger::TLogLevel COptions::GetLogLevel() |
|
270 { |
|
271 return iLogLevel; |
|
272 } |
|
273 |
|
274 std::string COptions::GetDbFileName() |
|
275 { |
|
276 return iScrDbName; |
|
277 } |
|
278 |
|
279 std::vector<std::string> COptions::GetEnvFileNames() |
|
280 { |
|
281 return iXmlFileNames; |
|
282 } |
|
283 |
|
284 std::string COptions::GetPrePovisionFileName() |
|
285 { |
|
286 return iPreProvisionFileName; |
|
287 } |
|
288 |
|
289 bool COptions::IsDbAbsent() |
|
290 { |
|
291 return !iDbExist; |
|
292 } |
|
293 |
|
294 bool COptions::IsPreProvisionInfoAvailable() |
|
295 { |
|
296 return iPreProvisionUpdate; |
|
297 } |