//============================================================================== // textstream.hpp // Generic I/O text stream. // // Notice // Copyright ©2008-2011 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_textstream_hpp #define drt_lib_textstream_hpp 107 // Identification #ifndef NO_H_IDENT static char drt_lib_textstream_hpp_REV[] = "@(#)drt/src/lib/textstream.hpp $Revision: 1.7 $$Date: 2011/04/30 00:21:08 $"; #endif // Includes #ifndef sys_stdio_h #include #define sys_stdio_h #endif //------------------------------------------------------------------------------ // class TextStream // Controls the reading or writing of data for a text file. // // Handles the following file encodings: // 8-bit (ASCII or ISO 8859-1) // 7-bit, 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) // Any (input only) // None //------------------------------------------------------------------------------ #define TextStream_VS 201 // Class version class TextStream { // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Constants public: // I/O stream modes enum Mode { MODE_NONE = 0, // None (error) MODE_READ = 1, // Read MODE_WRITE = 2, // Write }; // Stream character types enum FileType { FTYPE_8BIT = 1, // 8-bit (ASCII or ISO 8859-x) FTYPE_7NONE = 10, // 7-bit ASCII, no (space) parity FTYPE_7MARK = 11, // 7-bit ASCII, mark parity FTYPE_7EVEN = 12, // 7-bit ASCII, even parity FTYPE_7ODD = 13, // 7-bit ASCII, odd parity FTYPE_UTF8 = 20, // UTF-8 Unicode FTYPE_UTF16 = 21, // UTF-16 Unicode, big-endian FTYPE_UTF16_R = 22, // UTF-16 Unicode, little-endian FTYPE_UTF32 = 23, // UTF-32 Unicode, big-endian FTYPE_UTF32_R = 24, // UTF-32 Unicode, little-endian FTYPE_24BIT = 30, // 24-bit Unicode, big-endian FTYPE_24BIT_R = 31, // 24-bit Unicode, little-endian }; // Newline types enum EolnType { EOLN_NONE = 0x00, // No newline conversion EOLN_CR = 0x01, // CR (0D) EOLN_LF = 0x02, // LF (0A) EOLN_CRLF = 0x04, // CR+LF (0D 0A) EOLN_ANY = 0x07, // Any (CR/LF/CR+LF, input only) EOLN_NEL = 0x08, // NEL (85) }; // Special Unicode character codes enum { CH_EOF = -1, // End of stream CH_EOLN = -2, // End of line (newline) CH_NUL = 0x0000, // NUL, null CH_BEL = 0x0007, // BEL, bell (alarm) CH_BS = 0x0008, // BS, backspace CH_HT = 0x0009, // HT, tab CH_LF = 0x000A, // LF, linefeed CH_VT = 0x000B, // VT, vertical tab CH_FF = 0x000C, // FF, formfeed CH_CR = 0x000D, // CR, return CH_SUB = 0x001A, // SUB, ctrl-Z CH_ESC = 0x001B, // ESC, escape CH_SP = 0x0020, // SP, space CH_DEL = 0x007F, // DEL, delete CH_NEL = 0x0085, // NEL, next line CH_NBSP = 0x00A0, // NBSP, non-breaking space CH_BOM = 0xFEFF, // BOM, byte order mark CH_BOM_R = 0xFFFe, // BOM(r), byte-swapped byte order mark }; // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Static functions public: static TextStream * open(const char *fname, enum Mode mode, enum FileType ftype, enum EolnType eoln); // Open a file stream static TextStream * open(FILE *fp, enum Mode mode, enum FileType ftype, enum EolnType eoln); // Open an I/O stream // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Functions public: virtual /**/ ~TextStream(); // Destructor /**/ TextStream(); // Constructor virtual bool open(const char *fname, enum FileType ftype, enum EolnType eoln) = NULL; // Open a file stream virtual bool open(FILE *fp, enum FileType ftype, enum EolnType eoln) = NULL; // Open an I/O stream virtual bool close() = NULL; // Close the stream int error() const; // Get last error code FILE * getStream() const; // Get I/O stream enum Mode getMode() const; // Get I/O mode enum FileType getFileType() const; // Get file type enum EolnType getEolnType() const; // Get newline type private: /**/ TextStream(const TextStream &o); const TextStream & operator =(const TextStream &o); // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ // Variables protected: FILE * m_fp; // I/O stream char * m_vbuf; // I/O stream buffer int m_errno; // Last error code enum Mode m_mode; // I/O mode enum FileType m_ftype; // File type enum EolnType m_eoln; // Newline type }; #endif // drt_lib_textstream_hpp // End textstream.hpp