]>
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
)
31 wxASSERT_MSG( str
, wxT("NULL string in wxCharBuffer") );
33 m_str
= str
? strdup(str
) : (char *)NULL
;
35 wxCharBuffer(size_t len
)
37 m_str
= (char *)malloc(len
+1);
40 // no need to check for NULL, free() does it
41 ~wxCharBuffer() { free(m_str
); }
43 wxCharBuffer(const wxCharBuffer
& src
)
46 // no reference count yet...
47 ((wxCharBuffer
*)&src
)->m_str
= (char *)NULL
;
49 wxCharBuffer
& operator=(const wxCharBuffer
& src
)
52 // no reference count yet...
53 ((wxCharBuffer
*)&src
)->m_str
= (char *)NULL
;
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
]; }
69 wxWCharBuffer(const wchar_t *wcs
)
71 wxASSERT_MSG( wcs
, wxT("NULL string in wxWCharBuffer") );
74 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
75 || ( defined(__MWERKS__) && defined(__WXMSW__) )
76 size_t siz
= (std::wcslen(wcs
)+1)*sizeof(wchar_t);
78 size_t siz
= (::wcslen(wcs
)+1)*sizeof(wchar_t);
80 m_wcs
= (wchar_t *)malloc(siz
);
81 memcpy(m_wcs
, wcs
, siz
);
83 else m_wcs
= (wchar_t *)NULL
;
85 wxWCharBuffer(size_t len
)
87 m_wcs
= (wchar_t *)malloc((len
+1)*sizeof(wchar_t));
91 // no need to check for NULL, free() does it
92 ~wxWCharBuffer() { free(m_wcs
); }
94 wxWCharBuffer(const wxWCharBuffer
& src
)
97 // no reference count yet...
98 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
100 wxWCharBuffer
& operator=(const wxWCharBuffer
& src
)
103 // no reference count yet...
104 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
108 const wchar_t *data() const { return m_wcs
; }
109 operator const wchar_t *() const { return m_wcs
; }
110 wchar_t operator[](size_t n
) const { return m_wcs
[n
]; }
118 #define wxMB2WXbuf wxWCharBuffer
119 #define wxWX2MBbuf wxCharBuffer
120 #define wxWC2WXbuf wxChar*
121 #define wxWX2WCbuf wxChar*
123 #define wxMB2WXbuf wxChar*
124 #define wxWX2MBbuf wxChar*
125 #define wxWC2WXbuf wxCharBuffer
126 #define wxWX2WCbuf wxWCharBuffer
127 #endif // Unicode/ANSI
131 // ----------------------------------------------------------------------------
132 // A class for holding growable data buffers (not necessarily strings)
133 // ----------------------------------------------------------------------------
138 enum { BLOCK_SIZE
= 1024 };
139 wxMemoryBuffer(size_t size
=wxMemoryBuffer::BLOCK_SIZE
)
142 m_data
= malloc(size
);
143 wxASSERT(m_data
!= NULL
);
148 ~wxMemoryBuffer() { free(m_data
); }
151 void* GetData() { return m_data
; }
152 size_t GetBufSize() { return m_size
; }
153 size_t GetDataLen() { return m_len
; }
155 void SetBufSize(size_t size
) { ResizeIfNeeded(size
); }
156 void SetDataLen(size_t len
)
158 wxASSERT(len
<= m_size
);
162 // Ensure the buffer is big enough and return a pointer to it
163 void* GetWriteBuf(size_t sizeNeeded
)
165 ResizeIfNeeded(sizeNeeded
);
168 // Update the length after the write
169 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
171 // Like the above, but appends to the buffer
172 void* GetAppendBuf(size_t sizeNeeded
)
174 ResizeIfNeeded(m_len
+ sizeNeeded
);
175 return (char*)m_data
+ m_len
;
177 void UngetAppendBuf(size_t sizeUsed
) { SetDataLen(m_len
+ sizeUsed
); }
179 // Other ways to append to the buffer
180 void AppendByte(char data
) {
181 ResizeIfNeeded(m_len
+ 1);
182 *((char*)m_data
) = data
;
185 void AppendData(void* data
, size_t len
)
187 memcpy(GetAppendBuf(len
), data
, len
);
191 operator const char *() const { return (const char*)m_data
; }
195 void ResizeIfNeeded(size_t newSize
)
197 if (newSize
> m_size
)
199 m_data
= realloc(m_data
, newSize
+ wxMemoryBuffer::BLOCK_SIZE
);
200 wxASSERT(m_data
!= NULL
);
201 m_size
= newSize
+ wxMemoryBuffer::BLOCK_SIZE
;
205 DECLARE_NO_COPY_CLASS(wxMemoryBuffer
)
215 // ----------------------------------------------------------------------------
216 // template class for any kind of data
217 // ----------------------------------------------------------------------------
221 #endif // _WX_BUFFER_H