ReaderWriter Class Reference
 
 
 
ReaderWriter Class Reference

This reference page is linked to from the following overview topics: Unicode.


#include <maxtextfile.h>

Inheritance diagram for ReaderWriter:
BaseTextReader BaseTextWriter MaxHeapOperators MaxHeapOperators

Class Description

This class is used to access text file with both reader and writer functionality.

See also:
Reader class when file IO requirements are only for reading.
Writer class when file IO requirements are only for writing.

Public Member Functions

  ReaderWriter ()
  Default Constructor.
virtual  ~ReaderWriter ()
  Destructor.
bool  Open (const MCHAR *fileName, unsigned int streamMode, unsigned int encoding=0, LineEndMode mode=Text)
  Open a file using a file name.
bool  Open (const MaxSDK::Util::MaxString &fileName, unsigned int streamMode, unsigned int encoding=0, LineEndMode mode=Text)
  Open a file using a file name.
void  Close ()
  Close the opened file.
void  Flush ()
  Flush to file the cached data.
virtual size_t  Seek (long offset, int origin)
  Seek inside the file.
virtual Char  ReadChar (bool peek=false) const
  Read a character from the file.
virtual unsigned int  ReadCharUTF32 (bool peek=false) const
  Read a char in UTF32 encoding.
virtual MaxString  ReadChars (size_t nchars) const
  Reads characters from the file.
virtual MaxString  ReadLine (size_t nchars=(size_t)-1, bool dontReturnEOL=false) const
  Read a line from the file.
virtual bool  IsEndOfFile () const
  Used to determine if we read the end of file.
virtual size_t  Position () const
  used to determine the current position in the file
virtual size_t  LineNumber () const
  Used to obtain the current line number.
virtual size_t  UnreadChar (const Char &c)
  Unread the passed character.
virtual size_t  Write (const char *string, size_t nchars=(size_t)-1)
  Write string to file.
virtual size_t  Write (const wchar_t *string, size_t nchars=(size_t)-1)
  Write string to file.
virtual size_t  Write (const MaxString &string)
  Write string to file.
virtual bool  CanWrite () const
  Determine if we can write to the file.
virtual bool  IsFileOpen () const
  Returns true if file is open.
void  EnsureAllocated (size_t s)

Protected Member Functions

bool  Open (BinaryStream *stream, unsigned int streamMode, unsigned int encoding, LineEndMode mode=Text, bool closeOnDelete=true)
  Open a file or stream for both read and write actions.
size_t  SeekToAbsolute (long offset)
  Used to seek to a position in the file.
size_t  SeekToEnd (long offset=0)
  Used to seek to end.
size_t  SeekFromCurrent (long offset)
  Advance "offset" characters.
virtual size_t  Unread (const MaxString &string)
  Unread the passed string.

Protected Attributes

BinaryStream _stream
bool  _closeOnDelete
unsigned int  _streamMode
LineEndMode  _endOfLineMode
unsigned int  _encoding
unsigned int *  _chars
size_t  _allocated
size_t  _len
size_t  _pos
bool  _dirty

Friends

class  CharBinaryStream

Constructor & Destructor Documentation

Default Constructor.

virtual ~ReaderWriter ( ) [virtual]

Destructor.


Member Function Documentation

bool Open ( BinaryStream stream,
unsigned int  streamMode,
unsigned int  encoding,
LineEndMode  mode = Text,
bool  closeOnDelete = true 
) [protected]

Open a file or stream for both read and write actions.

Parameters:
[in,out] stream If non-null, the stream.
encoding The file encoding or preferred encoding
streamMode Streaming mode
See also:
BinaryStreamMode
Parameters:
LineEndMode Text file end of line termination mode. Default Text
closeOnDelete true to close on delete. Default true
Returns:
true if it succeeds, false if it fails.
size_t SeekToAbsolute ( long  offset ) [protected]

Used to seek to a position in the file.

Parameters:
nchars Position we which we want to position the cursor.
Returns:
The current position in the file
size_t SeekToEnd ( long  offset = 0 ) [protected]

Used to seek to end.

Parameters:
nchars The nchars.
Returns:
The current position in the file
size_t SeekFromCurrent ( long  offset ) [protected]

Advance "offset" characters.

Returns:
Returns the absolute position of the text file. (in chars)
virtual size_t Unread ( const MaxString string ) [protected, virtual]

Unread the passed string.

Parameters:
string The string that will be put back into the buffer
Returns:
size in nb of characters that has been written
bool Open ( const MCHAR *  fileName,
unsigned int  streamMode,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Open a file using a file name.

Parameters:
fileName File name to open. If file does not exist, it will be created.
streamMode Streaming mode
See also:
BinaryStreamMode
Parameters:
encoding This parameter can contains hint to the detection algorithm. Acceptable values are all codepages numbers that are recognized by Windows.

In addition to that, you can also specify FAVOR_UTF8. It can be used to cascade the detection of the codepage. For example, if you specify "CP_ACP | FAVOR_UTF8", the detection algorithm will treat any non-UTF8 data as ACP.

See also:
TextFileReaderEncoding
Parameters:
mode
See also:
LineEndMode - default Text
Returns:
true if successful, false otherwise
bool Open ( const MaxSDK::Util::MaxString fileName,
unsigned int  streamMode,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Open a file using a file name.

Parameters:
fileName File name to open. If file does not exist, it will be created.
streamMode Streaming mode
See also:
BinaryStreamMode
Parameters:
encoding This parameter can contains hint to the detection algorithm. Acceptable values are all codepages numbers that are recognized by Windows.

In addition to that, you can also specify FAVOR_UTF8. It can be used to cascade the detection of the codepage. For example, if you specify "CP_ACP | FAVOR_UTF8", the detection algorithm will treat any non-UTF8 data as ACP.

See also:
TextFileReaderEncoding
Parameters:
mode
See also:
LineEndMode - default Text
Returns:
true if successful, false otherwise
void Close ( )

Close the opened file.

void Flush ( ) [virtual]

Flush to file the cached data.

Implements BaseTextWriter.

virtual size_t Seek ( long  offset,
int  origin 
) [virtual]

Seek inside the file.

Parameters:
offset The seeks operations are done in number of characters (not bytes).
origin The direction to move. Origin can be one of the following * SEEK_CUR Current position of file pointer. * SEEK_END End of file. * SEEK_SET Beginning of file.
Returns:
Returns the absolute position of the text file. (in chars)

Implements BaseTextReader.

virtual Char ReadChar ( bool  peek = false ) const [virtual]

Read a character from the file.

Parameters:
peek Read a char but does not move the internal pointer to next char.
Returns:
Character read.

Implements BaseTextReader.

virtual unsigned int ReadCharUTF32 ( bool  peek = false ) const [virtual]

Read a char in UTF32 encoding.

Parameters:
peek Read a char but does not move the internal pointer to next char.
Returns:
The UTF32 character.

Implements BaseTextReader.

virtual MaxString ReadChars ( size_t  nchars ) const [virtual]

Reads characters from the file.

Parameters:
nchars Stop reading after 'nchars' characters.
Returns:
Line read.

Implements BaseTextReader.

virtual MaxString ReadLine ( size_t  nchars = (size_t)-1,
bool  dontReturnEOL = false 
) const [virtual]

Read a line from the file.

Parameters:
nchars Max number of characters to read Default -1 - read until end of line character
dontReturnEOL Control if end of line character is in the returned string
Returns:
The line read

Implements BaseTextReader.

virtual bool IsEndOfFile ( ) const [virtual]

Used to determine if we read the end of file.

Returns:
true if end of file, false if not.

Implements BaseTextReader.

virtual size_t Position ( ) const [virtual]

used to determine the current position in the file

Returns:
The position in the file

Implements BaseTextReader.

virtual size_t LineNumber ( ) const [virtual]

Used to obtain the current line number.

Returns:
The line number

Implements BaseTextReader.

virtual size_t UnreadChar ( const Char c ) [virtual]

Unread the passed character.

Parameters:
c The character that will be put back into the buffer
Returns:
Number of character written

Implements BaseTextReader.

virtual size_t Write ( const char *  string,
size_t  nchars = (size_t)-1 
) [virtual]

Write string to file.

Parameters:
string The string to write
nchars The number of characters to write. Alternatively, this parameter can be set to -1 if the string is null-terminated. Note that, if string is NULL, the function fails. If this parameter is -1, the function processes the entire input string, up to the terminating NULL. If this parameter is set to a positive integer, the function processes exactly the specified number of characters up to the terminating NULL.
Returns:
The number of characters written

Implements BaseTextWriter.

virtual size_t Write ( const wchar_t *  string,
size_t  nchars = (size_t)-1 
) [virtual]

Write string to file.

Parameters:
string The string to write
nchars The number of characters to write. Alternatively, this parameter can be set to -1 if the string is null-terminated. Note that, if string is NULL, the function fails. If this parameter is -1, the function processes the entire input string, up to the terminating NULL. If this parameter is set to a positive integer, the function processes exactly the specified number of characters up to the terminating NULL.
Returns:
The number of characters written

Implements BaseTextWriter.

virtual size_t Write ( const MaxString string ) [virtual]

Write string to file.

Parameters:
string The string to write
Returns:
The number of characters written

Implements BaseTextWriter.

virtual bool CanWrite ( ) const [virtual]

Determine if we can write to the file.

Returns:
true if we can write, false otherwise

Implements BaseTextWriter.

virtual bool IsFileOpen ( ) const [virtual]

Returns true if file is open.

Implements BaseTextReader.

void EnsureAllocated ( size_t  s )

Friends And Related Function Documentation


Member Data Documentation

unsigned int _streamMode [protected]
unsigned int _encoding [protected]
unsigned int* _chars [protected]
size_t _len [mutable, protected]
size_t _pos [mutable, protected]