home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / classsource.lha / ClassSource / DataStructures / String / String.c
Encoding:
C/C++ Source or Header  |  1995-02-12  |  3.6 KB  |  219 lines

  1. #include <classes/DataStructures/String.h>
  2.  
  3. #include <string.h>
  4. #include <iostream.h>
  5.  
  6. StringC::StringC(const STRPTR s)
  7.     : len(s ? strlen(s) : 0),
  8.       buffer(s ? len+1 : 64)
  9. {
  10.     if (s)
  11.         memcpy(buffer.buffer(),s,len+1)
  12.     else
  13.         *((UBYTE *) buffer.buffer()) = 0;
  14. }
  15.  
  16. StringC::StringC(ULONG l, STRPTR s)
  17.     : len(s ? l : 0),
  18.       buffer(s ? len+1 : 64)
  19. {
  20.     if (s)
  21.     {
  22.         memcpy(buffer.buffer(),s,len);
  23.         *((UBYTE *) buffer.buffer() + len) = 0;
  24.     }
  25.     else
  26.         *((UBYTE *) buffer.buffer()) = 0;
  27. }
  28.  
  29. StringC::StringC(const StringC &s)
  30.     : len(s.len),
  31.       buffer(s.buffer)
  32. {
  33. }
  34.  
  35. StringC::StringC(UBYTE c)
  36.     : len(c ? 1 : 0 ),
  37.       buffer(64)
  38. {
  39.     *((UBYTE *) buffer.buffer()) = c;
  40.     *((UBYTE *) buffer.buffer() + 1) = 0;
  41. }
  42.  
  43. StringC::~StringC()
  44. {
  45. }
  46.  
  47. StringC::operator STRPTR() const
  48. {
  49.     return (STRPTR) buffer.buffer();
  50. }
  51.  
  52. StringC &StringC::operator= (const StringC &s)
  53. {
  54.     if (this != &s)
  55.     {
  56.         len = s.len;
  57.         buffer = s.buffer;
  58.     };
  59.     return *this;
  60. }
  61.  
  62. StringC &StringC::operator= (const STRPTR s)
  63. {
  64.     if (s)
  65.     {
  66.         len = strlen(s);
  67.         buffer.setBufferSize(len+1);
  68.         memcpy(buffer.buffer(),s,len+1);
  69.     }
  70.     else {
  71.         buffer.setBufferSize(64);
  72.         *((UBYTE *) buffer.buffer()) = 0;
  73.     };
  74.     return *this;
  75. }
  76.  
  77. StringC &StringC::operator+= (const StringC &s)
  78. {
  79.     if (s)
  80.     {
  81.         buffer.setBufferSize(len+s.len+1);
  82.         memcpy(((UBYTE *) buffer.buffer()) + len,s.buffer.buffer(),s.len+1);
  83.         len += s.len;
  84.     };
  85.     return *this;
  86. }
  87.  
  88. StringC &StringC::operator+= (const STRPTR s)
  89. {
  90.     if (s)
  91.     {
  92.         ULONG l = strlen(s);
  93.         buffer.setBufferSize(len+l+1);
  94.         memcpy(((UBYTE *) buffer.buffer()) + len,s,l+1);
  95.         len += l;
  96.     };
  97.     return *this;
  98. }
  99.  
  100. UBYTE &StringC::operator[] (ULONG i)
  101. {
  102.     while (i >= buffer.size())
  103.         buffer.doubleBuffer();
  104.     return (UBYTE &) *((UBYTE *) buffer.buffer() + i);
  105. }
  106.  
  107. ULONG StringC::length() const
  108. {
  109.     return len;
  110. }
  111.  
  112. ULONG StringC::bufsize() const
  113. {
  114.     return buffer.size();
  115. }
  116.  
  117. StringC StringC::left(ULONG i) const
  118. {
  119.     if (i > len)
  120.         i = len;
  121.     return StringC(i,(STRPTR) buffer.buffer());
  122. }
  123.  
  124. StringC StringC::right(ULONG i) const
  125. {
  126.     if (i > len)
  127.         i = len;
  128.     return StringC(i,(STRPTR) buffer.buffer()+len-i);
  129. }
  130.  
  131. StringC StringC::mid(ULONG i, ULONG j) const
  132. {
  133.     if (i > len)
  134.         i = len;
  135.     if (i + j > len)
  136.         j = len - i;
  137.     return StringC(i,(STRPTR) buffer.buffer()+j);
  138. }
  139.  
  140. VOID StringC::doubleBuffer()
  141. {
  142.     buffer.doubleBuffer();
  143. }
  144.  
  145. VOID StringC::shrinkBuffer()
  146. {
  147.     len = strlen((STRPTR) buffer.buffer());
  148.     buffer.setBufferSize(len+1);
  149. }
  150.  
  151. VOID StringC::setBufferSize(ULONG i)
  152. {
  153.     buffer.setBufferSize(i);
  154.     if (i <= len)
  155.     {
  156.         len = i - 1;
  157.         *((UBYTE *) buffer.buffer() + len) = 0;
  158.     };
  159. }
  160.  
  161. // *************************************************************
  162.  
  163. StringC operator+ (const StringC &s1, const StringC &s2)
  164. {
  165.     StringC s = s1;
  166.     s += s2;
  167.     return s;
  168. }
  169.  
  170. // *************************************************************
  171.  
  172. BOOL operator== (const StringC &s1, const StringC &s2)
  173. {
  174.     return strcmp((STRPTR) s1,(STRPTR) s2) == 0;
  175. }
  176.  
  177. BOOL operator!= (const StringC &s1, const StringC &s2)
  178. {
  179.     return strcmp((STRPTR) s1,(STRPTR) s2) != 0;
  180. }
  181.  
  182. BOOL operator< (const StringC &s1, const StringC &s2)
  183. {
  184.     return strcmp((STRPTR) s1,(STRPTR) s2) < 0;
  185. }
  186.  
  187. BOOL operator> (const StringC &s1, const StringC &s2)
  188. {
  189.     return strcmp((STRPTR) s1,(STRPTR) s2) > 0;
  190. }
  191.  
  192. BOOL operator<= (const StringC &s1, const StringC &s2)
  193. {
  194.     return strcmp((STRPTR) s1,(STRPTR) s2) <= 0;
  195. }
  196.  
  197. BOOL operator>= (const StringC &s1, const StringC &s2)
  198. {
  199.     return strcmp((STRPTR) s1,(STRPTR) s2) >= 0;
  200. }
  201.  
  202. // *************************************************************
  203.  
  204. class ostream &operator<< (ostream &o, const StringC &s)
  205. {
  206.     o << (char *) (STRPTR) s;
  207.     return o;
  208. }
  209.  
  210. // *************************************************************
  211.  
  212. class istream &operator>> (istream &i, StringC &s)
  213. {
  214.     char c[STREAM_MAXSTRING];
  215.     i >> c;
  216.     s = (STRPTR) c;
  217.     return i;
  218. }
  219.