home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / GCSTRI / GCSTRING.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  4.5 KB  |  203 lines

  1. /**************************************************************************
  2. These C++ classes are copyright 1989, 1990, 1991 by William Herrera.
  3. I hereby release this source code for free distrubution and use.
  4. If you modify it and distribute it, please indicate any changes you
  5. make as your own and the code as copyrighted above.
  6. **************************************************************************/
  7.  
  8. #include <string.h>
  9.  
  10. #include "gcstring.hpp"
  11. #include "error.hpp"
  12.  
  13. gcobject * gcstring::gc = new gcobject;
  14. PointerHeap * gcstring::po = new PointerHeap;
  15.  
  16. void gcstring::Initialize(size_t bufsize, int maxstrings, int gc_interval)
  17. {
  18.     if(gc != NULL)
  19.         delete gc;
  20.     if(po != NULL)
  21.         delete po;
  22.     gc = new gcobject(bufsize, gc_interval);
  23.     po = new PointerHeap(maxstrings);
  24.     if(gc == NULL || po == NULL)
  25.         Error("Cannot allocate memory for gcstring::Initailize");
  26. }
  27.  
  28. gcstring::gcstring()   // string s; default constructor
  29. {
  30.     str = (data_record **)po->Allocate();
  31.     if(str == NULL)
  32.         Error(handle_alloc_err);
  33.     gc->Allocate(1, str);
  34.     if(*str == NULL)
  35.         Error(gcobject_alloc_err, " gcstring()");
  36.     (*str)->data[0] = 0;
  37. }
  38.  
  39. gcstring::gcstring(const char * s)
  40. {
  41.     int len = strlen(s) + 1;
  42.     str = (data_record **)po->Allocate();
  43.     if(str == NULL)
  44.         Error(handle_alloc_err);
  45.     gc->Allocate(len, str);
  46.     if(*str == NULL)
  47.         Error(gcobject_alloc_err, " gcstring(const char *)");
  48.     memcpy((*str)->data, s, len);
  49. }
  50.  
  51. gcstring::gcstring(const gcstring & st)
  52. // copy-initializer avoids dup of char vector.
  53. {
  54.     str = st.str;            // minimizes copying.
  55.     (*str)->ref_count++;    // increment reference count.
  56. }
  57.  
  58.  
  59. gcstring::gcstring(const char * p1, const char * p2)
  60. {
  61.     int len = strlen(p1) + strlen(p2) + 1;
  62.     str = (data_record **)po->Allocate();
  63.     if(str == NULL)
  64.         Error(handle_alloc_err);
  65.     gc->Allocate(len, str);
  66.     if(*str == NULL)
  67.         Error(gcobject_alloc_err,
  68.             " gcstring(const char*,const char*)");
  69.     strcpy((*str)->data, p1);
  70.     strcat((*str)->data, p2);
  71. }
  72.  
  73. gcstring::~gcstring()           // destructor
  74. {
  75.     int & refcount = (*str)->ref_count;
  76.     --refcount;
  77.     if(refcount < 0)
  78.         Error("\ngcstring::~gcstring() deallocation error: refcount < 0");
  79.     else if(refcount == 0)
  80.         po->Deallocate((void **)str);
  81.     gc->IncDeleteCount();
  82. }
  83.  
  84. gcstring & gcstring::operator =(const gcstring & st)
  85. {
  86.     int & refcount = (*str)->ref_count;
  87.     --refcount;
  88.     if(refcount < 0)
  89.         Error("\ngcstring::op=(const gcstring &) : refcount < 0");
  90.     else if(refcount == 0)
  91.         po->Deallocate((void **)str);
  92.     str = st.str;
  93.     (*str)->ref_count++;
  94.     return *this;
  95. }
  96.  
  97. gcstring & gcstring::operator =(const char * s)
  98. {
  99.     gcstring st(s);
  100.     int & refcount = (*str)->ref_count;
  101.     --refcount;
  102.     if(refcount < 0)
  103.         Error("\ngcstring::op=(const char *) : refcount < 0");
  104.     else if(refcount == 0)
  105.         po->Deallocate((void **)str);
  106.     str = st.str;
  107.     (*str)->ref_count++;
  108.     return *this;
  109. }
  110.  
  111. int gcstring::length()
  112. {
  113.     return strlen(GetPtr());
  114. }
  115.  
  116.  
  117. static const char dummychar[] = "\0";
  118. const char & gcstring::operator [](const int i) // reference a char in string.
  119. {
  120.     char * p = GetPtr();
  121.     if(p == NULL || i >= strlen(p) || i < 0)
  122.         return dummychar[0];
  123.     return p[i];
  124. }
  125.  
  126. gcstring gcstring::operator +(const gcstring & st)
  127. {
  128.     gc->Lock();
  129.     gcstring s(GetPtr(), st.GetPtr());
  130.     gc->Unlock();
  131.     return s;    // return by value will copy s.
  132. }
  133.  
  134. int gcstring::compare(const gcstring & s) const
  135. {
  136.     return strcmp(GetPtr(), s.GetPtr());
  137. }
  138.  
  139. int gcstring::compare(const char * p) const
  140. {
  141.     return strcmp(GetPtr(), p);
  142. }
  143.  
  144. gcstring gcstring::uppercase()
  145. {
  146.     gc->CompactBuffer();
  147.     gc->Lock();
  148.     char * p = GetPtr();
  149.     gcstring temp(p);
  150.     p  = temp.GetPtr();
  151.     strupr(p);            // sneaky but hidden...
  152.     gc->Unlock();
  153.     return temp;
  154. }
  155.  
  156. gcstring gcstring::lowercase()
  157. {
  158.     gc->CompactBuffer();
  159.     gc->Lock();
  160.     char * p = GetPtr();
  161.     gcstring temp(p);
  162.     p  = temp.GetPtr();
  163.     strlwr(p);            // sneaky but hidden...
  164.     gc->Unlock();
  165.     return temp;
  166. }
  167.  
  168. gcstring operator +(char * p, gcstring & g)
  169. {
  170.     g.gc->CompactBuffer();
  171.     g.gc->Lock();
  172.     gcstring st(p, g.GetPtr());
  173.     g.gc->Unlock();
  174.     return st;
  175. }
  176.  
  177. gcstring operator +(gcstring & g, char * p)
  178. {
  179.     g.gc->CompactBuffer();
  180.     g.gc->Lock();
  181.     gcstring st(g.GetPtr(), p);
  182.     g.gc->Unlock();
  183.     return st;
  184. }
  185.  
  186. istream & operator >>(istream & ist, gcstring & st)
  187. {
  188.     ist >> ws;
  189.     char buf[1024];
  190.     ist.get(buf, 1023, '\n');
  191.     ist >> ws;
  192.     st = buf;
  193.     return ist;
  194. }
  195.  
  196. ostream & operator <<(ostream & ost, const gcstring & st)
  197. {
  198.     return ost << st.GetPtr();
  199. }
  200.  
  201. // end of file gcstring.cpp
  202.  
  203.