diff -r 000000000000 -r a41df078684a userlibandfileserver/fileserver/sfsrv/cl_ftext.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/userlibandfileserver/fileserver/sfsrv/cl_ftext.cpp Mon Oct 19 15:55:17 2009 +0100 @@ -0,0 +1,342 @@ +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of the License "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// f32\sfsrv\cl_ftext.cpp +// +// + +#include "cl_std.h" + + + + +EXPORT_C TFileText::TFileText() +/** +Default constructor. +*/ + {} + + + + +EXPORT_C void TFileText::Set(RFile& aFile) +/** +Sets the Unicode file to be read from, or written to. + +This function must be called before +Read(), Write() or Seek() can be used. + +@param aFile The file to be used. Must be open. + +@see TFileText::Read +@see TFileText::Write +@see TFileText::Seek +*/ + { +#ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API + iFile=aFile; +#else + iFile=(RFile64&)aFile; +#endif + iReadBuf.Zero(); + iNext=(TText*)iReadBuf.Ptr(); + iEnd=iNext; +#ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API + TInt pos = 0; +#else + TInt64 pos = 0; +#endif + iFile.Seek(ESeekCurrent,pos); + if (pos == 0) + iState = EStartOfFile; + else + iState = ENormal; + } + + + + +EXPORT_C TInt TFileText::Read(TDes& aDes) +/** +Reads single line text record from a Unicode file into the specified descriptor. + +The read operation begins at the current file position, and ends when +a line delimiter character is read. + +If the maximum length of the descriptor is insufficient to hold the record, +the function returns KErrTooBig and the descriptor is filled to its maximum +length. + +If Read() is called when the current position is the end of the file (that +is, after the last line delimiter in the file), KErrEof is returned, and the +length of the buffer is set to zero. + +@param aDes On return, contains the single record read from the file. Any + previous contents are overwritten. + +@return KErrNone if successful, otherwise one of the other system-wide error + codes. +*/ + { + + TText* pD=(TText*)aDes.Ptr(); + TInt len=aDes.MaxLength(); + TInt newLen=0; + while (newLen=iEnd) + { + TInt r=FillBuffer(); + if (r!=KErrNone && r!=KErrEof) + return(r); + if (r==KErrEof) + { + aDes.SetLength(newLen); + return(newLen ? KErrNone : KErrEof); + } + continue; + } + TBool terminate=newLen; + TInt r=CheckForTerminator(terminate); + if (r!=KErrNone || terminate) + { + aDes.SetLength(newLen); + return(r); + } + *pD++=(*iNext++); + newLen++; + } + aDes.SetLength(newLen); + TBool terminate=newLen; + TInt r=CheckForTerminator(terminate); + if (r!=KErrNone || terminate) + return(r); + NextRecord(); + return(KErrTooBig); + } + +void TFileText::NextRecord() +// +// Move to the start of the next record +// + { + + FOREVER + { + TBool terminate=EFalse; + TInt r=CheckForTerminator(terminate); + if (r!=KErrNone || terminate) + return; + iNext++; + } + } + +static void SwapWords(TText* aStart,TInt aCount) + { + TUint8* p = (TUint8*)aStart; + while (aCount-- > 0) + { + TUint8 temp = *p; + *p = p[1]; + p[1] = temp; + p += 2; + } + } + +TInt TFileText::FillBuffer() +// +// Read the new data from the file +// + { + + TInt r=iFile.Read(iReadBuf); + if (r!=KErrNone) + return(r); + if (iReadBuf.Length()==0) + return(KErrEof); + iNext=(const TText*)iReadBuf.Ptr(); + iEnd=iNext+iReadBuf.Length()/sizeof(TText); + + // Use any leading byte order marker to determine endianness. + if (iState == EStartOfFile) + { + iState = ENormal; + + // Ignore an ordinary byte order marker. + if (*iNext == 0xFEFF) + iNext++; + + // Set the endianness state to 'reverse' if a reversed byte order marker is found. + else if (*iNext == 0xFFFE) + { + iNext++; + iState = EReverse; + } + + if (iNext == iEnd) + return KErrEof; + } + + if (iState == EReverse) + SwapWords((TText*)iNext,(iEnd - iNext)); + + return(KErrNone); + } + +TInt TFileText::CheckForTerminator(TBool& anAnswer) +// +// Return ETrue if the next char is a record terminator: PARAGRAPH SEPARATOR (U+2029), LINE SEPARATOR (U+2028), +// CR-LF (U+000D, U+000A), or LF (U+000A) +// + { + + if (iNext>=iEnd) + { + TInt r=FillBuffer(); + if (r!=KErrNone) + { + if (r==KErrEof && anAnswer) + return(KErrNone); + return(r); + } + } + + anAnswer=EFalse; + const TText* oldNext=iNext; + TInt oldBufferLength=iReadBuf.Length(); + TText c=(*iNext); + TBool peek=EFalse; + + // Check for unambiguous paragraph or line separator. + if (c == 0x2029 || c == 0x2028) + { + iNext++; + anAnswer = ETrue; + return KErrNone; + } + + // Check for CR-LF or LF. + if (c == 0x000D) + { + iNext++; + if (iNext