//============================================================================== // incharstream.cpp // Generic input 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/incharstream.cpp $Revision: 1.1 $$Date: 2011/05/08 02:40:13 $"; // 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 "incharstream.hpp" // Manifest constants #define BUF_SIZE (16 * 1024) // I/O vbuffer size //------------------------------------------------------------------------------ // InCharStream::~InCharStream() // Destructor. //------------------------------------------------------------------------------ /*virtual*/ InCharStream::~InCharStream() { #if InCharStream_VS != 100 #error class InCharStream has changed #endif // De-initialize close(); } //------------------------------------------------------------------------------ // InCharStream::InCharStream() // Constructor. //------------------------------------------------------------------------------ InCharStream::InCharStream(): CharStream() { #if InCharStream_VS != 100 #error class InCharStream has changed #endif // Initialize } //------------------------------------------------------------------------------ // InCharStream::close() // Close the input stream. //------------------------------------------------------------------------------ /*virtual*/ bool InCharStream::close() { #if InCharStream_VS/100 != 1 #error class InCharStream has changed #endif // Do nothing return true; } //------------------------------------------------------------------------------ // InCharStream::read() // Read a character from the input stream. // // Notes // This function assumes that InCharStream::read(int[],int) has been // implemented in an overridding subclass. // // Returns // A Unicode character code in the range [0x0000,0xFFFF], or // CH_EOLN if a newline sequence was read, or // CH_EOF if the end of the stream (end of file) was reached. //------------------------------------------------------------------------------ /*virtual*/ int InCharStream::read() { #if InCharStream_VS/100 != 1 #error class InCharStream has changed #endif int buf[1]; int len; // Read a single char from the input if (read(buf, 1) > 0) return buf[0]; return CH_EOF; } //------------------------------------------------------------------------------ // InCharStream::read() // Read characters from the input stream. // // Notes // This function assumes that InCharStream::read() has been implemented in // an overridding subclass. // // Returns // Number of characters written into 'buf', or -1 if the end of the stream // (end of file) was reached. // Note that newline sequences are written into 'buf' as CH_EOLN codes. //------------------------------------------------------------------------------ /*virtual*/ int InCharStream::read(int buf[], int len) { #if InCharStream_VS/100 != 1 #error class InCharStream has changed #endif int cnt; // Sanity checks if (len <= 0) return 0; if (buf == NULL) return -1; // Read characters from the input stream cnt = 0; while (cnt < len) { int ch; // Read the next input char ch = read(); if (ch == CH_EOF) { // End of file return (cnt == 0 ? -1 : cnt); } buf[cnt++] = ch; } return cnt; } // End incharstream.cpp