// Echo Stream: an input stream with line echo. // // File: estream.cc // Author: course // Version: 2 #include "estream.h" #include #include // isatty for g++ #include // isatty for ObjectCenter estream ein (cin, cout); int estreambuf::echo() { if (echo_enable < 0) echo_enable = ! isatty (((filebuf *) (in->rdbuf())) -> fd()); return echo_enable; } int estreambuf::underflow () { // In case we are called with characters left in the // get buffer. // if (in_avail()) return (unsigned char) *gptr(); // Leave 4 character positions at the beginning of // the get buffer for putback, and two at the end // for line feed and NUL. // char * buffer = base() + 4; char * ebuffer = ebuf() - 2; int count = 0; while ( ! in->eof() ) { char c = in->get(); if ( c == '\n' || c == EOF ) break; else buffer[count++] = c; } buffer[count] = '\0'; if (count == 0 && in->eof()) { // End of file. Indicate no more input // available. // setg (NULL, NULL, NULL); return EOF; } else { // Not end of file. Count does not include // the '\0' at the end of the buffer, and does // NOT include any '\n' (which was discarded). ebuffer = buffer + count; // If echo is automatic, set it. // if (echo_enable < 0) echo(); // Echo line if echo switch on. // if (echo_enable) *out << buffer << endl; // Return buffer to estream, complete with '\n'. // * ebuffer ++ = '\n'; setg (buffer, buffer, ebuffer); return (unsigned char) *buffer; } }