/////// File: TextLines.C /////// #include #include #include #include "TextLines.h" TextLines::TextLines (Uint size /* = 1024 */, Uint ll /* = 256 */) { maxlines = size; linelen = ll; lines = new String[size]; } void TextLines::input(istream& in) { len = read_lines(in); if (len < 0 ) { if (len == -1) cerr << "TextLines: Input file too large\n"; else if (len < 0 ) cerr << "TextLines: Line " << 1-len << " too long\n"; exit(1); } } int TextLines::read_lines(istream& in) { int k, m = linelen + 2; String buf, s; String *lptr = lines, *end = lines + maxlines ; buf = new char[ m ]; while( in.getline(buf, m, '\n') ) { k = strlen(buf); // line length if ( lptr >= end ) // too many lines return(-1); else if ( k == m-1 ) // line too long return(lines - lptr); else if ( (s = new char[k+1]) == NULL ) return(-1); // no free store else { strcpy(s, buf); // copy buffer into s *lptr++ = s; // put s on array lines } } return(lptr - lines); // total number of lines } void TextLines::display() { String *lptr = lines, *end = lines + maxlines ; while(lptr < end) cout << lptr++ << '\n'; } TextLines::~TextLines() { String *lptr = lines; while(len-- > 0) delete [] *lptr++; delete [] lines; }