filehandling/htmltorichtextconverter/src/CHtmlToCrtConvBuffer.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "CHtmlToCrtConvBuffer.h"
       
    17 #include "CHtmlToCrtConvAssertDefines.h"
       
    18 
       
    19 const TInt KReadBufferSize = 256;
       
    20 
       
    21 CHtmlToCrtConvBuffer::CHtmlToCrtConvBuffer(CCnvCharacterSetConverter& aCnvCharacterSetConverter)
       
    22 :iCnvCharacterSetConverter(aCnvCharacterSetConverter)
       
    23 	{
       
    24 	}
       
    25 
       
    26 CHtmlToCrtConvBuffer* CHtmlToCrtConvBuffer::NewL(const TDesC& aFile, RFs& aFsSession, CCnvCharacterSetConverter& aCnvCharacterSetConverter)
       
    27 	{
       
    28 	CHtmlToCrtConvBuffer* self=new(ELeave) CHtmlToCrtConvBuffer(aCnvCharacterSetConverter);
       
    29 	CleanupStack::PushL(self);
       
    30 	self->ConstructL(aFile, aFsSession);
       
    31 	CleanupStack::Pop(self);
       
    32 	return self;
       
    33 	}
       
    34 
       
    35 void CHtmlToCrtConvBuffer::ConstructL(const TDesC& aFile, RFs& aFsSession)
       
    36 	{
       
    37 	User::LeaveIfError(iFile.Open(aFsSession, aFile, EFileShareReadersOnly));
       
    38 	iSourceBuffer=HBufC8::NewL(KReadBufferSize);
       
    39 	iUnicodeBuffer=HBufC16::NewL(KReadBufferSize);
       
    40 	}
       
    41 
       
    42 CHtmlToCrtConvBuffer::~CHtmlToCrtConvBuffer()
       
    43 	{
       
    44 	iFile.Close();
       
    45 	delete iSourceBuffer;
       
    46 	delete iUnicodeBuffer;
       
    47 	}
       
    48 
       
    49 //===================================================================================================
       
    50 //ReadCharacterL - returns EFalse if no character read, ie when end of file and end of buffer reached
       
    51 //===================================================================================================
       
    52 TBool CHtmlToCrtConvBuffer::ReadCharacterL(TChar& aCharacter, TInt& aBufferPosition, TBool& aEndOfBuffer)
       
    53 	{
       
    54 	ASSERT((iBufferPosition>=0 && iBufferPosition<=KReadBufferSize));
       
    55 
       
    56 	aEndOfBuffer=EFalse;
       
    57 	if (iBufferPosition < iUnicodeBuffer->Length())
       
    58 		//character in buffer
       
    59 		{
       
    60 		aBufferPosition=iBufferPosition;
       
    61 		aCharacter=(*iUnicodeBuffer)[iBufferPosition++];
       
    62 		if (iBufferPosition==iUnicodeBuffer->Length())
       
    63 			{
       
    64 			aEndOfBuffer=ETrue;
       
    65 			}
       
    66 		return ETrue;
       
    67 		}
       
    68 	else
       
    69 		//no characters remaining in buffer
       
    70 		{
       
    71 		aBufferPosition=0;
       
    72 		if (FillBufferL())
       
    73 			//file was not empty
       
    74 			{
       
    75 			aCharacter=(*iUnicodeBuffer)[iBufferPosition++];
       
    76 			if (iBufferPosition==iUnicodeBuffer->Length())
       
    77 				{
       
    78 				aEndOfBuffer=ETrue;
       
    79 				}
       
    80 			return ETrue;
       
    81 			}
       
    82 		else
       
    83 			//file was empty
       
    84 			{
       
    85 			aEndOfBuffer=ETrue;
       
    86 			return EFalse;
       
    87 			}
       
    88 		}
       
    89 	}
       
    90 
       
    91 void CHtmlToCrtConvBuffer::GetPartOfBufferL(TPtrC16& aPartOfBuffer, TInt aStartPosition, TInt aEndPosition) const
       
    92 	{
       
    93 	TPtrC16 partOfBuffer(iUnicodeBuffer->Mid(aStartPosition,(aEndPosition - aStartPosition + 1)));
       
    94 	aPartOfBuffer.Set(partOfBuffer);
       
    95 	}
       
    96 
       
    97 void CHtmlToCrtConvBuffer::GetToEndOfBufferL(TPtrC16& aPartOfBuffer, TInt aStartPosition) const
       
    98 	{
       
    99 	TPtrC16 partOfBuffer(iUnicodeBuffer->Mid(aStartPosition));
       
   100 	aPartOfBuffer.Set(partOfBuffer);
       
   101 	}
       
   102 
       
   103 void CHtmlToCrtConvBuffer::GetSampleOfTextFromFileL(TDes8& aSample, TInt aLength, TInt aOffset)
       
   104 	{
       
   105 	TInt fileSize=0;
       
   106 	User::LeaveIfError(iFile.Size(fileSize));
       
   107 
       
   108 	//adjust aOffset - used to get sample of text from file, so should read aLength chars
       
   109 	if(fileSize<aLength)
       
   110 		aOffset=0;
       
   111 	else if(fileSize<aOffset+aLength)
       
   112 		aOffset=fileSize-aLength;
       
   113 
       
   114 	User::LeaveIfError(iFile.Read(aOffset, aSample, aLength));
       
   115 	ResetL();
       
   116 	}
       
   117 
       
   118 void CHtmlToCrtConvBuffer::ResetL()
       
   119 	{
       
   120 	//set read file position to start of file
       
   121 	TInt pos=0;
       
   122 	User::LeaveIfError(iFile.Seek(ESeekStart, pos));
       
   123 	}
       
   124 
       
   125 TBool CHtmlToCrtConvBuffer::FillBufferL()
       
   126 	{
       
   127 	iBufferPosition = 0;
       
   128 	//iSourceBuffer contains any remaining unconverted characters from last call to ConvertToUnicode.
       
   129 	TPtr8 sourcePtr(iSourceBuffer->Des());
       
   130 	TInt sourceLength=iSourceBuffer->Length();
       
   131 	TBool endOfFileReached=EFalse;
       
   132 
       
   133 	//newBuffer gets characters from the file to fill the remaining space in iSourceBuffer
       
   134 	HBufC8* newBuffer=HBufC8::NewLC(KReadBufferSize-sourceLength);
       
   135 	TPtr8 newBufferPtr(newBuffer->Des());
       
   136 	User::LeaveIfError(iFile.Read(newBufferPtr, KReadBufferSize-sourceLength));
       
   137 	sourcePtr.Append(newBufferPtr);
       
   138 	CleanupStack::PopAndDestroy(newBuffer);
       
   139 
       
   140 	sourceLength=sourcePtr.Length();
       
   141 	if(sourceLength < KReadBufferSize)
       
   142 		{
       
   143 		endOfFileReached=ETrue;
       
   144 		if(sourcePtr.Length()==0)
       
   145 			{
       
   146 			return EFalse;
       
   147 			}
       
   148 		}
       
   149 
       
   150 	TPtr16 unicodePtr(iUnicodeBuffer->Des());
       
   151 	unicodePtr.Zero();
       
   152 
       
   153 	DoConvertBufferL(sourcePtr, unicodePtr, sourceLength, endOfFileReached);
       
   154 
       
   155 	return ETrue;
       
   156 	}
       
   157 
       
   158 //=============================================================================
       
   159 //DoConvertBufferL - sets iUnicodeBuffer to contain the converted characters,
       
   160 //and iSourceBuffer to contain only the unconverted characters.
       
   161 //=============================================================================
       
   162 void CHtmlToCrtConvBuffer::DoConvertBufferL(TPtr8& aSourcePtr, TPtr16& aUnicodePtr, TInt& aSourceLength, TBool& aEndOfFile)
       
   163 	{
       
   164 	HBufC16* tempUnicode=HBufC16::NewLC(aSourceLength);
       
   165 	TPtr16 tempUnicodePtr(tempUnicode->Des());
       
   166 
       
   167 	//returnValue = number of unconverted characters at end of buffer
       
   168 	TInt returnValue=iCnvCharacterSetConverter.ConvertToUnicode(tempUnicodePtr, aSourcePtr, iState);
       
   169 
       
   170 	if(!aEndOfFile)
       
   171 		{
       
   172 		if(returnValue==CCnvCharacterSetConverter::EErrorIllFormedInput)
       
   173 			User::Leave(KErrCorrupt);
       
   174 		}
       
   175 	if(returnValue<0)//future-proof against 'TError' expanding
       
   176 		User::Leave(KErrGeneral);
       
   177 
       
   178 	aUnicodePtr.Append(tempUnicodePtr);
       
   179 	CleanupStack::PopAndDestroy(tempUnicode);
       
   180 	
       
   181 	//set iSourceBuffer to contain only the unconverted characters
       
   182 	aSourcePtr.Copy(aSourcePtr.Right(returnValue));
       
   183 	aSourceLength=aSourcePtr.Length();
       
   184 	}