]>
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
)
34 wxCharBuffer(size_t len
)
35 : m_str((char *)malloc((len
+ 1)*sizeof(char)))
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
]; }
70 wxWCharBuffer(const wchar_t *wcs
)
71 : m_wcs((wchar_t *)NULL
)
74 size_t siz
= (wxWcslen(wcs
) + 1)*sizeof(wchar_t);
75 m_wcs
= (wchar_t *)malloc(siz
);
76 memcpy(m_wcs
, wcs
, siz
);
79 wxWCharBuffer(size_t len
)
80 : m_wcs((wchar_t *)malloc((len
+ 1)*sizeof(wchar_t)))
85 // no need to check for NULL, free() does it
86 ~wxWCharBuffer() { free(m_wcs
); }
88 wxWCharBuffer(const wxWCharBuffer
& src
)
91 // no reference count yet...
92 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
94 wxWCharBuffer
& operator=(const wxWCharBuffer
& src
)
97 // no reference count yet...
98 ((wxWCharBuffer
*)&src
)->m_wcs
= (wchar_t *)NULL
;
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
]; }
110 #endif // wxUSE_WCHAR_T
113 #define wxMB2WXbuf wxWCharBuffer
114 #define wxWX2MBbuf wxCharBuffer
115 #define wxWC2WXbuf wxChar*
116 #define wxWX2WCbuf wxChar*
118 #define wxMB2WXbuf wxChar*
119 #define wxWX2MBbuf wxChar*
120 #define wxWC2WXbuf wxCharBuffer
121 #define wxWX2WCbuf wxWCharBuffer
122 #endif // Unicode/ANSI
124 // ----------------------------------------------------------------------------
125 // A class for holding growable data buffers (not necessarily strings)
126 // ----------------------------------------------------------------------------
131 enum { BLOCK_SIZE
= 1024 };
132 wxMemoryBuffer(size_t size
= wxMemoryBuffer::BLOCK_SIZE
)
133 : m_data(malloc(size
)), m_size(size
), m_len(0)
137 ~wxMemoryBuffer() { free(m_data
); }
140 void* GetData() const { return m_data
; }
141 size_t GetBufSize() const { return m_size
; }
142 size_t GetDataLen() const { return m_len
; }
144 void SetBufSize(size_t size
) { ResizeIfNeeded(size
); }
145 void SetDataLen(size_t len
)
147 wxASSERT(len
<= m_size
);
151 // Ensure the buffer is big enough and return a pointer to it
152 void* GetWriteBuf(size_t sizeNeeded
)
154 ResizeIfNeeded(sizeNeeded
);
157 // Update the length after the write
158 void UngetWriteBuf(size_t sizeUsed
) { SetDataLen(sizeUsed
); }
160 // Like the above, but appends to the buffer
161 void* GetAppendBuf(size_t sizeNeeded
)
163 ResizeIfNeeded(m_len
+ sizeNeeded
);
164 return (char*)m_data
+ m_len
;
166 void UngetAppendBuf(size_t sizeUsed
) { SetDataLen(m_len
+ sizeUsed
); }
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
;
174 void AppendData(void* data
, size_t len
)
176 memcpy(GetAppendBuf(len
), data
, len
);
180 operator const char *() const { return (const char*)m_data
; }
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
)
187 // no reference count yet...
188 ((wxMemoryBuffer
*)&src
)->m_data
= NULL
;
189 ((wxMemoryBuffer
*)&src
)->m_size
= 0;
190 ((wxMemoryBuffer
*)&src
)->m_len
= 0;
193 wxMemoryBuffer
& operator=(const wxMemoryBuffer
& src
)
199 // no reference count yet...
200 ((wxMemoryBuffer
*)&src
)->m_data
= NULL
;
201 ((wxMemoryBuffer
*)&src
)->m_size
= 0;
202 ((wxMemoryBuffer
*)&src
)->m_len
= 0;
209 void ResizeIfNeeded(size_t newSize
)
211 if (newSize
> m_size
)
213 m_data
= realloc(m_data
, newSize
+ wxMemoryBuffer::BLOCK_SIZE
);
214 wxASSERT(m_data
!= NULL
);
215 m_size
= newSize
+ wxMemoryBuffer::BLOCK_SIZE
;
225 // ----------------------------------------------------------------------------
226 // template class for any kind of data
227 // ----------------------------------------------------------------------------
231 #endif // _WX_BUFFER_H