don't initialize m_str twice in wxCharBuffer ctor
[wxWidgets.git] / include / wx / buffer.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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(str ? strdup(str) : NULL)
31 {
32 wxASSERT_MSG( str, wxT("NULL string in wxCharBuffer") );
33 }
34
35 wxCharBuffer(size_t len)
36 : m_str((char *)malloc((len + 1)*sizeof(char)))
37 {
38 m_str[len] = '\0';
39 }
40
41 // no need to check for NULL, free() does it
42 ~wxCharBuffer() { free(m_str); }
43
44 wxCharBuffer(const wxCharBuffer& src)
45 : m_str(src.m_str)
46 {
47 // no reference count yet...
48 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
49 }
50 wxCharBuffer& operator=(const wxCharBuffer& src)
51 {
52 m_str = src.m_str;
53 // no reference count yet...
54 ((wxCharBuffer*)&src)->m_str = (char *)NULL;
55 return *this;
56 }
57
58 const char *data() const { return m_str; }
59 operator const char *() const { return m_str; }
60 char operator[](size_t n) const { return m_str[n]; }
61
62 private:
63 char *m_str;
64 };
65
66 #if wxUSE_WCHAR_T
67
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 *)malloc((len + 1)*sizeof(wchar_t)))
89 {
90 m_wcs[len] = L'\0';
91 }
92
93 // no need to check for NULL, free() does it
94 ~wxWCharBuffer() { free(m_wcs); }
95
96 wxWCharBuffer(const wxWCharBuffer& src)
97 : m_wcs(src.m_wcs)
98 {
99 // no reference count yet...
100 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
101 }
102 wxWCharBuffer& operator=(const wxWCharBuffer& src)
103 {
104 m_wcs = src.m_wcs;
105 // no reference count yet...
106 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
107 return *this;
108 }
109
110 const wchar_t *data() const { return m_wcs; }
111 operator const wchar_t *() const { return m_wcs; }
112 wchar_t operator[](size_t n) const { return m_wcs[n]; }
113
114 private:
115 wchar_t *m_wcs;
116 };
117
118 #endif // wxUSE_WCHAR_T
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 // A class for holding growable data buffers (not necessarily strings)
134 // ----------------------------------------------------------------------------
135
136 class wxMemoryBuffer
137 {
138 public:
139 enum { BLOCK_SIZE = 1024 };
140 wxMemoryBuffer(size_t size = wxMemoryBuffer::BLOCK_SIZE)
141 : m_data(malloc(size)), m_size(size), m_len(0)
142 {
143 }
144
145 ~wxMemoryBuffer() { free(m_data); }
146
147 // Accessors
148 void* GetData() const { return m_data; }
149 size_t GetBufSize() const { return m_size; }
150 size_t GetDataLen() const { return m_len; }
151
152 void SetBufSize(size_t size) { ResizeIfNeeded(size); }
153 void SetDataLen(size_t len)
154 {
155 wxASSERT(len <= m_size);
156 m_len = len;
157 }
158
159 // Ensure the buffer is big enough and return a pointer to it
160 void* GetWriteBuf(size_t sizeNeeded)
161 {
162 ResizeIfNeeded(sizeNeeded);
163 return m_data;
164 }
165 // Update the length after the write
166 void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
167
168 // Like the above, but appends to the buffer
169 void* GetAppendBuf(size_t sizeNeeded)
170 {
171 ResizeIfNeeded(m_len + sizeNeeded);
172 return (char*)m_data + m_len;
173 }
174 void UngetAppendBuf(size_t sizeUsed) { SetDataLen(m_len + sizeUsed); }
175
176 // Other ways to append to the buffer
177 void AppendByte(char data) {
178 ResizeIfNeeded(m_len + 1);
179 *(((char*)m_data)+m_len) = data;
180 m_len += 1;
181 }
182 void AppendData(void* data, size_t len)
183 {
184 memcpy(GetAppendBuf(len), data, len);
185 UngetAppendBuf(len);
186 }
187
188 operator const char *() const { return (const char*)m_data; }
189
190
191 // Copy and assignment
192 wxMemoryBuffer(const wxMemoryBuffer& src)
193 : m_data(src.m_data), m_size(src.m_size), m_len(src.m_len)
194 {
195 // no reference count yet...
196 ((wxMemoryBuffer*)&src)->m_data = NULL;
197 ((wxMemoryBuffer*)&src)->m_size = 0;
198 ((wxMemoryBuffer*)&src)->m_len = 0;
199 }
200
201 wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
202 {
203 m_data = src.m_data;
204 m_size = src.m_size;
205 m_len = src.m_len;
206
207 // no reference count yet...
208 ((wxMemoryBuffer*)&src)->m_data = NULL;
209 ((wxMemoryBuffer*)&src)->m_size = 0;
210 ((wxMemoryBuffer*)&src)->m_len = 0;
211
212 return *this;
213 }
214
215
216 protected:
217 void ResizeIfNeeded(size_t newSize)
218 {
219 if (newSize > m_size)
220 {
221 m_data = realloc(m_data, newSize + wxMemoryBuffer::BLOCK_SIZE);
222 wxASSERT(m_data != NULL);
223 m_size = newSize + wxMemoryBuffer::BLOCK_SIZE;
224 }
225 }
226
227 private:
228 void* m_data;
229 size_t m_size;
230 size_t m_len;
231 };
232
233 // ----------------------------------------------------------------------------
234 // template class for any kind of data
235 // ----------------------------------------------------------------------------
236
237 // TODO
238
239 #endif // _WX_BUFFER_H