X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0e0126c2204732b9dfeefef0de838d7d375cf1f0..ce4b7808ca940be201422a066de141ca12f13863:/include/wx/buffer.h?ds=sidebyside diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 17fd98fff2..f17aa0c1ab 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -6,17 +6,15 @@ // Created: 12.04.99 // RCS-ID: $Id$ // Copyright: (c) 1998 Vadim Zeitlin -// Licence: wxWindows license +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// -// these classes are for private use only for now, they're not documented - #ifndef _WX_BUFFER_H #define _WX_BUFFER_H #include "wx/wxchar.h" -#include // strdup +#include // malloc() and free() // ---------------------------------------------------------------------------- // Special classes for (wide) character strings: they use malloc/free instead @@ -24,10 +22,10 @@ // ---------------------------------------------------------------------------- #define DEFINE_BUFFER(classname, chartype, strdupfunc) \ -class classname \ +class WXDLLIMPEXP_BASE classname \ { \ public: \ - classname(const chartype *str) \ + classname(const chartype *str = NULL) \ : m_str(str ? strdupfunc(str) : NULL) \ { \ } \ @@ -66,6 +64,12 @@ public: \ return p; \ } \ \ + void reset() \ + { \ + free(m_str); \ + m_str = NULL; \ + } \ + \ classname(const classname& src) \ : m_str(src.release()) \ { \ @@ -86,6 +90,18 @@ public: \ return *this; \ } \ \ + bool extend(size_t len) \ + { \ + chartype * \ + str = (chartype *)realloc(m_str, (len + 1)*sizeof(chartype)); \ + if ( !str ) \ + return false; \ + \ + m_str = str; \ + \ + return true; \ + } \ + \ chartype *data() { return m_str; } \ const chartype *data() const { return m_str; } \ operator const chartype *() const { return m_str; } \ @@ -95,43 +111,26 @@ private: \ chartype *m_str; \ } -#ifndef strdup -inline char *strdup(const char *cs) -{ - size_t len = 0; - while (cs[len] != 0) - len++; - const size_t siz = (len + 1)*sizeof(char); - char *csCopy = (char *)malloc(siz); - memcpy(csCopy, cs, siz); - return csCopy; -} -#endif - -DEFINE_BUFFER(wxCharBuffer, char, strdup); +DEFINE_BUFFER(wxCharBuffer, char, wxStrdupA); #if wxUSE_WCHAR_T -inline wchar_t *wxWcsdupReplacement(const wchar_t *wcs) -{ - const size_t siz = (wxWcslen(wcs) + 1)*sizeof(wchar_t); - wchar_t *wcsCopy = (wchar_t *)malloc(siz); - memcpy(wcsCopy, wcs, siz); - return wcsCopy; -} - -DEFINE_BUFFER(wxWCharBuffer, wchar_t, wxWcsdupReplacement); +DEFINE_BUFFER(wxWCharBuffer, wchar_t, wxStrdupW); #endif // wxUSE_WCHAR_T #undef DEFINE_BUFFER #if wxUSE_UNICODE + typedef wxWCharBuffer wxWxCharBuffer; + #define wxMB2WXbuf wxWCharBuffer #define wxWX2MBbuf wxCharBuffer #define wxWC2WXbuf wxChar* #define wxWX2WCbuf wxChar* #else // ANSI + typedef wxCharBuffer wxWxCharBuffer; + #define wxMB2WXbuf wxChar* #define wxWX2MBbuf wxChar* #define wxWC2WXbuf wxCharBuffer @@ -147,13 +146,13 @@ class wxMemoryBufferData { public: // the initial size and also the size added by ResizeIfNeeded() - enum { BLOCK_SIZE = 1024 }; + enum { DefBufSize = 1024 }; friend class wxMemoryBuffer; // everyting is private as it can only be used by wxMemoryBuffer private: - wxMemoryBufferData(size_t size = wxMemoryBufferData::BLOCK_SIZE) + wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize) : m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0) { } @@ -165,13 +164,13 @@ private: if (newSize > m_size) { void *dataOld = m_data; - m_data = realloc(m_data, newSize + wxMemoryBufferData::BLOCK_SIZE); + m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize); if ( !m_data ) { free(dataOld); } - m_size = newSize + wxMemoryBufferData::BLOCK_SIZE; + m_size = newSize + wxMemoryBufferData::DefBufSize; } } @@ -195,6 +194,8 @@ private: // the reference count size_t m_ref; + + DECLARE_NO_COPY_CLASS(wxMemoryBufferData) }; @@ -202,7 +203,7 @@ class wxMemoryBuffer { public: // ctor and dtor - wxMemoryBuffer(size_t size = wxMemoryBufferData::BLOCK_SIZE) + wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize) { m_bufdata = new wxMemoryBufferData(size); m_bufdata->IncRef(); @@ -272,7 +273,7 @@ public: m_bufdata->m_len += 1; } - void AppendData(void* data, size_t len) + void AppendData(const void *data, size_t len) { memcpy(GetAppendBuf(len), data, len); UngetAppendBuf(len);