Strings.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 3k
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.  }