]> git.saurik.com Git - wxWidgets.git/blame - include/wx/buffer.h
Committing in .
[wxWidgets.git] / include / wx / buffer.h
CommitLineData
14971e5b
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: buffer.h
3// Purpose: auto buffer classes: buffers which automatically free memory
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 12.04.99
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// these classes are for private use only for now, they're not documented
13
14#ifndef _WX_BUFFER_H
15#define _WX_BUFFER_H
16
853d7d3d 17#include "wx/wxchar.h"
e90c1d2a 18
f93d01be 19#include <string.h> // strdup
14971e5b
VZ
20
21// ----------------------------------------------------------------------------
22// Special classes for (wide) character strings: they use malloc/free instead
23// of new/delete
24// ----------------------------------------------------------------------------
25
26class wxCharBuffer
27{
28public:
29 wxCharBuffer(const char *str)
7ee31b00 30 : m_str(NULL)
14971e5b 31 {
223d09f6 32 wxASSERT_MSG( str, wxT("NULL string in wxCharBuffer") );
14971e5b 33
01357b50 34 m_str = str ? strdup(str) : (char *)NULL;
14971e5b 35 }
f93d01be 36 wxCharBuffer(size_t len)
7ee31b00 37 : m_str(NULL)
f93d01be
OK
38 {
39 m_str = (char *)malloc(len+1);
e90c1d2a 40 m_str[len] = '\0';
f93d01be 41 }
14971e5b
VZ
42 // no need to check for NULL, free() does it
43 ~wxCharBuffer() { free(m_str); }
44
f93d01be 45 wxCharBuffer(const wxCharBuffer& src)
7ee31b00 46 : m_str(src.m_str)
f93d01be 47 {
7ee31b00
GD
48 // no reference count yet...
49 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
f93d01be
OK
50 }
51 wxCharBuffer& operator=(const wxCharBuffer& src)
52 {
7ee31b00
GD
53 m_str = src.m_str;
54 // no reference count yet...
55 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
56 return *this;
f93d01be
OK
57 }
58
f6bcfd97 59 const char *data() const { return m_str; }
14971e5b 60 operator const char *() const { return m_str; }
b1fa8b4e 61 char operator[](size_t n) const { return m_str[n]; }
14971e5b
VZ
62
63private:
64 char *m_str;
65};
66
7a3e402c 67#if wxUSE_WCHAR_T
14971e5b
VZ
68class wxWCharBuffer
69{
70public:
71 wxWCharBuffer(const wchar_t *wcs)
7ee31b00 72 : m_wcs((wchar_t *)NULL)
14971e5b 73 {
223d09f6 74 wxASSERT_MSG( wcs, wxT("NULL string in wxWCharBuffer") );
14971e5b 75
853d7d3d 76 if (wcs) {
de85a884
VZ
77#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
78 || ( defined(__MWERKS__) && defined(__WXMSW__) )
d834f22c
JS
79 size_t siz = (std::wcslen(wcs)+1)*sizeof(wchar_t);
80#else
c3b177ae 81 size_t siz = (::wcslen(wcs)+1)*sizeof(wchar_t);
d834f22c 82#endif
853d7d3d
OK
83 m_wcs = (wchar_t *)malloc(siz);
84 memcpy(m_wcs, wcs, siz);
85 }
f93d01be
OK
86 }
87 wxWCharBuffer(size_t len)
7ee31b00 88 : m_wcs((wchar_t *)NULL)
f93d01be
OK
89 {
90 m_wcs = (wchar_t *)malloc((len+1)*sizeof(wchar_t));
e90c1d2a 91 m_wcs[len] = L'\0';
14971e5b
VZ
92 }
93
94 // no need to check for NULL, free() does it
95 ~wxWCharBuffer() { free(m_wcs); }
96
f93d01be 97 wxWCharBuffer(const wxWCharBuffer& src)
7ee31b00 98 : m_wcs(src.m_wcs)
f93d01be 99 {
7ee31b00
GD
100 // no reference count yet...
101 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
f93d01be
OK
102 }
103 wxWCharBuffer& operator=(const wxWCharBuffer& src)
104 {
7ee31b00
GD
105 m_wcs = src.m_wcs;
106 // no reference count yet...
107 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
108 return *this;
f93d01be
OK
109 }
110
f6bcfd97 111 const wchar_t *data() const { return m_wcs; }
14971e5b 112 operator const wchar_t *() const { return m_wcs; }
b1fa8b4e 113 wchar_t operator[](size_t n) const { return m_wcs[n]; }
e90c1d2a 114
14971e5b
VZ
115private:
116 wchar_t *m_wcs;
117};
7a3e402c 118#endif
14971e5b 119
f93d01be 120#if wxUSE_UNICODE
e90c1d2a
VZ
121 #define wxMB2WXbuf wxWCharBuffer
122 #define wxWX2MBbuf wxCharBuffer
123 #define wxWC2WXbuf wxChar*
124 #define wxWX2WCbuf wxChar*
125#else // ANSI
126 #define wxMB2WXbuf wxChar*
127 #define wxWX2MBbuf wxChar*
128 #define wxWC2WXbuf wxCharBuffer
129 #define wxWX2WCbuf wxWCharBuffer
130#endif // Unicode/ANSI
f93d01be 131
b9fdb397
RD
132
133
134// ----------------------------------------------------------------------------
135// A class for holding growable data buffers (not necessarily strings)
136// ----------------------------------------------------------------------------
137
138class wxMemoryBuffer
139{
140public:
141 enum { BLOCK_SIZE = 1024 };
142 wxMemoryBuffer(size_t size=wxMemoryBuffer::BLOCK_SIZE)
7ee31b00 143 : m_data(NULL), m_size(0), m_len(0)
b9fdb397
RD
144 {
145 wxASSERT(size > 0);
146 m_data = malloc(size);
147 wxASSERT(m_data != NULL);
148 m_size = size;
b9fdb397
RD
149 }
150
151 ~wxMemoryBuffer() { free(m_data); }
152
153 // Accessors
ef1cae87
RD
154 void* GetData() const { return m_data; }
155 size_t GetBufSize() const { return m_size; }
156 size_t GetDataLen() const { return m_len; }
b9fdb397
RD
157
158 void SetBufSize(size_t size) { ResizeIfNeeded(size); }
159 void SetDataLen(size_t len)
160 {
161 wxASSERT(len <= m_size);
162 m_len = len;
163 }
164
165 // Ensure the buffer is big enough and return a pointer to it
166 void* GetWriteBuf(size_t sizeNeeded)
167 {
168 ResizeIfNeeded(sizeNeeded);
169 return m_data;
170 }
171 // Update the length after the write
172 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
173
174 // Like the above, but appends to the buffer
175 void* GetAppendBuf(size_t sizeNeeded)
176 {
177 ResizeIfNeeded(m_len + sizeNeeded);
178 return (char*)m_data + m_len;
179 }
180 void UngetAppendBuf(size_t sizeUsed) { SetDataLen(m_len + sizeUsed); }
181
182 // Other ways to append to the buffer
183 void AppendByte(char data) {
184 ResizeIfNeeded(m_len + 1);
ef1cae87 185 *(((char*)m_data)+m_len) = data;
b9fdb397
RD
186 m_len += 1;
187 }
188 void AppendData(void* data, size_t len)
189 {
190 memcpy(GetAppendBuf(len), data, len);
191 UngetAppendBuf(len);
192 }
193
194 operator const char *() const { return (const char*)m_data; }
195
ef1cae87
RD
196
197 // Copy and assignment
198 wxMemoryBuffer(const wxMemoryBuffer& src)
7ee31b00 199 : m_data(src.m_data), m_size(src.m_size), m_len(src.m_len)
ef1cae87 200 {
ef1cae87
RD
201 // no reference count yet...
202 ((wxMemoryBuffer*)&src)->m_data = NULL;
203 ((wxMemoryBuffer*)&src)->m_size = 0;
204 ((wxMemoryBuffer*)&src)->m_len = 0;
205 }
206
207 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
208 {
209 m_data = src.m_data;
210 m_size = src.m_size;
211 m_len = src.m_len;
7ee31b00 212
ef1cae87
RD
213 // no reference count yet...
214 ((wxMemoryBuffer*)&src)->m_data = NULL;
215 ((wxMemoryBuffer*)&src)->m_size = 0;
216 ((wxMemoryBuffer*)&src)->m_len = 0;
217
218 return *this;
219 }
220
221
b9fdb397 222protected:
b9fdb397
RD
223
224 void ResizeIfNeeded(size_t newSize)
225 {
226 if (newSize > m_size)
227 {
228 m_data = realloc(m_data, newSize + wxMemoryBuffer::BLOCK_SIZE);
229 wxASSERT(m_data != NULL);
230 m_size = newSize + wxMemoryBuffer::BLOCK_SIZE;
231 }
232 }
233
234private:
235 void* m_data;
236 size_t m_size;
237 size_t m_len;
238};
239
240
241
14971e5b
VZ
242// ----------------------------------------------------------------------------
243// template class for any kind of data
244// ----------------------------------------------------------------------------
245
246// TODO
247
248#endif // _WX_BUFFER_H