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 OW_STRINGBUFFER_HPP_INCLUDE_GUARD_
00037 #define OW_STRINGBUFFER_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_String.hpp"
00040 #include "OW_Char16.hpp"
00041 #include "OW_Bool.hpp"
00042 #include "OW_CIMFwd.hpp"
00043 #include <iosfwd>
00044 #include <cstring>
00045
00046 namespace OW_NAMESPACE
00047 {
00048
00049 class OW_COMMON_API StringBuffer
00050 {
00051 public:
00052 #if defined(OW_AIX)
00053 static const size_t OW_DEFAULT_ALLOCATION_UNIT;
00054 #else
00055 static const size_t OW_DEFAULT_ALLOCATION_UNIT = 128;
00056 #endif // OW_AIX
00057 StringBuffer(size_t allocSize = OW_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);
00110 StringBuffer& operator += (const CIMDateTime& arg) OW_DEPRECATED;
00114 StringBuffer& operator += (const CIMObjectPath& arg) OW_DEPRECATED;
00115 StringBuffer& operator += (const StringBuffer& arg)
00116 {
00117 return append(arg);
00118 }
00119 char operator[] (size_t ndx) const;
00120 String toString() const
00121 { return String(m_bfr); }
00122
00123 String releaseString()
00124 {
00125 char * bfr = m_bfr;
00126 m_bfr = 0;
00127 return String(String::E_TAKE_OWNERSHIP, bfr, m_len);
00128 }
00129 size_t length() const { return m_len; }
00130
00138 void truncate(size_t index);
00139
00146 const char* getLine(std::istream& is, bool resetBuffer=true);
00147
00148 bool endsWith(char ch) const;
00149 bool startsWith(char ch) const;
00150 void chop();
00151 void trim();
00152
00153 size_t allocated() const { return m_allocated; }
00154 void reset();
00155 const char* c_str() const { return m_bfr; }
00156 bool equals(const char* arg) const;
00157 bool equals(const StringBuffer& arg) const;
00158 friend OW_COMMON_API std::ostream& operator<<(std::ostream& ostr, const StringBuffer& b);
00159 private:
00160 void checkAvail(size_t len=1)
00161 {
00162 size_t freeSpace = m_allocated - (m_len+1);
00163
00164 if (len > freeSpace)
00165 {
00166 size_t toalloc = m_allocated * 2 + len;
00167 char* bfr = new char[toalloc];
00168 ::memmove(bfr, m_bfr, m_len);
00169 delete [] m_bfr;
00170 m_allocated = toalloc;
00171 m_bfr = bfr;
00172 }
00173 }
00174 size_t m_len;
00175 size_t m_allocated;
00176 char* m_bfr;
00177 };
00178
00179 bool operator==(const StringBuffer& x, const StringBuffer& y);
00180 bool operator!=(const StringBuffer& x, const StringBuffer& y);
00181 bool operator==(const StringBuffer& x, const String& y);
00182 bool operator!=(const StringBuffer& x, const String& y);
00183 bool operator==(const String& x, const StringBuffer& y);
00184 bool operator!=(const String& x, const StringBuffer& y);
00185
00186 }
00187
00188 #endif