X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e90c1d2a19361551eb07778280f22be3e759cf64..27e2d606626e9604141abe1ba8a536c507c314dc:/include/wx/buffer.h diff --git a/include/wx/buffer.h b/include/wx/buffer.h index feb1c3c33f..dac2adf1bd 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -28,7 +28,7 @@ class wxCharBuffer public: wxCharBuffer(const char *str) { - wxASSERT_MSG( str, T("NULL string in wxCharBuffer") ); + wxASSERT_MSG( str, wxT("NULL string in wxCharBuffer") ); m_str = str ? strdup(str) : (char *)NULL; } @@ -54,6 +54,7 @@ public: return *this; } + const char *data() const { return m_str; } operator const char *() const { return m_str; } char operator[](size_t n) const { return m_str[n]; } @@ -67,10 +68,15 @@ class wxWCharBuffer public: wxWCharBuffer(const wchar_t *wcs) { - wxASSERT_MSG( wcs, T("NULL string in wxWCharBuffer") ); + wxASSERT_MSG( wcs, wxT("NULL string in wxWCharBuffer") ); if (wcs) { - size_t siz = (wcslen(wcs)+1)*sizeof(wchar_t); +#if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \ + || ( defined(__MWERKS__) && defined(__WXMSW__) ) + size_t siz = (std::wcslen(wcs)+1)*sizeof(wchar_t); +#else + size_t siz = (::wcslen(wcs)+1)*sizeof(wchar_t); +#endif m_wcs = (wchar_t *)malloc(siz); memcpy(m_wcs, wcs, siz); } @@ -99,6 +105,7 @@ public: return *this; } + const wchar_t *data() const { return m_wcs; } operator const wchar_t *() const { return m_wcs; } wchar_t operator[](size_t n) const { return m_wcs[n]; } @@ -119,6 +126,119 @@ private: #define wxWX2WCbuf wxWCharBuffer #endif // Unicode/ANSI + + +// ---------------------------------------------------------------------------- +// A class for holding growable data buffers (not necessarily strings) +// ---------------------------------------------------------------------------- + +class wxMemoryBuffer +{ +public: + enum { BLOCK_SIZE = 1024 }; + wxMemoryBuffer(size_t size=wxMemoryBuffer::BLOCK_SIZE) + { + wxASSERT(size > 0); + m_data = malloc(size); + wxASSERT(m_data != NULL); + m_size = size; + m_len = 0; + } + + ~wxMemoryBuffer() { free(m_data); } + + // Accessors + void* GetData() const { return m_data; } + size_t GetBufSize() const { return m_size; } + size_t GetDataLen() const { return m_len; } + + void SetBufSize(size_t size) { ResizeIfNeeded(size); } + void SetDataLen(size_t len) + { + wxASSERT(len <= m_size); + m_len = len; + } + + // Ensure the buffer is big enough and return a pointer to it + void* GetWriteBuf(size_t sizeNeeded) + { + ResizeIfNeeded(sizeNeeded); + return m_data; + } + // Update the length after the write + void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); } + + // Like the above, but appends to the buffer + void* GetAppendBuf(size_t sizeNeeded) + { + ResizeIfNeeded(m_len + sizeNeeded); + return (char*)m_data + m_len; + } + void UngetAppendBuf(size_t sizeUsed) { SetDataLen(m_len + sizeUsed); } + + // Other ways to append to the buffer + void AppendByte(char data) { + ResizeIfNeeded(m_len + 1); + *(((char*)m_data)+m_len) = data; + m_len += 1; + } + void AppendData(void* data, size_t len) + { + memcpy(GetAppendBuf(len), data, len); + UngetAppendBuf(len); + } + + operator const char *() const { return (const char*)m_data; } + + + // Copy and assignment + wxMemoryBuffer(const wxMemoryBuffer& src) + { + m_data = src.m_data; + m_size = src.m_size; + m_len = src.m_len; + + // no reference count yet... + ((wxMemoryBuffer*)&src)->m_data = NULL; + ((wxMemoryBuffer*)&src)->m_size = 0; + ((wxMemoryBuffer*)&src)->m_len = 0; + } + + wxMemoryBuffer& operator=(const wxMemoryBuffer& src) + { + m_data = src.m_data; + m_size = src.m_size; + m_len = src.m_len; + + // no reference count yet... + ((wxMemoryBuffer*)&src)->m_data = NULL; + ((wxMemoryBuffer*)&src)->m_size = 0; + ((wxMemoryBuffer*)&src)->m_len = 0; + + return *this; + } + + +protected: + + void ResizeIfNeeded(size_t newSize) + { + if (newSize > m_size) + { + m_data = realloc(m_data, newSize + wxMemoryBuffer::BLOCK_SIZE); + wxASSERT(m_data != NULL); + m_size = newSize + wxMemoryBuffer::BLOCK_SIZE; + } + } + +private: + void* m_data; + size_t m_size; + size_t m_len; +}; + + + // ---------------------------------------------------------------------------- // template class for any kind of data // ----------------------------------------------------------------------------