home *** CD-ROM | disk | FTP | other *** search
- // Maxon C++
- // Stringtools
- // Jens Gelhar 21.02.92, 12.11.94
-
- #include <tools/str.h>
- #include <string.h>
- #include <iostream.h>
-
- void String::alloc (const char *initstr)
- { if (initstr)
- { len = strlen(initstr);
- str = new char[len+1];
- if (str)
- strcpy(str, initstr);
- else
- len = 0;
- }
- else
- { len = 0;
- str = 0;
- }
- }
-
- void String::free()
- { if (str)
- { delete [] str;
- str = 0;
- }
- }
-
- String::String (int len, char *str) : len(len), str(str) { }
-
- String::String (const char *initstr)
- { alloc(initstr); }
-
- String::String (char c)
- { char st[] = {c, 0};
- alloc(st);
- }
-
- String::String (const String &s)
- { alloc (s.str); }
-
- String::~String()
- { free(); }
-
- String operator + (String s1, String s2)
- {
- int newlen = s1.length()+s2.length();
- char *newstr = new char[newlen+1];
- if (newstr)
- { if (s1)
- strncpy (newstr,s1,s1.length());
- else
- *newstr = '\0';
- if (s2)
- strncat (newstr,s2,s2.length());
- newstr[newlen] = '\0';
- }
- return String(newlen, newstr);
- }
-
- String &String::operator = (const char* st)
- { free();
- alloc(st);
- return *this;
- }
-
- String &String::operator = (const String &s)
- { free();
- alloc(s.str);
- return *this;
- }
-
- String &String::operator += (const String &s)
- { *this = *this+s;
- return *this;
- }
-
- char String::operator[] (int index) const
- { if(str && unsigned(index) <= unsigned(len) )
- return str[index];
- else
- return '\0';
- }
-
- String String::left(int z) const
- { if(str && z>0)
- { if(z>len) z=len;
- char *initstr = new char[z+1];
- if (str)
- strncpy(initstr, str, z);
- initstr[z] = 0;
- return String(z, initstr);
- }
- else
- return String();
- }
-
- String String::right(int z) const
- { if(str && z>0)
- { if (z>len) z=len;
- return String(&str[len-z]);
- }
- else
- return String();
- }
-
- String String::mid(int pos, int z) const
- { if(str && pos <= len && z>0)
- { if(z>len-pos) z=len-pos;
- char *initstr = new char[z+1];
- strncpy(initstr, str+pos, z);
- return String(z, initstr);
- }
- else
- return String();
- }
-
- static int compare(const String &s1, const String &s2)
- { return strcmp(s1,s2); }
-
- int operator == (const String &s1, const String &s2)
- { return !compare(s1,s2); }
-
- int operator != (const String &s1, const String &s2)
- { return compare(s1,s2); }
-
- int operator > (const String &s1, const String &s2)
- { return compare(s1,s2) > 0; }
-
- int operator < (const String &s1, const String &s2)
- { return compare(s1,s2) < 0; }
-
- int operator <= (const String &s1, const String &s2)
- { return !compare(s1,s2) <= 0; }
-
- int operator >= (const String &s1, const String &s2)
- { return !compare(s1,s2) >= 0; }
-
- ostream &operator << (ostream& out, const String& s)
- { return out << (char*)s; }
-
- istream &operator >> (istream& in, String &s)
- { char buf[STREAM_MAXSTRING];
- in >> buf;
- s = buf;
- return in;
- }
-
-
-