//============================================================================== // textstream.cpp // 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. //============================================================================== // Identification static char REV[] = "@(#)drt/src/lib/textstream.cpp $Revision: 1.7 $$Date: 2011/04/30 00:21:17 $"; // Includes #include #define sys_ctype_h #include #define sys_errno_h #include #define sys_iso646_h #include #define sys_stdio_h #include #define sys_string_h #if _WIN32 #define WINDOWS_LEAN_AND_MEAN #include #include #endif #include "textstream.hpp" #include "intextstream.hpp" #include "outtextstream.hpp" //------------------------------------------------------------------------------ // TextStream::~TextStream() // Destructor. //------------------------------------------------------------------------------ TextStream::~TextStream() { #if TextStream_VS != 201 #error class TextStream has changed #endif // De-initialize } //------------------------------------------------------------------------------ // TextStream::TextStream() // Constructor. //------------------------------------------------------------------------------ TextStream::TextStream(): m_fp(NULL), m_vbuf(NULL), m_errno(0), m_mode(MODE_NONE), m_ftype(FTYPE_8BIT), m_eoln(EOLN_NONE) { #if TextStream_VS != 201 #error class TextStream has changed #endif // Initialize } //------------------------------------------------------------------------------ // TextStream::open() // Open an existing stream as an I/O stream. //------------------------------------------------------------------------------ /*static*/ TextStream * TextStream::open(FILE *fp, enum Mode mode, enum FileType ftype, enum EolnType eoln) { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif TextStream * st; // Sanity check if (fp == NULL) return NULL; // Open the stream switch (mode) { case MODE_READ: st = new InTextStream(); break; case MODE_WRITE: st = new OutTextStream(); break; default: errno = EINVAL; return NULL; } if (not st->open(fp, ftype, eoln)) { delete[] st; st = NULL; } // Done st->m_mode = mode; return st; } //------------------------------------------------------------------------------ // TextStream::open() // Open a file as an I/O stream. //------------------------------------------------------------------------------ /*static*/ TextStream * TextStream::open(const char *fname, enum Mode mode, enum FileType ftype, enum EolnType eoln) { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif TextStream * st; // Sanity check if (fname == NULL or fname[0] == '\0') return false; // Open the stream switch (mode) { case MODE_READ: st = new InTextStream(); break; case MODE_WRITE: st = new OutTextStream(); break; default: errno = EINVAL; return NULL; } if (not st->open(fname, ftype, eoln)) { delete[] st; st = NULL; } // Done st->m_mode = mode; return st; } //------------------------------------------------------------------------------ // TextStream::error() // Retrieve the last error (::errno) code. //------------------------------------------------------------------------------ int TextStream::error() const { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif return m_errno; } //------------------------------------------------------------------------------ // TextStream::getStream() // Retrieve the underlying I/O stream. //------------------------------------------------------------------------------ FILE * TextStream::getStream() const { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif return m_fp; } //------------------------------------------------------------------------------ // TextStream::getMode() // Retrieve the I/O mode of this I/O stream. //------------------------------------------------------------------------------ enum TextStream::Mode TextStream::getMode() const { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif return m_mode; } //------------------------------------------------------------------------------ // TextStream::getFileType() // Retrieve the file type of this I/O stream. //------------------------------------------------------------------------------ enum TextStream::FileType TextStream::getFileType() const { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif return m_ftype; } //------------------------------------------------------------------------------ // TextStream::getEolnType() // Retrieve the newline mode of this I/O stream. //------------------------------------------------------------------------------ enum TextStream::EolnType TextStream::getEolnType() const { #if TextStream_VS/100 != 2 #error class TextStream has changed #endif return m_eoln; } // End textstream.cpp