First public contribution.
1 // Copyright (c) 1996-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 the License "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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32\sfsrv\cl_ftext.cpp
23 EXPORT_C TFileText::TFileText()
32 EXPORT_C void TFileText::Set(RFile& aFile)
34 Sets the Unicode file to be read from, or written to.
36 This function must be called before
37 Read(), Write() or Seek() can be used.
39 @param aFile The file to be used. Must be open.
46 #ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
49 iFile=(RFile64&)aFile;
52 iNext=(TText*)iReadBuf.Ptr();
54 #ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
59 iFile.Seek(ESeekCurrent,pos);
61 iState = EStartOfFile;
69 EXPORT_C TInt TFileText::Read(TDes& aDes)
71 Reads single line text record from a Unicode file into the specified descriptor.
73 The read operation begins at the current file position, and ends when
74 a line delimiter character is read.
76 If the maximum length of the descriptor is insufficient to hold the record,
77 the function returns KErrTooBig and the descriptor is filled to its maximum
80 If Read() is called when the current position is the end of the file (that
81 is, after the last line delimiter in the file), KErrEof is returned, and the
82 length of the buffer is set to zero.
84 @param aDes On return, contains the single record read from the file. Any
85 previous contents are overwritten.
87 @return KErrNone if successful, otherwise one of the other system-wide error
92 TText* pD=(TText*)aDes.Ptr();
93 TInt len=aDes.MaxLength();
100 if (r!=KErrNone && r!=KErrEof)
104 aDes.SetLength(newLen);
105 return(newLen ? KErrNone : KErrEof);
109 TBool terminate=newLen;
110 TInt r=CheckForTerminator(terminate);
111 if (r!=KErrNone || terminate)
113 aDes.SetLength(newLen);
119 aDes.SetLength(newLen);
120 TBool terminate=newLen;
121 TInt r=CheckForTerminator(terminate);
122 if (r!=KErrNone || terminate)
128 void TFileText::NextRecord()
130 // Move to the start of the next record
136 TBool terminate=EFalse;
137 TInt r=CheckForTerminator(terminate);
138 if (r!=KErrNone || terminate)
144 static void SwapWords(TText* aStart,TInt aCount)
146 TUint8* p = (TUint8*)aStart;
156 TInt TFileText::FillBuffer()
158 // Read the new data from the file
162 TInt r=iFile.Read(iReadBuf);
165 if (iReadBuf.Length()==0)
167 iNext=(const TText*)iReadBuf.Ptr();
168 iEnd=iNext+iReadBuf.Length()/sizeof(TText);
170 // Use any leading byte order marker to determine endianness.
171 if (iState == EStartOfFile)
175 // Ignore an ordinary byte order marker.
176 if (*iNext == 0xFEFF)
179 // Set the endianness state to 'reverse' if a reversed byte order marker is found.
180 else if (*iNext == 0xFFFE)
190 if (iState == EReverse)
191 SwapWords((TText*)iNext,(iEnd - iNext));
196 TInt TFileText::CheckForTerminator(TBool& anAnswer)
198 // Return ETrue if the next char is a record terminator: PARAGRAPH SEPARATOR (U+2029), LINE SEPARATOR (U+2028),
199 // CR-LF (U+000D, U+000A), or LF (U+000A)
208 if (r==KErrEof && anAnswer)
215 const TText* oldNext=iNext;
216 TInt oldBufferLength=iReadBuf.Length();
220 // Check for unambiguous paragraph or line separator.
221 if (c == 0x2029 || c == 0x2028)
228 // Check for CR-LF or LF.
238 if (r!=KErrNone && r!=KErrEof)
256 #ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
257 TInt pos=(-1)*(oldBufferLength+iReadBuf.Length());
259 TInt64 pos=(-1)*(oldBufferLength+iReadBuf.Length());
261 TInt r=iFile.Seek(ESeekCurrent,pos);
273 EXPORT_C TInt TFileText::Write(const TDesC& aDes)
275 Writes the contents of a descriptor to the end of a Unicode file.
277 A line delimiter is appended to the descriptor, and the current file position
278 is set to the new end of file.
280 If the descriptor contains one or more paragraph delimiters, Read() will treat
281 the contents of the descriptor as more than one record.
283 @param aDes The descriptor content to be appended to the file.
285 @return KErrNone if successful, otherwise one of the other system-wide error
292 TInt r=Seek(ESeekEnd);
295 TPtrC8 writeBuf((const TUint8*)aDes.Ptr(),aDes.Size());
296 r=iFile.Write(writeBuf);
300 TPtrC8 lf8((const TUint8*)&lf,sizeof(TText));
308 EXPORT_C TInt TFileText::Seek(TSeek aMode)
310 Seeks to start or end of file.
312 It is only necessary to call this function before
313 using Read() because Write() always seeks to the end of the file
316 @param aMode ESeekStart to seek to the start of the file;
317 ESeekEnd to seek to the end.
319 @return KErrNone if successful, otherwise one of the other system-wide error
322 @panic FSCLIENT 5 if aMode is neither ESeekStart nor ESeekEnd.
325 @see TFileText::Write
329 __ASSERT_ALWAYS(aMode==ESeekStart || aMode==ESeekEnd,Panic(EFTextIllegalSeekMode));
330 #ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
335 TInt ret=iFile.Seek(aMode,pos);
336 if (ret == 0 && aMode == ESeekStart)
337 iState = EStartOfFile;
338 iNext = (TText*)iReadBuf.Ptr();