os/boardsupport/haitest/bspsvs/suite/e32/src/T_ActiveCallbackIO.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-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 "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 #include "T_ActiveCallbackIO.h"
    19 
    20 /*@{*/
    21 _LIT(KStrNull,								" ");
    22 
    23 _LIT(KLogErrorFileNotFound,					"File '%S' Not Found. Error %d");
    24 _LIT(KLogErrorFileNotOpened,				"File '%S' Not Opened. Error %d");
    25 _LIT(KLogErrorFormatError,					"Format Error");
    26 /*@}*/
    27 
    28 CT_ActiveCallbackIO* CT_ActiveCallbackIO::NewL(CDataWrapperBase& aCallback, TInt aPriority)
    29 	{
    30 	CT_ActiveCallbackIO*	self=new(ELeave) CT_ActiveCallbackIO(aCallback, aPriority);
    31 	CleanupStack::PushL(self);
    32 	self->ConstructL();
    33 	CleanupStack::Pop();
    34 	return self;
    35 	}
    36 
    37 CT_ActiveCallbackIO::~CT_ActiveCallbackIO()
    38 	{
    39 	delete iBuffer;
    40 	iBuffer=NULL;
    41 	}
    42 
    43 CT_ActiveCallbackIO::CT_ActiveCallbackIO(CDataWrapperBase& aCallback, TInt aPriority)
    44 :	CActiveCallbackBase(aCallback, aPriority)
    45 ,	iBuffer(NULL)
    46 	{
    47 	}
    48 
    49 
    50 TInt CT_ActiveCallbackIO::BufferLength()
    51 	{
    52 	return iBuffer->Length();
    53 	}
    54 
    55 const TDesC8& CT_ActiveCallbackIO::Buffer()
    56 	{
    57 	return *iBuffer;
    58 	}
    59 
    60 void CT_ActiveCallbackIO::PrepareFromStringL(TInt aRepeat, const TDesC& aString)
    61 	{
    62 	delete iBuffer;
    63 	iBuffer=NULL;
    64 
    65 	TInt	bufferLength=aString.Length()*aRepeat;
    66 	iBuffer=HBufC8::NewL(bufferLength);
    67 	iBuffer->Des().Copy(aString);
    68 	ConvertAndRepeatBuffer(aRepeat);
    69 	}
    70 
    71 void CT_ActiveCallbackIO::PrepareFromFileL(TInt aRepeat, const TDesC& aFilename)
    72 	{
    73 	delete iBuffer;
    74 	iBuffer=NULL;
    75 
    76 	TFindFile	findFile(iDataWrapperBase.FileServer());
    77 	TInt		err=findFile.FindByDir(aFilename, KStrNull);
    78 	if ( err!=KErrNone )
    79 		{
    80 		iDataWrapperBase.ERR_PRINTF3(KLogErrorFileNotFound, &aFilename, err);
    81 		iDataWrapperBase.SetBlockResult(EFail);
    82 		}
    83 	else
    84 		{
    85 		const TDesC&	fileName=findFile.File();
    86 		RFile			file;
    87 		err=file.Open(iDataWrapperBase.FileServer(), fileName, EFileRead|EFileShareAny);
    88 		if ( err!=KErrNone )
    89 			{
    90 			iDataWrapperBase.ERR_PRINTF3(KLogErrorFileNotOpened, &aFilename, err);
    91 			iDataWrapperBase.SetBlockResult(EFail);
    92 			}
    93 		else
    94 			{
    95 			CleanupClosePushL(file);
    96 			iDataWrapperBase.INFO_PRINTF1(_L("File Opened."));
    97 
    98 			// get size
    99 			TInt	bufferLength;
   100 			User::LeaveIfError(file.Size(bufferLength));
   101 			bufferLength*=aRepeat;
   102 			iDataWrapperBase.INFO_PRINTF2(_L("File Size=%d."), bufferLength);
   103 
   104 			// read file
   105 			iBuffer=HBufC8::NewL(bufferLength);
   106 			iDataWrapperBase.INFO_PRINTF1(_L("Buffer created."));
   107 
   108 			TPtr8	fileData = iBuffer->Des();
   109 			User::LeaveIfError(file.Read(fileData));
   110 			CleanupStack::PopAndDestroy(&file);
   111 
   112 			ConvertAndRepeatBuffer(aRepeat);
   113 			}
   114 		}
   115 	}
   116 
   117 void CT_ActiveCallbackIO::ConvertAndRepeatBuffer(TInt aRepeat)
   118 	{
   119 	ConvertEscapeChars(iBuffer->Des());
   120 
   121 	TPtr8	ptr=iBuffer->Des();
   122 
   123 	for ( TInt index=1; index<aRepeat; ++index )
   124 		{
   125 		iBuffer->Des().Append(ptr);
   126 		}
   127 	}
   128 
   129 void CT_ActiveCallbackIO::ConvertEscapeChars(TPtr8 aBuffer)
   130 	{
   131 	TInt	ret=KErrNone;
   132 	TInt	length=aBuffer.Length();
   133 	TInt	position=0;
   134 	TBool	escapeFound=EFalse;
   135 
   136 	for ( TInt index=0; index<length; ++index )
   137 		{
   138 		TChar	nextChar=aBuffer[index];
   139 		if ( escapeFound )
   140 			{
   141 			switch ( nextChar )
   142 				{
   143 			case 'x':
   144 				{
   145 				//	Check that we have a further 2 characters
   146 				if ( (index+2)<length )
   147 					{
   148 					++index;
   149 					TUint charCode = 0;
   150 					TLex8 lex(aBuffer.Mid(index,2));
   151 					ret = lex.Val(charCode ,EHex);
   152 					aBuffer[position++]=TChar(charCode);
   153 					++index;
   154 					nextChar = aBuffer[index];
   155 					}
   156 				else
   157 					{
   158 					ret=KErrArgument;
   159 					}
   160 				escapeFound=EFalse;
   161 				}
   162 				break;
   163 			case 'n':
   164 				//	\n found newline
   165 				{
   166 				aBuffer[position++]='\n';
   167 				escapeFound=EFalse;
   168 				}
   169 				break;
   170 			case 't':
   171 				//	\t found horizontal tab
   172 				{
   173 				aBuffer[position++]='\t';
   174 				escapeFound=EFalse;
   175 				}
   176 				break;
   177 			case 'v':
   178 				//	\v found vertical tab
   179 				{
   180 				aBuffer[position++]='\v';
   181 				escapeFound=EFalse;
   182 				}
   183 				break;
   184 			case 'b':
   185 				//	\b found backspace
   186 				{
   187 				aBuffer[position++]='\b';
   188 				escapeFound=EFalse;
   189 				}
   190 				break;
   191 			case 'r':
   192 				//	\r found carriage return
   193 				{
   194 				aBuffer[position++]='\r';
   195 				escapeFound=EFalse;
   196 				}
   197 				break;
   198 			case 'f':
   199 				//	\f found form feed
   200 				{
   201 				aBuffer[position++]='\f';
   202 				escapeFound=EFalse;
   203 				}
   204 				break;
   205 			case 'a':
   206 				//	\a found audible alert (bell)
   207 				{
   208 				aBuffer[position++]='\a';
   209 				escapeFound=EFalse;
   210 				}
   211 				break;
   212 			default:
   213 				aBuffer[position++]=nextChar;
   214 				escapeFound=EFalse;
   215 				break;
   216 				}
   217 			}
   218 		else
   219 			{
   220 			if ( aBuffer[index]=='\\' )
   221 				{
   222 				escapeFound=ETrue;
   223 				}
   224 			else
   225 				{
   226 				aBuffer[position++]=aBuffer[index];
   227 				}
   228 			}
   229 		}
   230 
   231 	if ( ret!=KErrNone )
   232 		{
   233 		iDataWrapperBase.ERR_PRINTF1(KLogErrorFormatError);
   234 		iDataWrapperBase.SetBlockResult(EFail);
   235 		}
   236 
   237 	aBuffer.SetLength(position);
   238 	}