blocxx

StringBuffer.hpp

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