]>
Commit | Line | Data |
---|---|---|
14971e5b | 1 | /////////////////////////////////////////////////////////////////////////////// |
c12ef8a2 | 2 | // Name: wx/buffer.h |
14971e5b VZ |
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 | ||
26 | class wxCharBuffer | |
27 | { | |
28 | public: | |
29 | wxCharBuffer(const char *str) | |
c12ef8a2 | 30 | : m_str(str ? strdup(str) : NULL) |
14971e5b | 31 | { |
14971e5b | 32 | } |
c12ef8a2 | 33 | |
f93d01be | 34 | wxCharBuffer(size_t len) |
c12ef8a2 | 35 | : m_str((char *)malloc((len + 1)*sizeof(char))) |
f93d01be | 36 | { |
e90c1d2a | 37 | m_str[len] = '\0'; |
f93d01be | 38 | } |
c12ef8a2 | 39 | |
14971e5b | 40 | // no need to check for NULL, free() does it |
c12ef8a2 | 41 | ~wxCharBuffer() { free(m_str); } |
14971e5b | 42 | |
c12ef8a2 VZ |
43 | wxCharBuffer(const wxCharBuffer& src) |
44 | : m_str(src.m_str) | |
45 | { | |
46 | // no reference count yet... | |
47 | ((wxCharBuffer*)&src)->m_str = (char *)NULL; | |
48 | } | |
49 | wxCharBuffer& operator=(const wxCharBuffer& src) | |
50 | { | |
51 | m_str = src.m_str; | |
52 | // no reference count yet... | |
53 | ((wxCharBuffer*)&src)->m_str = (char *)NULL; | |
54 | return *this; | |
55 | } | |
f93d01be | 56 | |
c12ef8a2 VZ |
57 | const char *data() const { return m_str; } |
58 | operator const char *() const { return m_str; } | |
59 | char operator[](size_t n) const { return m_str[n]; } | |
14971e5b VZ |
60 | |
61 | private: | |
c12ef8a2 | 62 | char *m_str; |
14971e5b VZ |
63 | }; |
64 | ||
7a3e402c | 65 | #if wxUSE_WCHAR_T |
c12ef8a2 | 66 | |
14971e5b VZ |
67 | class wxWCharBuffer |
68 | { | |
69 | public: | |
70 | wxWCharBuffer(const wchar_t *wcs) | |
7ee31b00 | 71 | : m_wcs((wchar_t *)NULL) |
14971e5b | 72 | { |
853d7d3d | 73 | if (wcs) { |
e93e5b0b | 74 | size_t siz = (wxWcslen(wcs) + 1)*sizeof(wchar_t); |
853d7d3d OK |
75 | m_wcs = (wchar_t *)malloc(siz); |
76 | memcpy(m_wcs, wcs, siz); | |
77 | } | |
f93d01be OK |
78 | } |
79 | wxWCharBuffer(size_t len) | |
c12ef8a2 | 80 | : m_wcs((wchar_t *)malloc((len + 1)*sizeof(wchar_t))) |
f93d01be | 81 | { |
e90c1d2a | 82 | m_wcs[len] = L'\0'; |
14971e5b VZ |
83 | } |
84 | ||
85 | // no need to check for NULL, free() does it | |
c12ef8a2 | 86 | ~wxWCharBuffer() { free(m_wcs); } |
14971e5b | 87 | |
c12ef8a2 VZ |
88 | wxWCharBuffer(const wxWCharBuffer& src) |
89 | : m_wcs(src.m_wcs) | |
90 | { | |
7ee31b00 GD |
91 | // no reference count yet... |
92 | ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL; | |
c12ef8a2 VZ |
93 | } |
94 | wxWCharBuffer& operator=(const wxWCharBuffer& src) | |
95 | { | |
96 | m_wcs = src.m_wcs; | |
97 | // no reference count yet... | |
98 | ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL; | |
99 | return *this; | |
100 | } | |
f93d01be | 101 | |
c12ef8a2 VZ |
102 | const wchar_t *data() const { return m_wcs; } |
103 | operator const wchar_t *() const { return m_wcs; } | |
104 | wchar_t operator[](size_t n) const { return m_wcs[n]; } | |
e90c1d2a | 105 | |
14971e5b | 106 | private: |
c12ef8a2 | 107 | wchar_t *m_wcs; |
14971e5b | 108 | }; |
c12ef8a2 VZ |
109 | |
110 | #endif // wxUSE_WCHAR_T | |
14971e5b | 111 | |
f93d01be | 112 | #if wxUSE_UNICODE |
e90c1d2a VZ |
113 | #define wxMB2WXbuf wxWCharBuffer |
114 | #define wxWX2MBbuf wxCharBuffer | |
115 | #define wxWC2WXbuf wxChar* | |
116 | #define wxWX2WCbuf wxChar* | |
117 | #else // ANSI | |
118 | #define wxMB2WXbuf wxChar* | |
119 | #define wxWX2MBbuf wxChar* | |
120 | #define wxWC2WXbuf wxCharBuffer | |
121 | #define wxWX2WCbuf wxWCharBuffer | |
122 | #endif // Unicode/ANSI | |
f93d01be | 123 | |
b9fdb397 RD |
124 | // ---------------------------------------------------------------------------- |
125 | // A class for holding growable data buffers (not necessarily strings) | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | class wxMemoryBuffer | |
129 | { | |
130 | public: | |
131 | enum { BLOCK_SIZE = 1024 }; | |
c12ef8a2 VZ |
132 | wxMemoryBuffer(size_t size = wxMemoryBuffer::BLOCK_SIZE) |
133 | : m_data(malloc(size)), m_size(size), m_len(0) | |
b9fdb397 | 134 | { |
b9fdb397 RD |
135 | } |
136 | ||
137 | ~wxMemoryBuffer() { free(m_data); } | |
138 | ||
139 | // Accessors | |
ef1cae87 RD |
140 | void* GetData() const { return m_data; } |
141 | size_t GetBufSize() const { return m_size; } | |
142 | size_t GetDataLen() const { return m_len; } | |
b9fdb397 RD |
143 | |
144 | void SetBufSize(size_t size) { ResizeIfNeeded(size); } | |
145 | void SetDataLen(size_t len) | |
146 | { | |
147 | wxASSERT(len <= m_size); | |
148 | m_len = len; | |
149 | } | |
150 | ||
151 | // Ensure the buffer is big enough and return a pointer to it | |
152 | void* GetWriteBuf(size_t sizeNeeded) | |
153 | { | |
154 | ResizeIfNeeded(sizeNeeded); | |
155 | return m_data; | |
156 | } | |
157 | // Update the length after the write | |
158 | void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); } | |
159 | ||
160 | // Like the above, but appends to the buffer | |
161 | void* GetAppendBuf(size_t sizeNeeded) | |
162 | { | |
163 | ResizeIfNeeded(m_len + sizeNeeded); | |
164 | return (char*)m_data + m_len; | |
165 | } | |
166 | void UngetAppendBuf(size_t sizeUsed) { SetDataLen(m_len + sizeUsed); } | |
167 | ||
168 | // Other ways to append to the buffer | |
169 | void AppendByte(char data) { | |
170 | ResizeIfNeeded(m_len + 1); | |
ef1cae87 | 171 | *(((char*)m_data)+m_len) = data; |
b9fdb397 RD |
172 | m_len += 1; |
173 | } | |
174 | void AppendData(void* data, size_t len) | |
175 | { | |
176 | memcpy(GetAppendBuf(len), data, len); | |
177 | UngetAppendBuf(len); | |
178 | } | |
179 | ||
180 | operator const char *() const { return (const char*)m_data; } | |
181 | ||
ef1cae87 RD |
182 | |
183 | // Copy and assignment | |
184 | wxMemoryBuffer(const wxMemoryBuffer& src) | |
7ee31b00 | 185 | : m_data(src.m_data), m_size(src.m_size), m_len(src.m_len) |
ef1cae87 | 186 | { |
ef1cae87 RD |
187 | // no reference count yet... |
188 | ((wxMemoryBuffer*)&src)->m_data = NULL; | |
189 | ((wxMemoryBuffer*)&src)->m_size = 0; | |
190 | ((wxMemoryBuffer*)&src)->m_len = 0; | |
191 | } | |
192 | ||
193 | wxMemoryBuffer& operator=(const wxMemoryBuffer& src) | |
194 | { | |
195 | m_data = src.m_data; | |
196 | m_size = src.m_size; | |
197 | m_len = src.m_len; | |
c12ef8a2 | 198 | |
ef1cae87 RD |
199 | // no reference count yet... |
200 | ((wxMemoryBuffer*)&src)->m_data = NULL; | |
201 | ((wxMemoryBuffer*)&src)->m_size = 0; | |
202 | ((wxMemoryBuffer*)&src)->m_len = 0; | |
203 | ||
204 | return *this; | |
205 | } | |
206 | ||
207 | ||
b9fdb397 | 208 | protected: |
b9fdb397 RD |
209 | void ResizeIfNeeded(size_t newSize) |
210 | { | |
211 | if (newSize > m_size) | |
212 | { | |
213 | m_data = realloc(m_data, newSize + wxMemoryBuffer::BLOCK_SIZE); | |
214 | wxASSERT(m_data != NULL); | |
215 | m_size = newSize + wxMemoryBuffer::BLOCK_SIZE; | |
216 | } | |
217 | } | |
218 | ||
219 | private: | |
220 | void* m_data; | |
221 | size_t m_size; | |
222 | size_t m_len; | |
223 | }; | |
224 | ||
14971e5b VZ |
225 | // ---------------------------------------------------------------------------- |
226 | // template class for any kind of data | |
227 | // ---------------------------------------------------------------------------- | |
228 | ||
229 | // TODO | |
230 | ||
231 | #endif // _WX_BUFFER_H |