os/kernelhwsrv/userlibandfileserver/fileserver/sfsrv/cl_ftext.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// f32\sfsrv\cl_ftext.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "cl_std.h"
sl@0
    19
sl@0
    20
sl@0
    21
sl@0
    22
sl@0
    23
EXPORT_C TFileText::TFileText()
sl@0
    24
/**
sl@0
    25
Default constructor.
sl@0
    26
*/
sl@0
    27
	{}
sl@0
    28
sl@0
    29
sl@0
    30
sl@0
    31
sl@0
    32
EXPORT_C void TFileText::Set(RFile& aFile)
sl@0
    33
/**
sl@0
    34
Sets the Unicode file to be read from, or written to.
sl@0
    35
sl@0
    36
This function must be called before 
sl@0
    37
Read(), Write() or Seek() can be used.
sl@0
    38
sl@0
    39
@param aFile The file to be used. Must be open.
sl@0
    40
sl@0
    41
@see TFileText::Read
sl@0
    42
@see TFileText::Write
sl@0
    43
@see TFileText::Seek
sl@0
    44
*/
sl@0
    45
	{
sl@0
    46
#ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
sl@0
    47
	iFile=aFile;
sl@0
    48
#else
sl@0
    49
	iFile=(RFile64&)aFile;
sl@0
    50
#endif
sl@0
    51
	iReadBuf.Zero();
sl@0
    52
	iNext=(TText*)iReadBuf.Ptr();
sl@0
    53
	iEnd=iNext;
sl@0
    54
#ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
sl@0
    55
	TInt pos = 0;
sl@0
    56
#else
sl@0
    57
	TInt64 pos = 0;
sl@0
    58
#endif
sl@0
    59
	iFile.Seek(ESeekCurrent,pos);
sl@0
    60
 	if (pos == 0)
sl@0
    61
 		iState = EStartOfFile;
sl@0
    62
 	else
sl@0
    63
 		iState = ENormal;
sl@0
    64
	}
sl@0
    65
sl@0
    66
sl@0
    67
sl@0
    68
sl@0
    69
EXPORT_C TInt TFileText::Read(TDes& aDes)
sl@0
    70
/**
sl@0
    71
Reads single line text record from a Unicode file into the specified descriptor.
sl@0
    72
sl@0
    73
The read operation begins at the current file position, and ends when
sl@0
    74
a line delimiter character is read.
sl@0
    75
sl@0
    76
If the maximum length of the descriptor is insufficient to hold the record, 
sl@0
    77
the function returns KErrTooBig and the descriptor is filled to its maximum 
sl@0
    78
length.
sl@0
    79
sl@0
    80
If Read() is called when the current position is the end of the file (that 
sl@0
    81
is, after the last line delimiter in the file), KErrEof is returned, and the 
sl@0
    82
length of the buffer is set to zero.
sl@0
    83
sl@0
    84
@param aDes On return, contains the single record read from the file. Any 
sl@0
    85
            previous contents are overwritten.
sl@0
    86
sl@0
    87
@return KErrNone if successful, otherwise one of the other system-wide error 
sl@0
    88
        codes.
sl@0
    89
*/
sl@0
    90
	{
sl@0
    91
sl@0
    92
	TText* pD=(TText*)aDes.Ptr();
sl@0
    93
	TInt len=aDes.MaxLength();
sl@0
    94
	TInt newLen=0;
sl@0
    95
	while (newLen<len)
sl@0
    96
		{
sl@0
    97
		if (iNext>=iEnd)
sl@0
    98
			{
sl@0
    99
			TInt r=FillBuffer();
sl@0
   100
			if (r!=KErrNone && r!=KErrEof)
sl@0
   101
				return(r);
sl@0
   102
			if (r==KErrEof)
sl@0
   103
				{
sl@0
   104
				aDes.SetLength(newLen);
sl@0
   105
				return(newLen ? KErrNone : KErrEof);
sl@0
   106
				}
sl@0
   107
			continue;
sl@0
   108
			}
sl@0
   109
		TBool terminate=newLen;
sl@0
   110
		TInt r=CheckForTerminator(terminate);
sl@0
   111
		if (r!=KErrNone || terminate)
sl@0
   112
			{
sl@0
   113
			aDes.SetLength(newLen);
sl@0
   114
			return(r);
sl@0
   115
			}
sl@0
   116
		*pD++=(*iNext++);
sl@0
   117
		newLen++;
sl@0
   118
		}
sl@0
   119
	aDes.SetLength(newLen);
sl@0
   120
	TBool terminate=newLen;
sl@0
   121
	TInt r=CheckForTerminator(terminate);
sl@0
   122
	if (r!=KErrNone || terminate)
sl@0
   123
		return(r);
sl@0
   124
	NextRecord();
sl@0
   125
	return(KErrTooBig);
sl@0
   126
	}
sl@0
   127
sl@0
   128
void TFileText::NextRecord()
sl@0
   129
//
sl@0
   130
// Move to the start of the next record
sl@0
   131
//
sl@0
   132
	{
sl@0
   133
sl@0
   134
	FOREVER
sl@0
   135
		{
sl@0
   136
		TBool terminate=EFalse;
sl@0
   137
		TInt r=CheckForTerminator(terminate);
sl@0
   138
		if (r!=KErrNone || terminate)
sl@0
   139
			return;
sl@0
   140
		iNext++;
sl@0
   141
		}
sl@0
   142
	}
sl@0
   143
sl@0
   144
static void SwapWords(TText* aStart,TInt aCount)
sl@0
   145
 	{
sl@0
   146
 	TUint8* p = (TUint8*)aStart;
sl@0
   147
 	while (aCount-- > 0)
sl@0
   148
 		{
sl@0
   149
 		TUint8 temp = *p;
sl@0
   150
 		*p = p[1];
sl@0
   151
 		p[1] = temp;
sl@0
   152
 		p += 2;
sl@0
   153
   		}
sl@0
   154
   	}
sl@0
   155
sl@0
   156
TInt TFileText::FillBuffer()
sl@0
   157
//
sl@0
   158
// Read the new data from the file
sl@0
   159
//
sl@0
   160
	{
sl@0
   161
	
sl@0
   162
	TInt r=iFile.Read(iReadBuf);
sl@0
   163
	if (r!=KErrNone)
sl@0
   164
		return(r);
sl@0
   165
	if (iReadBuf.Length()==0)
sl@0
   166
		return(KErrEof);
sl@0
   167
	iNext=(const TText*)iReadBuf.Ptr();
sl@0
   168
	iEnd=iNext+iReadBuf.Length()/sizeof(TText);
sl@0
   169
	 
sl@0
   170
 	// Use any leading byte order marker to determine endianness.
sl@0
   171
 	if (iState == EStartOfFile)
sl@0
   172
 		{
sl@0
   173
 		iState = ENormal;
sl@0
   174
sl@0
   175
 		// Ignore an ordinary byte order marker.
sl@0
   176
 		if (*iNext == 0xFEFF)
sl@0
   177
 			iNext++;
sl@0
   178
sl@0
   179
 		// Set the endianness state to 'reverse' if a reversed byte order marker is found.
sl@0
   180
 		else if (*iNext == 0xFFFE)
sl@0
   181
 			{
sl@0
   182
 			iNext++;
sl@0
   183
 			iState = EReverse;
sl@0
   184
 			}
sl@0
   185
 
sl@0
   186
 		if (iNext == iEnd)
sl@0
   187
 			return KErrEof;
sl@0
   188
 		}
sl@0
   189
 
sl@0
   190
 	if (iState == EReverse)
sl@0
   191
		SwapWords((TText*)iNext,(iEnd - iNext));
sl@0
   192
sl@0
   193
	return(KErrNone);
sl@0
   194
	}
sl@0
   195
sl@0
   196
TInt TFileText::CheckForTerminator(TBool& anAnswer)
sl@0
   197
//
sl@0
   198
// Return ETrue if the next char is a record terminator: PARAGRAPH SEPARATOR (U+2029), LINE SEPARATOR (U+2028),
sl@0
   199
// CR-LF (U+000D, U+000A), or LF (U+000A)
sl@0
   200
//
sl@0
   201
	{
sl@0
   202
sl@0
   203
	if (iNext>=iEnd)
sl@0
   204
		{
sl@0
   205
		TInt r=FillBuffer();
sl@0
   206
		if (r!=KErrNone)
sl@0
   207
			{
sl@0
   208
			if (r==KErrEof && anAnswer)
sl@0
   209
				return(KErrNone);
sl@0
   210
			return(r);
sl@0
   211
			}
sl@0
   212
		}
sl@0
   213
sl@0
   214
	anAnswer=EFalse;
sl@0
   215
	const TText* oldNext=iNext;
sl@0
   216
	TInt oldBufferLength=iReadBuf.Length();
sl@0
   217
	TText c=(*iNext);
sl@0
   218
	TBool peek=EFalse;
sl@0
   219
sl@0
   220
	// Check for unambiguous paragraph or line separator.
sl@0
   221
 	if (c == 0x2029 || c == 0x2028)
sl@0
   222
 		{
sl@0
   223
 		iNext++;
sl@0
   224
 		anAnswer = ETrue;
sl@0
   225
		return KErrNone;
sl@0
   226
 		}
sl@0
   227
 
sl@0
   228
 	// Check for CR-LF or LF.
sl@0
   229
 	if (c == 0x000D)
sl@0
   230
		{
sl@0
   231
		iNext++;
sl@0
   232
		if (iNext<iEnd)
sl@0
   233
			c=(*iNext);
sl@0
   234
		else
sl@0
   235
			{
sl@0
   236
			peek=ETrue;
sl@0
   237
			TInt r=FillBuffer();
sl@0
   238
			if (r!=KErrNone && r!=KErrEof)
sl@0
   239
				return(r);
sl@0
   240
			if (r==KErrNone)
sl@0
   241
				c=(*iNext);
sl@0
   242
			}
sl@0
   243
		}
sl@0
   244
sl@0
   245
	if (c == 0x000A)
sl@0
   246
		{
sl@0
   247
		iNext++;
sl@0
   248
		anAnswer=ETrue;
sl@0
   249
		return(KErrNone);
sl@0
   250
		}
sl@0
   251
sl@0
   252
	iNext=oldNext;
sl@0
   253
	if (!peek)
sl@0
   254
		return(KErrNone);
sl@0
   255
sl@0
   256
#ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
sl@0
   257
	TInt pos=(-1)*(oldBufferLength+iReadBuf.Length());
sl@0
   258
#else
sl@0
   259
	TInt64 pos=(-1)*(oldBufferLength+iReadBuf.Length());
sl@0
   260
#endif
sl@0
   261
	TInt r=iFile.Seek(ESeekCurrent,pos);
sl@0
   262
	if (r==KErrNone)
sl@0
   263
		r=FillBuffer();
sl@0
   264
	if (r!=KErrNone)
sl@0
   265
		return(r);
sl@0
   266
	iNext=oldNext;
sl@0
   267
	return(KErrNone);
sl@0
   268
	}
sl@0
   269
sl@0
   270
sl@0
   271
sl@0
   272
sl@0
   273
EXPORT_C TInt TFileText::Write(const TDesC& aDes)
sl@0
   274
/**
sl@0
   275
Writes the contents of a descriptor to the end of a Unicode file.
sl@0
   276
sl@0
   277
A line delimiter is appended to the descriptor, and the current file position
sl@0
   278
is set to the new end of file.
sl@0
   279
sl@0
   280
If the descriptor contains one or more paragraph delimiters, Read() will treat 
sl@0
   281
the contents of the descriptor as more than one record.
sl@0
   282
sl@0
   283
@param aDes The descriptor content to be appended to the file.
sl@0
   284
sl@0
   285
@return KErrNone if successful, otherwise one of the other system-wide error 
sl@0
   286
        codes.
sl@0
   287
        
sl@0
   288
@see TFileText::Read
sl@0
   289
*/
sl@0
   290
	{
sl@0
   291
sl@0
   292
	TInt r=Seek(ESeekEnd);
sl@0
   293
	if (r!=KErrNone)
sl@0
   294
		return(r);
sl@0
   295
	TPtrC8 writeBuf((const TUint8*)aDes.Ptr(),aDes.Size());
sl@0
   296
	r=iFile.Write(writeBuf);
sl@0
   297
	if (r!=KErrNone)
sl@0
   298
		return(r);
sl@0
   299
 	TText lf = 0x000A;
sl@0
   300
 	TPtrC8 lf8((const TUint8*)&lf,sizeof(TText));
sl@0
   301
 	r=iFile.Write(lf8);
sl@0
   302
	return(r);
sl@0
   303
	}
sl@0
   304
sl@0
   305
sl@0
   306
sl@0
   307
sl@0
   308
EXPORT_C TInt TFileText::Seek(TSeek aMode)
sl@0
   309
/**
sl@0
   310
Seeks to start or end of file.
sl@0
   311
sl@0
   312
It is only necessary to call this function before 
sl@0
   313
using Read() because Write() always seeks to the end of the file
sl@0
   314
before writing.
sl@0
   315
sl@0
   316
@param aMode ESeekStart to seek to the start of the file;
sl@0
   317
             ESeekEnd to seek to the end.
sl@0
   318
             
sl@0
   319
@return KErrNone if successful, otherwise one of the other system-wide error 
sl@0
   320
        codes.
sl@0
   321
sl@0
   322
@panic FSCLIENT 5 if aMode is neither ESeekStart nor ESeekEnd.
sl@0
   323
sl@0
   324
@see TFileText::Read
sl@0
   325
@see TFileText::Write
sl@0
   326
*/
sl@0
   327
	{
sl@0
   328
sl@0
   329
	__ASSERT_ALWAYS(aMode==ESeekStart || aMode==ESeekEnd,Panic(EFTextIllegalSeekMode));
sl@0
   330
#ifndef	SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
sl@0
   331
	TInt pos=0;
sl@0
   332
#else
sl@0
   333
	TInt64 pos = 0;
sl@0
   334
#endif
sl@0
   335
	TInt ret=iFile.Seek(aMode,pos);
sl@0
   336
 	if (ret == 0 && aMode == ESeekStart)
sl@0
   337
 		iState = EStartOfFile;
sl@0
   338
	iNext = (TText*)iReadBuf.Ptr();
sl@0
   339
 	iEnd = iNext;
sl@0
   340
	return ret;
sl@0
   341
	}
sl@0
   342