00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #ifndef OWBI1_STRINGBUFFER_HPP_INCLUDE_GUARD_
00037 #define OWBI1_STRINGBUFFER_HPP_INCLUDE_GUARD_
00038 #include "OWBI1_config.h"
00039 #include "OWBI1_String.hpp"
00040 #include "OWBI1_Char16.hpp"
00041 #include "OWBI1_Bool.hpp"
00042 #include "OWBI1_CIMFwd.hpp"
00043 #include <iosfwd>
00044 #include <cstring>
00045
00046 namespace OWBI1
00047 {
00048
00049 class OWBI1_OWBI1PROVIFC_API StringBuffer
00050 {
00051 public:
00052 #if defined(OWBI1_AIX)
00053 static const size_t OWBI1_DEFAULT_ALLOCATION_UNIT;
00054 #else
00055 static const size_t OWBI1_DEFAULT_ALLOCATION_UNIT = 128;
00056 #endif // OWBI1_AIX
00057 StringBuffer(size_t allocSize = OWBI1_DEFAULT_ALLOCATION_UNIT);
00058 StringBuffer(const char* arg);
00059 StringBuffer(const String& arg);
00060 StringBuffer(const StringBuffer& arg);
00061 ~StringBuffer() { delete [] m_bfr; }
00062 StringBuffer& operator= (const StringBuffer& arg);
00063 StringBuffer& operator= (const String& arg);
00064 StringBuffer& operator= (const char* str);
00065 void swap(StringBuffer& x);
00066 StringBuffer& append(char c)
00067 {
00068 checkAvail();
00069 m_bfr[m_len++] = c;
00070 m_bfr[m_len] = '\0';
00071 return *this;
00072 }
00073 StringBuffer& append(const char* str)
00074 {
00075 size_t len = ::strlen(str);
00076 checkAvail(len+1);
00077 ::strcpy(m_bfr+m_len, str);
00078 m_len += len;
00079 return *this;
00080 }
00081 StringBuffer& append(const char* str, const size_t len);
00082 StringBuffer& append(const String& arg)
00083 { return append(arg.c_str(), arg.length()); }
00084 StringBuffer& append(const StringBuffer& arg)
00085 {
00086 return append(arg.c_str(), arg.length());
00087 }
00088 StringBuffer& operator += (char c)
00089 { return append(c); }
00090 StringBuffer& operator += (Char16 c)
00091 { return append(c.toString()); }
00092 StringBuffer& operator += (const char* str)
00093 { return append(str); }
00094 StringBuffer& operator += (const String& arg)
00095 { return append(arg); }
00096 StringBuffer& operator += (Bool v);
00097 StringBuffer& operator += (UInt8 v);
00098 StringBuffer& operator += (Int8 v);
00099 StringBuffer& operator += (UInt16 v);
00100 StringBuffer& operator += (Int16 v);
00101 StringBuffer& operator += (UInt32 v);
00102 StringBuffer& operator += (Int32 v);
00103 StringBuffer& operator += (UInt64 v);
00104 StringBuffer& operator += (Int64 v);
00105 StringBuffer& operator += (Real32 v);
00106 StringBuffer& operator += (Real64 v);
00107 StringBuffer& operator += (const StringBuffer& arg)
00108 {
00109 return append(arg);
00110 }
00111 char operator[] (size_t ndx) const;
00112 String toString() const
00113 { return String(m_bfr); }
00114
00115 String releaseString()
00116 {
00117 char * bfr = m_bfr;
00118 m_bfr = 0;
00119 return String(String::E_TAKE_OWNERSHIP, bfr, m_len);
00120 }
00121 size_t length() const { return m_len; }
00122
00130 void truncate(size_t index);
00131
00138 const char* getLine(std::istream& is, bool resetBuffer=true);
00139
00140 bool endsWith(char ch) const;
00141 bool startsWith(char ch) const;
00142 void chop();
00143 void trim();
00144
00145 size_t allocated() const { return m_allocated; }
00146 void reset();
00147 const char* c_str() const { return m_bfr; }
00148 bool equals(const char* arg) const;
00149 bool equals(const StringBuffer& arg) const;
00150 friend OWBI1_OWBI1PROVIFC_API std::ostream& operator<<(std::ostream& ostr, const StringBuffer& b);
00151 private:
00152 void checkAvail(size_t len=1)
00153 {
00154 size_t freeSpace = m_allocated - (m_len+1);
00155
00156 if (len > freeSpace)
00157 {
00158 size_t toalloc = m_allocated * 2 + len;
00159 char* bfr = new char[toalloc];
00160 ::memmove(bfr, m_bfr, m_len);
00161 delete [] m_bfr;
00162 m_allocated = toalloc;
00163 m_bfr = bfr;
00164 }
00165 }
00166 size_t m_len;
00167 size_t m_allocated;
00168 char* m_bfr;
00169 };
00170
00171 bool operator==(const StringBuffer& x, const StringBuffer& y);
00172 bool operator!=(const StringBuffer& x, const StringBuffer& y);
00173 bool operator==(const StringBuffer& x, const String& y);
00174 bool operator!=(const StringBuffer& x, const String& y);
00175 bool operator==(const String& x, const StringBuffer& y);
00176 bool operator!=(const String& x, const StringBuffer& y);
00177
00178 }
00179
00180 #endif