sl@0: /*
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 the License "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: 
sl@0: 
sl@0: #include "keytool_defs.h"
sl@0: #include "keytoolfileview.h"
sl@0: 
sl@0: #include <e32cons.h>
sl@0: #include "keytool_utils.h"
sl@0: #include <s32file.h>
sl@0: 
sl@0: 
sl@0: /*static*/ CKeytoolFileView* CKeytoolFileView::NewLC(const TDesC& aInputFile)
sl@0: 	{
sl@0: 	CKeytoolFileView* self = new (ELeave) CKeytoolFileView();
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aInputFile);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CKeytoolFileView::CKeytoolFileView()
sl@0: 	{	
sl@0: 	}
sl@0: 	
sl@0: CKeytoolFileView::~CKeytoolFileView()
sl@0: 	{
sl@0: 	iFs.Close();
sl@0: 	iFile.Close();
sl@0: 	iArgs.ResetAndDestroy();
sl@0: 	}
sl@0: 	
sl@0: void CKeytoolFileView::ConstructL(const TDesC& aInputFile)
sl@0: 	{
sl@0: 	User::LeaveIfError(iFs.Connect());
sl@0: 	User::LeaveIfError(iFile.Open(iFs, aInputFile, EFileShareExclusive));
sl@0: 	}
sl@0: 
sl@0: void CKeytoolFileView::DisplayUsage()
sl@0: 	{	
sl@0: 	}
sl@0: 	
sl@0: void CKeytoolFileView::BoilerPlate()
sl@0: 	{
sl@0: 	
sl@0: 	}
sl@0: 	
sl@0: void CKeytoolFileView::DisplayKeyInfoL(CCTKeyInfo& aKey, TBool aIsDetailed, TBool /*aPageWise*/)
sl@0: 	{
sl@0: 	// Display the key infos
sl@0: 	KeyToolUtils::PrintKeyInfoL(aKey, aIsDetailed);
sl@0:  	KeyToolUtils::PrintInfoL(_L("\n"));
sl@0:  	}
sl@0: 	
sl@0: void CKeytoolFileView::DisplayErrorL(const TDesC& aError, TInt aErrorCode, TBool/* aPageWise*/)
sl@0: 	{
sl@0: 	KeyToolUtils::PrintInfoL(aError);
sl@0: 	KeyToolUtils::PrintInfoL(_L("\nError code: "));
sl@0: 	KeyToolUtils::WriteErrorL(aErrorCode);
sl@0: 	KeyToolUtils::PrintInfoL(_L("\n"));
sl@0: 	}
sl@0: 	 	
sl@0:  void CKeytoolFileView::DisplayErrorL(const TDesC& aError, TBool/* aPageWise*/)
sl@0: 	{
sl@0: 	KeyToolUtils::PrintInfoL(aError);
sl@0: 	KeyToolUtils::PrintInfoL(_L("\n"));
sl@0: 	}
sl@0: 	
sl@0:  void CKeytoolFileView::DisplayCertL(CCTCertInfo& aCert, CCertificate& aCertificate, RUidArray aApps, TBool aTrusted, TBool aIsDetailed, TBool/* aPageWise*/)
sl@0:  	{
sl@0: 	// Display the key infos
sl@0: 	KeyToolUtils::PrintCertInfoL(aCert, aCertificate, aApps, aTrusted, aIsDetailed);
sl@0:  	KeyToolUtils::PrintInfoL(_L("\n")); 	
sl@0:  	}
sl@0: 
sl@0: CArrayFixFlat<TPtrC>* CKeytoolFileView::ReadArrayArgumentsLC(TInt cmdIndex)
sl@0: 	{
sl@0: 	CArrayFixFlat<TPtrC>* currentCmd = new (ELeave) CArrayFixFlat<TPtrC> (10);
sl@0: 	CleanupStack::PushL(currentCmd);
sl@0: 	TPtr cmd = iArgs[cmdIndex]->Des();
sl@0: 	cmd.Trim();
sl@0: 	TLex arguments(cmd);
sl@0: 	while(!arguments.Eos())
sl@0:     	{
sl@0:         TPtrC token = arguments.NextToken();
sl@0:         currentCmd->AppendL(token);
sl@0:         }
sl@0:     
sl@0:     TBuf<150> string;
sl@0: 	string.Format(_L("Command :  %S\noutput :"), &cmd);
sl@0: 	KeyToolUtils::PrintInfoL(_L("================================================"));
sl@0: 	KeyToolUtils::PrintInfoL(_L("================================================\n"));
sl@0:     DisplayErrorL(string, 0);
sl@0:     return currentCmd;
sl@0: 	}
sl@0: 
sl@0: TInt CKeytoolFileView::SplitFileInputToArrayL()
sl@0: 	{
sl@0: 	TInt fSize;
sl@0: 	iFile.Size(fSize);
sl@0: 
sl@0: 	HBufC8* fileContents = HBufC8::NewLC(fSize);
sl@0: 	TPtr8 ptr(fileContents->Des());
sl@0: 	ptr.SetLength(fSize);
sl@0: 	
sl@0: 	// create file stream and Read the content from the file
sl@0: 	RFileReadStream inputFileStream(iFile);
sl@0:     CleanupClosePushL(inputFileStream);
sl@0: 	inputFileStream.ReadL(ptr, fSize);
sl@0: 	CleanupStack::PopAndDestroy(&inputFileStream);
sl@0: 	
sl@0: 	TInt readPos = 0;
sl@0: 	TPtrC8 lineContents;
sl@0: 
sl@0: 	TInt lineCount = 0;
sl@0: 	while (!ReadLine(*fileContents, readPos, lineContents))
sl@0: 		{
sl@0: 		TInt lineLength = lineContents.Length();
sl@0: 		if (lineLength)
sl@0: 			{
sl@0: 			lineCount++;
sl@0: 			HBufC* currentLine = HBufC::NewLC(lineLength);
sl@0: 			currentLine->Des().Copy(lineContents);
sl@0: 			iArgs.AppendL(currentLine);
sl@0: 			CleanupStack::Pop(currentLine);
sl@0: 			}
sl@0: 		}
sl@0: 	CleanupStack::PopAndDestroy(fileContents);
sl@0: 	return lineCount;
sl@0: 	}
sl@0: 	
sl@0: TInt CKeytoolFileView::ReadLine(const TDesC8& aBuffer, TInt& aPos, TPtrC8& aLine)
sl@0: 	{
sl@0: 	TBool endOfBuffer = EFalse;
sl@0: 	const TChar KCarriageReturn = '\r';
sl@0: 	const TChar KLineReturn = '\n';
sl@0: 
sl@0: 	TInt bufferLength = aBuffer.Length();
sl@0: 	if ( aPos > bufferLength || aPos < 0 )
sl@0: 		{	
sl@0: 		return ETrue; // End of buffer
sl@0: 		}
sl@0: 		
sl@0: 	// find the position of the next delimter		
sl@0: 	TInt endPos = aPos;	
sl@0: 	while (endPos < bufferLength)
sl@0: 		{
sl@0: 		TChar c = aBuffer[endPos];
sl@0: 
sl@0: 		if (c == KCarriageReturn || c == KLineReturn) 
sl@0: 			{
sl@0: 			break;
sl@0: 			}	
sl@0: 		endPos++;
sl@0: 		}
sl@0: 
sl@0: 	if (endPos != aPos)	
sl@0: 		{
sl@0: 		TInt tokenLen = endPos - aPos;
sl@0: 		aLine.Set(&aBuffer[aPos], tokenLen);
sl@0: 		endPos += 2;
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		return ETrue; // End of buffer
sl@0: 		}
sl@0: 
sl@0: 	aPos = endPos;
sl@0: 	return endOfBuffer;
sl@0: 	}
sl@0: 	
sl@0: