#pragma once #ifndef _UNICODE #define TCString ACString #define tstrchr_new StrchrNew #define tstrnew StrNew #endif class ACString { public: ACString(int len = 256); ACString(const ACString &src); ACString(const char *format, ...); ~ACString(); int operator ! () const; int operator == (const char *src) const; int operator == (const ACString &src) const; int operator != (const char *src) const; int operator != (const ACString &src) const; // str的空间要足够大; ACString & operator << (const char *s); ACString & operator << (int i); ACString & operator << (long i); ACString & operator << (double r); ACString & operator << (double * pt); // ACString & operator = (int nLength); ACString & operator = (const ACString & src); ACString & operator = (const char * src); operator const char * () const; operator char * (); char & operator [] (int index); const char & operator [] (int index) const; #ifndef _UNICODE ACString(const CString &src); ACString & operator = (const CString &src); ACString & operator << (const CString &src); int operator == (const CString &src) const; int operator != (const CString &src) const; #endif int Length(); char * _StrChr(char c); public: char *str; }; inline ACString::ACString(int len) { if (len <= 0) { exit(1); } str = new char [len]; str[0] = 0; } inline ACString::ACString(const ACString &src) : str(NULL) { (*this) = src.str; } inline ACString::~ACString() { if (str) { delete [] str; } } #ifndef _UNICODE inline ACString::ACString(const CString &src) : str(NULL) { (*this) = (const char *)src; } inline ACString & ACString::operator = (const CString &src) { return ((*this) = (const char *)src); } inline ACString & ACString::operator << (const CString &src) { // str的空间要足够大 return ((*this) << (const char *)src); } inline int ACString::operator == (const CString &src) const { return ((*this) == (const char *)src); } inline int ACString::operator != (const CString &src) const { return ((*this) != (const char *)src); } #endif inline int ACString::Length() { if (str) { return static_cast(strlen(str)); } else { return 0; } } inline char * ACString::_StrChr(char c) { return strchr(str,c); } inline ACString::operator const char * () const { return str; } inline ACString::operator char * () { return str; } inline char& ACString::operator [] (int index) { return str[index]; } inline const char& ACString::operator [] (int index) const { return str[index]; } inline ACString& ACString::operator << (const char *s) { // str的空间要足够大 if (!str) { str = new char [256]; str[0] = 0; } strcat(str, s); return *this; } inline ACString& ACString::operator << (int i) { if (!str) { str = new char [256]; str[0] = 0; } char s[30]; sprintf(s, "%d", i); strcat(str, s); return *this; } inline ACString & ACString::operator << (long i) { if (!str) { str = new char [256]; str[0] = 0; } char s[30]; sprintf(s,"%ld", i); strcat(str, s); return *this; } inline ACString & ACString::operator << (double r) { if (!str) { str = new char [256]; str[0] = 0; } char s[60]; sprintf(s, "%g", r); strcat(str, s); return *this; } inline ACString & ACString::operator << (double * pt) { if (!str) { str = new char [256]; str[0] = 0; } char s[120]; sprintf(s, "(%g,%g,%g)", pt[0], pt[1], pt[2]); strcat(str, s); return *this; } inline int ACString::operator !() const { return (!str || !str[0]) ? 1 : 0; } inline int ACString::operator == (const char *src) const { if (!str || !src) { exit(1); } return !strcmp(str, src); } inline int ACString::operator != (const char *src) const { if (!str || !src) { exit(1); } return strcmp(str, src); } inline int ACString::operator == (const ACString &src) const { if (!str || !src.str) { exit(1); } return !strcmp(str, src.str); } inline int ACString::operator != (const ACString &src) const { if (!str || !src.str) { exit(1); } return strcmp(str, src.str); }