sl@0: // Copyright (c) 2007-2009 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: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "tcl.h" sl@0: sl@0: _LIT(KTestExt, "*.test"); sl@0: _LIT(KTclExt, "*.tcl"); sl@0: _LIT(KExplainExt, "*.explain"); sl@0: _LIT(KTclTempFilePrefix, "tcl*"); sl@0: sl@0: //Note: every time when you add a new KTestFileXX constant here, don't forget to add that constant as a new entry into sl@0: //"TPtrC fileNames[]" array in DoDeleteTestFilesL() function. sl@0: _LIT(KTestFile1, "test1.bt"); sl@0: _LIT(KTestFile2, "test.bu"); sl@0: _LIT(KTestFile3, "backup.db"); sl@0: _LIT(KTestFile4, "bak.db"); sl@0: _LIT(KTestFile5, "corrupt.db"); sl@0: _LIT(KTestFile6, "ptf2.db"); sl@0: _LIT(KTestFile7, "test1.db"); sl@0: _LIT(KTestFile8, "test2.db"); sl@0: _LIT(KTestFile9, "test3.db"); sl@0: _LIT(KTestFile10,"test4.db"); sl@0: _LIT(KTestFile11,"test.db"); sl@0: _LIT(KTestFile12,"test.db2"); sl@0: _LIT(KTestFile13,"bak.db-journal"); sl@0: _LIT(KTestFile14,"test.db-journal-bu"); sl@0: _LIT(KTestFile15,"mydir"); sl@0: _LIT(KTestFile16,"testdb"); sl@0: _LIT(KTestFile17,"test2-script.tcl"); sl@0: _LIT(KTestFile18,"test.tcl"); sl@0: _LIT(KTestFile19,"speed1.txt"); sl@0: _LIT(KTestFile20,"speed2.txt"); sl@0: _LIT(KTestFile21,"test.db-bu1"); sl@0: _LIT(KTestFile22,"test.db-bu2"); sl@0: _LIT(KTestFile23,"test.db-template"); sl@0: _LIT(KTestFile24,"invalid.db"); sl@0: _LIT(KTestFile25,"log"); sl@0: _LIT(KTestFile26,"test5.db"); sl@0: sl@0: _LIT(KTestDir1,"mydir-journal"); sl@0: sl@0: #ifdef _DEBUG sl@0: sl@0: #define PRINT_DELFILE_ERROR(aFileName, aErr) \ sl@0: if(aErr != KErrNone && aErr != KErrNotFound)\ sl@0: {\ sl@0: RDebug::Print(_L("###TclSqlite3: Failed to delete file \"%S\". Error=%d\r\n"), &aFileName, aErr);\ sl@0: } sl@0: sl@0: #else sl@0: sl@0: #define PRINT_DELFILE_ERROR(aFileName, aErr) void(0) sl@0: sl@0: #endif sl@0: sl@0: //Connects the file session argument. sl@0: //Creates application's private datacage on drive C: (if does not exist). sl@0: //Copies the private path as string to the aPrivatePath argument (without the drive name). sl@0: //aPrivatePath must point to a big enough place (ideally TFileName object). sl@0: static void GetFsAndPrivatePathL(RFs& aFs, TDes& aPrivatePath) sl@0: { sl@0: User::LeaveIfError(aFs.Connect()); sl@0: TInt err = aFs.CreatePrivatePath(EDriveC); sl@0: if(!(err == KErrNone || err == KErrAlreadyExists)) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: sl@0: User::LeaveIfError(aFs.PrivatePath(aPrivatePath)); sl@0: } sl@0: sl@0: //The function constructs a full script file path, containing wildcards, in aFullPath argument sl@0: // (from aDriveNumber and aPrivatePath arguments, using aMask parameter as a file name). sl@0: //aFullPath must point to a big enough place (ideally TFileName object). sl@0: static void ConstructFilePathByMask(TDriveNumber aDriveNumber, const TDesC& aPrivatePath, const TDesC& aMask, TDes& aFullPath) sl@0: { sl@0: TDriveName srcDriveName = TDriveUnit(aDriveNumber).Name(); sl@0: aFullPath.Copy(srcDriveName); sl@0: aFullPath.Append(aMask); sl@0: TParse parse; sl@0: parse.Set(aPrivatePath, &aFullPath, 0); sl@0: aFullPath.Copy(parse.FullName()); sl@0: } sl@0: sl@0: //The function constructs a full file path in aFullPath argument (from aDriveNumber, aFileName and aPrivatePath arguments). sl@0: //aFullPath must point to a big enough place (ideally TFileName object). sl@0: static void ConstructFilePathByName(TDriveNumber aDriveNumber, const TDesC& aFileName, const TDesC& aPrivatePath, TDes& aFullPath) sl@0: { sl@0: TDriveName srcDriveName = TDriveUnit(aDriveNumber).Name(); sl@0: aFullPath.Copy(srcDriveName); sl@0: aFullPath.Append(aFileName); sl@0: TParse parse; sl@0: parse.Set(aPrivatePath, &aFullPath, 0); sl@0: aFullPath.Copy(parse.FullName()); sl@0: } sl@0: sl@0: //The function constructs a full path in aDestPath argument (from aDriveNumber and aPrivatePath arguments). sl@0: //aDestPath must point to a big enough place (ideally TFileName object). sl@0: static void ConstructDestPath(TDriveNumber aDriveNumber, const TDesC& aPrivatePath, TDes& aDestPath) sl@0: { sl@0: TDriveName destDriveName = TDriveUnit(aDriveNumber).Name(); sl@0: TParse parse; sl@0: parse.Set(aPrivatePath, &destDriveName, 0); sl@0: aDestPath.Copy(parse.FullName()); sl@0: } sl@0: sl@0: //The function copies all test script files from Z: to C: drive, in application's private data cage. sl@0: static void DoCopyTestFilesL() sl@0: { sl@0: RDebug::Print(_L("###TclSqlite3: Construct private data cage on drive C:\r\n")); sl@0: RFs fs; sl@0: CleanupClosePushL(fs); sl@0: TFileName privatePath; sl@0: GetFsAndPrivatePathL(fs, privatePath); sl@0: sl@0: CFileMan* fm = CFileMan::NewL(fs); sl@0: CleanupStack::PushL(fm); sl@0: sl@0: TFileName srcPath, destPath; sl@0: ConstructFilePathByMask(EDriveZ, privatePath, KTestExt, srcPath); sl@0: ConstructDestPath(EDriveC, privatePath, destPath); sl@0: RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath); sl@0: User::LeaveIfError(fm->Copy(srcPath, destPath)); sl@0: sl@0: ConstructFilePathByMask(EDriveZ, privatePath, KTclExt, srcPath); sl@0: ConstructDestPath(EDriveC, privatePath, destPath); sl@0: RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath); sl@0: User::LeaveIfError(fm->Copy(srcPath, destPath)); sl@0: sl@0: ConstructFilePathByMask(EDriveZ, privatePath, KExplainExt, srcPath); sl@0: ConstructDestPath(EDriveC, privatePath, destPath); sl@0: RDebug::Print(_L("###TclSqlite3: Copying \"%S\" to \"%S\"\r\n"), &srcPath, &destPath); sl@0: User::LeaveIfError(fm->Copy(srcPath, destPath)); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: } sl@0: sl@0: //The function deletes a file, identified by the aFullPath argument. sl@0: //The function leaves if the delete operation error is different than KErrNone and KErrNotFound. sl@0: static void DoDeleteTestFileL(CFileMan& aFm, const TDesC& aFullPath) sl@0: { sl@0: TInt err = aFm.Attribs(aFullPath, 0, KEntryAttReadOnly, TTime(0)); sl@0: if(err == KErrNone) sl@0: { sl@0: err = aFm.Delete(aFullPath); sl@0: } sl@0: if(err != KErrNone && err != KErrNotFound) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: sl@0: //The function deletes a directory, identified by the aFullPath argument. sl@0: //The function leaves if the delete operation error is different than KErrNone and KErrNotFound. sl@0: static void DoDeleteTestDirL(CFileMan& aFm, const TDesC& aFullPath) sl@0: { sl@0: TInt err = aFm.Attribs(aFullPath, 0, KEntryAttReadOnly, TTime(0)); sl@0: if(err == KErrNone) sl@0: { sl@0: err = aFm.RmDir(aFullPath); sl@0: } sl@0: if(err != KErrNone && err != KErrNotFound) sl@0: { sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: sl@0: //Deletes the test scripts and test output files from C: drive. sl@0: static void DoDeleteTestFilesL() sl@0: { sl@0: RFs fs; sl@0: CleanupClosePushL(fs); sl@0: TFileName privatePath; sl@0: GetFsAndPrivatePathL(fs, privatePath); sl@0: sl@0: CFileMan* fm = CFileMan::NewL(fs); sl@0: CleanupStack::PushL(fm); sl@0: sl@0: TFileName filePath; sl@0: ConstructFilePathByMask(EDriveC, privatePath, KExplainExt, filePath); sl@0: TRAPD(err, DoDeleteTestFileL(*fm, filePath)); sl@0: PRINT_DELFILE_ERROR(filePath, err); sl@0: sl@0: ConstructFilePathByMask(EDriveC, privatePath, KTclExt, filePath); sl@0: TRAP(err, DoDeleteTestFileL(*fm, filePath)); sl@0: PRINT_DELFILE_ERROR(filePath, err); sl@0: sl@0: ConstructFilePathByMask(EDriveC, privatePath, KTestExt, filePath); sl@0: TRAP(err, DoDeleteTestFileL(*fm, filePath)); sl@0: PRINT_DELFILE_ERROR(filePath, err); sl@0: sl@0: ConstructFilePathByMask(EDriveC, privatePath, KTclTempFilePrefix, filePath); sl@0: TRAP(err, DoDeleteTestFileL(*fm, filePath)); sl@0: PRINT_DELFILE_ERROR(filePath, err); sl@0: sl@0: TPtrC fileNames[] = sl@0: { sl@0: KTestFile1(), KTestFile2(), KTestFile3(), KTestFile4(), KTestFile5(), sl@0: KTestFile6(), KTestFile7(), KTestFile8(), KTestFile9(), KTestFile10(), sl@0: KTestFile11(), KTestFile12(), KTestFile13(), KTestFile14(), KTestFile15(), sl@0: KTestFile16(), KTestFile17(), KTestFile18(), KTestFile19(), KTestFile20(), sl@0: KTestFile21(), KTestFile22(), KTestFile23(), KTestFile24(), KTestFile25(), KTestFile26() sl@0: }; sl@0: for(TInt i=0;i<(sizeof(fileNames)/sizeof(fileNames[0]));++i) sl@0: { sl@0: ConstructFilePathByName(EDriveC, fileNames[i], privatePath, filePath); sl@0: TRAP(err, DoDeleteTestFileL(*fm, filePath)); sl@0: PRINT_DELFILE_ERROR(filePath, err); sl@0: } sl@0: sl@0: ConstructFilePathByName(EDriveC, KTestDir1, privatePath, filePath); sl@0: RDebug::Print(_L("###TclSqlite3: test dir to be removed - \"%S\".\r\n"), &filePath); sl@0: TRAP(err, DoDeleteTestDirL(*fm, filePath)); sl@0: PRINT_DELFILE_ERROR(filePath, err); sl@0: sl@0: CleanupStack::PopAndDestroy(2); sl@0: } sl@0: sl@0: //Deletes the test scripts from C: drive sl@0: //Because the TCL SQLITE exe has mutiple exit points, there is no right place in the code where this function sl@0: //can be called from. sl@0: //The way how the test cleanup is implemented is: sl@0: // - a new script function has been definied and registered (tclsqlite.c)- "delete_test_files" sl@0: // - the "delete_test_files" function is called from the "finalize_testing" procedure (tester.tcl file), sl@0: // that is guaranteed to be called at the end of any test script execution sl@0: extern "C" int DeleteTestFiles(void) sl@0: { sl@0: RDebug::Print(_L("###TclSqlite3: Begin \"Delete test files\" operation\r\n")); sl@0: TRAP_IGNORE(DoDeleteTestFilesL()); sl@0: RDebug::Print(_L("###TclSqlite3: \"Delete test files\" operation has completed\r\n")); sl@0: return 0; sl@0: } sl@0: sl@0: //Copies the test scripts from Z: to C: drive sl@0: //This function is called from main() (tclsqlite.c file) sl@0: extern "C" TInt CopyTestFiles(void) sl@0: { sl@0: RDebug::Print(_L("###TclSqlite3: Begin \"Copy test files\" operation\r\n")); sl@0: TRAPD(err, DoCopyTestFilesL()); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Print(_L("###TclSqlite3: \"Copy test files\" operation has failed with error %d\r\n"), err); sl@0: DeleteTestFiles(); sl@0: } sl@0: else sl@0: { sl@0: RDebug::Print(_L("###TclSqlite3: \"Copy test files\" operation has completed successfully\r\n")); sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: //Used by GetFullFilePath() in test_hexio.c sl@0: //Seems that the OpenEnv library does not provide _splitpath(). sl@0: extern "C" char* FullFilePath(char* aPath, const char* aFileName) sl@0: { sl@0: static TFileName fname; sl@0: fname.Copy(TPtrC8((const TUint8*)aFileName)); sl@0: fname.Trim(); sl@0: for(TInt i=0;i fname2; sl@0: fname2.Copy(parse.NameAndExt()); sl@0: fname2.Append(TChar('\x0')); sl@0: strcat(aPath, (const char*)fname2.Ptr()); sl@0: } sl@0: else sl@0: { sl@0: strcpy(aPath, aFileName); sl@0: } sl@0: return aPath; sl@0: } sl@0: sl@0: extern "C" int PrintText(void*, Tcl_Interp*, int objc, Tcl_Obj* const* objv) sl@0: { sl@0: if(objc == 3) sl@0: { sl@0: const char* txt1 = Tcl_GetStringFromObj(objv[1], 0); sl@0: const char* txt2 = Tcl_GetStringFromObj(objv[2], 0); sl@0: if(txt1 && txt2) sl@0: { sl@0: TTime time; sl@0: time.HomeTime(); sl@0: TDateTime dt = time.DateTime(); sl@0: TBuf<16> tbuf; sl@0: tbuf.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond()); sl@0: sl@0: TBuf<256> buf1; sl@0: buf1.Copy(TPtrC8((const TUint8*)txt1)); sl@0: sl@0: TBuf<256> buf2; sl@0: buf2.Copy(TPtrC8((const TUint8*)txt2)); sl@0: sl@0: RDebug::Print(_L("%S: %S %S.\n"), &tbuf, &buf1, &buf2); sl@0: return TCL_OK; sl@0: } sl@0: } sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: extern "C" void PrintS(const char* aTxt) sl@0: { sl@0: if(!aTxt) sl@0: { sl@0: return; sl@0: } sl@0: TPtrC8 msg((const TUint8*)aTxt); sl@0: TInt msglen = msg.Length(); sl@0: TInt pos = 0; sl@0: const TInt KMaxLineLength = 220; sl@0: TBuf line; sl@0: do sl@0: { sl@0: if(pos == 0) sl@0: { sl@0: RProcess process; sl@0: TProcessId processId = process.Id(); sl@0: line.Format(_L("Process Id=%ld: "), processId.Id()); sl@0: } sl@0: TInt len = Min(msglen, (line.MaxLength() - line.Length())); sl@0: TPtrC8 ptr(msg.Ptr() + pos, len); sl@0: pos += len; sl@0: msglen -= len; sl@0: TPtr p2((TUint16*)line.Ptr() + line.Length(), 0, len); sl@0: p2.Copy(ptr); sl@0: line.SetLength(line.Length() + p2.Length()); sl@0: RDebug::Print(_L("%S\n"), &line); sl@0: line.Zero(); sl@0: } while(msglen > 0); sl@0: }