2 // file: SC_StringBuffer.cpp
3 // copyright: 2003 stefan kersten <steve@k-hornz.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 #include "SC_StringBuffer.h"
28 # define vsnprintf _vsnprintf
35 SC_StringBuffer::SC_StringBuffer(size_t initialSize
)
36 : mCapacity(0), mPtr(0), mData(0)
41 SC_StringBuffer::SC_StringBuffer(const SC_StringBuffer
& other
)
42 : mCapacity(0), mPtr(0), mData(0)
44 growBy(other
.getSize());
45 append(other
.getData(), other
.getSize());
48 SC_StringBuffer::~SC_StringBuffer()
53 void SC_StringBuffer::append(const char* src
, size_t size
)
56 size_t remaining
= getRemaining();
57 if (size
> remaining
) {
58 growBy(size
- remaining
);
60 memcpy(mPtr
, src
, size
);
65 void SC_StringBuffer::append(char c
)
67 append(&c
, sizeof(c
));
70 void SC_StringBuffer::append(const char* str
)
72 append(str
, strlen(str
));
75 void SC_StringBuffer::vappendf(const char* fmt
, va_list ap
)
78 size_t remaining
= getRemaining();
80 // Calling vsnprintf may invalidate vargs, so keep a copy
87 // NOTE: This only works since glibc 2.0.6!
88 int size
= vsnprintf(mPtr
, remaining
, fmt
, ap
);
91 // size returned excludes trailing \0
93 if ((size_t)size
> remaining
) {
94 growBy(size
- remaining
);
95 vsnprintf(mPtr
, size
, fmt
, ap2
);
97 mPtr
+= size
-1; // omit trailing \0
103 void SC_StringBuffer::appendf(const char* fmt
, ...)
111 void SC_StringBuffer::growBy(size_t request
)
113 size_t oldSize
= getSize();
114 size_t newCapacity
= mCapacity
+ ((request
+ (size_t)kGrowAlign
) & (size_t)~kGrowMask
);
116 // fprintf(stderr, "%s: mCapacity %u, request %u, newCapacity %u\n",
117 // __PRETTY_FUNCTION__, mCapacity, request, newCapacity);
118 assert((newCapacity
>= (mCapacity
+ request
)) && ((newCapacity
& kGrowMask
) == 0));
120 char* newData
= (char*)realloc(mData
, newCapacity
);
123 mCapacity
= newCapacity
;
124 mPtr
= mData
+ oldSize
;
126 throw std::runtime_error(std::string("SC_StringBuffer: memory allocation failure"));