sl@0: // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: #include sl@0: #include "t_sqlcmdlineutil.h" sl@0: sl@0: static void GetCmdLine(RTest& aTest, const TDesC& aTestName, TDes& aCmdLine) sl@0: { sl@0: User::CommandLine(aCmdLine); sl@0: aCmdLine.TrimAll(); sl@0: if(aCmdLine.Length() == 0) sl@0: { sl@0: aTest.Printf(_L("Usage: %S [ [/enc=<16/8>] /drv=:] [/page=<512/1024/2048/4096/8192/16384/32768>] ] [/cache=] [/hlimit=]\r\n"), &aTestName); sl@0: return; sl@0: } sl@0: aCmdLine.Append(TChar('/')); sl@0: } sl@0: sl@0: static void ExtractCmdLineParams(TDes& aCmdLine, RArray& aPrmNames, RArray& aPrmValues) sl@0: { sl@0: aPrmNames.Reset(); sl@0: aPrmValues.Reset(); sl@0: sl@0: enum TState{EWaitPrmStart, EReadPrmName, EReadPrmValue}; sl@0: TState state = EWaitPrmStart; sl@0: TInt startPos = -1; sl@0: TPtr prmName(0, 0); sl@0: TPtr prmValue(0, 0); sl@0: sl@0: aCmdLine.Append(TChar('/')); sl@0: sl@0: for(TInt i=0;i& aPrmNames, const RArray& aPrmValues, TCmdLineParams& aCmdLineParams) sl@0: { sl@0: __ASSERT_ALWAYS(aPrmNames.Count() == aPrmValues.Count(), User::Invariant()); sl@0: sl@0: aCmdLineParams.SetDefaults(); sl@0: sl@0: for(TInt i=0;i= TChar('a') && ch <= TChar('z')) sl@0: aCmdLineParams.iDriveName.Copy(aPrmValues[i]); sl@0: } sl@0: } sl@0: else if(aPrmNames[i].CompareF(_L("page")) == 0) sl@0: { sl@0: TLex lex(aPrmValues[i]); sl@0: TInt pageSize = 0; sl@0: TInt err = lex.Val(pageSize); sl@0: if(err == KErrNone && (pageSize == 512 || pageSize == 1024 || pageSize == 2048 || sl@0: pageSize == 4096 || pageSize == 8192 || pageSize == 16384 || pageSize == 32768)) sl@0: { sl@0: aCmdLineParams.iPageSize = pageSize; sl@0: } sl@0: } sl@0: else if(aPrmNames[i].CompareF(_L("cache")) == 0) sl@0: { sl@0: TLex lex(aPrmValues[i]); sl@0: TInt cacheSize = 0; sl@0: TInt err = lex.Val(cacheSize); sl@0: if(err == KErrNone && (cacheSize > 0 && cacheSize < 1000000000)) sl@0: { sl@0: aCmdLineParams.iCacheSize = cacheSize; sl@0: } sl@0: } sl@0: else if(aPrmNames[i].CompareF(_L("hlimit")) == 0) sl@0: { sl@0: TLex lex(aPrmValues[i]); sl@0: TInt softHeapLimit = 0; sl@0: TInt err = lex.Val(softHeapLimit); sl@0: if(err == KErrNone && (softHeapLimit >= 0 && softHeapLimit < 1000000000)) sl@0: { sl@0: aCmdLineParams.iSoftHeapLimitKb = softHeapLimit; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: static void PrepareSqlConfigString(RTest& aTest, const TCmdLineParams& aCmdLineParams, TDes8& aConfigStr) sl@0: { sl@0: aConfigStr.Zero(); sl@0: sl@0: if(aCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf8) sl@0: { sl@0: aTest.Printf(_L("--PRM--Database Encoding: UTF8\r\n")); sl@0: aConfigStr.Append(_L8("encoding=\"UTF-8\";")); sl@0: } sl@0: else sl@0: { sl@0: aTest.Printf(_L("--PRM--Database Encoding: UTF16\r\n")); sl@0: aConfigStr.Append(_L8("encoding=\"UTF-16\";")); sl@0: } sl@0: sl@0: aTest.Printf(_L("--PRM--Database page size: %d\r\n"), aCmdLineParams.iPageSize); sl@0: TBuf8<20> pageSizeBuf; sl@0: pageSizeBuf.Format(_L8("page_size=%d;"), aCmdLineParams.iPageSize); sl@0: aConfigStr.Append(pageSizeBuf); sl@0: sl@0: aTest.Printf(_L("--PRM--Database cache size: %d\r\n"), aCmdLineParams.iCacheSize); sl@0: TBuf8<20> cacheSizeBuf; sl@0: cacheSizeBuf.Format(_L8("cache_size=%d;"), aCmdLineParams.iCacheSize); sl@0: aConfigStr.Append(cacheSizeBuf); sl@0: sl@0: aTest.Printf(_L("--PRM--Database drive: %S\r\n"), &aCmdLineParams.iDriveName); sl@0: sl@0: if(aCmdLineParams.iSoftHeapLimitKb > 0) sl@0: { sl@0: aTest.Printf(_L("--PRM--Soft heap limit: %d Kb\r\n"), aCmdLineParams.iSoftHeapLimitKb); sl@0: } sl@0: else sl@0: { sl@0: aTest.Printf(_L("--PRM--Soft heap limit: default\r\n")); sl@0: } sl@0: } sl@0: sl@0: #ifdef SQL_SOFT_HEAP_LIMIT_TEST sl@0: sl@0: static TInt KillProcess(const TDesC& aProcessName) sl@0: { sl@0: TFullName name; sl@0: TBuf<64> pattern(aProcessName); sl@0: TInt length = pattern.Length(); sl@0: pattern += _L("*"); sl@0: TFindProcess procFinder(pattern); sl@0: sl@0: while (procFinder.Next(name) == KErrNone) sl@0: { sl@0: if (name.Length() > length) sl@0: {//If found name is a string containing aProcessName string. sl@0: TChar c(name[length]); sl@0: if (c.IsAlphaDigit() || sl@0: c == TChar('_') || sl@0: c == TChar('-')) sl@0: { sl@0: // If the found name is other valid application name sl@0: // starting with aProcessName string. sl@0: continue; sl@0: } sl@0: } sl@0: RProcess proc; sl@0: if (proc.Open(name) == KErrNone) sl@0: { sl@0: proc.Kill(0); sl@0: } sl@0: proc.Close(); sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: _LIT(KSqlSrvName, "sqlsrv.exe"); sl@0: _LIT(KSqlSrvConfigFile, "c:\\test\\t_sqlserver.cfg"); sl@0: sl@0: static void ReplaceConfigFile(const TDesC16& aConfig) sl@0: { sl@0: RFs fs; sl@0: TInt err = fs.Connect(); sl@0: __ASSERT_ALWAYS(err == KErrNone, User::Invariant()); sl@0: sl@0: (void)KillProcess(KSqlSrvName); sl@0: sl@0: (void)fs.MkDirAll(KSqlSrvConfigFile); sl@0: (void)fs.Delete(KSqlSrvConfigFile); sl@0: sl@0: RFile file; sl@0: err = file.Create(fs, KSqlSrvConfigFile, EFileRead | EFileWrite); sl@0: __ASSERT_ALWAYS(err == KErrNone, User::Invariant()); sl@0: sl@0: TPtrC8 p((const TUint8*)aConfig.Ptr(), aConfig.Length() * sizeof(TUint16)); sl@0: err = file.Write(p); sl@0: file.Close(); sl@0: __ASSERT_ALWAYS(err == KErrNone, User::Invariant()); sl@0: sl@0: fs.Close(); sl@0: } sl@0: sl@0: static void DeleteConfigFile() sl@0: { sl@0: RFs fs; sl@0: TInt err = fs.Connect(); sl@0: __ASSERT_ALWAYS(err == KErrNone, User::Invariant()); sl@0: sl@0: (void)KillProcess(KSqlSrvName); sl@0: sl@0: (void)fs.MkDirAll(KSqlSrvConfigFile); sl@0: (void)fs.Delete(KSqlSrvConfigFile); sl@0: sl@0: fs.Close(); sl@0: } sl@0: sl@0: #endif //SQL_SOFT_HEAP_LIMIT_TEST sl@0: sl@0: void GetCmdLineParamsAndSqlConfigString(RTest& aTest, const TDesC& aTestName, TCmdLineParams& aCmdLineParams, TDes8& aConfigStr) sl@0: { sl@0: TBuf<200> cmdLine; sl@0: GetCmdLine(aTest, aTestName, cmdLine); sl@0: RArray prmNames; sl@0: RArray prmValues; sl@0: ExtractCmdLineParams(cmdLine, prmNames, prmValues); sl@0: ExtractParamNamesAndValues(prmNames, prmValues, aCmdLineParams); sl@0: prmValues.Close(); sl@0: prmNames.Close(); sl@0: PrepareSqlConfigString(aTest, aCmdLineParams, aConfigStr); sl@0: } sl@0: sl@0: void PrepareDbName(const TDesC& aDeafultDbName, const TDriveName& aDriveName, TDes& aDbName) sl@0: { sl@0: TParse parse; sl@0: parse.Set(aDriveName, &aDeafultDbName, 0); sl@0: const TDesC& dbFilePath = parse.FullName(); sl@0: aDbName.Copy(dbFilePath); sl@0: } sl@0: sl@0: void SetSoftHeapLimit(TInt aSoftHeapLimit) sl@0: { sl@0: if(aSoftHeapLimit > 0) sl@0: { sl@0: #ifdef SQL_SOFT_HEAP_LIMIT_TEST sl@0: TBuf<50> configBuf; sl@0: configBuf.Format(_L("soft_heap_limit_kb=%d"), aSoftHeapLimit); sl@0: ReplaceConfigFile(configBuf); sl@0: #else sl@0: RDebug::Print(_L("The soft heap limit cannot be set if \"SQL_SOFT_HEAP_LIMIT_TEST\" macro is not defined!\r\n")); sl@0: #endif sl@0: } sl@0: else if(aSoftHeapLimit < 0) sl@0: { sl@0: RDebug::Print(_L("Soft heap limit of %d Kb cannot be set!\r\n"), aSoftHeapLimit); sl@0: } sl@0: } sl@0: sl@0: void ResetSoftHeapLimit() sl@0: { sl@0: #ifdef SQL_SOFT_HEAP_LIMIT_TEST sl@0: DeleteConfigFile(); sl@0: #endif sl@0: } sl@0: