Writer Class Reference
 
 
 
Writer Class Reference

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


#include <maxtextfile.h>

Inheritance diagram for Writer:
BaseTextWriter MaxHeapOperators

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

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

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.

Protected Attributes

BinaryStream _stream
bool  _streamDelete
bool  _append
unsigned int  _encoding
LineEndMode  _endOfLineMode
TextFileWriterError  _error

Friends

class  ReaderWriter
class  CharBinaryStream
class  TextFileWriterTest

Member Enumeration Documentation

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.

Enumerator:
FAVOR_UTF8 
WRITE_BOM 
FLIPPED 
                                    {
                /* If the file's encoding cannot be detected, favor UTF-8.
                By default, we favor ACP encoding. */
                FAVOR_UTF8 = 0x10000000,

                /* Write a BOM at the beginning of the file. */
                WRITE_BOM = 0x20000000,

                /* Write flipped UTF-16 data. */
                FLIPPED = 0x80000000
        };

Constructor & Destructor Documentation

Writer ( )

Default Constructor.

virtual ~Writer ( ) [virtual]

Destructor.


Member Function Documentation

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

Open an abstract BinaryStream.

Parameters:
stream Stream that the TextFile::Writer must use
append Append to file or truncate file
encoding If 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:
closeOnDelete Delete the "stream" at the same time of this object. Default false
Returns:
true if successful, false otherwize
bool Open ( FILE *  file,
bool  append = false,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Bind on a FILE pointer.

Parameters:
file File pointer to use User is responsible for closing the file.
append Append to file or truncate file
encoding If 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
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:
file File handle to use User is responsible for closing the file.
append Append to file or truncate file
encoding If 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
bool Open ( const MCHAR *  fileName,
bool  append = false,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Open a file.

Parameters:
fileName File name to open
append Append to file or truncate file
encoding If 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
bool Open ( const MaxString fileName,
bool  append = false,
unsigned int  encoding = 0,
LineEndMode  mode = Text 
)

Open a file.

Parameters:
fileName File name to open
append Append to file or truncate file
encoding If 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
void Close ( )

Close the file.

MaxString LastError ( ) const

Returns the last error return by the BinaryStream.

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
size_t Write ( const char *  string,
size_t  nbchars = (size_t)-1 
) [virtual]

Write string to file.

Parameters:
string The string to write
nbchars The 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.

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

Write string to file.

Parameters:
string The string to write.
nbchars The 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.

size_t Write ( const MaxString string ) [virtual]

Write string to file.

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

Implements BaseTextWriter.

size_t WriteChar ( char  character )

Write an ASCII char.

Parameters:
character The character to write
Returns:
Number of characters written. -1 if error
size_t WriteChar ( wchar_t  character )

Write an ASCII char.

Parameters:
character The character to write
Returns:
Number of bytes written. -1 if error
bool CanWrite ( ) const [virtual]

Returns true if we can write to file.

Implements BaseTextWriter.

virtual bool IsFileOpen ( ) const [virtual]

Returns true if file is open.

Implements BaseTextWriter.

virtual bool IsEndOfFile ( ) const [virtual]

Returns true if file pointer is at end if file.

Implements BaseTextWriter.

virtual size_t Position ( ) const [virtual]

Get the current position in the file.

Implements BaseTextWriter.

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

Seek inside the stream.

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

void Flush ( ) [virtual]

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

Implements BaseTextWriter.


Friends And Related Function Documentation

friend class TextFileWriterTest [friend]

Member Data Documentation

unsigned int _encoding [protected]