Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

StdString.h

Go to the documentation of this file.
00001 #ifndef _STCL_STRING_H_
00002 #define _STCL_STRING_H_
00003 
00004 
00005 #ifndef GEN_NO_ASSERTD
00006 #undef  assertd
00007 #define assertd(exp)                    ((void)NULL)
00008 #endif
00009 
00010 
00011 #include <vector>
00012 #include <limits.h>
00013 //using namespace std;
00014 
00015 //DECLARE_CLASS(CCString);
00016 //DECLARE_CLASS(CStream);
00017 class CCString;
00018 
00019 typedef  unsigned long dword;
00020 typedef const CCString& rcCCString;
00021 typedef unsigned char byte;
00022 
00026 
00027 class CCString
00028 {
00029 public:
00030         // Initialization
00031         CCString();
00032         CCString(char c);
00033         CCString(char c, int len);
00034         CCString(const char *str);
00035         CCString(const char *str, int len);
00036         CCString(const CCString &str);
00037         CCString(const CCString & str, int len);
00038         CCString(const char *src1, int src1_len, const char *src2, int src2_len);
00039         explicit CCString(int val);
00040         explicit CCString(dword val);
00041         explicit CCString(float val);
00042         explicit CCString(double val);
00043         inline ~CCString()                                                                                              { delete[] pData; }
00044 
00045         // Operations
00046         CCString&                       Format(const char *fmt, ...);                           // Set formatted text to string
00047         CCString&                       FormatAdd(const char *fmt, ...);                        // Add formatted text to string
00048         inline void                     Clear()                                                                         { Len = pData[0] = 0; }
00049         inline const CCString   & Reverse()                                                                     

00050         { 
00051             for (int i1=0, i2=Len-1; i1<i2; i1++, i2--) 
00052                                 std::swap(pData[i1], pData[i2]); return *this; 
00053         }
00054 
00055         // Properties
00056         bool                            IsUpper() const;
00057         bool                            IsLower() const;
00058         inline bool                     IsEmpty() const                                                         { return Len==0; }
00059         inline int                      Length() const                                                          { return Len; }
00060 
00061         // Data access
00062         inline CCString&        SetLength(int len)                                                      { AllocBuffer(len, true); Len=len; pData[len]=0; return *this; }
00063         inline char                     GetAt(int pos) const                                            { assertd(pos<Len); return pData[pos]; }
00064         inline void                     SetAt(int pos, char c)                                          { assertd(pos<Len); pData[pos]=c; }
00065         inline void                     SetAt(int pos, rcCCString str);
00066         inline void                     SetAt(int pos, int num, rcCCString str);
00067         void                            Replace(int pos, rcCCString str);
00068         void                            Replace(int pos, int num, rcCCString str);
00069 
00070         // Substrings
00071         inline CCString Left(int num) const                                                             { num = (num < Len ? num:Len ); return CCString(pData, num); }
00072         inline CCString Right(int num) const                                                    { num = (num < Len ? num:Len ); return CCString(pData+Len-num, num); }
00073         inline CCString Mid(int pos, int num = INT_MAX) const                   { pos = (pos < Len ? pos:Len ); num = (num < (Len - pos) ? num : (Len - pos)); return CCString(pData+pos, num); }
00074 
00075         // Comparison
00076         int                             Compare(rcCCString rhs) const;
00077         int                             CompareNoCase(rcCCString rhs) const;
00078         inline bool             IsEqual(rcCCString rhs) const                                   { return (Compare(rhs)==0); }
00079         inline bool             IsEqualNoCase(rcCCString rhs) const                             { return (CompareNoCase(rhs)==0); }
00080 
00081         // Search/replace
00082         int                             Find(char c, int pos = 0) const;
00083         int                             Find(const char *str, int pos = 0) const;
00084         int                             Find(rcCCString str, int pos = 0) const;
00085         int                             RFind(char c) const;
00086         int                             FindOneOf(const char *set, int pos = 0) const;
00087         int                             RFindOneOf(const char *set, int pos = 0) const;
00088 
00089         std::vector<CCString>   Explode(char c) const;
00090 
00091         // Formatting
00092         CCString&                       Upper();
00093         CCString&                       Lower();
00094         CCString&                       Capitalize();
00095         CCString&                       TrimLeft();
00096         CCString&                       TrimLeft(char *set);
00097         CCString&                       TrimRight();
00098         CCString&                       TrimRight(char *set);
00099         CCString&                       Trim();
00100         CCString&                       Trim(char *set);
00101         CCString&                       TrimQuotes();
00102 
00103         // Conversions
00104 //      inline operator char*()                                                                                         { return pData; }
00105         inline operator const char *() const                                                            { return pData; }
00106         inline char *Ptr()                                                                                                      { return pData; }
00107         inline const char *ReadPtr() const                                                                      { return pData; }
00108         inline float    ToFloat() const                                                                         { return (float) atof(pData); }
00109         inline int              ToInt() const                                                                           { return atoi(pData); }
00110 
00111         // Operators
00112         inline rcCCString       operator=(const byte *rhs)                                              { return Copy((char *)rhs, strlen((char *)rhs)); }
00113         inline rcCCString       operator=(rcCCString rhs)                                               { return Copy(rhs.ReadPtr(), rhs.Length()); }
00114         inline rcCCString       operator=(char rhs)                                                             { return Copy(&rhs, 1); }
00115         inline rcCCString       operator=(const char *rhs)                                              { return Copy(rhs, strlen(rhs)); }
00116 
00117         inline rcCCString       operator+=(const char *rhs)                                             { return Concat(rhs, strlen(rhs)); }
00118         inline rcCCString       operator+=(rcCCString rhs)                                              { return Concat(rhs.ReadPtr(), rhs.Length()); }
00119         inline rcCCString       operator+=(char rhs)                                                    { return Concat(&rhs, 1); }
00120 
00121         
00122         inline friend bool operator<(rcCCString      lhs, rcCCString            rhs)    { return (strcmp(lhs, rhs)<0); }
00123         inline friend bool operator<(rcCCString      lhs, const char            *rhs)   { return (strcmp(lhs, rhs)<0); };
00124         inline friend bool operator<(const char         *lhs, rcCCString     rhs)       { return (strcmp(lhs, rhs)<0); }
00125         inline friend bool operator>(rcCCString      lhs, rcCCString            rhs)    { return (strcmp(lhs, rhs)>0); }
00126         inline friend bool operator>(rcCCString      lhs, const char            *rhs)   { return (strcmp(lhs, rhs)>0); }
00127         inline friend bool operator>(const char         *lhs, rcCCString     rhs)       { return (strcmp(lhs, rhs)>0); }
00128         inline friend bool operator<=(rcCCString     lhs, rcCCString            rhs)    { return (strcmp(lhs, rhs)<=0); }
00129         inline friend bool operator<=(rcCCString     lhs, const char            *rhs)   { return (strcmp(lhs, rhs)<=0); }
00130         inline friend bool operator<=(const char        *lhs, rcCCString     rhs)       { return (strcmp(lhs, rhs)<=0); }
00131         inline friend bool operator>=(rcCCString     lhs, rcCCString            rhs)    { return (strcmp(lhs, rhs)>=0); }
00132         inline friend bool operator>=(rcCCString     lhs, const char            *rhs)   { return (strcmp(lhs, rhs)>=0); }
00133         inline friend bool operator>=(const char        *lhs, rcCCString     rhs)       { return (strcmp(lhs, rhs)>=0); }
00134         inline friend bool operator==(rcCCString     lhs, rcCCString            rhs)    { return ((lhs.Length() == rhs.Length()) && (memcmp(lhs, rhs, lhs.Length())==0)); }
00135         inline friend bool operator==(rcCCString     lhs, const char            *rhs)   { return (memcmp(lhs, rhs, lhs.Length()+1)==0); }
00136         inline friend bool operator==(const char        *lhs, rcCCString     rhs)       { return (memcmp(lhs, rhs, rhs.Length()+1)==0); }
00137         inline friend bool operator!=(rcCCString     lhs, rcCCString            rhs)    { return ((lhs.Length() != rhs.Length()) || (memcmp(lhs, rhs, lhs.Length())!=0)); }
00138         inline friend bool operator!=(rcCCString     lhs, const char            *rhs)   { return (memcmp(lhs, rhs, lhs.Length()+1)!=0); }
00139         inline friend bool operator!=(const char        *lhs, rcCCString     rhs)       { return (memcmp(lhs, rhs, rhs.Length()+1)!=0); }
00140 
00141         // serializing
00142         //int                   Serialize(pCStream stream);
00143 
00144 protected:
00145         // Implementation
00146         void    AllocBuffer(int len, bool keep_contents);
00147         rcCCString Copy(const char *src, int len);
00148         rcCCString Concat(const char *data, int len);
00149 
00150         static bool             isLower(char c)                                                                 { return !isUpper(c); }
00151         static bool             isUpper(char c)                                                                 { return (c>='A') && (c <= 'Z'); }
00152         static bool             isSpace(char c)                                                                 { return (c==' ') || (c=='\t'); }
00153 
00154         char   *pData;                                                                                                  // -> CCString data
00155         int        Len;                                                                                                         // Data length
00156         int        Max;                                                                                                         // Space in data buffer
00157 };
00158 
00159 inline  CCString operator+(rcCCString    lhs, rcCCString   rhs) { return CCString(lhs.ReadPtr(), lhs.Length(), rhs.ReadPtr(), rhs.Length()); }
00160 inline  CCString operator+(rcCCString    lhs, char        rhs)  { return CCString(lhs.ReadPtr(), lhs.Length(), &rhs, 1); }
00161 inline  CCString operator+(char         lhs, rcCCString   rhs)  { return CCString(&lhs, 1, rhs.ReadPtr(), rhs.Length()); }
00162 inline  CCString operator+(rcCCString    lhs, const char *rhs)  { return CCString(lhs.ReadPtr(), lhs.Length(), rhs, strlen(rhs)); }
00163 inline  CCString operator+(const char  *lhs, rcCCString   rhs)  { return CCString(lhs, strlen(lhs), rhs.ReadPtr(), rhs.Length()); }
00164 
00165 
00166 #endif

Generated at Thu Feb 1 13:03:12 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000