os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytoolfileview.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2007-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 "keytool_defs.h"
    20 #include "keytoolfileview.h"
    21 
    22 #include <e32cons.h>
    23 #include "keytool_utils.h"
    24 #include <s32file.h>
    25 
    26 
    27 /*static*/ CKeytoolFileView* CKeytoolFileView::NewLC(const TDesC& aInputFile)
    28 	{
    29 	CKeytoolFileView* self = new (ELeave) CKeytoolFileView();
    30 	CleanupStack::PushL(self);
    31 	self->ConstructL(aInputFile);
    32 	return self;
    33 	}
    34 
    35 CKeytoolFileView::CKeytoolFileView()
    36 	{	
    37 	}
    38 	
    39 CKeytoolFileView::~CKeytoolFileView()
    40 	{
    41 	iFs.Close();
    42 	iFile.Close();
    43 	iArgs.ResetAndDestroy();
    44 	}
    45 	
    46 void CKeytoolFileView::ConstructL(const TDesC& aInputFile)
    47 	{
    48 	User::LeaveIfError(iFs.Connect());
    49 	User::LeaveIfError(iFile.Open(iFs, aInputFile, EFileShareExclusive));
    50 	}
    51 
    52 void CKeytoolFileView::DisplayUsage()
    53 	{	
    54 	}
    55 	
    56 void CKeytoolFileView::BoilerPlate()
    57 	{
    58 	
    59 	}
    60 	
    61 void CKeytoolFileView::DisplayKeyInfoL(CCTKeyInfo& aKey, TBool aIsDetailed, TBool /*aPageWise*/)
    62 	{
    63 	// Display the key infos
    64 	KeyToolUtils::PrintKeyInfoL(aKey, aIsDetailed);
    65  	KeyToolUtils::PrintInfoL(_L("\n"));
    66  	}
    67 	
    68 void CKeytoolFileView::DisplayErrorL(const TDesC& aError, TInt aErrorCode, TBool/* aPageWise*/)
    69 	{
    70 	KeyToolUtils::PrintInfoL(aError);
    71 	KeyToolUtils::PrintInfoL(_L("\nError code: "));
    72 	KeyToolUtils::WriteErrorL(aErrorCode);
    73 	KeyToolUtils::PrintInfoL(_L("\n"));
    74 	}
    75 	 	
    76  void CKeytoolFileView::DisplayErrorL(const TDesC& aError, TBool/* aPageWise*/)
    77 	{
    78 	KeyToolUtils::PrintInfoL(aError);
    79 	KeyToolUtils::PrintInfoL(_L("\n"));
    80 	}
    81 	
    82  void CKeytoolFileView::DisplayCertL(CCTCertInfo& aCert, CCertificate& aCertificate, RUidArray aApps, TBool aTrusted, TBool aIsDetailed, TBool/* aPageWise*/)
    83  	{
    84 	// Display the key infos
    85 	KeyToolUtils::PrintCertInfoL(aCert, aCertificate, aApps, aTrusted, aIsDetailed);
    86  	KeyToolUtils::PrintInfoL(_L("\n")); 	
    87  	}
    88 
    89 CArrayFixFlat<TPtrC>* CKeytoolFileView::ReadArrayArgumentsLC(TInt cmdIndex)
    90 	{
    91 	CArrayFixFlat<TPtrC>* currentCmd = new (ELeave) CArrayFixFlat<TPtrC> (10);
    92 	CleanupStack::PushL(currentCmd);
    93 	TPtr cmd = iArgs[cmdIndex]->Des();
    94 	cmd.Trim();
    95 	TLex arguments(cmd);
    96 	while(!arguments.Eos())
    97     	{
    98         TPtrC token = arguments.NextToken();
    99         currentCmd->AppendL(token);
   100         }
   101     
   102     TBuf<150> string;
   103 	string.Format(_L("Command :  %S\noutput :"), &cmd);
   104 	KeyToolUtils::PrintInfoL(_L("================================================"));
   105 	KeyToolUtils::PrintInfoL(_L("================================================\n"));
   106     DisplayErrorL(string, 0);
   107     return currentCmd;
   108 	}
   109 
   110 TInt CKeytoolFileView::SplitFileInputToArrayL()
   111 	{
   112 	TInt fSize;
   113 	iFile.Size(fSize);
   114 
   115 	HBufC8* fileContents = HBufC8::NewLC(fSize);
   116 	TPtr8 ptr(fileContents->Des());
   117 	ptr.SetLength(fSize);
   118 	
   119 	// create file stream and Read the content from the file
   120 	RFileReadStream inputFileStream(iFile);
   121     CleanupClosePushL(inputFileStream);
   122 	inputFileStream.ReadL(ptr, fSize);
   123 	CleanupStack::PopAndDestroy(&inputFileStream);
   124 	
   125 	TInt readPos = 0;
   126 	TPtrC8 lineContents;
   127 
   128 	TInt lineCount = 0;
   129 	while (!ReadLine(*fileContents, readPos, lineContents))
   130 		{
   131 		TInt lineLength = lineContents.Length();
   132 		if (lineLength)
   133 			{
   134 			lineCount++;
   135 			HBufC* currentLine = HBufC::NewLC(lineLength);
   136 			currentLine->Des().Copy(lineContents);
   137 			iArgs.AppendL(currentLine);
   138 			CleanupStack::Pop(currentLine);
   139 			}
   140 		}
   141 	CleanupStack::PopAndDestroy(fileContents);
   142 	return lineCount;
   143 	}
   144 	
   145 TInt CKeytoolFileView::ReadLine(const TDesC8& aBuffer, TInt& aPos, TPtrC8& aLine)
   146 	{
   147 	TBool endOfBuffer = EFalse;
   148 	const TChar KCarriageReturn = '\r';
   149 	const TChar KLineReturn = '\n';
   150 
   151 	TInt bufferLength = aBuffer.Length();
   152 	if ( aPos > bufferLength || aPos < 0 )
   153 		{	
   154 		return ETrue; // End of buffer
   155 		}
   156 		
   157 	// find the position of the next delimter		
   158 	TInt endPos = aPos;	
   159 	while (endPos < bufferLength)
   160 		{
   161 		TChar c = aBuffer[endPos];
   162 
   163 		if (c == KCarriageReturn || c == KLineReturn) 
   164 			{
   165 			break;
   166 			}	
   167 		endPos++;
   168 		}
   169 
   170 	if (endPos != aPos)	
   171 		{
   172 		TInt tokenLen = endPos - aPos;
   173 		aLine.Set(&aBuffer[aPos], tokenLen);
   174 		endPos += 2;
   175 		}
   176 	else
   177 		{
   178 		return ETrue; // End of buffer
   179 		}
   180 
   181 	aPos = endPos;
   182 	return endOfBuffer;
   183 	}
   184 	
   185