home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / STRINGS.CPP < prev    next >
C/C++ Source or Header  |  1993-11-13  |  3KB  |  171 lines

  1. // -------- strings.cpp
  2.  
  3. #include "strings.h"
  4.  
  5. // ------- put a string into a String
  6. void String::putstr(const char *s)
  7. {
  8.     delete sptr;
  9.     length = 0;
  10.     sptr = 0;
  11.     if (s)    {
  12.         length = strlen(s);
  13.         sptr = new char[length+1];
  14.     strcpy(sptr, s);
  15.     }
  16. }
  17.  
  18. // --- convert from char *
  19. String::String(const char *s)
  20. {
  21.     sptr = 0;
  22.     putstr(s);
  23. }
  24.  
  25. // ------- copy constructor
  26. String::String(String& s)
  27. {
  28.     sptr = 0;
  29.     putstr(s.sptr);
  30. }
  31.  
  32. // -------- construct with a size and fill character
  33. String::String(int len, char fill)
  34. {
  35.     length = len;
  36.     sptr = new char[length+1];
  37.     memset(sptr, fill, length);
  38.     *(sptr+len) = '\0';
  39. }
  40.  
  41. // -------- assignment
  42. String& String::operator=(String &s)
  43. {
  44.     if (this != &s)
  45.         putstr(s.sptr);
  46.     return *this;
  47. }
  48.  
  49. // ------- concatenation operator (str1 + str2)
  50. String String::operator+(String &s)
  51. {
  52.     int ln = 0;
  53.     if (sptr)
  54.         ln += strlen(sptr);
  55.     if (s.sptr)
  56.         ln += strlen(s.sptr);
  57.     String temp(ln);
  58.     if (sptr)
  59.         strcpy(temp.sptr, sptr);
  60.     if (s.sptr)
  61.         strcat(temp.sptr, s.sptr);
  62.     return temp;
  63. }
  64.  
  65. // --- concatenation operator (str1 += str2;)
  66. void String::operator+=(String& s)
  67. {
  68.     String temp = *this;
  69.     temp = temp + s;
  70.     *this = temp;
  71. }
  72.  
  73. // --- concatenation operator (str1 + "str")
  74. String String::operator+(const char *s)
  75. {
  76.     String s1(s);
  77.     String temp = *this;
  78.     return temp + s1;
  79. }
  80.  
  81. // --- concatenation operator (str1 += "str")
  82. void String::operator+=(const char *s)
  83. {
  84.     String s1(s);
  85.     String temp = *this + s1;
  86.     *this = temp;
  87. }
  88.  
  89. // ------ substring: right len chars
  90. String String::right(int len)
  91. {
  92.     String tmp;
  93.     if (sptr && len)    {
  94.         int sln = strlen(sptr);
  95.         if (len > sln)
  96.             len = sln;
  97.         if (len)
  98.             tmp = String(sptr + sln - len);
  99.     }
  100.     return tmp;
  101. }
  102.  
  103. // ------ substring: left len chars
  104. String String::left(int len)
  105. {
  106.     String tmp(len);
  107.     if (sptr && len)    {
  108.         int sln = strlen(sptr);
  109.         if (len > sln)
  110.             len = sln;
  111.         if (len)
  112.             strncpy(tmp.sptr, sptr, len);
  113.     }
  114.     return tmp;
  115. }
  116.  
  117. // ------ substring: middle len chars starting from where
  118. String String::mid(int len, int where)
  119. {
  120.     String tmp(len);
  121.     if (len && sptr && where < (int) strlen(sptr))
  122.         strncpy(tmp.sptr,sptr+where,len);
  123.     return tmp;
  124. }
  125.  
  126. // ---- find offset to first instance of specified char
  127. int String::FindChar(unsigned char ch)
  128. {
  129.     if (sptr)    {
  130.         char *cp = strchr(sptr, ch);
  131.         if (cp)
  132.             return (int) (cp - sptr);
  133.     }
  134.     return -1;
  135. }
  136.  
  137. // ------ change the buffer length of a string
  138. void String::ChangeLength(unsigned int newlen)
  139. {
  140.     if (length == 0)
  141.         *this = String(newlen);
  142.     else    {
  143.         char *cp = new char[newlen+1];
  144.         memset(cp, '\0', newlen+1);
  145.         if (sptr != 0)    {
  146.             strncpy(cp, sptr, newlen);
  147.             delete sptr;
  148.         }
  149.         sptr = cp;
  150.         length = newlen;
  151.     }
  152. }
  153.  
  154. // ------- stream I/O
  155. ostream& operator<< (ostream& os, String& str)
  156. {
  157.     os << str.sptr;
  158.     return os;
  159. }
  160.  
  161. istream& operator>> (istream& is, String& str)
  162. {
  163.     *str.sptr = '\0';
  164.     while (*str.sptr == '\0' && !is.eof())
  165.         is.getline(str.sptr, str.length+1);
  166.     return is;
  167. }
  168.  
  169.  
  170.  
  171.