home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / cppdemo.lha / C++ / strlibdemo.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-30  |  2.1 KB  |  110 lines

  1. #pragma +
  2.  
  3. class String
  4. {  char *str;
  5.    int len;
  6.    void free();
  7.    void alloc (const char*);
  8.    String (int, char*);
  9.  public:
  10.    String (const char* = 0);
  11.    String (const String &);
  12.    ~String();
  13.    operator char*() const
  14.     { if (str) return str;
  15.       else return "";
  16.     }
  17.    String &operator = (const String &);
  18.    char operator[] (int i) const;
  19.    int length() const
  20.      { return len; }
  21.    friend String operator + (const String &s1, const String &s2);
  22. };
  23.  
  24.  
  25. int operator == (const String &s1, const String &s2);
  26.  
  27. class ostream& operator << (ostream&, const String&);
  28.  
  29. #include <string.h>
  30. #include <stream.h>
  31.  
  32. void String::alloc (const char *initstr)
  33.  { if (initstr)
  34.     { len = strlen(initstr);
  35.       str = new char[len+1];
  36.       if (str)
  37.         strcpy(str, initstr);
  38.       else
  39.         len = 0;
  40.     }
  41.    else
  42.     { len = 0;
  43.       str = 0;
  44.     }
  45.  }
  46.  
  47. void String::free()
  48. { if (str)
  49.     { delete [] str;
  50.       str = 0;
  51.     }
  52. }
  53.  
  54. String::String (int len, char *str) : len(len), str(str) { }
  55.  
  56. String::String (const char *initstr)
  57.  { alloc(initstr); }
  58.  
  59. String::String (const String &s)
  60.  { alloc (s.str); }
  61.  
  62. String::~String()
  63.  { free(); }
  64.  
  65. String operator + (const String &s1, const String &s2)
  66. {
  67.   int newlen = s1.length()+s2.length();
  68.   char *newstr = new char[newlen+1];
  69.   if (newstr)
  70.    { strncpy (newstr,s1,s1.length());
  71.      strncat (newstr,s2,s2.length())
  72.    }
  73.   return String(newlen, newstr);
  74. }
  75.  
  76. String &String::operator = (const String &s)
  77.  { free();
  78.    alloc(s.str);
  79.    return *this;
  80.  }
  81.  
  82. char String::operator[] (int index) const
  83.  { if(str && unsigned(index) <= unsigned(len) )
  84.      return str[index];
  85.    else
  86.      return '\0';
  87.  }
  88.  
  89. int operator == (const String &s1, const String &s2)
  90. { return !strcmp(s1,s2); }
  91.  
  92. ostream &operator << (ostream& out, const String& s)
  93. { return out << (char*)s; }
  94.  
  95. // Testroutine:
  96.  
  97. void main()
  98. {
  99.   String s1, s2="I didn't expect the", s3;
  100.   s1 = "Spanish Inquisition";
  101.   s3 = s2 + " " + s1;
  102.  
  103.   char *awurx = "Vor"+"sicht";       // unsichere Operation!
  104.  
  105.   int i = "Test"=="Test",            // implementationsabhängig
  106.       j = String("Test")==String("Test");    // true
  107.  
  108.   cout << s3+".\n";
  109. }
  110.