]>
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
)
30 : m_str(str
? strdup(str
) : NULL
)
32 wxASSERT_MSG( str
, wxT("NULL string in wxCharBuffer") );
35 wxCharBuffer(size_t len
)
36 : m_str((char *)malloc((len
+ 1)*sizeof(char)))
41 // no need to check for NULL, free() does it
42 ~wxCharBuffer() { free(m_str
); }
44 wxCharBuffer(const wxCharBuffer
& src
)
47 // no reference count yet...
48 ((wxCharBuffer
*)&src
)->m_str
= (char *)NULL
;
50 wxCharBuffer
& operator=(const wxCharBuffer
& src
)
53 // no reference count yet...
54 ((wxCharBuffer
*)&src
)->m_str
= (char *)NULL
;
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
]; }
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 *)malloc((len
+ 1)*sizeof(wchar_t)))
93 // no need to check for NULL, free() does it
94 ~wxWCharBuffer() { free(m_wcs
); }
96 wxWCharBuffer(const wxWCharBuffer
& src
)
99 // no reference count yet...
100 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
102 wxWCharBuffer
& operator=(const wxWCharBuffer
& src
)
105 // no reference count yet...
106 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
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
]; }
118 #endif // wxUSE_WCHAR_T
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
132 // ----------------------------------------------------------------------------
133 // A class for holding growable data buffers (not necessarily strings)
134 // ----------------------------------------------------------------------------
139 enum { BLOCK_SIZE
= 1024 };
140 wxMemoryBuffer(size_t size
= wxMemoryBuffer::BLOCK_SIZE
)
141 : m_data(malloc(size
)), m_size(size
), m_len(0)
145 ~wxMemoryBuffer() { free(m_data
); }
148 void* GetData() const { return m_data
; }
149 size_t GetBufSize() const { return m_size
; }
150 size_t GetDataLen() const { return m_len
; }
152 void SetBufSize(size_t size
) { ResizeIfNeeded(size
); }
153 void SetDataLen(size_t len
)
155 wxASSERT(len
<= m_size
);
159 // Ensure the buffer is big enough and return a pointer to it
160 void* GetWriteBuf(size_t sizeNeeded
)
162 ResizeIfNeeded(sizeNeeded
);
165 // Update the length after the write
166 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
168 // Like the above, but appends to the buffer
169 void* GetAppendBuf(size_t sizeNeeded
)
171 ResizeIfNeeded(m_len
+ sizeNeeded
);
172 return (char*)m_data
+ m_len
;
174 void UngetAppendBuf(size_t sizeUsed
) { SetDataLen(m_len
+ sizeUsed
); }
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
;
182 void AppendData(void* data
, size_t len
)
184 memcpy(GetAppendBuf(len
), data
, len
);
188 operator const char *() const { return (const char*)m_data
; }
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
)
195 // no reference count yet...
196 ((wxMemoryBuffer
*)&src
)->m_data
= NULL
;
197 ((wxMemoryBuffer
*)&src
)->m_size
= 0;
198 ((wxMemoryBuffer
*)&src
)->m_len
= 0;
201 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
207 // no reference count yet...
208 ((wxMemoryBuffer
*)&src
)->m_data
= NULL
;
209 ((wxMemoryBuffer
*)&src
)->m_size
= 0;
210 ((wxMemoryBuffer
*)&src
)->m_len
= 0;
217 void ResizeIfNeeded(size_t newSize
)
219 if (newSize
> m_size
)
221 m_data
= realloc(m_data
, newSize
+ wxMemoryBuffer::BLOCK_SIZE
);
222 wxASSERT(m_data
!= NULL
);
223 m_size
= newSize
+ wxMemoryBuffer::BLOCK_SIZE
;
233 // ----------------------------------------------------------------------------
234 // template class for any kind of data
235 // ----------------------------------------------------------------------------
239 #endif // _WX_BUFFER_H