]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/buffer.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: auto buffer classes: buffers which automatically free memory
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // these classes are for private use only for now, they're not documented
17 #include "wx/wxchar.h"
19 #include <string.h> // strdup
21 // ----------------------------------------------------------------------------
22 // Special classes for (wide) character strings: they use malloc/free instead
24 // ----------------------------------------------------------------------------
29 wxCharBuffer(const char *str
)
32 wxASSERT_MSG( str
, wxT("NULL string in wxCharBuffer") );
34 m_str
= str
? strdup(str
) : (char *)NULL
;
36 wxCharBuffer(size_t len
)
39 m_str
= (char *)malloc(len
+1);
42 // no need to check for NULL, free() does it
43 ~wxCharBuffer() { free(m_str
); }
45 wxCharBuffer(const wxCharBuffer
& src
)
48 // no reference count yet...
49 ((wxCharBuffer
*)&src
)->m_str
= (char *)NULL
;
51 wxCharBuffer
& operator=(const wxCharBuffer
& src
)
54 // no reference count yet...
55 ((wxCharBuffer
*)&src
)->m_str
= (char *)NULL
;
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
]; }
71 wxWCharBuffer(const wchar_t *wcs
)
72 : m_wcs((wchar_t *)NULL
)
74 wxASSERT_MSG( wcs
, wxT("NULL string in wxWCharBuffer") );
77 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
78 || ( defined(__MWERKS__) && defined(__WXMSW__) )
79 size_t siz
= (std::wcslen(wcs
)+1)*sizeof(wchar_t);
81 size_t siz
= (::wcslen(wcs
)+1)*sizeof(wchar_t);
83 m_wcs
= (wchar_t *)malloc(siz
);
84 memcpy(m_wcs
, wcs
, siz
);
87 wxWCharBuffer(size_t len
)
88 : m_wcs((wchar_t *)NULL
)
90 m_wcs
= (wchar_t *)malloc((len
+1)*sizeof(wchar_t));
94 // no need to check for NULL, free() does it
95 ~wxWCharBuffer() { free(m_wcs
); }
97 wxWCharBuffer(const wxWCharBuffer
& src
)
100 // no reference count yet...
101 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
103 wxWCharBuffer
& operator=(const wxWCharBuffer
& src
)
106 // no reference count yet...
107 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
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
]; }
121 #define wxMB2WXbuf wxWCharBuffer
122 #define wxWX2MBbuf wxCharBuffer
123 #define wxWC2WXbuf wxChar*
124 #define wxWX2WCbuf wxChar*
126 #define wxMB2WXbuf wxChar*
127 #define wxWX2MBbuf wxChar*
128 #define wxWC2WXbuf wxCharBuffer
129 #define wxWX2WCbuf wxWCharBuffer
130 #endif // Unicode/ANSI
134 // ----------------------------------------------------------------------------
135 // A class for holding growable data buffers (not necessarily strings)
136 // ----------------------------------------------------------------------------
141 enum { BLOCK_SIZE
= 1024 };
142 wxMemoryBuffer(size_t size
=wxMemoryBuffer::BLOCK_SIZE
)
143 : m_data(NULL
), m_size(0), m_len(0)
146 m_data
= malloc(size
);
147 wxASSERT(m_data
!= NULL
);
151 ~wxMemoryBuffer() { free(m_data
); }
154 void* GetData() const { return m_data
; }
155 size_t GetBufSize() const { return m_size
; }
156 size_t GetDataLen() const { return m_len
; }
158 void SetBufSize(size_t size
) { ResizeIfNeeded(size
); }
159 void SetDataLen(size_t len
)
161 wxASSERT(len
<= m_size
);
165 // Ensure the buffer is big enough and return a pointer to it
166 void* GetWriteBuf(size_t sizeNeeded
)
168 ResizeIfNeeded(sizeNeeded
);
171 // Update the length after the write
172 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
174 // Like the above, but appends to the buffer
175 void* GetAppendBuf(size_t sizeNeeded
)
177 ResizeIfNeeded(m_len
+ sizeNeeded
);
178 return (char*)m_data
+ m_len
;
180 void UngetAppendBuf(size_t sizeUsed
) { SetDataLen(m_len
+ sizeUsed
); }
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
;
188 void AppendData(void* data
, size_t len
)
190 memcpy(GetAppendBuf(len
), data
, len
);
194 operator const char *() const { return (const char*)m_data
; }
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
)
201 // no reference count yet...
202 ((wxMemoryBuffer
*)&src
)->m_data
= NULL
;
203 ((wxMemoryBuffer
*)&src
)->m_size
= 0;
204 ((wxMemoryBuffer
*)&src
)->m_len
= 0;
207 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
213 // no reference count yet...
214 ((wxMemoryBuffer
*)&src
)->m_data
= NULL
;
215 ((wxMemoryBuffer
*)&src
)->m_size
= 0;
216 ((wxMemoryBuffer
*)&src
)->m_len
= 0;
224 void ResizeIfNeeded(size_t newSize
)
226 if (newSize
> m_size
)
228 m_data
= realloc(m_data
, newSize
+ wxMemoryBuffer::BLOCK_SIZE
);
229 wxASSERT(m_data
!= NULL
);
230 m_size
= newSize
+ wxMemoryBuffer::BLOCK_SIZE
;
242 // ----------------------------------------------------------------------------
243 // template class for any kind of data
244 // ----------------------------------------------------------------------------
248 #endif // _WX_BUFFER_H