StringBuffer.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_STRINGBUFFER_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_STRINGBUFFER_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/String.hpp"
00042 #include "blocxx/Char16.hpp"
00043 #include "blocxx/Bool.hpp"
00044 #include <iosfwd>
00045 #include <cstring>
00046 
00047 namespace BLOCXX_NAMESPACE
00048 {
00049 
00050 class BLOCXX_COMMON_API StringBuffer
00051 {
00052 public:
00053 #if defined(BLOCXX_AIX)
00054    static const size_t BLOCXX_DEFAULT_ALLOCATION_UNIT;
00055 #else
00056    static const size_t BLOCXX_DEFAULT_ALLOCATION_UNIT = 128;
00057 #endif // BLOCXX_AIX
00058    StringBuffer(size_t allocSize = BLOCXX_DEFAULT_ALLOCATION_UNIT);
00059    StringBuffer(const char* arg);
00060    StringBuffer(const String& arg);
00061    StringBuffer(const StringBuffer& arg);
00062    ~StringBuffer() { delete [] m_bfr; }
00063    StringBuffer& operator= (const StringBuffer& arg);
00064    StringBuffer& operator= (const String& arg);
00065    StringBuffer& operator= (const char* str);
00066    void swap(StringBuffer& x);
00067    StringBuffer& append(char c)
00068    {
00069       checkAvail();
00070       m_bfr[m_len++] = c;
00071       m_bfr[m_len] = '\0';
00072       return *this;
00073    }
00074    StringBuffer& append(const char* str)
00075    {
00076       size_t len = ::strlen(str);
00077       checkAvail(len+1);
00078       ::strcpy(m_bfr+m_len, str);
00079       m_len += len;
00080       return *this;
00081    }
00082    StringBuffer& append(const char* str, const size_t len);
00083    StringBuffer& append(const String& arg)   
00084       { return append(arg.c_str(), arg.length()); }
00085    StringBuffer& append(const StringBuffer& arg)
00086    {
00087       return append(arg.c_str(), arg.length());
00088    }
00089    StringBuffer& operator += (char c)
00090       { return append(c); }
00091    StringBuffer& operator += (Char16 c)
00092       { return append(c.toString()); }
00093    StringBuffer& operator += (const char* str)
00094       { return append(str); }
00095    StringBuffer& operator += (const String& arg)
00096       { return append(arg); }
00097    StringBuffer& operator += (Bool v);
00098    StringBuffer& operator += (UInt8 v);
00099    StringBuffer& operator += (Int8 v);
00100    StringBuffer& operator += (UInt16 v);
00101    StringBuffer& operator += (Int16 v);
00102    StringBuffer& operator += (UInt32 v);
00103    StringBuffer& operator += (Int32 v);
00104    StringBuffer& operator += (UInt64 v);
00105    StringBuffer& operator += (Int64 v);
00106    StringBuffer& operator += (Real32 v);
00107    StringBuffer& operator += (Real64 v);
00108    StringBuffer& operator += (const StringBuffer& arg)
00109    {
00110       return append(arg);
00111    }
00112    char operator[] (size_t ndx) const;
00113    String toString() const
00114          { return String(m_bfr); }
00115    // After calling this function, the StringBuffer is unusable
00116    String releaseString()
00117    {
00118       char * bfr = m_bfr;
00119       m_bfr = 0;
00120       return String(String::E_TAKE_OWNERSHIP, bfr, m_len);
00121    }
00122    size_t length() const {  return m_len; }
00123 
00131    void truncate(size_t index);
00132 
00140    const char* getLine(std::istream& is, bool resetBuffer=true);
00141 
00142    bool endsWith(char ch) const;
00143    bool startsWith(char ch) const;
00144    void chop();
00145    void trim();
00146 
00147    size_t allocated() const {  return m_allocated; }
00148    void reset();
00149    const char* c_str() const {  return m_bfr; }
00150    bool equals(const char* arg) const;
00151    bool equals(const StringBuffer& arg) const;
00152    friend BLOCXX_COMMON_API std::ostream& operator<<(std::ostream& ostr, const StringBuffer& b);
00153 private:
00154    void checkAvail(size_t len=1)
00155    {
00156       size_t freeSpace = m_allocated - (m_len+1);
00157    
00158       if (len > freeSpace)
00159       {
00160          size_t toalloc = m_allocated * 2 + len;
00161          char* bfr = new char[toalloc];
00162          ::memmove(bfr, m_bfr, m_len);
00163          delete [] m_bfr;
00164          m_allocated = toalloc;
00165          m_bfr = bfr;
00166       }
00167    }
00168    size_t m_len;
00169    size_t m_allocated;
00170    char* m_bfr;
00171 };
00172 
00173 bool operator==(const StringBuffer& x, const StringBuffer& y);
00174 bool operator!=(const StringBuffer& x, const StringBuffer& y);
00175 bool operator==(const StringBuffer& x, const String& y);
00176 bool operator!=(const StringBuffer& x, const String& y);
00177 bool operator==(const String& x, const StringBuffer& y);
00178 bool operator!=(const String& x, const StringBuffer& y);
00179 
00180 } // end namespace BLOCXX_NAMESPACE
00181 
00182 #endif

Generated on Fri Jun 16 15:39:09 2006 for blocxx by  doxygen 1.4.6