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
38
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
48namespace BLOCXX_NAMESPACE
49{
50
51class BLOCXX_COMMON_API StringBuffer
52{
53public:
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
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);
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);
85 { return append(arg.c_str(), arg.length()); }
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;
120 { return String(m_bfr); }
121 // After calling this function, the StringBuffer is unusable
123 {
124 char * bfr = m_bfr;
125 m_bfr = 0;
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);
158private:
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;
175 char* m_bfr;
176};
177
178BLOCXX_COMMON_API bool operator==(const StringBuffer& x, const StringBuffer& y);
179BLOCXX_COMMON_API bool operator!=(const StringBuffer& x, const StringBuffer& y);
180BLOCXX_COMMON_API bool operator==(const StringBuffer& x, const String& y);
181BLOCXX_COMMON_API bool operator!=(const StringBuffer& x, const String& y);
182BLOCXX_COMMON_API bool operator==(const String& x, const StringBuffer& y);
183BLOCXX_COMMON_API bool operator!=(const String& x, const StringBuffer& y);
184
185} // end namespace BLOCXX_NAMESPACE
186
187#endif
The Bool class is an abstraction for the boolean data type.
Definition Bool.hpp:57
The Char16 class is an abstraction for a double byte character.
Definition Char16.hpp:57
String toString() const
Convert this to UTF8.
Definition Char16.cpp:72
StringBuffer(size_t allocSize=BLOCXX_DEFAULT_ALLOCATION_UNIT)
StringBuffer & append(const char *str)
StringBuffer & append(char c)
static const size_t BLOCXX_DEFAULT_ALLOCATION_UNIT
StringBuffer & append(const String &arg)
StringBuffer & append(const StringBuffer &arg)
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
const char * c_str() const
Definition String.cpp:905
size_t length() const
Definition String.cpp:354
Taken from RFC 1321.
unsigned char UInt8
Definition Types.hpp:66
signed char Int8
Definition Types.hpp:67
bool operator==(const Array< T > &x, const Array< T > &y)
ostream & operator<<(ostream &ostrm, const Bool &arg)
Definition Bool.cpp:77
bool operator!=(const Array< T > &x, const Array< T > &y)
Determine two Arrays are not equal.
Definition Array.hpp:446
void swap(Array< T > &x, Array< T > &y)