]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/buffer.h
declare wxEntry() as taking char **Argv, not char *argv[]
[wxWidgets.git] / include / wx / buffer.h
... / ...
CommitLineData
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
26class wxCharBuffer
27{
28public:
29 wxCharBuffer(const char *str)
30 : m_str(str ? strdup(str) : NULL)
31 {
32 }
33
34 wxCharBuffer(size_t len)
35 : m_str((char *)malloc((len + 1)*sizeof(char)))
36 {
37 m_str[len] = '\0';
38 }
39
40 // no need to check for NULL, free() does it
41 ~wxCharBuffer() { free(m_str); }
42
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 }
56
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]; }
60
61private:
62 char *m_str;
63};
64
65#if wxUSE_WCHAR_T
66
67class wxWCharBuffer
68{
69public:
70 wxWCharBuffer(const wchar_t *wcs)
71 : m_wcs((wchar_t *)NULL)
72 {
73 if (wcs) {
74 size_t siz = (wxWcslen(wcs) + 1)*sizeof(wchar_t);
75 m_wcs = (wchar_t *)malloc(siz);
76 memcpy(m_wcs, wcs, siz);
77 }
78 }
79 wxWCharBuffer(size_t len)
80 : m_wcs((wchar_t *)malloc((len + 1)*sizeof(wchar_t)))
81 {
82 m_wcs[len] = L'\0';
83 }
84
85 // no need to check for NULL, free() does it
86 ~wxWCharBuffer() { free(m_wcs); }
87
88 wxWCharBuffer(const wxWCharBuffer& src)
89 : m_wcs(src.m_wcs)
90 {
91 // no reference count yet...
92 ((wxWCharBuffer*)&src)->m_wcs = (wchar_t *)NULL;
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 }
101
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]; }
105
106private:
107 wchar_t *m_wcs;
108};
109
110#endif // wxUSE_WCHAR_T
111
112#if wxUSE_UNICODE
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
123
124// ----------------------------------------------------------------------------
125// A class for holding growable data buffers (not necessarily strings)
126// ----------------------------------------------------------------------------
127
128class wxMemoryBuffer
129{
130public:
131 enum { BLOCK_SIZE = 1024 };
132 wxMemoryBuffer(size_t size = wxMemoryBuffer::BLOCK_SIZE)
133 : m_data(malloc(size)), m_size(size), m_len(0)
134 {
135 }
136
137 ~wxMemoryBuffer() { free(m_data); }
138
139 // Accessors
140 void* GetData() const { return m_data; }
141 size_t GetBufSize() const { return m_size; }
142 size_t GetDataLen() const { return m_len; }
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);
171 *(((char*)m_data)+m_len) = data;
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
182
183 // Copy and assignment
184 wxMemoryBuffer(const wxMemoryBuffer& src)
185 : m_data(src.m_data), m_size(src.m_size), m_len(src.m_len)
186 {
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;
198
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
208protected:
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
219private:
220 void* m_data;
221 size_t m_size;
222 size_t m_len;
223};
224
225// ----------------------------------------------------------------------------
226// template class for any kind of data
227// ----------------------------------------------------------------------------
228
229// TODO
230
231#endif // _WX_BUFFER_H