00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #ifndef _string_buffer_h
00018 #define _string_buffer_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Debug.h>
00022 
00023 #ifdef _WINDOWS
00024 #include <spl/configwin32.h>
00025 #else
00026 #include <spl/autoconf/config.h>
00027 #endif
00028 
00029 #include <spl/Char.h>
00030 #include <spl/Exception.h>
00031 #include <spl/Memory.h>
00032 #include <spl/String.h>
00033 #include <spl/RefCountPtr.h>
00034 #include <spl/WeakReference.h>
00035 
00041 class StringBuffer;
00042 
00044 typedef RefCountPtr<StringBuffer> StringBufferPtr;      
00045 typedef WeakReference<StringBuffer, StringBufferPtr> StringBufferRef;
00046 
00047 REGISTER_TYPEOF( 486, StringBufferPtr );
00048 REGISTER_TYPEOF( 488, StringBufferRef );
00049 
00053 class StringBuffer : public IMemoryValidate
00054 {
00055 private:
00056 
00057         char *m_buf;
00058         int m_used;
00059         int m_len;
00060 
00061         void Init(int size);
00062 
00063 public:
00064 
00065 #ifdef BSTR
00066         StringBuffer(BSTR str);
00067 #endif
00068 
00069         StringBuffer();
00070         StringBuffer(const char *str);
00071         StringBuffer(int size);
00072         StringBuffer(const StringBuffer& sb);
00073         virtual ~StringBuffer();
00074 
00075         void Trim();
00076 
00077         inline void SetLength(int l)
00078         {
00079                 ASSERT (l >= 0);
00080                 ASSERT_MEM(m_buf, m_len);
00081                 if (l > m_len)
00082                 {
00083                         ExtendTo(l);
00084                 }
00085                 m_used = l;
00086                 m_buf[m_used] = '\0';
00087         }
00088 
00089         inline void Clear() { SetLength(0); }
00090 
00091         
00092 
00093 
00094 
00095 
00096         void Append(const char *str);
00097 
00098         inline void Append(const String *str) 
00099         { 
00100                 Append(str->GetChars()); 
00101         }
00102 
00103         inline void Append(StringPtr strp)
00104         {
00105                 Append(*strp);
00106         }
00107 
00108         inline void Append(const String& str)
00109         {
00110                 Append(str.GetChars());
00111         }
00112 
00113         inline void Append(const char c)
00114         {
00115                 ASSERT_MEM(m_buf, m_len);
00116 
00117                 if (m_used + 1 >= m_len)
00118                 {
00119                         Extend();
00120                 }
00121                 m_buf[m_used] = c;
00122                 m_buf[m_used + 1] = '\0';
00123                 m_used++;
00124 
00125                 ASSERT(m_buf[m_used] == '\0');
00126         }
00127 
00128         inline void Append(const char *str, int start, int len)
00129         {
00130                 ASSERT_MEM(m_buf, m_len);
00131                 for (int x = 0; x < len; x++)
00132                 {
00133                         Append(str[x+start]);
00134                 }
00135         }
00136 
00137         inline void Append(const char *str, int len) 
00138         { 
00139                 Append(str, 0, len); 
00140         }
00141         
00142         inline void Append(byte b)
00143         {
00144                 Append((char *)&b, 1);
00145         }
00146 
00147         inline void Append(Array<byte>& data, int len)
00148         {
00149                 Append((const char *)data.Data(), len);
00150         }
00151 
00152         inline void Append(Array<char>& data, int len)
00153         {
00154                 Append((const char *)data.Data(), len);
00155         }
00156 
00159         void Fill(char ch, int len);
00160 
00161         inline int Length() const
00162         {
00163                 ASSERT_MEM(m_buf, m_len);
00164                 return m_used;
00165         }
00166 
00167         
00168         
00169         
00170         
00171         
00172 
00173         inline char CharAt(int index) const
00174         {
00175                 if (index > m_used)
00176                 {
00177                         return '\0';
00178                 }
00179                 return m_buf[index];
00180         }
00181 
00182         inline char& operator[](int index) const
00183         {
00184                 ASSERT_MEM(m_buf, m_len);
00185 
00186                 if (index > m_used)
00187                 {
00188                         return m_buf[0];
00189                 }
00190                 return m_buf[index];
00191         }
00192 
00193         inline StringBuffer& operator = (StringPtr str) { *this = str->GetChars(); return *this; }
00194         StringBuffer& operator = (const char *str);
00195         StringBuffer& operator =(const StringBuffer& sb );
00196 
00197         StringBuffer& operator = (const String& str)
00198         {
00199                 SetLength(0);
00200                 Append(str);
00201                 return *this;
00202         }
00203 
00204         inline void Set(const char *str)
00205         {
00206                 SetLength(0);
00207                 Append(str);
00208         }
00209 
00210         inline void Set(const String& str)
00211         {
00212                 SetLength(0);
00213                 Append(str);
00214         }
00215 
00216         void SetCharAt( const int index, char c );
00217         void RemoveCharAt( const int idx );
00218         
00219         inline void InsertCharAt( const char ch, const int idx )
00220         {
00221                 char buf[2];
00222                 buf[0] = ch;
00223                 buf[1] = '\0';
00224                 Insert(idx, buf);
00225         }
00226 
00231         void Insert(int index, const char *str);
00232 
00233         void Replace( const char from, const char to );
00234 
00235         inline char *GetChars() const { return m_buf; }
00236 
00237         bool IsNumeric() const;
00238         int ToInt() const;
00239 
00240         void ToLower();
00241         void ToUpper();
00242 
00243         int IndexOf( const char *cp, const int start ) const;
00244         inline int IndexOf( const char ch ) const { return IndexOf(ch, 0); }
00245 
00246         inline int IndexOf( const char ch, const int start ) const
00247         {
00248                 return spl::IndexofchfromWithLen(m_buf, ch, start, m_used);
00249         }
00250 
00251         int IndexOfIgnoreCase( const char *cp, const int start ) const;
00252 
00253         inline bool Equals(const char *cp) const { return strcmp(cp, m_buf) == 0; }
00254 
00255         StringPtr ToString() const;
00256 
00257         RefCountPtr<Array<byte> > ToByteArray() const;
00258 
00259 #if defined(DEBUG)
00260         virtual void CheckMem() const;
00261         virtual void ValidateMem() const;
00262 #endif
00263 
00264 private:
00265 
00266         void ExtendTo(int index);
00267 
00268         void Extend();
00269 };
00270 
00271 REGISTER_TYPEOF( 490, StringBuffer );
00272 
00275 #endif
00276 
00277