home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 2.ddi / GCOBJECT.ZIP / GCSTRING.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-09  |  3.4 KB  |  142 lines

  1. /**************************************************************************
  2. These C++ classes are copyright 1989, 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and used for any purpose.
  7. contacts:
  8. GENIE
  9. FidoNet: c_plusplus
  10. **************************************************************************/
  11. #include "gcstring.hpp"
  12.  
  13. void gcstring::Initialize(unsigned int bufsize, int gc_interval)
  14. {
  15.     gcobject::Initialize(bufsize, gc_interval);
  16. }
  17.  
  18. gcstring::gcstring()   // string s; default constructor
  19. {
  20.     str = new (data_record *);
  21.     if(str == NULL)
  22.         FatalError(heap_alloc_err);
  23.     *str = Allocate(1, str);
  24.     if(*str == NULL)
  25.         FatalError(gcobject_alloc_err, " gcstring()");
  26.     (*str)->data[0] = 0;
  27. }
  28.  
  29. gcstring::gcstring(const char * s)
  30. {
  31.     int len = strlen(s) + 1;
  32.     str = new (data_record *);
  33.     if(str == NULL)
  34.         FatalError(heap_alloc_err);
  35.     *str = Allocate(len, str);
  36.     if(*str == NULL)
  37.         FatalError(gcobject_alloc_err, " gcstring(const char *)");
  38.     memcpy((*str)->data, s, len);
  39. }
  40.  
  41. gcstring::gcstring(const gcstring &st)
  42. // copy-initializer avoids dup of char vector.
  43. {
  44.     str = st.str;           // minimizes copying.
  45.     (*str)->ref_count++;    // increment reference count.
  46. }
  47.  
  48. gcstring::gcstring(const char * p1, const char * p2)
  49. {
  50.     int len = strlen(p1) + strlen(p2) + 1;
  51.     str = new (data_record *);
  52.     if(str == NULL)
  53.         FatalError(heap_alloc_err);
  54.     *str = Allocate(len, str);
  55.     if(*str == NULL)
  56.         FatalError(gcobject_alloc_err, " gcstring(const char*,const char*)");
  57.     strcpy((*str)->data, p1);
  58.     strcat((*str)->data, p2);
  59. }
  60.  
  61. gcstring::~gcstring()           // destructor
  62. {
  63.     int & refcount = (*str)->ref_count;
  64.     --refcount;
  65.     if(refcount < 0)
  66.         FatalError("deallocate error");
  67.     else if(refcount == 0)
  68.         delete str;
  69.     IncDeleteCount();
  70. }
  71.  
  72. gcstring gcstring::operator =(const gcstring & st)
  73. {
  74.     (*str)->ref_count--;
  75.     str = st.str;
  76.     (*str)->ref_count++;
  77.     return *this;
  78. }
  79.  
  80. gcstring gcstring::operator =(const char * s)
  81. {
  82.     (*str)->ref_count--;
  83.     int len = strlen(s) + 1;
  84.     str = new (data_record *);
  85.     if(str == NULL)
  86.         FatalError(heap_alloc_err);
  87.     *str = Allocate(len, str);
  88.     if(*str == NULL)
  89.         FatalError(gcobject_alloc_err, " operator=gcstring&");
  90.     memcpy((*str)->data, s, len);
  91.     return *this;
  92. }
  93.  
  94. int gcstring::operator [](const int i) // reference a char in string.
  95. {
  96.     char * p = (*str)->data;
  97.     return (p == NULL || i > strlen(p)) ? '\0' : p[i];
  98. }
  99.  
  100. gcstring gcstring::operator +(const gcstring & st)
  101. {
  102.     gcstring s((char *)(*str)->data, (char *)(*(st.str))->data);
  103.     return s;    // return by value will copy s.
  104. }
  105.  
  106. int gcstring::cmp(const gcstring & s)
  107. {
  108.     return strcmp((*str)->data, (*(s.str))->data);
  109. }
  110.  
  111. int gcstring::cmp(char * p)
  112. {
  113.     return strcmp((*str)->data, p);
  114. }
  115.  
  116. istream & operator >>(istream & ist, gcstring & st)
  117. {
  118. #ifdef __ZTC__
  119.     whitespace WS;
  120.     ist >> WS;
  121.     char buf[256];
  122.     ist.get(buf, 255, '\r');
  123.     ist >> WS;
  124.     st = buf;
  125.     return ist;
  126. #else ifdef __TURBOC__
  127.     ist >> ws;
  128.     char buf[256];
  129.     ist.get(buf, 255, '\n');
  130.     ist >> ws;
  131.     st = buf;
  132.     return ist;
  133. #endif
  134. }
  135.  
  136. ostream & operator <<(ostream & ost, const gcstring & st)
  137. {
  138.     return ost << (char *)(*st.str)->data;
  139. }
  140.  
  141. // end of file gcstring.cpp
  142.