#ifndef SPSTRING #define SPSTRING //#define NDEBUG //Get rid of the assert()'s #include #include #include #include #include #define DEFAULT_ALLOC 16 #define MAXLENGTH 1024 class StringTokenizer; class String { friend class StringTokenizer; protected: unsigned len; //actual length of string unsigned sz; //allocated length char *s; //where the actual chars are stored. enum {MaxLength=MAXLENGTH}; public: String(); String(int n); String(char *t); String::String(int n, char c); String(const String&); ~String(); String& operator=(const String&); String& operator=(const char *); char & operator[](unsigned i); const char & operator[](unsigned i) const; //conversion of numbers to strings void ConvertInt(int i); void ConvertDouble(double d); //conversion of strings to numbers double Double(void); long Long(void); int Int(); char * chars(); //returns the actual char_string // unsigned Unsigned(void); //IO friend istream& operator>>(istream & InStream, String& S); friend ostream& operator <<(ostream& OutStream,const String& S); /* The next function uses a call to istream::getline() that may or may not behave the same on different systems. It seems to add an ending zero to the target buffer in Borland's version. One could always use istream::get() and then read the terminating character from the stream. */ istream& ReadLine(istream& In,char Until='\n'); void ReadLine(FILE *fp); void Print(); //String Operations int Length()const ; int Delete(unsigned Start, unsigned End); int Insert(const String& S,unsigned Start); int Replace(const String& S,unsigned Start, unsigned End); int Locate(const String& S) const; void Replace(const String& Target,const String& Source); //char * is converted to String, so the next 3 aren't necessary void Replace(const String& Target,char * S); void Replace(char * T,const String& Source); void Replace(char * T,char * S); //Concatenate friend String operator+(const String& S, const String& T); //char* is converted to String friend String operator+(const String& S, char *T); friend String operator+(char *T, const String& S); int BelongsTo(const char C)const ; //could just use Locate("C") //Comparison operators friend operator==(const String& S, const String& T); friend operator==(const String& S, char * T); friend operator==(char * T, const String& S); friend operator!=(const String& S, const String& T); friend operator!=(const String& S, char * T); friend operator!=(char * T, const String& S); friend operator<(const String& S, const String& T); friend operator<(const String& S, char * T); friend operator<(char * T, const String& S); friend operator>(const String& S, const String& T); friend operator>(const String& S, char * T); friend operator>(char * T, const String& S); friend operator>=(const String& S, const String& T); friend operator>=(const String& S, char * T); friend operator>=(char * T, const String& S); friend operator<=(const String& S, const String& T); friend operator<=(const String& S, char * T); friend operator<=(char * T, const String& S); //Modify string void UpperCase(); void LowerCase(); }; /*StringTokenizer is a class to perfrom strtok() like operations on a string. When Next(String&) is called, it returns the next token from Source, split according to the elements of Next's argument.*/ class StringTokenizer :public String { char * CurrentStart; char *Buffer; public: StringTokenizer(const String& Source); StringTokenizer(int size); StringTokenizer(); ~StringTokenizer(); void ReSet(const String& S); String Next(const String &); String Next(char *t); int Empty(); }; #endif