#if !defined H_STRING_H #define H_STRING_H #include #include #include #include #include #include "hbool.h" // Lean implementation of the class Cstring derived from the standard template library basic_string // Size of HSting is automatically adjusted. No need to allocate memory beforehand. // Fully compatible with char *. // ADDITIONAL FEATURES: // automatic conversion of int,float, and double to HString and vice versa (if possible) // HString s(i) converts the content of i to a string as HString s=i does. // EXAMPLES: int i;HString s="20 liter"; i=s or i=int(s) => i=20 // float x=8.75; HString s="default="; s+=x => s="default=8.75" // The function token allows to e.g. parse strings // (save implementation of strtok(...) // Wolfgang Koenig 4.02.97 (W.Koenig@GSI.de) class HString { public: // creates empty string with terminator HString(){string=new char[1];*string='\0';} // creates a HString of length 1 containing character 'c'. HString(char c) { string=new char [2]; *string=c; *(string+1)='\0'; } // creates a string of length 'l'. // All 'l' elements are initialized with 'c' of type char HString(char c, int l) { string=new char [l+1]; for(string[l]='\0'; l--; string[l]=c); } // creates a string which is initialized with 'name' of type char * HString(const char * name) { string=new char [strlen(name)+1]; for(register char* p=string; *p++=*name++;); } // creates a string of length 'l'which is initialized with 'name' of type char * // Your problem, if name is to short. Internal use, don't kill it. HString(const char * name, int l) { string=new char [l+1]; for(string[l]='\0'; l--; string[l]=name[l]); } // creates a string which is initialized with 'h' of type HString HString(const HString & h) { int l=strlen(h.string); string=new char [++l]; while(l--) string[l]=h.string[l]; } //create a HString by concatenation of two strings HString(const HString & h1, const HString& h2); HString(const char * name, const HString& h); HString(const HString& h, const char * name); // create a string initialized with the character representation // of a number HString(int number) {string=0;*this=number;} HString(long number) {string=0;*this=number;} HString(float number) {string=0;*this=number;} HString(double number) {string=0;*this=number;} // destroy the string ~HString() {delete [] string;} // allocate memory for character string with 'l' characters. // Deletes old string and resizes string array, if necessary. // In general, this function is not needed, since the size of // HString is adjusted automatically. Only for cin and c/c++ // functions which write into a character string. // initializes all string characters with ' ' (blank). HString& assign(int l); // dito, but initialize all string characters with character c. HString& assign(char c, int l); // Sets the string left of '=' equal to string right of '=', // independently of the previous lenght of the string left of '='. HString& operator = (char c); HString& operator = (const char * name); HString& operator = (const HString & h); // converts a signed integer, float or double to a HString (character string) HString& operator = (int number); HString& operator = (long number); HString& operator = (float number); HString& operator = (double number); // convert a HString object into a char * object operator char*() {return string;} operator const char*() {return (const char*) string;} // converts a string into a number operator int(){char* p;return int(strtol(string,&p,10));} operator long(){char* p;return strtol(string,&p,10);} operator float(){char* p;return float(strtod(string,&p));} operator double(){char* p;return strtod(string,&p);} // appends string right of '+=' to string left of '+=' HString& operator +=(char c); HString& operator +=(const char * name); HString& operator +=(const HString &h); HString& operator +=(int number); HString& operator +=(long number); HString& operator +=(float number); HString& operator +=(double number); // creates string consisting of 'r' appended to 'l'. // Original strings 'l' and 'r' remain unchanged. friend HString operator + (const HString &l,char r); friend HString operator + (char l, const HString &r); friend HString operator + (const HString &l, const char * r); friend HString operator + (const HString &l, char * r); friend HString operator + (const char * l, const HString &r); friend HString operator + (char * l, const HString &r); friend HString operator + (const HString &l,const HString &r); friend HString operator + (const HString &l,int number); friend HString operator + (const HString &l,long number); friend HString operator + (const HString &l,float number); friend HString operator + (const HString &l,double number); friend HString operator + (int number, const HString &l); friend HString operator + (long number, const HString &l); friend HString operator + (float number, const HString &l); friend HString operator + (double number, const HString &l); // checks whether or not the two strings are equal friend HBool operator == (const HString &l, const char * name); friend HBool operator == (const HString &l, char * name); friend HBool operator == (const char* name, const HString &r); friend HBool operator == (char* name, const HString &r); friend HBool operator == (char c, const HString &r); friend HBool operator == (const HString &r, char c); friend HBool operator == (const HString &l, const HString &r); friend HBool operator != (const HString &l, const char * name); friend HBool operator != (const HString &l, char * name); friend HBool operator != (const char* name, const HString &r); friend HBool operator != (char* name, const HString &r); friend HBool operator != (char c, const HString &r); friend HBool operator != (const HString &r, char c); friend HBool operator != (const HString &l, const HString &r); // uses strcmp for comparison friend HBool operator < (const HString &l,const HString &r); friend HBool operator < (const HString &l,char c); friend HBool operator < (char c,const HString &r); friend HBool operator > (const HString &l, char c); friend HBool operator > (char c, const HString &r); friend HBool operator > (const HString &l, const HString &r); friend HBool operator <= (const HString &l, const HString &r); friend HBool operator <= (const HString &l, char c); friend HBool operator <= (char c, const HString &r); friend HBool operator >= (const HString &l, const HString &r); friend HBool operator >= (const HString &l, char c); friend HBool operator >= (char c, const HString &r); // make sure that 'string' is large enough to accept the input friend istream& operator >> (istream& get, HString& h); friend ostream& operator << (ostream& put,const HString& h); // guess what it does int length() const {return strlen(string);} HBool empty() const {return !*string;} void clear() { delete [] string; string=new char[1]; *string='\0'; return; } // Sets the string equal to the upper case version of 's'. // If 's' is omitted, the string itself is set to its uppercase version. HString & upper(const HString &s); HString & upper(); // Sets this string equal to the lower case version of 's'. See above. HString & lower(const HString &s); HString & lower(); // inserts string 'insertString' at position 'pos' >= 0. HString & insert(int pos, const HString &insertString); // replace part of the string by replaceString. Start at position. // Truncates replaceString, if necessary. HString & replace(int pos, const HString &replaceString); // replace the char at position 'pos' by 'newChar' HString & replace(int pos, char newChar) {(*this)[pos]=newChar;return *this;} // replaces all characters 'old' in the string by the character 'new' // e.g. replaces separation char. like ',' by blanks HString & replace(char oldChar, char newChar); // returns the (first) position of the specified char within the string. // Returns -1 if char is not found. int find(const char search) const; // returns the (first) position of the specified searchString within the string. // If searchString is not found it returns -1 . Starts search position 'pos' . int find(const HString &search) const; int find(const HString &search,int pos) const; // returns the substring of length 'size' // starting at the character with index 'position'. 0 is lowest index // If position + size > stringlength substring is truncated. HString operator() (int position, int size); // returns the character at specified position. 0 is lowest index char& operator() (int position); char& operator[] (int position); // parses the string and returns the next token. // Tokens are separated by blanks (default) or by charakters specified // in 'charset'. If pointer 'pNext' is set to 0, parse starts at the // beginning of parseString, otherwise 'pNext' contains pointer to // remaining stringpart. HString token(char* &pNext, char *charSet=" "); HString & token(char *charSet=" \t"); protected: char * string; void errMsg(int index); }; inline HString operator + (const HString &l,char r) {HString s(l); return s+=r;} inline HString operator + (char l, const HString &r) {HString s(l); return s+=r;} inline HString operator + (const HString &l,const char* r) {return HString(l,r);} inline HString operator + (const HString &l,char* r) {return HString(l,r);} inline HString operator + (const char* l, const HString &r) {return HString(l,r);} inline HString operator + (char* l, const HString &r) {return HString(l,r);} inline HString operator + (const HString &l,const HString& r) {return HString(l,r);} inline HString operator + (const HString &l,int number) {HString s(l); return s+=number;} inline HString operator + (const HString &l,long number) {HString s(l); return s+=number;} inline HString operator + (const HString &l,float number) {HString s(l); return s+=number;} inline HString operator + (const HString &l,double number) {HString s(l); return s+=number;} inline HBool operator == (const HString &l,const char * name) {return !strcmp(l.string,name);} inline HBool operator == (const HString &l,char * name) {return !strcmp(l.string,name);} inline HBool operator == (const char * name, const HString &r) {return !strcmp(name,r.string);} inline HBool operator == (char * name, const HString &r) {return !strcmp(name,r.string);} inline HBool operator == (char c, const HString &h) {return h==c;} inline HBool operator == (const HString &l,const HString &r) {return !strcmp(l.string,r.string);} inline HBool operator != (const HString &l,const char * name) {return strcmp(l.string,name);} inline HBool operator != (const HString &l,char * name) {return strcmp(l.string,name);} inline HBool operator != (const char * name, const HString &r) {return strcmp(name,r.string);} inline HBool operator != (char * name, const HString &r) {return strcmp(name,r.string);} inline HBool operator != (const HString &h, char c) {return !(h==c);} inline HBool operator != (char c, const HString &h) {return !(h==c);} inline HBool operator != (const HString &l,const HString &r) {return strcmp(l.string,r.string);} inline HBool operator < (const HString &l,const HString &r) {return strcmp(l.string,r.string)<0;} inline HBool operator < (const HString &l,char c) {return strcmp(l.string,HString(c).string)<0;} inline HBool operator < (char c,const HString &r) {return strcmp(HString(c).string,r.string)<0;} inline HBool operator > (const HString &l,const HString &r) {return strcmp(l.string,r.string)>0;} inline HBool operator > (const HString &l,char c) {return strcmp(l.string,HString(c).string)>0;} inline HBool operator > (char c,const HString &r) {return strcmp(HString(c).string,r.string)>0;} inline HBool operator <= (const HString &l,const HString &r) {return strcmp(l.string,r.string)<=0;} inline HBool operator <= (const HString &l,char c) {return strcmp(l.string,HString(c).string)<=0;} inline HBool operator <= (char c,const HString &r) {return strcmp(HString(c).string,r.string)<=0;} inline HBool operator >= (const HString &l,const HString &r) {return strcmp(l.string,r.string)>=0;} inline HBool operator >= (const HString &l,char c) {return strcmp(l.string,HString(c).string)>=0;} inline HBool operator >= (char c,const HString &r) {return strcmp(HString(c).string,r.string)>=0;} inline ostream& operator<< (ostream& put,const HString &h) {return put<> (istream& get, HString &h) { if(h.empty()) { h.errMsg(0); return get; } return get>>h.string; } inline HString & HString::upper() { for(register char* p=string;*p;p++) *p=toupper(*p); return *this; } inline HString & HString::lower() { for(register char* p=string;*p;p++) *p=tolower(*p); return *this; } inline int HString::find(const HString &search) const { register char * s=strstr(string,search.string); if(!s) return -1; return s-string; } inline char& HString::operator() (int position) { return (*this)[position]; } #endif