|
1 // Copyright (c) 2010 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 #include <e32test.h> |
|
16 #include "t_sqlcmdlineutil.h" |
|
17 |
|
18 static void GetCmdLine(RTest& aTest, const TDesC& aTestName, TDes& aCmdLine) |
|
19 { |
|
20 User::CommandLine(aCmdLine); |
|
21 aCmdLine.TrimAll(); |
|
22 if(aCmdLine.Length() == 0) |
|
23 { |
|
24 aTest.Printf(_L("Usage: %S [ [/enc=<16/8>] /drv=<drive letter>:] [/page=<512/1024/2048/4096/8192/16384/32768>] ] [/cache=<number>]\r\n"), &aTestName); |
|
25 return; |
|
26 } |
|
27 aCmdLine.Append(TChar('/')); |
|
28 } |
|
29 |
|
30 static void ExtractCmdLineParams(TDes& aCmdLine, RArray<TPtrC>& aPrmNames, RArray<TPtrC>& aPrmValues) |
|
31 { |
|
32 aPrmNames.Reset(); |
|
33 aPrmValues.Reset(); |
|
34 |
|
35 enum TState{EWaitPrmStart, EReadPrmName, EReadPrmValue}; |
|
36 TState state = EWaitPrmStart; |
|
37 TInt startPos = -1; |
|
38 TPtr prmName(0, 0); |
|
39 TPtr prmValue(0, 0); |
|
40 |
|
41 aCmdLine.Append(TChar('/')); |
|
42 |
|
43 for(TInt i=0;i<aCmdLine.Length();++i) |
|
44 { |
|
45 switch(state) |
|
46 { |
|
47 case EWaitPrmStart: |
|
48 if(aCmdLine[i] == TChar('/')) |
|
49 { |
|
50 startPos = i + 1; |
|
51 prmName.Zero(); |
|
52 state = EReadPrmName; |
|
53 } |
|
54 break; |
|
55 case EReadPrmName: |
|
56 if(aCmdLine[i] == TChar('=')) |
|
57 { |
|
58 TPtr p = aCmdLine.MidTPtr(startPos, i - startPos); |
|
59 prmName.Set(p); |
|
60 prmName.TrimRight(); |
|
61 startPos = i + 1; |
|
62 prmValue.Zero(); |
|
63 state = EReadPrmValue; |
|
64 } |
|
65 break; |
|
66 case EReadPrmValue: |
|
67 if(aCmdLine[i] == TChar('/')) |
|
68 { |
|
69 TPtr p = aCmdLine.MidTPtr(startPos, i - startPos); |
|
70 prmValue.Set(p); |
|
71 prmValue.Trim(); |
|
72 startPos = i + 1; |
|
73 aPrmNames.Append(prmName); |
|
74 aPrmValues.Append(prmValue); |
|
75 prmName.Zero(); |
|
76 prmValue.Zero(); |
|
77 state = EReadPrmName; |
|
78 } |
|
79 break; |
|
80 default: |
|
81 break; |
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 static void ExtractParamNamesAndValues(const RArray<TPtrC>& aPrmNames, const RArray<TPtrC>& aPrmValues, TCmdLineParams& aCmdLineParams) |
|
87 { |
|
88 __ASSERT_ALWAYS(aPrmNames.Count() == aPrmValues.Count(), User::Invariant()); |
|
89 |
|
90 aCmdLineParams.SetDefaults(); |
|
91 |
|
92 for(TInt i=0;i<aPrmNames.Count();++i) |
|
93 { |
|
94 if(aPrmNames[i].CompareF(_L("enc")) == 0) |
|
95 { |
|
96 TLex lex(aPrmValues[i]); |
|
97 TInt enc = 0; |
|
98 TInt err = lex.Val(enc); |
|
99 if(err == KErrNone) |
|
100 { |
|
101 if(enc == 8) |
|
102 { |
|
103 aCmdLineParams.iDbEncoding = TCmdLineParams::EDbUtf8; |
|
104 } |
|
105 else if(enc == 16) |
|
106 { |
|
107 aCmdLineParams.iDbEncoding = TCmdLineParams::EDbUtf16; |
|
108 } |
|
109 } |
|
110 } |
|
111 else if(aPrmNames[i].CompareF(_L("drv")) == 0) |
|
112 { |
|
113 if(aPrmValues[i].Length() == 2 && aPrmValues[i][1] == TChar(':')) |
|
114 { |
|
115 TChar ch(aPrmValues[i][0]); |
|
116 ch.LowerCase(); |
|
117 if(ch >= TChar('a') && ch <= TChar('z')) |
|
118 aCmdLineParams.iDriveName.Copy(aPrmValues[i]); |
|
119 } |
|
120 } |
|
121 else if(aPrmNames[i].CompareF(_L("page")) == 0) |
|
122 { |
|
123 TLex lex(aPrmValues[i]); |
|
124 TInt pageSize = 0; |
|
125 TInt err = lex.Val(pageSize); |
|
126 if(err == KErrNone && (pageSize == 512 || pageSize == 1024 || pageSize == 2048 || |
|
127 pageSize == 4096 || pageSize == 8192 || pageSize == 16384 || pageSize == 32768)) |
|
128 { |
|
129 aCmdLineParams.iPageSize = pageSize; |
|
130 } |
|
131 } |
|
132 else if(aPrmNames[i].CompareF(_L("cache")) == 0) |
|
133 { |
|
134 TLex lex(aPrmValues[i]); |
|
135 TInt cacheSize = 0; |
|
136 TInt err = lex.Val(cacheSize); |
|
137 if(err == KErrNone && (cacheSize > 0 && cacheSize < 1000000000)) |
|
138 { |
|
139 aCmdLineParams.iCacheSize = cacheSize; |
|
140 } |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 static void PrepareSqlConfigString(RTest& aTest, const TCmdLineParams& aCmdLineParams, TDes8& aConfigStr) |
|
146 { |
|
147 aConfigStr.Zero(); |
|
148 |
|
149 if(aCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf8) |
|
150 { |
|
151 aTest.Printf(_L("--PRM--Database Encoding: UTF8\r\n")); |
|
152 aConfigStr.Append(_L8("encoding=\"UTF-8\";")); |
|
153 } |
|
154 else |
|
155 { |
|
156 aTest.Printf(_L("--PRM--Database Encoding: UTF16\r\n")); |
|
157 aConfigStr.Append(_L8("encoding=\"UTF-16\";")); |
|
158 } |
|
159 |
|
160 aTest.Printf(_L("--PRM--Database page size: %d\r\n"), aCmdLineParams.iPageSize); |
|
161 TBuf8<20> pageSizeBuf; |
|
162 pageSizeBuf.Format(_L8("page_size=%d;"), aCmdLineParams.iPageSize); |
|
163 aConfigStr.Append(pageSizeBuf); |
|
164 |
|
165 aTest.Printf(_L("--PRM--Database cache size: %d\r\n"), aCmdLineParams.iCacheSize); |
|
166 TBuf8<20> cacheSizeBuf; |
|
167 cacheSizeBuf.Format(_L8("cache_size=%d;"), aCmdLineParams.iCacheSize); |
|
168 aConfigStr.Append(cacheSizeBuf); |
|
169 |
|
170 aTest.Printf(_L("--PRM--Database drive: %S\r\n"), &aCmdLineParams.iDriveName); |
|
171 } |
|
172 |
|
173 void GetCmdLineParamsAndSqlConfigString(RTest& aTest, const TDesC& aTestName, TCmdLineParams& aCmdLineParams, TDes8& aConfigStr) |
|
174 { |
|
175 TBuf<200> cmdLine; |
|
176 GetCmdLine(aTest, aTestName, cmdLine); |
|
177 RArray<TPtrC> prmNames; |
|
178 RArray<TPtrC> prmValues; |
|
179 ExtractCmdLineParams(cmdLine, prmNames, prmValues); |
|
180 ExtractParamNamesAndValues(prmNames, prmValues, aCmdLineParams); |
|
181 prmValues.Close(); |
|
182 prmNames.Close(); |
|
183 PrepareSqlConfigString(aTest, aCmdLineParams, aConfigStr); |
|
184 } |
|
185 |
|
186 void PrepareDbName(const TDesC& aDeafultDbName, const TDriveName& aDriveName, TDes& aDbName) |
|
187 { |
|
188 TParse parse; |
|
189 parse.Set(aDriveName, &aDeafultDbName, 0); |
|
190 const TDesC& dbFilePath = parse.FullName(); |
|
191 aDbName.Copy(dbFilePath); |
|
192 } |