//============================================================================== // outcharfile.hpp // Generic output character stream. // // See also // incharfile.hpp // // Notice // Copyright ©2010 by David R. Tribble, all rights reserved. // Permission is granted to any person or entity except those designated // by the United States Department of State as a terrorist, or terrorist // government or agency, to use and distribute this source code provided // that the original copyright notice remains present and unaltered. //============================================================================== #ifndef drt_lib_outcharfile_hpp #define drt_lib_outcharfile_hpp 101 // Identification #ifndef NO_H_IDENT static char drt_lib_outcharfile_hpp_REV[] = "@(#)drt/src/lib/outcharfile.hpp $Revision: 1.1 $$Date: 2011/05/08 02:41:02 $"; #endif // Includes #ifndef sys_stdio_h #include #define sys_stdio_h #endif #ifndef drt_lib_charstream_hpp #include "charstream.hpp" #endif #ifndef drt_lib_outcharstream_hpp #include "outcharstream.hpp" #endif //------------------------------------------------------------------------------ // class OutCharFile // Controls the writing of data for an output text file. // // Handles the following file encodings: // 8-bit (ASCII or ISO 8859-1) // 7-bit ASCII, with none(space)/mark/even/odd parity modes // UTF-8 // UTF-16 (big-endian) // UTF-16 reversed (little-endian) // UTF-32 (big-endian) // UTF-32 reversed (little-endian) // 24-bit (big-endian) (non-standard) // 24-bit reversed (little-endian) (non-standard) // // Handles the following newline sequences: // CR (0D) // LF (0A) // CR LF (0D 0A) // NEL (85) // None //------------------------------------------------------------------------------ #define OutCharFile_VS 100 // Class version class OutCharFile: public OutCharStream { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Constants private: static const unsigned char parity_even[128]; // 7-bit even parity // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Static functions private: static int put_8bit(FILE *out, int ch); // Write an 8-bit char static int put_7none(FILE *out, int ch); // Write 7-bit no parity static int put_7mark(FILE *out, int ch); // Write 7-bit mark static int put_7even(FILE *out, int ch); // Write 7-bit even static int put_7odd(FILE *out, int ch); // Write 7-bit odd static int put_utf8(FILE *out, int ch); // Write a UTF-8 char static int put_utf16(FILE *out, int ch); // Write a UTF-16BE char static int put_utf16r(FILE *out, int ch); // Write a UTF-16LE char static int put_utf32(FILE *out, int ch); // Write a UTF-32BE char static int put_utf32r(FILE *out, int ch); // Write a UTF-32LE char static int put_24bit(FILE *out, int ch); // Write 24-bit BE char static int put_24bitr(FILE *out, int ch); // Write 24-bit LE char // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Functions public: virtual /**/ ~OutCharFile(); // Destructor /**/ OutCharFile(); // Constructor bool open(const char *fname, enum FileType ftype, enum EolnType eoln); // Open a file stream bool open(FILE *fp, enum FileType ftype, enum EolnType eoln); // Open an I/O stream FILE * getStream() const; // Get I/O stream virtual bool close(); // Close the stream virtual bool flush(); // Flush pending output virtual bool write(int ch); // Write a Unicode char virtual int write(const char *s); // Write a string virtual int write(const int buf[], int len); // Write Unicode chars virtual bool writeln(); // Write a newline private: /**/ OutCharFile(const OutCharFile &o); const OutCharFile & operator =(const OutCharFile &o); bool openPrep(enum FileType ftype, enum EolnType eoln); // Prepare to open a stream // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Variables protected: FILE * m_fp; // I/O stream char * m_vbuf; // I/O stream buffer int (*m_put)(FILE *out, int ch); // Write a char }; #endif // drt_lib_outcharfile_hpp // End outcharfile.hpp