-inline char *StringDup(const char *s) {
- if (!s)
- return 0;
- char *sNew = new char[strlen(s) + 1];
- if (sNew)
- strcpy(sNew, s);
- return sNew;
-}
-
-class SString {
- char *s;
-public:
- SString() {
- s = 0;
- }
- SString(const SString &source) {
- s = StringDup(source.s);
- }
- SString(const char *s_) {
- s = StringDup(s_);
- }
- SString(int i) {
- char number[100];
- sprintf(number, "%0d", i);
- //itoa(i, number, 10);
- s = StringDup(number);
- }
- ~SString() {
- delete []s;
- s = 0;
- }
- SString &operator=(const SString &source) {
- if (this != &source) {
- delete []s;
- s = StringDup(source.s);
- }
- return *this;
- }
- bool operator==(const SString &other) const {
- if ((s == 0) && (other.s == 0))
- return true;
- if ((s == 0) || (other.s == 0))
- return false;
- return strcmp(s, other.s) == 0;
- }
- bool operator==(const char *sother) const {
- if ((s == 0) && (sother == 0))
- return true;
- if ((s == 0) || (sother == 0))
- return false;
- return strcmp(s, sother) == 0;
- }
- const char *c_str() const {
- if (s)
- return s;
- else
- return "";
- }
- int length() const {
- if (s)
- return strlen(s);
- else
- return 0;
- }
- char operator[](int i) {
- if (s)
- return s[i];
- else
- return '\0';
- }
- SString &operator +=(const char *sother) {
- int len = length();
- int lenOther = strlen(sother);
- char *sNew = new char[len + lenOther + 1];
- if (sNew) {
- if (s)
- memcpy(sNew, s, len);
- memcpy(sNew + len, sother, lenOther);
- sNew[len + lenOther] = '\0';
- delete []s;
- s = sNew;
- }
- return *this;
- }
- int value() {
- if (s)
- return atoi(s);
- else
- return 0;
- }