blocxx
StringBuffer.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_STRINGBUFFER_HPP_INCLUDE_GUARD_
40 #define BLOCXX_STRINGBUFFER_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/String.hpp"
43 #include "blocxx/Char16.hpp"
44 #include "blocxx/Bool.hpp"
45 #include <iosfwd>
46 #include <cstring>
47 
48 namespace BLOCXX_NAMESPACE
49 {
50 
51 class BLOCXX_COMMON_API StringBuffer
52 {
53 public:
54 #if defined(BLOCXX_AIX)
55  static const size_t BLOCXX_DEFAULT_ALLOCATION_UNIT;
56 #else
57  static const size_t BLOCXX_DEFAULT_ALLOCATION_UNIT = 128;
58 #endif // BLOCXX_AIX
59  StringBuffer(size_t allocSize = BLOCXX_DEFAULT_ALLOCATION_UNIT);
60  StringBuffer(const char* arg);
61  StringBuffer(const String& arg);
62  StringBuffer(const StringBuffer& arg);
63  ~StringBuffer() { delete [] m_bfr; }
64  StringBuffer& operator= (const StringBuffer& arg);
65  StringBuffer& operator= (const String& arg);
66  StringBuffer& operator= (const char* str);
67  void swap(StringBuffer& x);
68  StringBuffer& append(char c)
69  {
70  checkAvail();
71  m_bfr[m_len++] = c;
72  m_bfr[m_len] = '\0';
73  return *this;
74  }
75  StringBuffer& append(const char* str)
76  {
77  size_t len = ::strlen(str);
78  checkAvail(len+1);
79  ::strcpy(m_bfr+m_len, str);
80  m_len += len;
81  return *this;
82  }
83  StringBuffer& append(const char* str, const size_t len);
84  StringBuffer& append(const String& arg)
85  { return append(arg.c_str(), arg.length()); }
86  StringBuffer& append(const StringBuffer& arg)
87  {
88  return append(arg.c_str(), arg.length());
89  }
90  StringBuffer& operator += (char c)
91  { return append(c); }
92  StringBuffer& operator += (Char16 c)
93  { return append(c.toString()); }
94  StringBuffer& operator += (const char* str)
95  { return append(str); }
96  StringBuffer& operator += (const String& arg)
97  { return append(arg); }
98  StringBuffer& operator += (Bool v);
99  StringBuffer& operator += (UInt8 v);
100  StringBuffer& operator += (Int8 v);
101  StringBuffer& operator += (UInt16 v);
102  StringBuffer& operator += (Int16 v);
103  StringBuffer& operator += (UInt32 v);
104  StringBuffer& operator += (Int32 v);
105  StringBuffer& operator += (UInt64 v);
106  StringBuffer& operator += (Int64 v);
107 // do this check so we fill in the gaps and have int, long & long long constructors if necessary
108 #if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG)
109  StringBuffer& operator += (long v);
110  StringBuffer& operator += (unsigned long v);
111 #endif
112  StringBuffer& operator += (Real32 v);
113  StringBuffer& operator += (Real64 v);
114  StringBuffer& operator += (const StringBuffer& arg)
115  {
116  return append(arg);
117  }
118  char operator[] (size_t ndx) const;
119  String toString() const
120  { return String(m_bfr); }
121  // After calling this function, the StringBuffer is unusable
122  String releaseString()
123  {
124  char * bfr = m_bfr;
125  m_bfr = 0;
126  return String(String::E_TAKE_OWNERSHIP, bfr, m_len);
127  }
128  size_t length() const { return m_len; }
129 
137  void truncate(size_t index);
138 
145  const char* getLine(std::istream& is, bool resetBuffer=true);
146 
147  bool endsWith(char ch) const;
148  bool startsWith(char ch) const;
149  void chop();
150  void trim();
151 
152  size_t allocated() const { return m_allocated; }
153  void reset();
154  const char* c_str() const { return m_bfr; }
155  bool equals(const char* arg) const;
156  bool equals(const StringBuffer& arg) const;
157  friend BLOCXX_COMMON_API std::ostream& operator<<(std::ostream& ostr, const StringBuffer& b);
158 private:
159  void checkAvail(size_t len=1)
160  {
161  size_t freeSpace = m_allocated - (m_len+1);
162 
163  if (len > freeSpace)
164  {
165  size_t toalloc = m_allocated * 2 + len;
166  char* bfr = new char[toalloc];
167  ::memmove(bfr, m_bfr, m_len);
168  delete [] m_bfr;
169  m_allocated = toalloc;
170  m_bfr = bfr;
171  }
172  }
173  size_t m_len;
174  size_t m_allocated;
175  char* m_bfr;
176 };
177 
178 BLOCXX_COMMON_API bool operator==(const StringBuffer& x, const StringBuffer& y);
179 BLOCXX_COMMON_API bool operator!=(const StringBuffer& x, const StringBuffer& y);
180 BLOCXX_COMMON_API bool operator==(const StringBuffer& x, const String& y);
181 BLOCXX_COMMON_API bool operator!=(const StringBuffer& x, const String& y);
182 BLOCXX_COMMON_API bool operator==(const String& x, const StringBuffer& y);
183 BLOCXX_COMMON_API bool operator!=(const String& x, const StringBuffer& y);
184 
185 } // end namespace BLOCXX_NAMESPACE
186 
187 #endif