3ds Max C++ API Reference
ReaderWriter Class Reference

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

#include <maxtextfile.h>

+ Inheritance diagram for ReaderWriter:

Public Member Functions

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

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. More...
 
size_t SeekToAbsolute (long offset)
 Used to seek to a position in the file. More...
 
size_t SeekToEnd (long offset=0)
 Used to seek to end. More...
 
size_t SeekFromCurrent (long offset)
 Advance "offset" characters. More...
 
virtual size_t Unread (const MaxString &string)
 Unread the passed string. More...
 

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
 

Additional Inherited Members

- Static Public Member Functions inherited from MaxHeapOperators
static UtilExport voidoperator new (size_t size)
 Standard new operator used to allocate objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e)
 Standard new operator used to allocate objects if there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate objects that takes the type of memory, filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate arrays of objects. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport void operator delete (void *ptr)
 Standard delete operator used to deallocate an object If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an object If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an object that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, void *placement_ptr)
 Placement new operator. More...
 
static UtilExport void operator delete (void *ptr, void *placement_ptr)
 Placement delete operator. More...
 
static UtilExport voidaligned_malloc (size_t size, size_t alignment)
 Allocates memory on a specified alignment boundary. More...
 
static UtilExport voidaligned_realloc (void *ptr, size_t size, size_t alignment)
 Reallocates memory on a specified alignment boundary. More...
 
static UtilExport void aligned_free (void *ptr)
 Frees a block of memory that was allocated with aligned_malloc/aligned_realloc. More...
 

Detailed 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.

Constructor & Destructor Documentation

◆ ReaderWriter()

Default Constructor.

◆ ~ReaderWriter()

virtual ~ReaderWriter ( )
virtual

Destructor.

Member Function Documentation

◆ Open() [1/3]

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]streamIf non-null, the stream.
encodingThe file encoding or preferred encoding
streamModeStreaming mode
See also
BinaryStreamMode
Parameters
modeText file end of line termination mode. Default Text
closeOnDeletetrue to close on delete. Default true
Returns
true if it succeeds, false if it fails.

◆ SeekToAbsolute()

size_t SeekToAbsolute ( long  offset)
protected

Used to seek to a position in the file.

Parameters
offsetPosition we which we want to position the cursor.
Returns
The current position in the file

◆ SeekToEnd()

size_t SeekToEnd ( long  offset = 0)
protected

Used to seek to end.

Parameters
offsetThe nchars.
Returns
The current position in the file

◆ SeekFromCurrent()

size_t SeekFromCurrent ( long  offset)
protected

Advance "offset" characters.

Returns
Returns the absolute position of the text file. (in chars)

◆ Unread()

virtual size_t Unread ( const MaxString string)
protectedvirtual

Unread the passed string.

Parameters
stringThe string that will be put back into the buffer
Returns
size in nb of characters that has been written

◆ Open() [2/3]

bool Open ( const MCHAR fileName,
unsigned int  streamMode,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Open a file using a file name.

Parameters
fileNameFile name to open. If file does not exist, it will be created.
streamModeStreaming mode
See also
BinaryStreamMode
Parameters
encodingThis 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

◆ Open() [3/3]

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
fileNameFile name to open. If file does not exist, it will be created.
streamModeStreaming mode
See also
BinaryStreamMode
Parameters
encodingThis 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

◆ Close()

void Close ( )

Close the opened file.

◆ Flush()

void Flush ( )
virtual

Flush to file the cached data.

Implements BaseTextWriter.

◆ Seek()

virtual size_t Seek ( long  offset,
int  origin 
)
virtual

Seek inside the file.

Parameters
offsetThe seeks operations are done in number of characters (not bytes).
originThe 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.

◆ ReadChar()

virtual Char ReadChar ( bool  peek = false) const
virtual

Read a character from the file.

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

Implements BaseTextReader.

◆ ReadCharUTF32()

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

Read a char in UTF32 encoding.

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

Implements BaseTextReader.

◆ ReadChars()

virtual MaxString ReadChars ( size_t  nchars) const
virtual

Reads characters from the file.

Parameters
ncharsStop reading after 'nchars' characters.
Returns
Line read.

Implements BaseTextReader.

◆ ReadLine()

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

Read a line from the file.

Parameters
ncharsMax number of characters to read Default -1 - read until end of line character
dontReturnEOLControl if end of line character is in the returned string
Returns
The line read

Implements BaseTextReader.

◆ IsEndOfFile()

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.

◆ Position()

virtual size_t Position ( ) const
virtual

used to determine the current position in the file

Returns
The position in the file

Implements BaseTextReader.

◆ LineNumber()

virtual size_t LineNumber ( ) const
virtual

Used to obtain the current line number.

Returns
The line number

Implements BaseTextReader.

◆ UnreadChar()

virtual size_t UnreadChar ( const Char c)
virtual

Unread the passed character.

Parameters
cThe character that will be put back into the buffer
Returns
Number of character written

Implements BaseTextReader.

◆ Write() [1/3]

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

Write string to file.

Parameters
stringThe string to write
ncharsThe 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.

◆ Write() [2/3]

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

Write string to file.

Parameters
stringThe string to write
ncharsThe 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.

◆ Write() [3/3]

virtual size_t Write ( const MaxString string)
virtual

Write string to file.

Parameters
stringThe string to write
Returns
The number of characters written

Implements BaseTextWriter.

◆ CanWrite()

virtual bool CanWrite ( ) const
virtual

Determine if we can write to the file.

Returns
true if we can write, false otherwise

Implements BaseTextWriter.

◆ IsFileOpen()

virtual bool IsFileOpen ( ) const
virtual

Returns true if file is open.

Implements BaseTextReader.

◆ EnsureAllocated()

void EnsureAllocated ( size_t  s)

Friends And Related Function Documentation

◆ CharBinaryStream

friend class CharBinaryStream
friend

Member Data Documentation

◆ _stream

BinaryStream* _stream
protected

◆ _closeOnDelete

bool _closeOnDelete
protected

◆ _streamMode

unsigned int _streamMode
protected

◆ _endOfLineMode

LineEndMode _endOfLineMode
protected

◆ _encoding

unsigned int _encoding
protected

◆ _chars

unsigned int* _chars
protected

◆ _allocated

size_t _allocated
protected

◆ _len

size_t _len
mutableprotected

◆ _pos

size_t _pos
mutableprotected

◆ _dirty

bool _dirty
protected