//============================================================================== // outcharstream.cpp // Generic output character stream. // // 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. //============================================================================== // Identification static char REV[] = "@(#)drt/src/lib/outcharstream.cpp $Revision: 1.1 $$Date: 2011/05/08 02:40:26 $"; // 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 "outcharstream.hpp" //------------------------------------------------------------------------------ // OutCharStream::~OutCharStream() // Destructor. //------------------------------------------------------------------------------ /*virtual*/ OutCharStream::~OutCharStream() { #if OutCharStream_VS != 100 #error class OutCharStream has changed #endif // De-initialize close(); } //------------------------------------------------------------------------------ // OutCharStream::OutCharStream() // Constructor. //------------------------------------------------------------------------------ OutCharStream::OutCharStream(): CharStream() { #if OutCharStream_VS != 100 #error class OutCharStream has changed #endif // Initialize } //------------------------------------------------------------------------------ // OutCharStream::close() // Close the output stream. //------------------------------------------------------------------------------ /*virtual*/ bool OutCharStream::close() { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif // Do nothing return true; } //------------------------------------------------------------------------------ // OutCharStream::flush() // Flush any pending output to the stream. //------------------------------------------------------------------------------ /*virtual*/ bool OutCharStream::flush() { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif // Do nothing return true; } //------------------------------------------------------------------------------ // OutCharStream::write() // Write a character to the (output) stream. // // Notes // This function assumes that InCharStream::write(int[],int) has been // implemented in an overridding subclass. //------------------------------------------------------------------------------ /*virtual*/ bool OutCharStream::write(int ch) { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif // Write a character if (ch == CH_EOLN) return writeln(); else return write(&ch, 1); } //------------------------------------------------------------------------------ // OutCharStream::write() // Write a character string to the (output) stream. // Newline ('\n') characters within 's' cause newlines to be written by // invoking writeln(). // All other control characters are written as is to the output stream. // // Notes // This function assumes that InCharStream::write(int) has been // implemented in an overridding subclass. // // Return // Number of characters written to the output stream from 'buf'. //------------------------------------------------------------------------------ /*virtual*/ int OutCharStream::write(const char *s) { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif int cnt; // Sanity checks if (s == NULL) return -1; // Write characters for (cnt = 0; *s != '\0'; cnt++) { int ch; // Write a character ch = *s; s++; if (ch == '\n') { if (not writeln()) break; } else if (write(&ch, 1) < 1) break; } return cnt; } //------------------------------------------------------------------------------ // OutCharStream::write() // Write characters to the (output) stream. // CH_EOLN codes within 'buf' cause newlines to be written by invoking // writeln(). // All other character codes are written as is to the output stream. // // Notes // This function assumes that InCharStream::write(int) has been // implemented in an overridding subclass. // // Return // Number of characters written to the output stream from 'buf'. //------------------------------------------------------------------------------ /*virtual*/ int OutCharStream::write(const int buf[], int len) { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif int cnt; // Sanity checks if (len <= 0) return 0; if (buf == NULL) return -1; // Write characters for (cnt = 0; len-- > 0; cnt++) { int ch; // Write a character ch = buf[cnt]; if (ch == CH_EOLN) { if (not writeln()) break; } else if (not write(ch)) break; } return cnt; } //------------------------------------------------------------------------------ // OutCharStream::writeln() // Write a newline sequence to the (output) stream. //------------------------------------------------------------------------------ /*virtual*/ bool OutCharStream::writeln() { #if OutCharStream_VS/100 != 1 #error class OutCharStream has changed #endif // Do nothing return false; } // End outcharstream.cpp