OW_StringBuffer.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
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    // After calling this function, the StringBuffer is unusable
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 } // end namespace OW_NAMESPACE
00187 
00188 #endif

Generated on Thu Feb 9 08:48:16 2006 for openwbem by  doxygen 1.4.6