common: more pathscale compile fixes
[supercollider.git] / common / SC_StringBuffer.cpp
blobb86dd2d5d1dbd11b13f64287303c267266946862
1 // emacs: -*- c++ -*-
2 // file: SC_StringBuffer.cpp
3 // copyright: 2003 stefan kersten <steve@k-hornz.de>
4 // cvs: $Id$
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
19 // USA
21 #include "SC_StringBuffer.h"
22 #include <assert.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdexcept>
27 #ifdef _WIN32
28 # define vsnprintf _vsnprintf
29 # include <stdio.h>
30 # include <stdarg.h>
31 #endif
33 #include <string>
35 SC_StringBuffer::SC_StringBuffer(size_t initialSize)
36 : mCapacity(0), mPtr(0), mData(0)
38 growBy(initialSize);
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()
50 free(mData);
53 void SC_StringBuffer::append(const char* src, size_t size)
55 if (size > 0) {
56 size_t remaining = getRemaining();
57 if (size > remaining) {
58 growBy(size - remaining);
60 memcpy(mPtr, src, size);
61 mPtr += 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)
77 va_list ap2;
78 size_t remaining = getRemaining();
80 // Calling vsnprintf may invalidate vargs, so keep a copy
81 #ifdef __va_copy
82 __va_copy(ap2, ap);
83 #else
84 ap2 = ap;
85 #endif
87 // NOTE: This only works since glibc 2.0.6!
88 int size = vsnprintf(mPtr, remaining, fmt, ap);
89 va_end(ap);
91 // size returned excludes trailing \0
92 if (size++ > 0) {
93 if ((size_t)size > remaining) {
94 growBy(size - remaining);
95 vsnprintf(mPtr, size, fmt, ap2);
97 mPtr += size-1; // omit trailing \0
100 va_end(ap2);
103 void SC_StringBuffer::appendf(const char* fmt, ...)
105 va_list ap;
106 va_start(ap, fmt);
107 vappendf(fmt, ap);
108 va_end(ap);
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);
121 if (newData) {
122 mData = newData;
123 mCapacity = newCapacity;
124 mPtr = mData + oldSize;
125 } else
126 throw std::runtime_error(std::string("SC_StringBuffer: memory allocation failure"));
129 // EOF