home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / MAXONC3_6OF8.DMS / in.adf / LIBSRC.LHA / LIBSRC / strtools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  2.8 KB  |  152 lines

  1. // Maxon C++
  2. // Stringtools
  3. // Jens Gelhar 21.02.92, 12.11.94
  4.  
  5. #include <tools/str.h>
  6. #include <string.h>
  7. #include <iostream.h>
  8.  
  9. void String::alloc (const char *initstr)
  10. { if (initstr)
  11.    { len = strlen(initstr);
  12.      str = new char[len+1];
  13.      if (str)
  14.        strcpy(str, initstr);
  15.      else
  16.        len    = 0;
  17.    }
  18.   else
  19.    { len = 0;
  20.      str    = 0;
  21.    }
  22. }
  23.  
  24. void String::free()
  25. { if (str)
  26.    { delete [] str;
  27.      str    = 0;
  28.    }
  29. }
  30.  
  31. String::String (int len, char *str) : len(len), str(str) { }
  32.  
  33. String::String (const char *initstr)
  34. { alloc(initstr); }
  35.  
  36. String::String (char c)
  37. { char st[] = {c, 0};
  38.   alloc(st);
  39. }
  40.  
  41. String::String (const String &s)
  42. { alloc (s.str); }
  43.  
  44. String::~String()
  45. { free(); }
  46.  
  47. String operator + (String s1, String s2)
  48. {
  49.   int newlen = s1.length()+s2.length();
  50.   char *newstr = new char[newlen+1];
  51.   if (newstr)
  52.    { if (s1)
  53.         strncpy (newstr,s1,s1.length());
  54.      else
  55.         *newstr = '\0';
  56.      if (s2)
  57.         strncat (newstr,s2,s2.length());
  58.      newstr[newlen] = '\0';
  59.    }
  60.   return String(newlen, newstr);
  61. }
  62.  
  63. String &String::operator = (const char* st)
  64. { free();
  65.   alloc(st);
  66.   return *this;
  67. }
  68.  
  69. String &String::operator = (const String &s)
  70. { free();
  71.   alloc(s.str);
  72.   return *this;
  73. }
  74.  
  75. String &String::operator += (const String &s)
  76. { *this = *this+s;
  77.   return *this;
  78. }
  79.  
  80. char String::operator[] (int index) const
  81. { if(str && unsigned(index) <= unsigned(len) )
  82.     return str[index];
  83.   else
  84.     return '\0';
  85. }
  86.  
  87. String String::left(int z) const
  88. { if(str && z>0)
  89.    { if(z>len) z=len;
  90.      char *initstr = new char[z+1];
  91.      if (str)
  92.        strncpy(initstr, str, z);
  93.      initstr[z] = 0;
  94.      return String(z, initstr);
  95.    }
  96.  else
  97.    return String();
  98. }
  99.  
  100. String String::right(int z) const
  101. { if(str && z>0)
  102.    { if (z>len) z=len;
  103.      return String(&str[len-z]);
  104.    }
  105.  else
  106.    return String();
  107. }
  108.  
  109. String String::mid(int pos, int z) const
  110. { if(str && pos <= len && z>0)
  111.    { if(z>len-pos) z=len-pos;
  112.      char *initstr = new char[z+1];
  113.      strncpy(initstr, str+pos, z);
  114.      return String(z, initstr);
  115.    }
  116.  else
  117.    return String();
  118. }
  119.  
  120. static int compare(const String &s1, const String &s2)
  121. { return strcmp(s1,s2); }
  122.  
  123. int operator == (const String &s1, const String &s2)
  124. { return !compare(s1,s2); }
  125.  
  126. int operator != (const String &s1, const String &s2)
  127. { return compare(s1,s2); }
  128.  
  129. int operator > (const String &s1, const String &s2)
  130. { return compare(s1,s2) > 0; }
  131.  
  132. int operator < (const String &s1, const String &s2)
  133. { return compare(s1,s2) < 0; }
  134.  
  135. int operator <= (const String &s1, const String &s2)
  136. { return !compare(s1,s2) <= 0; }
  137.  
  138. int operator >= (const String &s1, const String &s2)
  139. { return !compare(s1,s2) >= 0; }
  140.  
  141. ostream &operator << (ostream& out, const String& s)
  142. { return out << (char*)s; }
  143.  
  144. istream &operator >> (istream& in, String &s)
  145. { char buf[STREAM_MAXSTRING];
  146.  in >> buf;
  147.  s    = buf;
  148.  return in;
  149. }
  150.  
  151.  
  152.