#ifndef LSTRING_H_ #define LSTRING_H_ 1 #include // has assert() #include // has isspace() #include "seq.h" // has Seq template class class lstring { public: typedef unsigned int length_pos; // type for length or position // ----------------------------------------------------------------- // // Constructors. // // ======== ADD CODE HERE ======== // ----------------------------------------------------------------- // // Member functions. // // ======== ADD CODE HERE ======== // ----------------------------------------------------------------- // // Functions to overload operators. // // Stream input and output. friend istream & operator >> (istream & istr, lstring & s); friend ostream & operator << (ostream & ostr, const lstring & s); // ======== ADD CODE HERE ======== private: typedef Seq charseq; charseq seq; // JDO 2000Feb28: Added. Assumed // existence by operator>>. // ----------------------------------------------------------------- // // Member variables. // // ======== ADD CODE HERE ======== // ----------------------------------------------------------------- // // Helper functions. // // Convert C-style string to sequence of chars. // Pre: cstr is a C-style (null-terminated) string. // Post: Returns sequence of chars equivalent to cstr. static charseq cstr_toSeq(const char cstr[]) { if (cstr[0] == '\0') return charseq(); else // use "pointer arithmetic" -- not discussed yet -- // to call recursively with cstr minus its first // character, return charseq(cstr[0], cstr_toSeq(cstr+1)); } // Read sequence of characters from input stream. // (This code was adapted from the input code in unary.h, and // apparent peculiarities are probably an attempt to mimic that // allegedly correct code. Caveat reader.) // Pre: istr has been opened. // nonempty is true if we're in the middle of reading a // sequence of characters, false otherwise. // Post: Returns sequence of characters up to EOF or whitespace. // Sets "failbit" on istr if we didn't read any characters at // all (copied from unary.h code). static charseq cseq_getUpToWS(istream & istr, const bool & nonempty) { char c; if (istr.get(c)) { if (!isspace(c)) return charseq(c, cseq_getUpToWS(istr, true)); else { // whitespace istr.putback(c); if (!nonempty) istr.setstate(std::ios::failbit); return charseq(); } } else { if (istr.eof() && nonempty) // string ends the input istr.clear(std::ios::eofbit); return charseq(); } } // ======== ADD CODE HERE ======== }; // ----------------------------------------------------------------- // // Friend functions. // // Stream input and output operators. istream & operator >> (istream & istr, lstring & s) { // discard leading whitespace istr >> ws; // read until we reach EOF or whitespace lstring::charseq temp = lstring::cseq_getUpToWS(istr, false); // if we got something, save it if (!temp.empty()) s.seq = temp; // return (modified) istream return istr; } // ======== ADD CODE HERE ======== #endif // LSTRING_H_