3ds Max C++ API Reference
Writer Class Reference

Write Text files. More...

#include <maxtextfile.h>

+ Inheritance diagram for Writer:

Public Types

enum  TextFileWriterEncoding { FAVOR_UTF8 = 0x10000000 , WRITE_BOM = 0x20000000 , FLIPPED = 0x80000000 }
 

Public Member Functions

bool Open (FILE *file, bool append=false, unsigned int encoding=0, LineEndMode mode=Text)
 Bind on a FILE pointer. More...
 
bool Open (HANDLE fileHandle, bool append=false, unsigned int encoding=0, LineEndMode mode=Text)
 Bind on a file File handle 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...
 
 Writer ()
 Default Constructor. More...
 
virtual ~Writer ()
 Destructor. More...
 
bool Open (const MCHAR *fileName, bool append=false, unsigned int encoding=0, LineEndMode mode=Text)
 Open a file. More...
 
bool Open (const MaxString &fileName, bool append=false, unsigned int encoding=0, LineEndMode mode=Text)
 Open a file. More...
 
void Close ()
 Close the file. More...
 
MaxString LastError () const
 Returns the last error return by the BinaryStream. More...
 
unsigned int Encoding () const
 Returns the actual encoding used. More...
 
size_t Write (const char *string, size_t nbchars=(size_t) -1)
 Write string to file. More...
 
size_t Write (const wchar_t *string, size_t nbchars=(size_t) -1)
 Write string to file. More...
 
size_t Write (const MaxString &string)
 Write string to file. More...
 
size_t WriteChar (char character)
 Write an ASCII char. More...
 
size_t WriteChar (wchar_t character)
 Write an ASCII char. More...
 
bool CanWrite () const
 Returns true if we can write to file. More...
 
virtual bool IsFileOpen () const
 Returns true if file is open. More...
 
virtual bool IsEndOfFile () const
 Returns true if file pointer is at end if file. More...
 
virtual size_t Position () const
 Get the current position in the file. More...
 
virtual size_t Seek (long offset, int origin)
 Seek inside the stream. More...
 
void Flush ()
 Make sure that all the buffer are synced with the OS native objects. More...
 
- 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 Types

enum  TextFileWriterError {
  ALL_OK , STREAM_INVALID_ARGUMENT , STREAM_ALREADY_OPEN , STREAM_NOT_OPEN ,
  ERROR_PRINTING_BUFFER , ERROR_ALLOCATING_MEMORY , STREAM_ERROR
}
 Internal processing error code. More...
 

Protected Member Functions

bool Open (BinaryStream *stream, bool append=false, unsigned int encoding=0, LineEndMode mode=Text, bool closeOnDelete=false)
 Open an abstract BinaryStream. More...
 

Protected Attributes

BinaryStream_stream
 
bool _streamDelete
 
bool _append
 
unsigned int _encoding
 
LineEndMode _endOfLineMode
 
TextFileWriterError _error
 

Friends

class ReaderWriter
 
class CharBinaryStream
 
class TextFileWriterTest
 

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

Write Text files.

This class was intended to be used to write text file. It differ from the ANSI fopen, fprintf, et al, because it's smarter when it's to modify existing files. It detect the underlying file format and match it intelligently.

It was designed to resolve the following problems:

  • Avoid mixing different text format in the same file.
  • Convert intelligently strings into binary data.

Member Enumeration Documentation

◆ TextFileWriterError

enum TextFileWriterError
protected

Internal processing error code.

Used by LastError to determine the proper message to generate.

Enumerator
ALL_OK 
STREAM_INVALID_ARGUMENT 

No error occurred.

STREAM_ALREADY_OPEN 

Invalid argument error.

STREAM_NOT_OPEN 

Instance of class is already in used.

ERROR_PRINTING_BUFFER 

Stream not open.

ERROR_ALLOCATING_MEMORY 

Error printing buffer.

STREAM_ERROR 

Error allocating memory.

Windows API error.

930  {
931  ALL_OK,
937  STREAM_ERROR
938  };
@ STREAM_NOT_OPEN
Instance of class is already in used.
Definition: maxtextfile.h:934
@ ERROR_PRINTING_BUFFER
Stream not open.
Definition: maxtextfile.h:935
@ STREAM_ALREADY_OPEN
Invalid argument error.
Definition: maxtextfile.h:933
@ STREAM_ERROR
Error allocating memory.
Definition: maxtextfile.h:937
@ ALL_OK
Definition: maxtextfile.h:931
@ STREAM_INVALID_ARGUMENT
No error occurred.
Definition: maxtextfile.h:932
@ ERROR_ALLOCATING_MEMORY
Error printing buffer.
Definition: maxtextfile.h:936

◆ TextFileWriterEncoding

Enumerator
FAVOR_UTF8 
WRITE_BOM 
FLIPPED 
1000  {
1001  /* If the file's encoding cannot be detected, favor UTF-8.
1002  By default, we favor ACP encoding. */
1003  FAVOR_UTF8 = 0x10000000,
1004 
1005  /* Write a BOM at the beginning of the file. */
1006  WRITE_BOM = 0x20000000,
1007 
1008  /* Write flipped UTF-16 data. */
1009  FLIPPED = 0x80000000
1010  };
@ FLIPPED
Definition: maxtextfile.h:1009
@ WRITE_BOM
Definition: maxtextfile.h:1006
@ FAVOR_UTF8
Definition: maxtextfile.h:1003

Constructor & Destructor Documentation

◆ Writer()

Writer ( )

Default Constructor.

◆ ~Writer()

virtual ~Writer ( )
virtual

Destructor.

Member Function Documentation

◆ Open() [1/5]

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

Open an abstract BinaryStream.

Parameters
streamStream that the TextFile::Writer must use
appendAppend to file or truncate file
encodingIf the stream actually contains any data and append is true, it will detect it's format and match it. You can hint the detection algorithm by using FAVOR_UTF8. If the stream is empty, it will use the code page specified here. You can mix it with "WRITE_BOM" and/or "FLIPPED" bits.
See also
TextFileWriterEncoding
Parameters
mode
See also
LineEndMode - default Text mode
Parameters
closeOnDeleteDelete the "stream" at the same time of this object. Default false
Returns
true if successful, false otherwize

◆ Open() [2/5]

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

Bind on a FILE pointer.

Parameters
fileFile pointer to use User is responsible for closing the file.
appendAppend to file or truncate file
encodingIf the stream actually contains any data and append is true, it will detect it's format and match it. You can hint the detection algorithm by using FAVOR_UTF8. If the stream is empty, it will use the code page specified here. You can mix it with "WRITE_BOM" and/or "FLIPPED" bits.
See also
TextFileWriterEncoding
Parameters
mode
See also
LineEndMode - default Text
Returns
true if successful, false otherwize

◆ Open() [3/5]

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

Bind on a file File handle 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 now worry about character encoding The developer is responsible to close the file once he is done.

Parameters
fileHandleFile handle to use User is responsible for closing the file.
appendAppend to file or truncate file
encodingIf the stream actually contains any data and append is true, it will detect it's format and match it. You can hint the detection algorithm by using FAVOR_UTF8. If the stream is empty, it will use the code page specified here. You can mix it with "WRITE_BOM" and/or "FLIPPED" bits.
See also
TextFileWriterEncoding
Parameters
mode
See also
LineEndMode - default Text
Returns
true if successful, false otherwize

◆ Open() [4/5]

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

Open a file.

Parameters
fileNameFile name to open
appendAppend to file or truncate file
encodingIf the stream actually contains any data and append is true, it will detect it's format and match it. You can hint the detection algorithm by using FAVOR_UTF8. If the stream is empty, it will use the code page specified here. You can mix it with "WRITE_BOM" and/or "FLIPPED" bits.
See also
TextFileWriterEncoding
Parameters
mode
See also
LineEndMode - default Text
Returns
true if successful, false otherwize

◆ Open() [5/5]

bool Open ( const MaxString fileName,
bool  append = false,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Open a file.

Parameters
fileNameFile name to open
appendAppend to file or truncate file
encodingIf the stream actually contains any data and append is true, it will detect it's format and match it. You can hint the detection algorithm by using FAVOR_UTF8. If the stream is empty, it will use the code page specified here. You can mix it with "WRITE_BOM" and/or "FLIPPED" bits.
See also
TextFileWriterEncoding
Parameters
mode
See also
LineEndMode - default Text
Returns
true if successful, false otherwize

◆ Close()

void Close ( )

Close the file.

◆ LastError()

MaxString LastError ( ) const

Returns the last error return by the BinaryStream.

◆ Encoding()

unsigned int Encoding ( ) const

Returns the actual encoding used.

Returns
The encoding
See also
MaxSDK::Util::MaxStringDataEncoding. The actual code page can be retrieved this way : "Encoding() & MSDE_CP_MASK"
TextFileReaderEncoding

◆ Write() [1/3]

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

Write string to file.

Parameters
stringThe string to write
nbcharsThe size of the string in characters. -1 will write all chars up to '\0'
Returns
Number of characters written.
(size_t)-1 if error.

Implements BaseTextWriter.

◆ Write() [2/3]

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

Write string to file.

Parameters
stringThe string to write.
nbcharsThe size of the string in characters. -1 will write all chars up to '\0'
Returns
Number of characters written.
(size_t)-1 if error.

Implements BaseTextWriter.

◆ Write() [3/3]

size_t Write ( const MaxString string)
virtual

Write string to file.

Parameters
stringThe string to write
Returns
Number of characters written.
(size_t)-1 if error.

Implements BaseTextWriter.

◆ WriteChar() [1/2]

size_t WriteChar ( char  character)

Write an ASCII char.

Parameters
characterThe character to write
Returns
Number of characters written.
-1 if error

◆ WriteChar() [2/2]

size_t WriteChar ( wchar_t  character)

Write an ASCII char.

Parameters
characterThe character to write
Returns
Number of bytes written.
-1 if error

◆ CanWrite()

bool CanWrite ( ) const
virtual

Returns true if we can write to file.

Implements BaseTextWriter.

◆ IsFileOpen()

virtual bool IsFileOpen ( ) const
virtual

Returns true if file is open.

Implements BaseTextWriter.

◆ IsEndOfFile()

virtual bool IsEndOfFile ( ) const
virtual

Returns true if file pointer is at end if file.

Implements BaseTextWriter.

◆ Position()

virtual size_t Position ( ) const
virtual

Get the current position in the file.

Implements BaseTextWriter.

◆ 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 BaseTextWriter.

◆ Flush()

void Flush ( )
virtual

Make sure that all the buffer are synced with the OS native objects.

Implements BaseTextWriter.

Friends And Related Function Documentation

◆ ReaderWriter

friend class ReaderWriter
friend

◆ CharBinaryStream

friend class CharBinaryStream
friend

◆ TextFileWriterTest

friend class TextFileWriterTest
friend

Member Data Documentation

◆ _stream

BinaryStream* _stream
protected

◆ _streamDelete

bool _streamDelete
protected

◆ _append

bool _append
protected

◆ _encoding

unsigned int _encoding
protected

◆ _endOfLineMode

LineEndMode _endOfLineMode
protected

◆ _error

TextFileWriterError _error
mutableprotected