Strings.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 6k
Development Platform:

C++ Builder

  1. class Strings {
  2.    char *p;
  3.    int size;
  4.  public:
  5.    Strings(char *str);
  6.    Strings(void);
  7.    Strings(const Strings &obj);           // Copy constructor
  8.    ~Strings(void) {delete [] p;}
  9.    friend ostream &operator<<(ostream &stream, Strings &obj);
  10.    friend istream &operator>>(istream &stream, Strings &obj);
  11.    Strings operator=(Strings &obj);       // assign a Strings object
  12.    Strings operator=(char *s);            // assign a quoted string
  13.    Strings operator+(Strings &obj);       // concatenate a Strings object
  14.    Strings operator+(char *s);            // concatenate a quoted string
  15.    friend Strings operator+(char *s, Strings &obj);
  16.             /* concatenates a quoted string with a Strings object */
  17.    Strings operator-(Strings &obj);       // subtract a Strings object
  18.    Strings operator-(char *s);            // subtract a quoted string
  19.  /* relational operators between Strings objects. Note that the operators could
  20.     just as easily return bool, rather than int */
  21.    int operator==(Strings &obj) {return !strcmp(p, obj.p);}
  22.    int operator!=(Strings &obj) {return strcmp(p, obj.p);}
  23.    int operator<(Strings &obj) {return strcmp(p, obj.p) < 0;}
  24.    int operator>(Strings &obj) {return strcmp(p, obj.p) > 0;}
  25.    int operator<=(Strings &obj) {return strcmp(p, obj.p) <= 0;}
  26.    int operator>=(Strings &obj) {return strcmp(p, obj.p) >= 0;}
  27.  /* relational operators between Strings object and a quoted character string.
  28.     Note that the operators could just as easily return bool, rather than int */
  29.    int operator==(char *s) {return !strcmp(p, s);}
  30.    int operator!=(char *s) {return strcmp(p, s);}
  31.    int operator<(char *s) {return strcmp(p, s) < 0;}
  32.    int operator>(char *s) {return strcmp(p, s) > 0;}
  33.    int operator<=(char *s) {return strcmp(p, s) <= 0;}
  34.    int operator>=(char *s) {return strcmp(p, s) >= 0;}
  35.    int strsize(void) {return strlen(p);}      // return string size
  36.    void makestr(char *s) (strcpy(s, p);}  // make quoted string from Strings object
  37.    operator char *(void) {return p;}          // conversion to char
  38.  }
  39. Strings::Strings(void)
  40.  {
  41.    size = 1;
  42.    p = new char[size];
  43.    if(!p)
  44.     {
  45.       cout << "Allocation error!" << endl;
  46.       exit(1);
  47.     }
  48.    *p = '';
  49.  }
  50. Strings::Strings(char *str)
  51.  {
  52.    size = strlen(str) + 1;
  53.    p = new char[size];
  54.    if(!p)
  55.     {
  56.       cout << "Allocation error!" << endl;
  57.       exit(1);
  58.     }
  59.    strcpy(p, str);
  60.  }
  61. Strings::Strings(const Strings &obj)
  62.  {
  63.    size = obj.size;
  64.    p = new char[size];
  65.    if(!p)
  66.     {
  67.       cout << "Allocation error!" << endl;
  68.       exit(1);
  69.     }
  70.    strcpy(p, obj.p);
  71.  }
  72. ostream &operator<<(ostream &stream, Strings &obj)
  73.  {
  74.    stream << obj.p;
  75.    return stream;
  76.  }
  77. istream &operator>>(istream &stream, Strings &obj)
  78.  {
  79.    char t[255];      // arbitrary string length--yours can be larger
  80.    int len;
  81.    for(len=0; len<255; len++)
  82.     {
  83.       stream.get(t[len]);
  84.       if(t[len]=='n')
  85.          break;
  86.       if(t[len]=='b')
  87.          if(len)
  88.           {
  89.             len--;
  90.             cout << "'b'";
  91.           }
  92.     }
  93.    t[len]='';
  94.    len++;
  95.    if(len>obj.size)
  96.     {
  97.       delete obj.p;
  98.       obj.p = new char[len];
  99.       if(!obj.p)
  100.        {
  101.          cout << "Allocation error!" << endl;
  102.          exit(1);
  103.        }
  104.       obj.size = len;
  105.     }
  106.    strcpy(obj.p, t);
  107.    return stream;
  108.  }
  109. Strings Strings::operator=(Strings &obj)
  110.   {
  111.    Strings temp(obj.p);
  112.    if(obj.size > size)
  113.     {
  114.       delete p;
  115.       p = new char[obj.size];
  116.       size = obj.size;
  117.       if(!p)
  118.        {
  119.          cout << "Allocation error!" << endl;
  120.          exit(1);
  121.        }
  122.     }
  123.    strcpy(p, obj.p);
  124.    strcpy(temp.p, obj.p);
  125.    return temp;
  126.  }
  127. Strings Strings::operator=(char *s)
  128.   {
  129.    int len = strlen(s) + 1;
  130.    if(size < len)
  131.     {
  132.       delete p;
  133.       p = new char[len];
  134.       size = len;
  135.       if(!p)
  136.        {
  137.          cout << "Allocation error!" << endl;
  138.          exit(1);
  139.        }
  140.     }
  141.    strcpy(p, s);
  142.    return *this;
  143.  }
  144. Strings Strings::operator+(Strings &obj)
  145.  {
  146.    int len;
  147.    Strings temp;
  148.    delete temp.p;
  149.    len = strlen(obj.p) + strlen(p) + 1;
  150.    temp.p = new char[len];
  151.    temp.size = len;
  152.    if(!temp.p)
  153.     {
  154.       cout << "Allocation error!" << endl;
  155.       exit(1);
  156.     }
  157.    strcpy(temp.p, p);
  158.    strcat(temp.p, obj.p);
  159.    return temp;
  160.  }
  161. Strings Strings::operator+(char *s)
  162.  {
  163.    int len;
  164.    Strings temp;
  165.    delete temp.p;
  166.    len = strlen(s) + strlen(p) + 1;
  167.    temp.p = new char[len];
  168.    temp.size = len;
  169.    if(!temp.p)
  170.     {
  171.       cout << "Allocation error!" << endl;
  172.       exit(1);
  173.     }
  174.    strcpy(temp.p, p);
  175.    strcat(temp.p, s);
  176.    return temp;
  177.  }
  178. Strings operator+(char *s, Strings &obj)
  179.  {
  180.    int len;
  181.    Strings temp;
  182.    delete temp.p;
  183.    len = strlen(s) + strlen(p) + 1;
  184.    temp.p = new char[len];
  185.    temp.size = len;
  186.    if(!temp.p)
  187.     {
  188.       cout << "Allocation error!" << endl;
  189.       exit(1);
  190.     }
  191.    strcpy(temp.p, s);
  192.    strcat(temp.p, obj.p);
  193.    return temp;
  194.  }
  195. Strings Strings::operator-(Strings &substr)
  196.  {
  197.    Strings temp(p);
  198.    char *s1;
  199.    int i,j;
  200.    s1 = p;
  201.    for(i=0; *s1; i++)
  202.     {
  203.       if(*s1!=*substr.p)
  204.        {
  205.          temp.p[i] = *s1;
  206.          s1++;
  207.        }
  208.       else
  209.        {
  210.          for(j=0; substr.p[j]==s1[j] && substr.p[j]; j++)
  211.             ;
  212.          if(!substr.p[j])
  213.           {
  214.             s1 += j;
  215.             i--;
  216.           }
  217.          else
  218.           {
  219.             temp.p[i] = *s1;
  220.             s1++;
  221.           }
  222.        }
  223.     }
  224.     temp.p[i] = '';
  225.     return temp;
  226.  }
  227. Strings Strings::operator-(char *substr)
  228.  {
  229.    Strings temp(p);
  230.    char *s1;
  231.    int i,j;
  232.    s1 = p;
  233.    for(i=0; *s1; i++)
  234.     {
  235.       if(*s1!=*substr)
  236.        {
  237.          temp.p[i] = *s1;
  238.          s1++;
  239.        }
  240.       else
  241.        {
  242.          for(j=0; substr[j]==s1[j] && substr[j]; j++)
  243.             ;
  244.          if(!substr[j])
  245.           {
  246.             s1 += j;
  247.             i--;
  248.           }
  249.          else
  250.           {
  251.             temp.p[i] = *s1;
  252.             s1++;
  253.           }
  254.        }
  255.     }
  256.     temp.p[i] = '';
  257.     return temp;
  258.  }