#ifndef STRS_H #define STRS_H /////////////////////////////////////////////////////////////// // // // strs.h - text string class // // v2.8 // // manual page // // // // Language: Visual C++ ver 6, Borland C++, ver 5 // // Platform: Gateway 386/25 // // MS-DOS 6.2, win 95.400.950 // // Application: CSE 687 // // Author: Jim Fawcett, Syracuse University // // (315)443-2654, jfawcett@cat.syr.edu // // // // copyright (c) 1997-1999 // // all rights granted to users except right to publish // // // /////////////////////////////////////////////////////////////// /* strs class ========== This class provides facilities for creating, processing, and destroying null terminated character strings. constructors: str s; construct with 10 char alloc str s(15); construct with 15 char alloc str s2 = s1; copy constructor str s2 = "a string"; promotion constructor modifying string state: s.append(ch); append ch to end, expand mem s.del(index); delete char at index s.flush(); set logical length to zero s2 = s1; s2 = "a string"; assign new state to s2 s2 += s1; s2 += "a string"; concatenate rhs on lhs char ch = s[3] = 't'; read, write fourth char setLex(line); extract by line or word input >> s; collect one word from input s.len(n); set logical len, fill w/spaces s.size(n); set physical alloc to n chars displaying str state: cout << s << endl; insert str into cout const char *st = s.cStr(); return pointer to const chars s.len(); s.size(); return logical, physical length s.showState(); display current state int ok = s.check(); ok == 1 if s is valid str */ // /////////////////////////////////////////////////////////////// // // // Build Process: // // // // Files required for building test stub: // // strs.hpp, strs.cpp // // // // - command line build, no memory allocation test: // // cl /GX -DTEST_STRS strs.cpp // // // // - command line build, memory allocation test: // // cl /GX -DTEST_STRS -DTEST_ALLOC strs.cpp // // // /////////////////////////////////////////////////////////////// /* maintenance history: ==================== - v2.8 : 22 Jan 1999 . revised maintenance page build process - v2.7 : 24 Sep 1997 . refixed the same bug. My config mgmt isn't very good. - v2.6 : 27 Feb 1997 . fixed bug in _alloc::size(...) which will not affect any str member functions but would cause an error if a member function was added that reduced the size of allocated memory. Thanks for the bug report Yong Fan. - v2.5 : 21 Feb 1997 . fixed bugs in operator>>(...) which cause leading space and extraction of EOF char . fixed bug in del(index) which ate terminating null Thanks for bug reports Paul Kronenwetter and Jayme Manning - v2.4 : 15 Feb 1997 . got rid of a tricky construction process which saved writing code for a copy ctor in _alloc. It worked, but was not very intuitive so _alloc now has one. . the void construction in cmpstrs, manstrs, and mixins, had a bug, now fixed. Thanks Bush, Basu, and Dhalal for bug reports. . fixed bug in _alloc::size(...) - v2.3 : 6 Feb 1997 . simplified member code by making the substitution allocator.array()[] --> allocator[] - v2.2 : 28 Jan 1997 . added del(int index) - v2.1 : 27 Jan 1997 . added setLex(line) to extract by line or word - v2.0 : 19 Jan 1997 . factored out basic strs capability from estrs module to replace v1.0 (done to demonstrate mixin strategy) - v1.0 : 19 Jan 1997 . created strs module */ // /////////////////////////////////////////////////////////////// // // // declarations // // // // class str delegates its memory transactions to its // // data member allocator, of type _alloc. // // // // Class _alloc handles all memory allocation, and dealloc- // // ation. It also provides character handling with bounds // // checking on all reads and writes. // // // /////////////////////////////////////////////////////////////// class _alloc { public: _alloc(const _alloc& a); _alloc(int size=10); ~_alloc(); char& operator[](int n); _alloc& operator= (const _alloc &a); void size (int Size, int Force=0, int New=0); int size () const; char* array() const; static int alloc(); private: char* _array; // pointer to char storage int _size; // size of allocation in chars static int _bytes_alloc; // total allocation for all allocators // -- value shared by all objects }; inline int _alloc::alloc() { return _bytes_alloc; } inline int _alloc::size () const { return _size; } inline char* _alloc::array() const { return _array; } // #include // <<, >>, cout class str { public: str(int size=10); str(const str &s); str(const char *s); virtual ~str(); str& append(const char &c, int count=1); str& del(int index); int size () const; void size (int Size, int Force=0); int len () const; str& len (int n); str& flush(); char& operator[](int n); char operator[](int n) const; str& operator= (const str &s); str& operator+=(const str &s); str& operator= (const char *s); str& operator+=(const char *s); const char* cStr() const; enum lexType { line, word }; static void setLex(lexType lt); friend istream& operator>>(istream& in, str &s); friend ostream& operator<<(ostream& out, const str &s); int check () const; void showState() const; static int alloc (); protected: _alloc allocator; // char memory manager int _next; // number of chars static lexType lexer; // extraction by line or word static int isWhite(char ch); }; // inline int str::alloc() { return _alloc::alloc(); } inline str& str::flush() { _next = 0; allocator.array()[0] = '\0'; return *this; } inline int str::len () const { return _next; } inline int str::size() const { return allocator.size(); } inline void str::size(int Size, int Force) { allocator.size(Size+1, Force); } inline const char* str::cStr() const { return allocator.array(); } inline char& str::operator[](int n) { return allocator.array()[n]; } inline char str::operator[](int n) const { return allocator.array()[n]; } inline void str::setLex(lexType lt) { lexer = lt; } // /////////////////////////////////////////////////////////////// // // // Design Notes // // // /////////////////////////////////////////////////////////////// /* This class provides facilities for creating, processing, and destroying null terminated character strings. - strs design allows clients to declare and construct many instances of string objects at run time as well as statically at compile time. - strs recognizes two sizes: - logical str length, _next, which equals number of chars in str, not including terminating null - physical str size, _size, which equals size of allocated memory, in character spaces <------------ _size --------> [ a b c d e f g 0 ] <-- _next --> This means that the memory manager, which is slow, does not necessarily have to be called every time str length changes. A client designer can construct strs with enough memory to handle common applications, calling the memory manager only when unusually large requests occur. - Appends will automatically expand physical memory when needed. Automatic expansions always double existing allocation. - _next and _size can be determined at run time and readjusted by client code, using the len(n), size(n) member functions. - An index operation is defined for strs and returns a refer- ence to char at the cited index if in bounds, otherwize a safe static char is returned. Note that references returned by indexing should be used only immediately after index opera- tions since resizing physical memory will invalidate them. */ #endif