common: prevent buffer overflow
[supercollider.git] / include / common / SC_StringBuffer.h
blob16bc78f75e46d7db2344e7cc9ebf21bb1d8440bf
1 // emacs: -*- c++ -*-
2 // file: SC_StringBuffer.h
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 #ifndef SC_STRINGBUFFER_H_INCLUDED
22 #define SC_STRINGBUFFER_H_INCLUDED
24 #include <stdarg.h>
25 #include <stdlib.h>
27 // =====================================================================
28 // SC_StringBuffer - Autogrowing string buffer.
29 // =====================================================================
31 class SC_StringBuffer
33 public:
34 SC_StringBuffer(size_t initialSize=0);
35 SC_StringBuffer(const SC_StringBuffer& other);
36 ~SC_StringBuffer();
38 size_t getCapacity() const { return mCapacity; }
39 size_t getSize() const { return mPtr - mData; }
40 size_t getRemaining() const { return mCapacity - getSize(); }
41 char* getData() const { return mData; }
43 bool isEmpty() const { return getSize() == 0; }
45 void finish() { append('\0'); }
46 void reset() { mPtr = mData; }
47 void append(const char* src, size_t len);
48 void append(char c);
49 void append(const char* str);
50 void vappendf(const char* fmt, va_list vargs);
51 void appendf(const char* fmt, ...);
53 protected:
54 enum {
55 kGrowAlign = 256,
56 kGrowMask = kGrowAlign - 1
59 void growBy(size_t request);
61 private:
62 size_t mCapacity;
63 char* mPtr;
64 char* mData;
67 #endif // SC_STRINGBUFFER_H_INCLUDED