OWBI1_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 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    // After calling this function, the StringBuffer is unusable
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 } // end namespace OWBI1
00179 
00180 #endif

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