cryptomgmtlibs/securitytestfw/test/testhandler2/t_output.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 1998-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 "t_output.h"
       
    20 #include <securityerr.h>
       
    21 
       
    22 // Size used for stack based buffers
       
    23 const static TInt KMaxLineLength = 200;
       
    24 
       
    25 _LIT(KNewLine, "\r\n");
       
    26 	
       
    27 // Output //////////////////////////////////////////////////////////////////////
       
    28 
       
    29 EXPORT_C void Output::writeString(const TDesC& aString)
       
    30 	{
       
    31 	FixNewlinesAndWriteL(aString);
       
    32 	}
       
    33 
       
    34 EXPORT_C void Output::writeString(const TDesC8& aString)
       
    35 	{
       
    36 	HBufC16* des = HBufC16::NewLC(aString.Length());
       
    37 	des->Des().Copy(aString);
       
    38 	FixNewlinesAndWriteL(*des);
       
    39 	CleanupStack::PopAndDestroy(des);
       
    40 	}
       
    41 
       
    42 EXPORT_C void Output::write(TRefByValue<const TDesC16> aFmt, ...)
       
    43 	{
       
    44 	TBuf<KMaxLineLength> buf;
       
    45 	VA_LIST args;
       
    46 	VA_START(args, aFmt);
       
    47 	buf.AppendFormatList(aFmt, args);
       
    48 	VA_END(args);
       
    49 
       
    50 	FixNewlinesAndWriteL(buf);
       
    51 	}
       
    52 
       
    53 EXPORT_C void Output::writeSpaces(TInt aNum)
       
    54 	{
       
    55 	for (TInt i = 0; i < aNum; i++)
       
    56 		{
       
    57 		DoWriteL(_L(" "));
       
    58 		}
       
    59 	}
       
    60 
       
    61 EXPORT_C void Output::writeNewLine()
       
    62 	{
       
    63 	DoWriteL(KNewLine);
       
    64 	}
       
    65 
       
    66 EXPORT_C void Output::writeNum(TInt aNum)
       
    67 	{
       
    68 	write(_L("%d"), aNum);
       
    69 	}
       
    70 
       
    71 EXPORT_C void Output::writeHex(TInt aHex)
       
    72 	{
       
    73 	write(_L("%x"), aHex);	
       
    74 	}
       
    75 
       
    76 EXPORT_C void Output::writeError(TInt aError)
       
    77 	{
       
    78 	switch (aError)
       
    79 		{
       
    80 	case KErrNone:	// 0
       
    81 		DoWriteL(_L("KErrNone"));
       
    82 		break;
       
    83 
       
    84 	case KErrNotFound:	// -1
       
    85 		DoWriteL(_L("KErrNotFound"));
       
    86 		break;
       
    87 
       
    88 	case KErrNotSupported:	// -5
       
    89 		DoWriteL(_L("KErrNotSupported"));
       
    90 		break;
       
    91 
       
    92 	case KErrInUse:	// -14
       
    93 		DoWriteL(_L("KErrInUse"));
       
    94 		break;
       
    95 
       
    96 	case KErrNotReady:	// -18
       
    97 		DoWriteL(_L("KErrNotReady"));
       
    98 		break;
       
    99 
       
   100 	case KRequestPending:
       
   101 		DoWriteL(_L("KRequestPending"));
       
   102 		break;
       
   103 
       
   104 	case KErrAlreadyExists:
       
   105 		DoWriteL(_L("KErrAlreadyExists"));
       
   106 		break;
       
   107 
       
   108 	case KErrArgument:
       
   109 		DoWriteL(_L("KErrArgument"));
       
   110 		break;
       
   111 
       
   112 	case KErrBadName: // -28
       
   113 		DoWriteL(_L("KErrBadName"));
       
   114 		break;
       
   115 	case KErrPrivateKeyNotFound:
       
   116 		writeString(_L("KErrPrivateKeyNotFound"));
       
   117 		break;
       
   118 
       
   119 	default:
       
   120 		writeNum(aError);
       
   121 		break;
       
   122 		}
       
   123 	}
       
   124 
       
   125 EXPORT_C void Output::writeOctetString(const TDesC8& aString)
       
   126 	{
       
   127 	writeOctetStringL(aString);
       
   128 	}
       
   129 
       
   130 EXPORT_C void Output::writeOctetStringL(const TDesC8& aString)
       
   131 	{
       
   132     TInt len = aString.Length();
       
   133 	HBufC* buf = HBufC::NewLC(len * 3);
       
   134 	TPtr pBuf = buf->Des();
       
   135     for (TInt index = 0; index < len; ++index)
       
   136 		{
       
   137 		pBuf.AppendFormat(_L("%02x "),aString[index]);
       
   138 		}
       
   139 	DoWriteL(*buf);
       
   140 	CleanupStack::PopAndDestroy(buf);
       
   141 	}
       
   142 
       
   143 EXPORT_C void Output::writeBoolL(TBool aBool)
       
   144 	{
       
   145 	if (aBool)
       
   146 		{
       
   147 		DoWriteL(_L("ETrue"));
       
   148 		}
       
   149 	else
       
   150 		{
       
   151 		DoWriteL(_L("EFalse"));
       
   152 		}
       
   153 	}
       
   154 
       
   155 // Fix up newlines by turning any occurences of just "\n" into "\r\n".
       
   156 void Output::FixNewlinesAndWriteL(const TDesC& aString)
       
   157 	{
       
   158 	TPtrC remainder(aString);
       
   159 	TInt index = 0;
       
   160 	while (remainder.Length())
       
   161 		{
       
   162 		index = remainder.Locate('\n');
       
   163 
       
   164 		if (index == KErrNotFound)
       
   165 			{
       
   166 			DoWriteL(remainder);
       
   167 			break;
       
   168 			}
       
   169 
       
   170 		if (index == 0)
       
   171 			{
       
   172 			DoWriteL(KNewLine);
       
   173 			}
       
   174 		else 
       
   175 			{
       
   176 			if (remainder[index - 1] == '\r')
       
   177 				{
       
   178 				DoWriteL(remainder.Left(index + 1));
       
   179 				}
       
   180 			else
       
   181 				{
       
   182 				DoWriteL(remainder.Left(index));
       
   183 				DoWriteL(KNewLine);					 
       
   184 				}
       
   185 			}
       
   186 		remainder.Set(remainder.Mid(index + 1));
       
   187 		}
       
   188 	}
       
   189 
       
   190 // writeCapabilityL, writeCapabilitySetL, writeSecurityPolicyL implemented in
       
   191 // t_capability.cpp
       
   192 
       
   193 // NullOutput //////////////////////////////////////////////////////////////////
       
   194 
       
   195 EXPORT_C NullOutput::NullOutput()
       
   196 	{
       
   197 	}
       
   198 
       
   199 void NullOutput::DoWriteL(const TDesC& /*aString*/)
       
   200 	{
       
   201 	}
       
   202 
       
   203 // FileOutput //////////////////////////////////////////////////////////////////
       
   204 
       
   205 EXPORT_C FileOutput::FileOutput(RFile& aFile) :
       
   206 	iFile(aFile)
       
   207 	{
       
   208 	}
       
   209 
       
   210 void FileOutput::DoWriteL(const TDesC& aString)
       
   211 	{
       
   212 	TBuf8<KMaxLineLength> buf;
       
   213 	buf.Copy(aString.Left(KMaxLineLength));
       
   214 	User::LeaveIfError(iFile.Write(buf));
       
   215 	User::LeaveIfError(iFile.Flush()); // Commit data
       
   216 	}
       
   217 
       
   218 // ConsoleOutput ///////////////////////////////////////////////////////////////
       
   219 
       
   220 EXPORT_C ConsoleOutput::ConsoleOutput(CConsoleBase& aConsole) :
       
   221 	iConsole(aConsole)
       
   222 	{
       
   223 	}
       
   224 
       
   225 void ConsoleOutput::DoWriteL(const TDesC& aString)
       
   226 	{
       
   227 	iConsole.Printf(aString);
       
   228 	}
       
   229 
       
   230 // COutputTee //////////////////////////////////////////////////////////////////
       
   231 
       
   232 COutputTee::COutputTee()
       
   233 	{
       
   234 	}
       
   235 
       
   236 COutputTee::~COutputTee()
       
   237 	{
       
   238 	iChildren.ResetAndDestroy();
       
   239 	}
       
   240 
       
   241 void COutputTee::AddChildL(Output* aChild)
       
   242 	{
       
   243 	User::LeaveIfError(iChildren.Append(aChild));
       
   244 	}
       
   245 
       
   246 void COutputTee::DoWriteL(const TDesC& aString)
       
   247 	{
       
   248 	for (TInt i = 0 ; i < iChildren.Count() ; ++i)
       
   249 		{
       
   250 		iChildren[i]->writeString(aString);
       
   251 		}
       
   252 	}