3ds Max C++ API Reference
Reader Class Reference

Reads and interprets text files. More...

#include <maxtextfile.h>

+ Inheritance diagram for Reader:

Public Types

enum  TextFileReaderEncoding { FAVOR_UTF8 = 0x10000000 , FOUND_BOM = 0x20000000 , FOUND_COOKIE = 0x40000000 , FLIPPED = 0x80000000 }
 Text file reading encoding. More...
 
enum  EOFCharacterHandling { DEFAULT_EOF_HANDLING , STOP_READING_AT_EOF , FILTEROUT_EOF_CHARACTER , IGNORE_EOF_CHARACTER }
 EOF character handling. More...
 

Public Member Functions

 Reader ()
 Default Constructor. More...
 
virtual ~Reader ()
 Destructor. More...
 
bool Open (FILE *file, unsigned int encoding=0, LineEndMode mode=Text)
 Wrap ANSI C FILE pointer This service is used to allow a developer to access a file open with fopen but with the advantage to use the SDK API. More...
 
bool Open (HANDLE fileHandle, unsigned int encoding=0, LineEndMode mode=Text)
 Wrap a Win32 file handle. More...
 
bool Open (const MCHAR *fileName, unsigned int encoding=0, LineEndMode mode=Text)
 Open a file using a file name. More...
 
bool Open (const MaxSDK::Util::MaxString &fileName, unsigned int encoding=0, LineEndMode mode=Text)
 Open a file using a file name. More...
 
void Close ()
 Close the underlying stream and free any intermediate data. More...
 
MaxString LastError () const
 Returns the last error return by the BinaryStream. More...
 
unsigned int Encoding () const
 Returns the current encoding of this file. More...
 
LineEndMode Mode () const
 Determine how this reader handles line ending. More...
 
void SetReadBufferSize (size_t readSize)
 Set the read buffer size. More...
 
size_t GetReadBufferSize () const
 Returns the read buffer size. More...
 
void SetDetectSize (size_t detectSize)
 Set the default size of the buffer used to validate encoding. More...
 
size_t GetDetectSize () const
 Returns the detect buffer size. More...
 
size_t NumberOfChars () const
 Return the number of characters in the file. More...
 
size_t NumberOfLines () const
 Calculate the total number of line feeds in the file. More...
 
void SetEOFCharacterHandling (EOFCharacterHandling)
 Set the EOF character handling method and refilter the buffer. More...
 
EOFCharacterHandling GetEOFCharacterHandling () const
 Get the EOF character handling method. More...
 
virtual Char ReadChar (bool peek=false) const
 Reads a single char. More...
 
virtual unsigned int ReadCharUTF32 (bool peek=false) const
 Reads a single char and returns it's UTF32 representation. 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
 Reads a line from the file (or nchars, depending on which one comes first.) More...
 
virtual MaxString ReadChunk (size_t len, bool dontReturnLastEOL=false) const
 Reads up to "len" bytes from the file and convert it to a unicode-compliant string. More...
 
virtual MaxString ReadFull () const
 Reads the file in a single operation and returns it in a single string object. More...
 
virtual size_t PositionBytes () const
 Get the number of bytes read so far. More...
 
size_t Position () const
 Get the number of characters read so far. More...
 
size_t LineNumber () const
 Get the current line number. More...
 
virtual size_t Seek (long offset, int origin)
 Seek inside the stream. More...
 
virtual bool IsEndOfFile () const
 Returns true if file at the end. More...
 
virtual bool IsFileOpen () const
 Returns true if file is open. More...
 
virtual size_t UnreadChar (const Char &c)
 Unread a character. More...
 
- Public Member Functions inherited from BaseTextReader
virtual ~BaseTextReader ()
 

Static Public Member Functions

static bool Detect (void *data, size_t len, unsigned int &encoding, size_t *ignoreBytes)
 Detect the encoding of the passed buffer. More...
 
static size_t NumberOfChars (const void *data, size_t len, unsigned int encoding=CP_ACP)
 Determine the number of chars inside a buffer. More...
 
static size_t NumberOfLines (const void *data, size_t length, unsigned int encoding=CP_ACP)
 Determine the number of line feeds inside a buffer. More...
 
template<typename ChType , ChType ch>
static size_t RemoveCharacter (ChType *data, size_t len)
 Remove ch character from the passed buffer. More...
 
template<typename ChType , ChType ch>
static size_t TruncateAtCharacter (ChType *data, size_t len)
 Truncate buffer at ch character. More...
 
- 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...
 

Protected Types

enum  TextFileReaderError {
  ALL_OK , STREAM_INVALID_ARGUMENT , STREAM_ALREADY_OPEN , STREAM_NOT_OPEN ,
  ERR_INVALID_FORMAT , STREAM_ERROR
}
 Internal processing error code. More...
 

Protected Member Functions

void Detect (size_t len) const
 Detect the opened file encoding by analyzing the first "len" bytes of the file. More...
 
size_t FillBuffer (size_t len, bool force=false, bool binary=false, bool detecting_encoding=false) const
 Read and cache len bytes from stream. More...
 
size_t Filter (size_t pos) const
 Apply the selected open mode on the internal cache buffer up to position. More...
 
size_t Filter (size_t pos, bool processBufferBoundaryCRLF) const
 Apply the selected open mode on the internal cache buffer up to position. More...
 
size_t EnsureBufferContains (size_t len) const
 Ensure buffer size can contains the passed length. More...
 
size_t NumberOfChars (const void *data, size_t size) const
 Return the number of characters in the passed buffer depending on the current encoding. More...
 
size_t ConvertNumUTF8CharsToNumBytes (const char *data, size_t num)
 Return the number of bytes corresponding to the num of UTF8 chars in passed buffer. More...
 
size_t ConvertNumUTF16CharsToNumBytes (const MCHAR *data, size_t num)
 Return the number of bytes corresponding to the num of UTF16 chars in passed buffer. More...
 
size_t ConvertNumCharsToNumBytes (const char *data, size_t num, unsigned int encoding)
 Return the number of bytes corresponding to the num of chars in passed buffer depending on the encoding. More...
 
size_t NumberOfLines (const void *data, size_t size) const
 Calculate the total number of line feeds in the passed buffer. More...
 
template<typename ChType , typename CharLengthFunctor , int maxCharLength>
ChType * ReadChar (size_t &charLengthT, bool peek, const CharLengthFunctor &CharLengthFunction) const
 INTERNAL FUNCTION. More...
 
template<typename ChType >
MaxString MakeString (const ChType *data, size_t length, bool dontReturnEndingCRLF) const
 
size_t Unread (const MaxString &string)
 Unread String. More...
 
size_t SeekToEnd (long offset=0)
 INTERNAL FUNCTION. More...
 
size_t SeekToAbsolute (long offset)
 Seek to an absolute point inside the text stream. More...
 
size_t SeekFromCurrent (long offset)
 Advance "offset" characters. More...
 
bool Open (BinaryStream *stream, unsigned int encoding=0, LineEndMode mode=Text, bool closeOnDelete=false)
 Open an abstract BinaryStream. More...
 

Protected Attributes

BinaryStream_stream
 
bool _streamDelete
 
bool _readCR
 
bool _readLF
 
LineEndMode _endOfLineMode
 
size_t _detectSize
 
size_t _readSize
 
EOFCharacterHandling _eofCharacterHandling
 
TextFileReaderError _error
 
unsigned int _encoding
 
bool _encodingDetected
 
BinaryStreamMemory_backbuffer
 
size_t _ignoreBytes
 
size_t _positionBytes
 
size_t _positionChars
 
size_t _line
 

Friends

class ReaderWriter
 
class CharBinaryStream
 
class BinaryStreamMemory
 

Detailed Description

Reads and interprets text files.

This class was designed to perform file and stream I/O in a code page neutral way.

It was designed to perform resolve the following problems:

  • Reads and interprets correctly the BOM (an invisible character at the beginning of unicode files.)
  • Detect correctly UTF-8 and UTF-16 files. (even if it's not signed.)
  • Detect encoding cookies. XML files usually begins with "<?xml encoding='????'>". The detection algorithm will interpret this directive correctly.
  • Prevent from splitting a character. In UTF-16, UTF-8 and some ANSI codepage, characters can be stored on 1 to 6 bytes. All the operations of this object are designed to avoid to returns a partial character.

Plugin developer should consider using this class to perform File I/O to ensure that the files they generate remain compatible to previous version of Max.

See also
ReaderWriter class when file IO requirements are for reading and writing.

Member Enumeration Documentation

◆ TextFileReaderEncoding

Text file reading encoding.

Enumerator
FAVOR_UTF8 
FOUND_BOM 
FOUND_COOKIE 
FLIPPED 
382  {
383  /* If the file's encoding cannot be detected, favor UTF-8.
384  By default, we favor ACP encoding. */
385  FAVOR_UTF8 = 0x10000000,
386 
387  /* Found a BOM at the beginning of the file. */
388  FOUND_BOM = 0x20000000,
389 
390  /* Found a cookie at the beginning of the file. */
391  FOUND_COOKIE = 0x40000000,
392 
393  /* Found flipped UTF-16 data. */
394  FLIPPED = 0x80000000
395  };
@ FLIPPED
Definition: maxtextfile.h:394
@ FOUND_BOM
Definition: maxtextfile.h:388
@ FOUND_COOKIE
Definition: maxtextfile.h:391
@ FAVOR_UTF8
Definition: maxtextfile.h:385

◆ EOFCharacterHandling

EOF character handling.

Enumerator
DEFAULT_EOF_HANDLING 
STOP_READING_AT_EOF 

Used by ReaderWriter to override default value.

FILTEROUT_EOF_CHARACTER 

Reading of file terminate at EOF character - this is Reader in text mode.

IGNORE_EOF_CHARACTER 

EOF character are filter out - this is the ReaderWriter preferred mode.

EOF character are read as regular character - this is Reader in binary mode.

400  {
405  };
@ DEFAULT_EOF_HANDLING
Definition: maxtextfile.h:401
@ IGNORE_EOF_CHARACTER
EOF character are filter out - this is the ReaderWriter preferred mode.
Definition: maxtextfile.h:404
@ STOP_READING_AT_EOF
Used by ReaderWriter to override default value.
Definition: maxtextfile.h:402
@ FILTEROUT_EOF_CHARACTER
Reading of file terminate at EOF character - this is Reader in text mode.
Definition: maxtextfile.h:403

◆ TextFileReaderError

enum TextFileReaderError
protected

Internal processing error code.

Used by LastError to determine the proper message to generate.

Enumerator
ALL_OK 
STREAM_INVALID_ARGUMENT 

No errors.

STREAM_ALREADY_OPEN 

Invalid argument error.

STREAM_NOT_OPEN 

Stream already open.

ERR_INVALID_FORMAT 

Stream not open.

STREAM_ERROR 

Invalid.

Stream error

414  {
415  ALL_OK,
420  STREAM_ERROR
421  };
@ ERR_INVALID_FORMAT
Stream not open.
Definition: maxtextfile.h:419
@ STREAM_NOT_OPEN
Stream already open.
Definition: maxtextfile.h:418
@ STREAM_ALREADY_OPEN
Invalid argument error.
Definition: maxtextfile.h:417
@ STREAM_ERROR
Invalid.
Definition: maxtextfile.h:420
@ ALL_OK
Definition: maxtextfile.h:415
@ STREAM_INVALID_ARGUMENT
No errors.
Definition: maxtextfile.h:416

Constructor & Destructor Documentation

◆ Reader()

Reader ( )

Default Constructor.

◆ ~Reader()

virtual ~Reader ( )
virtual

Destructor.

Member Function Documentation

◆ Detect() [1/2]

void Detect ( size_t  len) const
protected

Detect the opened file encoding by analyzing the first "len" bytes of the file.

Parameters
lenSize of the buffer to use to detect the encoding
Returns
.

◆ FillBuffer()

size_t FillBuffer ( size_t  len,
bool  force = false,
bool  binary = false,
bool  detecting_encoding = false 
) const
protected

Read and cache len bytes from stream.

Parameters
len
force
binary
detecting_encoding

◆ Filter() [1/2]

size_t Filter ( size_t  pos) const
protected

Apply the selected open mode on the internal cache buffer up to position.

Parameters
posPosition to stop filtering
Returns
Size of the buffer filtered

◆ Filter() [2/2]

size_t Filter ( size_t  pos,
bool  processBufferBoundaryCRLF 
) const
protected

Apply the selected open mode on the internal cache buffer up to position.

Parameters
posPosition to stop filtering
processBufferBoundaryCRLFif true, take care of the CR or LF which is read in the last FillBuffer call
Returns
Size of the buffer filtered

◆ EnsureBufferContains()

size_t EnsureBufferContains ( size_t  len) const
protected

Ensure buffer size can contains the passed length.

Parameters
lenThe minimum size of the cache buffer
Returns
The remaining buffer size

◆ NumberOfChars() [1/3]

size_t NumberOfChars ( const void data,
size_t  size 
) const
protected

Return the number of characters in the passed buffer depending on the current encoding.

Parameters
dataBuffer to evaluate
sizeThe size of the buffer in bytes
Returns
The total number of chars.

◆ ConvertNumUTF8CharsToNumBytes()

size_t ConvertNumUTF8CharsToNumBytes ( const char *  data,
size_t  num 
)
protected

Return the number of bytes corresponding to the num of UTF8 chars in passed buffer.

Parameters
data- Buffer to evaluate
num- num of chars in the passed buffer
Returns
The total number of bytes.

◆ ConvertNumUTF16CharsToNumBytes()

size_t ConvertNumUTF16CharsToNumBytes ( const MCHAR data,
size_t  num 
)
protected

Return the number of bytes corresponding to the num of UTF16 chars in passed buffer.

Parameters
dataBuffer to evaluate
numnum of chars in the passed buffer
Returns
The total number of bytes.

◆ ConvertNumCharsToNumBytes()

size_t ConvertNumCharsToNumBytes ( const char *  data,
size_t  num,
unsigned int  encoding 
)
protected

Return the number of bytes corresponding to the num of chars in passed buffer depending on the encoding.

Parameters
dataBuffer to evaluate
numnumber of chars in the passed buffer
encoding
Returns
The total number of bytes.

◆ NumberOfLines() [1/3]

size_t NumberOfLines ( const void data,
size_t  size 
) const
protected

Calculate the total number of line feeds in the passed buffer.

Parameters
dataThe buffer to evaluate the number of line
sizeThe size of the buffer in bytes
Returns
The total number of lines.

◆ ReadChar() [1/2]

ChType* ReadChar ( size_t charLengthT,
bool  peek,
const CharLengthFunctor &  CharLengthFunction 
) const
protected

INTERNAL FUNCTION.

Used in the implementation of ReadChar() and ReadCharUTF32().

◆ MakeString()

MaxString MakeString ( const ChType *  data,
size_t  length,
bool  dontReturnEndingCRLF 
) const
protected

◆ Unread()

size_t Unread ( const MaxString string)
protected

Unread String.

Put back a sequence of character inside the buffer. The data will be re-read next time you call read. This is used internally when parsing max scripts.

Parameters
stringString to put back in the buffer
Returns
Unread size in number of characters.

◆ SeekToEnd()

size_t SeekToEnd ( long  offset = 0)
protected

INTERNAL FUNCTION.

Used by Seek Seek inside this text stream with the end as the reference point.

Parameters
offsetOffset characters from end of file to seek to
Returns
Returns the absolute position of the text file. (in chars)

◆ SeekToAbsolute()

size_t SeekToAbsolute ( long  offset)
protected

Seek to an absolute point inside the text stream.

Parameters
offsetOffset characters from end of file to seek to
Returns
Returns the absolute position of the text file. (in chars)

◆ SeekFromCurrent()

size_t SeekFromCurrent ( long  offset)
protected

Advance "offset" characters.

Parameters
offsetOffset characters from end of file to seek to
Returns
Returns the absolute position of the text file. (in chars)

◆ Open() [1/5]

bool Open ( BinaryStream stream,
unsigned int  encoding = 0,
LineEndMode  mode = Text,
bool  closeOnDelete = false 
)
protected

Open an abstract BinaryStream.

Parameters
streamOpened stream the Reader uses
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
Parameters
closeOnDeleteDelete the "stream" at the same time of this object.

◆ Open() [2/5]

bool Open ( FILE *  file,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Wrap ANSI C FILE pointer This service is used to allow a developer to access a file open with fopen but with the advantage to use the SDK API.

Using this service allows the developer to not worry about character encoding The developer is responsible to close the file once he is done.

Parameters
fileANSI C FILE pointer
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 The flag is only used when no BOM is present or if the file is a new file.
If the file has been open with css=<encoding>, a BOM is present and this parameter is ignored.
Parameters
mode
See also
LineEndMode - default Text
Returns
true if successful, false otherwise

◆ Open() [3/5]

bool Open ( HANDLE  fileHandle,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Wrap a Win32 file handle.

Parameters
fileHandleFile Handle
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() [4/5]

bool Open ( const MCHAR fileName,
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.
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() [5/5]

bool Open ( const MaxSDK::Util::MaxString fileName,
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.
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 underlying stream and free any intermediate data.

◆ LastError()

MaxString LastError ( ) const

Returns the last error return by the BinaryStream.

Returns
Error string

◆ Encoding()

unsigned int Encoding ( ) const

Returns the current encoding of this file.

See also
TextFileReaderEncoding The actual code page can be retrieved this way : "Encoding() & MSDE_CP_MASK"
Returns
Returns the actual encoding found

◆ Mode()

LineEndMode Mode ( ) const

Determine how this reader handles line ending.

Returns
LineEndMode

◆ SetReadBufferSize()

void SetReadBufferSize ( size_t  readSize)

Set the read buffer size.

The larger the buffer is, the better read performance is.

Parameters
readSizeSize of the buffer to read.
Default 4096

◆ GetReadBufferSize()

size_t GetReadBufferSize ( ) const

Returns the read buffer size.

◆ SetDetectSize()

void SetDetectSize ( size_t  detectSize)

Set the default size of the buffer used to validate encoding.


This parameter is used internally when calling Detect.

Parameters
detectSizeSize of the buffer used when detecting the current character type. Default 65536

◆ GetDetectSize()

size_t GetDetectSize ( ) const

Returns the detect buffer size.

◆ NumberOfChars() [2/3]

size_t NumberOfChars ( ) const

Return the number of characters in the file.

Returns
The total number of chars.

◆ NumberOfLines() [2/3]

size_t NumberOfLines ( ) const

Calculate the total number of line feeds in the file.

Returns
The total number of lines.

◆ SetEOFCharacterHandling()

void SetEOFCharacterHandling ( EOFCharacterHandling  )

Set the EOF character handling method and refilter the buffer.

See also
EOFCharacterHandling

◆ GetEOFCharacterHandling()

EOFCharacterHandling GetEOFCharacterHandling ( ) const

Get the EOF character handling method.

See also
EOFCharacterHandling

◆ ReadChar() [2/2]

virtual Char ReadChar ( bool  peek = false) const
virtual

Reads a single char.

Parameters
peekRead a char but does not move the internal pointer to next char.
Default is false so we move to next character
Returns
The character read.

Implements BaseTextReader.

◆ ReadCharUTF32()

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

Reads a single char and returns it's UTF32 representation.

Parameters
peekRead a char but does not move the internal pointer to next char. Default is false so we move to next character
Returns
The UTF32 char representation

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

Reads a line from the file (or nchars, depending on which one comes first.)

Parameters
ncharsStop reading after 'nchars' characters even if the EOL was not found.
dontReturnEOLBy default, this function will returns the line including it's end-of-line character(s) unless you set "dontReturnEOL" to true.

Implements BaseTextReader.

◆ ReadChunk()

virtual MaxString ReadChunk ( size_t  len,
bool  dontReturnLastEOL = false 
) const
virtual

Reads up to "len" bytes from the file and convert it to a unicode-compliant string.

Parameters
lenNumber of bytes to take out of the underlying stream.
dontReturnLastEOLDetermine if this function will trim the last EOL sequence.

◆ ReadFull()

virtual MaxString ReadFull ( ) const
virtual

Reads the file in a single operation and returns it in a single string object.

Returns
The full stream content

◆ PositionBytes()

virtual size_t PositionBytes ( ) const
virtual

Get the number of bytes read so far.

◆ Position()

size_t Position ( ) const
virtual

Get the number of characters read so far.

Implements BaseTextReader.

◆ LineNumber()

size_t LineNumber ( ) const
virtual

Get the current line number.

Implements BaseTextReader.

◆ Seek()

virtual size_t Seek ( long  offset,
int  origin 
)
virtual

Seek inside the stream.

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.

◆ IsEndOfFile()

virtual bool IsEndOfFile ( ) const
virtual

Returns true if file at the end.

Implements BaseTextReader.

◆ IsFileOpen()

virtual bool IsFileOpen ( ) const
virtual

Returns true if file is open.

Implements BaseTextReader.

◆ UnreadChar()

virtual size_t UnreadChar ( const Char c)
virtual

Unread a character.

Put back a character inside the buffer. The data will be re-read next time you call read.

Parameters
cChar to put back in the buffer
Returns
Number of character written.

Implements BaseTextReader.

◆ Detect() [2/2]

static bool Detect ( void data,
size_t  len,
unsigned int encoding,
size_t ignoreBytes 
)
static

Detect the encoding of the passed buffer.

Parameters
dataBuffer to detect the encoding
lenSize of the passed buffer
encoding(in/out) In input, tell the detector what to expect. On output it contains what the detector found.
ignoreBytes(out) On output, tell the caller how much bytes it must ignore at the beginning of the file because of the BOM.
Returns
Returns true if the encoding was formally detected. Or false, if it was guessed.

◆ NumberOfChars() [3/3]

static size_t NumberOfChars ( const void data,
size_t  len,
unsigned int  encoding = CP_ACP 
)
static

Determine the number of chars inside a buffer.

It's more complex than just strlen or wcslen. Those two functions will returns the number of char or WCHAR entries. This function will returns the number of of characters (or symbol).

Parameters
dataBuffer containing a string to count the number of symbols
lenThe size of the buffer in bytes
encodingEncoding to use to count the number of symbols
Returns
Number of symbols

◆ NumberOfLines() [3/3]

static size_t NumberOfLines ( const void data,
size_t  length,
unsigned int  encoding = CP_ACP 
)
static

Determine the number of line feeds inside a buffer.

Parameters
dataBuffer in which '
' are counted.
lengthThe size of the buffer in bytes
encodingEncoding of "data". Can be any valid encoding. ie. MSDE_CP_UTF16, CP_UTF8, CP_ACP, etc.
Returns
Number of line feed detected

◆ RemoveCharacter()

static size_t RemoveCharacter ( ChType *  data,
size_t  len 
)
static

Remove ch character from the passed buffer.

Parameters
dataBuffer in which ch are to be removed.
lenLength of data (in MCHAR)

◆ TruncateAtCharacter()

static size_t TruncateAtCharacter ( ChType *  data,
size_t  len 
)
static

Truncate buffer at ch character.

Parameters
dataBuffer to validate.
lenLength of data (in MCHAR)

Friends And Related Function Documentation

◆ ReaderWriter

friend class ReaderWriter
friend

◆ CharBinaryStream

friend class CharBinaryStream
friend

◆ BinaryStreamMemory

friend class BinaryStreamMemory
friend

Member Data Documentation

◆ _stream

BinaryStream* _stream
protected

◆ _streamDelete

bool _streamDelete
protected

◆ _readCR

bool _readCR
mutableprotected

◆ _readLF

bool _readLF
mutableprotected

◆ _endOfLineMode

LineEndMode _endOfLineMode
protected

◆ _detectSize

size_t _detectSize
protected

◆ _readSize

size_t _readSize
protected

◆ _eofCharacterHandling

EOFCharacterHandling _eofCharacterHandling
mutableprotected

◆ _error

TextFileReaderError _error
mutableprotected

◆ _encoding

unsigned int _encoding
mutableprotected

◆ _encodingDetected

bool _encodingDetected
mutableprotected

◆ _backbuffer

BinaryStreamMemory* _backbuffer
mutableprotected

◆ _ignoreBytes

size_t _ignoreBytes
mutableprotected

◆ _positionBytes

size_t _positionBytes
mutableprotected

◆ _positionChars

size_t _positionChars
mutableprotected

◆ _line

size_t _line
mutableprotected