home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- These C++ classes are copyright 1989, 1990, by William Herrera.
- All those who put this code or its derivatives in a commercial product MUST
- mention this copyright in their documentation for users of the products in
- which this code or its derivative classes are used. Otherwise, this code
- may be freely distributed and used for any purpose.
- contacts:
- GENIE
- FidoNet: c_plusplus
- **************************************************************************/
- #include "gcstring.hpp"
-
- void gcstring::Initialize(unsigned int bufsize, int gc_interval)
- {
- gcobject::Initialize(bufsize, gc_interval);
- }
-
- gcstring::gcstring() // string s; default constructor
- {
- str = new (data_record *);
- if(str == NULL)
- FatalError(heap_alloc_err);
- *str = Allocate(1, str);
- if(*str == NULL)
- FatalError(gcobject_alloc_err, " gcstring()");
- (*str)->data[0] = 0;
- }
-
- gcstring::gcstring(const char * s)
- {
- int len = strlen(s) + 1;
- str = new (data_record *);
- if(str == NULL)
- FatalError(heap_alloc_err);
- *str = Allocate(len, str);
- if(*str == NULL)
- FatalError(gcobject_alloc_err, " gcstring(const char *)");
- memcpy((*str)->data, s, len);
- }
-
- gcstring::gcstring(const gcstring &st)
- // copy-initializer avoids dup of char vector.
- {
- str = st.str; // minimizes copying.
- (*str)->ref_count++; // increment reference count.
- }
-
- gcstring::gcstring(const char * p1, const char * p2)
- {
- int len = strlen(p1) + strlen(p2) + 1;
- str = new (data_record *);
- if(str == NULL)
- FatalError(heap_alloc_err);
- *str = Allocate(len, str);
- if(*str == NULL)
- FatalError(gcobject_alloc_err, " gcstring(const char*,const char*)");
- strcpy((*str)->data, p1);
- strcat((*str)->data, p2);
- }
-
- gcstring::~gcstring() // destructor
- {
- int & refcount = (*str)->ref_count;
- --refcount;
- if(refcount < 0)
- FatalError("deallocate error");
- else if(refcount == 0)
- delete str;
- IncDeleteCount();
- }
-
- gcstring gcstring::operator =(const gcstring & st)
- {
- (*str)->ref_count--;
- str = st.str;
- (*str)->ref_count++;
- return *this;
- }
-
- gcstring gcstring::operator =(const char * s)
- {
- (*str)->ref_count--;
- int len = strlen(s) + 1;
- str = new (data_record *);
- if(str == NULL)
- FatalError(heap_alloc_err);
- *str = Allocate(len, str);
- if(*str == NULL)
- FatalError(gcobject_alloc_err, " operator=gcstring&");
- memcpy((*str)->data, s, len);
- return *this;
- }
-
- int gcstring::operator [](const int i) // reference a char in string.
- {
- char * p = (*str)->data;
- return (p == NULL || i > strlen(p)) ? '\0' : p[i];
- }
-
- gcstring gcstring::operator +(const gcstring & st)
- {
- gcstring s((char *)(*str)->data, (char *)(*(st.str))->data);
- return s; // return by value will copy s.
- }
-
- int gcstring::cmp(const gcstring & s)
- {
- return strcmp((*str)->data, (*(s.str))->data);
- }
-
- int gcstring::cmp(char * p)
- {
- return strcmp((*str)->data, p);
- }
-
- istream & operator >>(istream & ist, gcstring & st)
- {
- #ifdef __ZTC__
- whitespace WS;
- ist >> WS;
- char buf[256];
- ist.get(buf, 255, '\r');
- ist >> WS;
- st = buf;
- return ist;
- #else ifdef __TURBOC__
- ist >> ws;
- char buf[256];
- ist.get(buf, 255, '\n');
- ist >> ws;
- st = buf;
- return ist;
- #endif
- }
-
- ostream & operator <<(ostream & ost, const gcstring & st)
- {
- return ost << (char *)(*st.str)->data;
- }
-
- // end of file gcstring.cpp
-