1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/boardsupport/haitest/bspsvs/suite/e32/src/T_ActiveCallbackIO.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,238 @@
1.4 +/*
1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +#include "T_ActiveCallbackIO.h"
1.22 +
1.23 +/*@{*/
1.24 +_LIT(KStrNull, " ");
1.25 +
1.26 +_LIT(KLogErrorFileNotFound, "File '%S' Not Found. Error %d");
1.27 +_LIT(KLogErrorFileNotOpened, "File '%S' Not Opened. Error %d");
1.28 +_LIT(KLogErrorFormatError, "Format Error");
1.29 +/*@}*/
1.30 +
1.31 +CT_ActiveCallbackIO* CT_ActiveCallbackIO::NewL(CDataWrapperBase& aCallback, TInt aPriority)
1.32 + {
1.33 + CT_ActiveCallbackIO* self=new(ELeave) CT_ActiveCallbackIO(aCallback, aPriority);
1.34 + CleanupStack::PushL(self);
1.35 + self->ConstructL();
1.36 + CleanupStack::Pop();
1.37 + return self;
1.38 + }
1.39 +
1.40 +CT_ActiveCallbackIO::~CT_ActiveCallbackIO()
1.41 + {
1.42 + delete iBuffer;
1.43 + iBuffer=NULL;
1.44 + }
1.45 +
1.46 +CT_ActiveCallbackIO::CT_ActiveCallbackIO(CDataWrapperBase& aCallback, TInt aPriority)
1.47 +: CActiveCallbackBase(aCallback, aPriority)
1.48 +, iBuffer(NULL)
1.49 + {
1.50 + }
1.51 +
1.52 +
1.53 +TInt CT_ActiveCallbackIO::BufferLength()
1.54 + {
1.55 + return iBuffer->Length();
1.56 + }
1.57 +
1.58 +const TDesC8& CT_ActiveCallbackIO::Buffer()
1.59 + {
1.60 + return *iBuffer;
1.61 + }
1.62 +
1.63 +void CT_ActiveCallbackIO::PrepareFromStringL(TInt aRepeat, const TDesC& aString)
1.64 + {
1.65 + delete iBuffer;
1.66 + iBuffer=NULL;
1.67 +
1.68 + TInt bufferLength=aString.Length()*aRepeat;
1.69 + iBuffer=HBufC8::NewL(bufferLength);
1.70 + iBuffer->Des().Copy(aString);
1.71 + ConvertAndRepeatBuffer(aRepeat);
1.72 + }
1.73 +
1.74 +void CT_ActiveCallbackIO::PrepareFromFileL(TInt aRepeat, const TDesC& aFilename)
1.75 + {
1.76 + delete iBuffer;
1.77 + iBuffer=NULL;
1.78 +
1.79 + TFindFile findFile(iDataWrapperBase.FileServer());
1.80 + TInt err=findFile.FindByDir(aFilename, KStrNull);
1.81 + if ( err!=KErrNone )
1.82 + {
1.83 + iDataWrapperBase.ERR_PRINTF3(KLogErrorFileNotFound, &aFilename, err);
1.84 + iDataWrapperBase.SetBlockResult(EFail);
1.85 + }
1.86 + else
1.87 + {
1.88 + const TDesC& fileName=findFile.File();
1.89 + RFile file;
1.90 + err=file.Open(iDataWrapperBase.FileServer(), fileName, EFileRead|EFileShareAny);
1.91 + if ( err!=KErrNone )
1.92 + {
1.93 + iDataWrapperBase.ERR_PRINTF3(KLogErrorFileNotOpened, &aFilename, err);
1.94 + iDataWrapperBase.SetBlockResult(EFail);
1.95 + }
1.96 + else
1.97 + {
1.98 + CleanupClosePushL(file);
1.99 + iDataWrapperBase.INFO_PRINTF1(_L("File Opened."));
1.100 +
1.101 + // get size
1.102 + TInt bufferLength;
1.103 + User::LeaveIfError(file.Size(bufferLength));
1.104 + bufferLength*=aRepeat;
1.105 + iDataWrapperBase.INFO_PRINTF2(_L("File Size=%d."), bufferLength);
1.106 +
1.107 + // read file
1.108 + iBuffer=HBufC8::NewL(bufferLength);
1.109 + iDataWrapperBase.INFO_PRINTF1(_L("Buffer created."));
1.110 +
1.111 + TPtr8 fileData = iBuffer->Des();
1.112 + User::LeaveIfError(file.Read(fileData));
1.113 + CleanupStack::PopAndDestroy(&file);
1.114 +
1.115 + ConvertAndRepeatBuffer(aRepeat);
1.116 + }
1.117 + }
1.118 + }
1.119 +
1.120 +void CT_ActiveCallbackIO::ConvertAndRepeatBuffer(TInt aRepeat)
1.121 + {
1.122 + ConvertEscapeChars(iBuffer->Des());
1.123 +
1.124 + TPtr8 ptr=iBuffer->Des();
1.125 +
1.126 + for ( TInt index=1; index<aRepeat; ++index )
1.127 + {
1.128 + iBuffer->Des().Append(ptr);
1.129 + }
1.130 + }
1.131 +
1.132 +void CT_ActiveCallbackIO::ConvertEscapeChars(TPtr8 aBuffer)
1.133 + {
1.134 + TInt ret=KErrNone;
1.135 + TInt length=aBuffer.Length();
1.136 + TInt position=0;
1.137 + TBool escapeFound=EFalse;
1.138 +
1.139 + for ( TInt index=0; index<length; ++index )
1.140 + {
1.141 + TChar nextChar=aBuffer[index];
1.142 + if ( escapeFound )
1.143 + {
1.144 + switch ( nextChar )
1.145 + {
1.146 + case 'x':
1.147 + {
1.148 + // Check that we have a further 2 characters
1.149 + if ( (index+2)<length )
1.150 + {
1.151 + ++index;
1.152 + TUint charCode = 0;
1.153 + TLex8 lex(aBuffer.Mid(index,2));
1.154 + ret = lex.Val(charCode ,EHex);
1.155 + aBuffer[position++]=TChar(charCode);
1.156 + ++index;
1.157 + nextChar = aBuffer[index];
1.158 + }
1.159 + else
1.160 + {
1.161 + ret=KErrArgument;
1.162 + }
1.163 + escapeFound=EFalse;
1.164 + }
1.165 + break;
1.166 + case 'n':
1.167 + // \n found newline
1.168 + {
1.169 + aBuffer[position++]='\n';
1.170 + escapeFound=EFalse;
1.171 + }
1.172 + break;
1.173 + case 't':
1.174 + // \t found horizontal tab
1.175 + {
1.176 + aBuffer[position++]='\t';
1.177 + escapeFound=EFalse;
1.178 + }
1.179 + break;
1.180 + case 'v':
1.181 + // \v found vertical tab
1.182 + {
1.183 + aBuffer[position++]='\v';
1.184 + escapeFound=EFalse;
1.185 + }
1.186 + break;
1.187 + case 'b':
1.188 + // \b found backspace
1.189 + {
1.190 + aBuffer[position++]='\b';
1.191 + escapeFound=EFalse;
1.192 + }
1.193 + break;
1.194 + case 'r':
1.195 + // \r found carriage return
1.196 + {
1.197 + aBuffer[position++]='\r';
1.198 + escapeFound=EFalse;
1.199 + }
1.200 + break;
1.201 + case 'f':
1.202 + // \f found form feed
1.203 + {
1.204 + aBuffer[position++]='\f';
1.205 + escapeFound=EFalse;
1.206 + }
1.207 + break;
1.208 + case 'a':
1.209 + // \a found audible alert (bell)
1.210 + {
1.211 + aBuffer[position++]='\a';
1.212 + escapeFound=EFalse;
1.213 + }
1.214 + break;
1.215 + default:
1.216 + aBuffer[position++]=nextChar;
1.217 + escapeFound=EFalse;
1.218 + break;
1.219 + }
1.220 + }
1.221 + else
1.222 + {
1.223 + if ( aBuffer[index]=='\\' )
1.224 + {
1.225 + escapeFound=ETrue;
1.226 + }
1.227 + else
1.228 + {
1.229 + aBuffer[position++]=aBuffer[index];
1.230 + }
1.231 + }
1.232 + }
1.233 +
1.234 + if ( ret!=KErrNone )
1.235 + {
1.236 + iDataWrapperBase.ERR_PRINTF1(KLogErrorFormatError);
1.237 + iDataWrapperBase.SetBlockResult(EFail);
1.238 + }
1.239 +
1.240 + aBuffer.SetLength(position);
1.241 + }