home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / String.cpp < prev    next >
C/C++ Source or Header  |  2000-08-06  |  2KB  |  131 lines

  1. #include "String.h"
  2.  
  3. bool streq (const char* p, const char* q) {
  4.   if(p==q) return true;
  5.   if(!(p&&q)) return false;
  6.   char a=*p, b=*q;
  7.   while(a==b) {
  8.     if(a==0) return true;
  9.     p++;
  10.     q++;
  11.     a=*p; b=*q;
  12.   }
  13.   return false;
  14. }
  15.  
  16. int strlen (const char* p) {
  17.   int l=0;
  18.   if(p) 
  19.     while(*p) {
  20.       l++;
  21.       p++;
  22.     }
  23.   return l;
  24. }
  25.  
  26. char* strdup (const char* p) {
  27.   if(!p) {
  28.     return 0;
  29.   }
  30.   else {
  31.     int l = strlen(p);
  32.     char* string = new char[l+1];
  33.     char* s = string;
  34.     for(int i=l+1; i; i--) {
  35.       *s = *p;
  36.       p++;
  37.       s++;
  38.     }
  39.     return string;
  40.   }
  41. }
  42.  
  43. char* strcat (const char* a, const char* b) {
  44.   if(!a)
  45.     return strdup(b);
  46.   if(!b)
  47.     return strdup(a);
  48.   int la = strlen(a);
  49.   int lb = strlen(b);
  50.   char* string = new char[la+lb+1];
  51.   char* s = string;
  52.   for(int i=la; i; i--) {
  53.     *s = *a;
  54.     a++;
  55.     s++;
  56.   }
  57.   for(int i=lb+1; i; i--) {
  58.     *s = *b;
  59.     b++;
  60.     s++;
  61.   }
  62.   return string;
  63. }
  64.  
  65. String& String::operator = (const char *p)
  66. {
  67.   delete[] string;
  68.   string = strdup(p);
  69.   len = strlen(p);  
  70.   return *this;  
  71. }
  72.  
  73. String::String(const char* p = 0) {
  74.   string=0;
  75.   this->operator=(p);
  76. }
  77.  
  78. String::String(const String& x) {
  79.   this->operator=(x());
  80. }
  81.  
  82. String& String::operator = (const String& x) {
  83.   delete[] string;
  84.   string = strdup(x());
  85.   len = x.len;
  86.   return *this;
  87. }
  88.  
  89. String::~String() {
  90.   delete[] string;
  91. }
  92.  
  93. String& String::operator += (const char* p) {
  94.   char* __temp = strcat(string, p);
  95.   delete[] string;
  96.   string = __temp;
  97.   len = strlen(__temp);
  98.   return *this;
  99. }
  100.  
  101. String& String::operator += (const String & S) {
  102.   char* __temp = strcat(string, S.string);
  103.   delete[] string;
  104.   string = __temp;
  105.   len = strlen(__temp);
  106.   return *this;
  107. }
  108.  
  109.  
  110. String operator + (const String &x, const String &y) {
  111.   return String(strcat(x(), y()));
  112. }
  113.  
  114. bool operator == (const String& x, const char *p) __attribute__ ((const));
  115.  
  116. bool operator == (const String& x, const char *p) {
  117.   return streq(x(), p);
  118. }
  119.  
  120. bool operator == (const String& x, const String& y) __attribute__ ((const));
  121.  
  122. bool operator == (const String& x, const String& y) {
  123.   return streq(x(), y());
  124. }
  125.  
  126. ostream& operator << (ostream& s, const String& x) __attribute__ ((const));
  127.  
  128. ostream& operator << (ostream& s, const String& x) {
  129.   return (s<<x());
  130. }
  131.