/////// File: TLine_OS.C /////// #include #include #include #include "TLine_OS.h" TextLine_OS::TextLine_OS (Uint pos /* = 0*/, Uint size /* = 1024 */, Uint ll /* = 256 */, String dlm /* = DELIM */) : key(pos, dlm) { maxlen = size; linelen = ll; lines = new String[size]; } String TextLine_OS::operator[](Uint i) { if ( ! sorted() ) sort(); return( i < len ? lines[i] : NULL ); } void TextLine_OS::swap(Uint i, Uint j) { String tmp = lines[i]; lines[i] = lines[j]; lines[j] = tmp; } void TextLine_OS::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 TextLine_OS::read_lines(istream& in) { int k, m = linelen + 2; String buf, s; String *lptr = lines, *end = lines + maxlen ; 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]) == NULL ) return(-1); // no free store else { strcpy(s, buf); // copy buffer into s *lptr++ = s; // put s on array lines } } delete buf; return(lptr - lines); // total number of lines } void TextLine_OS::display(ostream& out) { String *lptr = lines, *end = lines + length() ; if ( ! sorted() ) sort(); while(lptr < end) out << *lptr++ << '\n'; } TextLine_OS::~TextLine_OS() { String *lptr = lines; while(len-- > 0) delete [] *lptr++; delete [] lines; } int TextLine_OS::append(void* line) // append entry at end { if ( len < maxlen ) { lines[len++] = (char*) line; return(0); } return(-1); // pointer array full } void TextLine_OS::remove(Uint i) // delete entry i { delete [] lines[i]; for ( int j=i ; j < len ; j++ ) lines[j] = lines[j+1]; len--; }