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