]> git.saurik.com Git - wxWidgets.git/blob - include/wx/buffer.h
blind fix for latest mingw32 3.1
[wxWidgets.git] / include / wx / buffer.h
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
17 #include "wx/wxchar.h"
18
19 #include <string.h> // strdup
20
21 // ----------------------------------------------------------------------------
22 // Special classes for (wide) character strings: they use malloc/free instead
23 // of new/delete
24 // ----------------------------------------------------------------------------
25
26 class wxCharBuffer
27 {
28 public:
29 wxCharBuffer(const char *str)
30 : m_str(NULL)
31 {
32 wxASSERT_MSG( str, wxT("NULL string in wxCharBuffer") );
33
34 m_str = str ? strdup(str) : (char *)NULL;
35 }
36 wxCharBuffer(size_t len)
37 : m_str(NULL)
38 {
39 m_str = (char *)malloc(len+1);
40 m_str[len] = '\0';
41 }
42 // no need to check for NULL, free() does it
43 ~wxCharBuffer() { free(m_str); }
44
45 wxCharBuffer(const wxCharBuffer& src)
46 : m_str(src.m_str)
47 {
48 // no reference count yet...
49 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
50 }
51 wxCharBuffer& operator=(const wxCharBuffer& src)
52 {
53 m_str = src.m_str;
54 // no reference count yet...
55 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
56 return *this;
57 }
58
59 const char *data() const { return m_str; }
60 operator const char *() const { return m_str; }
61 char operator[](size_t n) const { return m_str[n]; }
62
63 private:
64 char *m_str;
65 };
66
67 #if wxUSE_WCHAR_T
68 class wxWCharBuffer
69 {
70 public:
71 wxWCharBuffer(const wchar_t *wcs)
72 : m_wcs((wchar_t *)NULL)
73 {
74 wxASSERT_MSG( wcs, wxT("NULL string in wxWCharBuffer") );
75
76 if (wcs) {
77 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
78 || ( defined(__MWERKS__) && defined(__WXMSW__) )
79 size_t siz = (std::wcslen(wcs)+1)*sizeof(wchar_t);
80 #else
81 size_t siz = (::wcslen(wcs)+1)*sizeof(wchar_t);
82 #endif
83 m_wcs = (wchar_t *)malloc(siz);
84 memcpy(m_wcs, wcs, siz);
85 }
86 }
87 wxWCharBuffer(size_t len)
88 : m_wcs((wchar_t *)NULL)
89 {
90 m_wcs = (wchar_t *)malloc((len+1)*sizeof(wchar_t));
91 m_wcs[len] = L'\0';
92 }
93
94 // no need to check for NULL, free() does it
95 ~wxWCharBuffer() { free(m_wcs); }
96
97 wxWCharBuffer(const wxWCharBuffer& src)
98 : m_wcs(src.m_wcs)
99 {
100 // no reference count yet...
101 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
102 }
103 wxWCharBuffer& operator=(const wxWCharBuffer& src)
104 {
105 m_wcs = src.m_wcs;
106 // no reference count yet...
107 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
108 return *this;
109 }
110
111 const wchar_t *data() const { return m_wcs; }
112 operator const wchar_t *() const { return m_wcs; }
113 wchar_t operator[](size_t n) const { return m_wcs[n]; }
114
115 private:
116 wchar_t *m_wcs;
117 };
118 #endif
119
120 #if wxUSE_UNICODE
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
131
132
133
134 // ----------------------------------------------------------------------------
135 // A class for holding growable data buffers (not necessarily strings)
136 // ----------------------------------------------------------------------------
137
138 class wxMemoryBuffer
139 {
140 public:
141 enum { BLOCK_SIZE = 1024 };
142 wxMemoryBuffer(size_t size=wxMemoryBuffer::BLOCK_SIZE)
143 : m_data(NULL), m_size(0), m_len(0)
144 {
145 wxASSERT(size > 0);
146 m_data = malloc(size);
147 wxASSERT(m_data != NULL);
148 m_size = size;
149 }
150
151 ~wxMemoryBuffer() { free(m_data); }
152
153 // Accessors
154 void* GetData() const { return m_data; }
155 size_t GetBufSize() const { return m_size; }
156 size_t GetDataLen() const { return m_len; }
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);
185 *(((char*)m_data)+m_len) = data;
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
196
197 // Copy and assignment
198 wxMemoryBuffer(const wxMemoryBuffer& src)
199 : m_data(src.m_data), m_size(src.m_size), m_len(src.m_len)
200 {
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;
212
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
222 protected:
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
234 private:
235 void* m_data;
236 size_t m_size;
237 size_t m_len;
238 };
239
240
241
242 // ----------------------------------------------------------------------------
243 // template class for any kind of data
244 // ----------------------------------------------------------------------------
245
246 // TODO
247
248 #endif // _WX_BUFFER_H