// Definition of the echo stream class: // an input stream with line echo. // // File: estream.h // Author: course // Version: 4 #ifndef _ESTREAM_H_ #define _ESTREAM_H_ #include // Output: // // All output should be done by // // cout << value; // // unless otherwise stated. New lines should be // handled by // // cout << endl; // // though outputting '\n' will also work. // // Input: // // All input should be done by // // ein >> variable; // // unless otherwise stated. After trying to input a // value, the test // // ein.eof() // // will return true if the input failed because an // end-of-file was encountered. You can also test // // ein.fail() // // which will return true if the input failed because // the next non-whitespace character was the wrong // character or an end-of-file was encountered. // // If ein.fail() returns true then the fail flag of // ein must be cleared before doing anything else. // Both the fail and the EOF flag can be cleared by // // ein.clear(); // // You may also use the following functions: // // char ein.get() Read a single character // from the input stream. // // void ein.putback(char c) // Put c back into the input // stream. // // char ein.peek() Peek at the next character in // the input stream without // actually reading it. // // istream& ein.getline // (char *buffer, int buffer_length) // Read at most // (buffer_length - 1) // characters or a whole line of // input (whichever is shorter) // from the input stream into // the buffer. // // int ein.gcount() Return the actual number of // characters read by the // immedately preceding call // to getline. // // After calls to the above functions, ein.eof() may // be tested, but ein.fail() is not set. Getline // always terminates the buffer string with a '\0' // and discards any line ending '\n'. // The echo stream class estream: // // An estream is a pair of streams, an istream and an // ostream, plus an echo switch. When the echo switch // is turned on, each line read from the istream is // echoed to the ostream at the time the first // character of the line is read from the estream. // // The echo switch may also be set to "automatic" // (the value -1) in which case it is automatically set // to 0 or 1 the next time the value of the echo switch // is needed (either to echo a line or to be returned // to the user). The automatic setting is to 1 if the // istream is not a terminal, and to 0 if it is a // terminal. The echo switch is initialized to // "automatic", so it need not usually be set by the // programmer. // class estream; // The estream ein: // // The estream ein is just like the istream cin except // for echoing. It will echo lines read to cout if its // echo switch is on. // extern estream ein; // Estream member function descriptions: // // estream::estream (istream& in, ostream& out, // int buffer_length) // Construct an estream given an istream and // an ostream and a length for a line buffer. // Lines longer than the buffer are chopped // into smaller lines. // // void estream::echo(int echo_enable) // Set the echo switch: 0 is off, 1 is on, // -1 is automatic. // // int estream::echo() // Return the current value of the echo switch. // // The default estream buffer length: // const int ESTREAMBUF_LEN = 300; class estreambuf : public streambuf { public: // These estreambuf members implement the // associated estream member functions: see those // for documentation. // estreambuf (istream& in, ostream& out, int buffer_length = ESTREAMBUF_LEN) { char * buf = new char [buffer_length + 10]; setb (buf, buf + buffer_length + 10, 1); this->in = ∈ this->out = &out; echo_enable = -1; } void echo (int echo_enable) { this->echo_enable = echo_enable; } int echo (); private: istream* in; ostream* out; int echo_enable; virtual int underflow(); }; class estream : public istream { public: // See documentation at beginning of file. estream (istream& in, ostream& out, int buffer_length = ESTREAMBUF_LEN) // : istream // (new estreambuf (in, out, buffer_length)) // Above does not work for g++ { init (new estreambuf (in, out, buffer_length)); // Above is available for g++ } void echo (int echo_enable) { ((estreambuf *) rdbuf()) -> echo (echo_enable); } int echo () { return ((estreambuf *) rdbuf()) -> echo (); } }; #endif // ESTREAM_H