/////// 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 " << -len-1 << " too long\n"; exit(1); } } int TextLines::read_lines(istream& in) { int k, error=0, m = linelen + 2; String buf, s; String *lptr = lines, *end = lines + maxlines ; if ( (buf = new char[m]) == NULL ) return(-1); while( in.getline(buf, m, '\n') ) { k = strlen(buf) + 1; // ?? // line length if ( lptr >= end ) { error = -1; break; } // too many lines (1) else if ( k == m-1 ) { error = (lines-lptr)-2; break; } // line too long (2) else if ( (s = new char[k])==NULL ) { error = -1; break; } // no free store else // copy buffer into s { strcpy(s, buf); *lptr++ = s; } // put s on lines (3) } delete [] buf; return(error ? error : lptr-lines); // error or lines read (4) } void TextLines::display() { String *lptr = lines, *end = lines + len ; while(lptr < end) cout << *lptr++ << '\n'; } TextLines::~TextLines() { String *lptr = lines; while(len-- > 0) delete [] *lptr++; delete [] lines; }