// *************************************************************************************************************** // Encapsulation of a simple string // *************************************************************************************************************** #include "ezstring.h" EZString::EZString() : appendBuffer(30) { str=0; allocatedMemory=0; length=0; } EZString::EZString(const char* input) : appendBuffer(30) { if (input) length=strlen(input); else length=0; allocatedMemory=length + 1 + appendBuffer; str = new char [allocatedMemory]; if (input) strcpy (str, input); str[length]=0; } EZString::EZString(const EZString& input) : appendBuffer(30) { if (input.str) length=strlen(input.str); else length=0; allocatedMemory=length + 1 + appendBuffer; str = new char [allocatedMemory]; if (input.str) strcpy (str, input.str); str[length]=0; } EZString::~EZString() { if (str) delete [] str; allocatedMemory=0; length=0; } EZString& EZString::operator = (const EZString& input) { if (&input == this) return *this; if (input.str) length=strlen(input.str); else { length=0; if (allocatedMemory) str[0]=0; else str=0; return *this; } if (length >= allocatedMemory) { if (str) delete [] str; allocatedMemory=length + 1 + appendBuffer; str = new char [allocatedMemory]; } if (input.str) strcpy (str, input.str); str[length]=0; return *this; } void EZString::Clear(void) { if (str) delete [] str; str=0; length=0; } void EZString::Append(const char c) { if (allocatedMemory==0 || length >= (allocatedMemory-1)) { char* newStr; allocatedMemory+=30; newStr = new char [allocatedMemory]; if (str) { strcpy (newStr, str); delete [] str; } str = newStr; } str[length++]=c; str[length]=0; } void EZString::UnAppend(void) { if ((str==0) || (length==0)) return; str[--length]=0; } void EZString::PrefixName(char *name) { int nameLength; if (!name) return; nameLength=strlen(name); // Reallocate the string and shift it right char* newStr; allocatedMemory=length+nameLength + 3; newStr = new char [allocatedMemory]; if (str) { strcpy (newStr + nameLength + 2, str); delete [] str; str = newStr; strcpy(str, name); str[nameLength]=':'; str[nameLength+1]=' '; } else { str = newStr; strcpy(str, name); str[nameLength]=':'; str[nameLength+1]=' '; str[nameLength+2]=0; } length+=nameLength +2; } int operator==(const EZString& left, const EZString& right) { if (left.str==0 || right.str==0) return 0; return (! (strcmp(left.str, right.str))); } int operator > (const EZString& left, const EZString& right) { if (left.str==0 || right.str==0) return 0; return (strcmp(left.str, right.str) > 0); } int operator < (const EZString& left, const EZString& right) { if (left.str==0 || right.str==0) return 0; return (strcmp(left.str, right.str) < 0); }