1999-04-12 09:57:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2002-08-20 15:07:24 +00:00
|
|
|
// Name: wx/buffer.h
|
1999-04-12 09:57:22 +00:00
|
|
|
// Purpose: auto buffer classes: buffers which automatically free memory
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 12.04.99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
2004-05-23 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1999-04-12 09:57:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_BUFFER_H
|
|
|
|
#define _WX_BUFFER_H
|
|
|
|
|
2007-04-19 07:44:16 +00:00
|
|
|
#include "wx/chartype.h"
|
|
|
|
#include "wx/wxcrt.h"
|
1999-10-04 20:15:38 +00:00
|
|
|
|
2004-02-15 02:57:43 +00:00
|
|
|
#include <stdlib.h> // malloc() and free()
|
|
|
|
|
2007-04-19 11:25:34 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxCStrData;
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
inline char *wxStrDup(const char *s) { return wxStrdupA(s); }
|
|
|
|
#if wxUSE_WCHAR_T
|
|
|
|
inline wchar_t *wxStrDup(const wchar_t *ws) { return wxStrdupW(ws); }
|
|
|
|
#endif
|
|
|
|
|
1999-04-12 09:57:22 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Special classes for (wide) character strings: they use malloc/free instead
|
|
|
|
// of new/delete
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
template <typename T>
|
2007-03-23 00:04:15 +00:00
|
|
|
class wxCharTypeBuffer
|
2007-03-22 23:31:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T CharType;
|
|
|
|
|
|
|
|
wxCharTypeBuffer(const CharType *str = NULL)
|
2007-04-14 16:58:48 +00:00
|
|
|
: m_str(str ? wxStrDup(str) : NULL),
|
|
|
|
m_owned(true)
|
2007-03-22 23:31:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCharTypeBuffer(size_t len)
|
2007-04-14 16:58:48 +00:00
|
|
|
: m_str((CharType *)malloc((len + 1)*sizeof(CharType))),
|
|
|
|
m_owned(true)
|
2007-03-22 23:31:08 +00:00
|
|
|
{
|
|
|
|
m_str[len] = (CharType)0;
|
|
|
|
}
|
|
|
|
|
2007-04-16 06:59:04 +00:00
|
|
|
static const wxCharTypeBuffer CreateNonOwned(const CharType *str)
|
2007-04-14 16:58:48 +00:00
|
|
|
{
|
|
|
|
wxCharTypeBuffer buf;
|
2007-04-16 06:59:04 +00:00
|
|
|
buf.m_str = wx_const_cast(CharType*, str);
|
2007-04-14 16:58:48 +00:00
|
|
|
buf.m_owned = false;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
/* no need to check for NULL, free() does it */
|
2007-04-14 16:58:48 +00:00
|
|
|
~wxCharTypeBuffer()
|
|
|
|
{
|
|
|
|
if ( m_owned)
|
|
|
|
free(m_str);
|
|
|
|
}
|
2007-03-22 23:31:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
WARNING:
|
|
|
|
|
|
|
|
the copy ctor and assignment operators change the passed in object
|
|
|
|
even although it is declared as "const", so:
|
|
|
|
|
|
|
|
a) it shouldn't be really const
|
|
|
|
b) you shouldn't use it afterwards (or know that it was reset)
|
|
|
|
|
2007-04-14 16:58:48 +00:00
|
|
|
This is very ugly but is unfortunately needed to make the normal use
|
2007-03-22 23:31:08 +00:00
|
|
|
of wxCharTypeBuffer buffer objects possible and is very similar to what
|
|
|
|
std::auto_ptr<> does (as if it were an excuse...)
|
|
|
|
*/
|
1999-04-12 09:57:22 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
/*
|
|
|
|
because of the remark above, release() is declared const even if it
|
|
|
|
isn't really const
|
|
|
|
*/
|
|
|
|
CharType *release() const
|
|
|
|
{
|
2007-04-14 16:58:48 +00:00
|
|
|
wxASSERT_MSG( m_owned, _T("can't release non-owned buffer") );
|
|
|
|
return DoRelease();
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
2002-08-20 15:07:24 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
void reset()
|
|
|
|
{
|
2007-04-14 16:58:48 +00:00
|
|
|
if ( m_owned )
|
|
|
|
free(m_str);
|
2007-03-22 23:31:08 +00:00
|
|
|
m_str = NULL;
|
|
|
|
}
|
2002-08-20 15:07:24 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
wxCharTypeBuffer(const wxCharTypeBuffer& src)
|
|
|
|
{
|
2007-04-14 16:58:48 +00:00
|
|
|
CopyFrom(src);
|
2007-03-22 23:31:08 +00:00
|
|
|
}
|
1999-04-12 09:57:22 +00:00
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
wxCharTypeBuffer& operator=(const CharType *str)
|
|
|
|
{
|
2007-04-14 16:58:48 +00:00
|
|
|
if ( m_owned )
|
|
|
|
free(m_str);
|
2007-03-22 23:31:08 +00:00
|
|
|
m_str = str ? wxStrDup(str) : NULL;
|
2007-04-14 16:58:48 +00:00
|
|
|
m_owned = true;
|
2007-03-22 23:31:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCharTypeBuffer& operator=(const wxCharTypeBuffer& src)
|
|
|
|
{
|
2007-04-14 16:58:48 +00:00
|
|
|
if ( m_owned )
|
|
|
|
free(m_str);
|
|
|
|
CopyFrom(src);
|
2007-03-22 23:31:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool extend(size_t len)
|
|
|
|
{
|
2007-04-14 16:58:48 +00:00
|
|
|
wxASSERT_MSG( m_owned, _T("cannot extend non-owned buffer") );
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
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; }
|
|
|
|
CharType operator[](size_t n) const { return m_str[n]; }
|
|
|
|
|
2007-04-14 16:58:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
CharType *DoRelease() const
|
|
|
|
{
|
|
|
|
CharType *p = m_str;
|
|
|
|
((wxCharTypeBuffer *)this)->m_str = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CopyFrom(const wxCharTypeBuffer& src)
|
|
|
|
{
|
|
|
|
m_owned = src.m_owned;
|
|
|
|
m_str = src.DoRelease();
|
|
|
|
}
|
|
|
|
|
2007-03-22 23:31:08 +00:00
|
|
|
private:
|
|
|
|
CharType *m_str;
|
2007-04-14 16:58:48 +00:00
|
|
|
bool m_owned;
|
2007-03-22 23:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WXDLLIMPEXP_BASE wxCharBuffer : public wxCharTypeBuffer<char>
|
|
|
|
{
|
|
|
|
public:
|
2007-03-23 00:04:15 +00:00
|
|
|
typedef wxCharTypeBuffer<char> wxCharTypeBufferBase;
|
|
|
|
|
2007-04-16 06:59:04 +00:00
|
|
|
wxCharBuffer(const wxCharTypeBufferBase& buf)
|
|
|
|
: wxCharTypeBufferBase(buf) {}
|
|
|
|
|
2007-03-23 00:04:15 +00:00
|
|
|
wxCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
|
|
|
|
wxCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
|
|
|
|
|
|
|
|
wxCharBuffer(const wxCStrData& cstr);
|
2007-03-22 23:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if wxUSE_WCHAR_T
|
|
|
|
class WXDLLIMPEXP_BASE wxWCharBuffer : public wxCharTypeBuffer<wchar_t>
|
|
|
|
{
|
|
|
|
public:
|
2007-03-23 00:04:15 +00:00
|
|
|
typedef wxCharTypeBuffer<wchar_t> wxCharTypeBufferBase;
|
|
|
|
|
2007-04-16 06:59:04 +00:00
|
|
|
wxWCharBuffer(const wxCharTypeBufferBase& buf)
|
|
|
|
: wxCharTypeBufferBase(buf) {}
|
|
|
|
|
2007-03-23 00:04:15 +00:00
|
|
|
wxWCharBuffer(const CharType *str = NULL) : wxCharTypeBufferBase(str) {}
|
|
|
|
wxWCharBuffer(size_t len) : wxCharTypeBufferBase(len) {}
|
|
|
|
|
|
|
|
wxWCharBuffer(const wxCStrData& cstr);
|
2007-03-22 23:31:08 +00:00
|
|
|
};
|
|
|
|
#endif // wxUSE_WCHAR_T
|
2002-12-04 14:11:26 +00:00
|
|
|
|
2007-03-30 20:09:02 +00:00
|
|
|
// wxCharTypeBuffer<T> implicitly convertible to T*
|
|
|
|
template <typename T>
|
2007-03-30 20:30:34 +00:00
|
|
|
class wxWritableCharTypeBuffer : public wxCharTypeBuffer<T>
|
2007-03-30 20:09:02 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename wxCharTypeBuffer<T>::CharType CharType;
|
|
|
|
|
|
|
|
wxWritableCharTypeBuffer(const wxCharTypeBuffer<T>& src)
|
|
|
|
: wxCharTypeBuffer<T>(src) {}
|
|
|
|
// FIXME-UTF8: this won't be needed after converting mb_str()/wc_str() to
|
|
|
|
// always return a buffer
|
|
|
|
wxWritableCharTypeBuffer(const CharType *str = NULL)
|
|
|
|
: wxCharTypeBuffer<T>(str) {}
|
|
|
|
|
|
|
|
operator CharType*() { return this->data(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef wxWritableCharTypeBuffer<char> wxWritableCharBuffer;
|
|
|
|
typedef wxWritableCharTypeBuffer<wchar_t> wxWritableWCharBuffer;
|
|
|
|
|
|
|
|
|
1999-04-12 20:10:56 +00:00
|
|
|
#if wxUSE_UNICODE
|
2007-03-23 00:04:15 +00:00
|
|
|
#define wxWxCharBuffer wxWCharBuffer
|
2006-04-10 01:35:21 +00:00
|
|
|
|
1999-10-04 20:15:38 +00:00
|
|
|
#define wxMB2WXbuf wxWCharBuffer
|
|
|
|
#define wxWX2MBbuf wxCharBuffer
|
2007-04-12 21:15:07 +00:00
|
|
|
#if wxUSE_UNICODE_WCHAR
|
|
|
|
#define wxWC2WXbuf wxChar*
|
|
|
|
#define wxWX2WCbuf wxChar*
|
|
|
|
#elif wxUSE_UNICODE_UTF8
|
|
|
|
#define wxWC2WXbuf wxWCharBuffer
|
|
|
|
#define wxWX2WCbuf wxWCharBuffer
|
|
|
|
#endif
|
1999-10-04 20:15:38 +00:00
|
|
|
#else // ANSI
|
2007-03-23 00:04:15 +00:00
|
|
|
#define wxWxCharBuffer wxCharBuffer
|
2006-04-10 01:35:21 +00:00
|
|
|
|
1999-10-04 20:15:38 +00:00
|
|
|
#define wxMB2WXbuf wxChar*
|
|
|
|
#define wxWX2MBbuf wxChar*
|
|
|
|
#define wxWC2WXbuf wxCharBuffer
|
|
|
|
#define wxWX2WCbuf wxWCharBuffer
|
|
|
|
#endif // Unicode/ANSI
|
1999-04-12 20:10:56 +00:00
|
|
|
|
2002-03-13 22:05:05 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// A class for holding growable data buffers (not necessarily strings)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// This class manages the actual data buffer pointer and is ref-counted.
|
|
|
|
class wxMemoryBufferData
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2002-12-04 14:11:26 +00:00
|
|
|
// the initial size and also the size added by ResizeIfNeeded()
|
2005-10-29 17:46:12 +00:00
|
|
|
enum { DefBufSize = 1024 };
|
2002-12-04 14:11:26 +00:00
|
|
|
|
|
|
|
friend class wxMemoryBuffer;
|
|
|
|
|
|
|
|
// everyting is private as it can only be used by wxMemoryBuffer
|
|
|
|
private:
|
2005-10-29 17:46:12 +00:00
|
|
|
wxMemoryBufferData(size_t size = wxMemoryBufferData::DefBufSize)
|
2002-12-04 14:11:26 +00:00
|
|
|
: m_data(size ? malloc(size) : NULL), m_size(size), m_len(0), m_ref(0)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
|
|
|
}
|
2002-12-04 14:11:26 +00:00
|
|
|
~wxMemoryBufferData() { free(m_data); }
|
2002-03-13 22:05:05 +00:00
|
|
|
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
void ResizeIfNeeded(size_t newSize)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
if (newSize > m_size)
|
|
|
|
{
|
|
|
|
void *dataOld = m_data;
|
2005-10-29 17:46:12 +00:00
|
|
|
m_data = realloc(m_data, newSize + wxMemoryBufferData::DefBufSize);
|
2002-12-04 14:11:26 +00:00
|
|
|
if ( !m_data )
|
|
|
|
{
|
|
|
|
free(dataOld);
|
|
|
|
}
|
|
|
|
|
2005-10-29 17:46:12 +00:00
|
|
|
m_size = newSize + wxMemoryBufferData::DefBufSize;
|
2002-12-04 14:11:26 +00:00
|
|
|
}
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
void IncRef() { m_ref += 1; }
|
|
|
|
void DecRef()
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_ref -= 1;
|
|
|
|
if (m_ref == 0) // are there no more references?
|
|
|
|
delete this;
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
|
|
|
|
// the buffer containing the data
|
|
|
|
void *m_data;
|
|
|
|
|
|
|
|
// the size of the buffer
|
|
|
|
size_t m_size;
|
|
|
|
|
|
|
|
// the amount of data currently in the buffer
|
|
|
|
size_t m_len;
|
|
|
|
|
|
|
|
// the reference count
|
|
|
|
size_t m_ref;
|
2003-01-02 23:38:11 +00:00
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(wxMemoryBufferData)
|
2002-12-04 14:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class wxMemoryBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor and dtor
|
2005-10-29 17:46:12 +00:00
|
|
|
wxMemoryBuffer(size_t size = wxMemoryBufferData::DefBufSize)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_bufdata = new wxMemoryBufferData(size);
|
|
|
|
m_bufdata->IncRef();
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
~wxMemoryBuffer() { m_bufdata->DecRef(); }
|
|
|
|
|
|
|
|
|
|
|
|
// copy and assignment
|
|
|
|
wxMemoryBuffer(const wxMemoryBuffer& src)
|
|
|
|
: m_bufdata(src.m_bufdata)
|
|
|
|
{
|
|
|
|
m_bufdata->IncRef();
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
2002-12-04 14:11:26 +00:00
|
|
|
|
|
|
|
wxMemoryBuffer& operator=(const wxMemoryBuffer& src)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_bufdata->DecRef();
|
|
|
|
m_bufdata = src.m_bufdata;
|
|
|
|
m_bufdata->IncRef();
|
|
|
|
return *this;
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Accessors
|
|
|
|
void *GetData() const { return m_bufdata->m_data; }
|
|
|
|
size_t GetBufSize() const { return m_bufdata->m_size; }
|
|
|
|
size_t GetDataLen() const { return m_bufdata->m_len; }
|
2002-03-20 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
void SetBufSize(size_t size) { m_bufdata->ResizeIfNeeded(size); }
|
|
|
|
void SetDataLen(size_t len)
|
2002-03-20 20:13:17 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
wxASSERT(len <= m_bufdata->m_size);
|
|
|
|
m_bufdata->m_len = len;
|
2002-03-20 20:13:17 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Ensure the buffer is big enough and return a pointer to it
|
|
|
|
void *GetWriteBuf(size_t sizeNeeded)
|
2002-03-20 20:13:17 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
m_bufdata->ResizeIfNeeded(sizeNeeded);
|
|
|
|
return m_bufdata->m_data;
|
|
|
|
}
|
2002-08-20 15:07:24 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Update the length after the write
|
|
|
|
void UngetWriteBuf(size_t sizeUsed) { SetDataLen(sizeUsed); }
|
2002-03-20 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Like the above, but appends to the buffer
|
|
|
|
void *GetAppendBuf(size_t sizeNeeded)
|
|
|
|
{
|
|
|
|
m_bufdata->ResizeIfNeeded(m_bufdata->m_len + sizeNeeded);
|
|
|
|
return (char*)m_bufdata->m_data + m_bufdata->m_len;
|
|
|
|
}
|
2002-03-20 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Update the length after the append
|
|
|
|
void UngetAppendBuf(size_t sizeUsed)
|
|
|
|
{
|
|
|
|
SetDataLen(m_bufdata->m_len + sizeUsed);
|
|
|
|
}
|
2002-03-20 20:13:17 +00:00
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
// Other ways to append to the buffer
|
|
|
|
void AppendByte(char data)
|
2002-03-13 22:05:05 +00:00
|
|
|
{
|
2002-12-04 14:11:26 +00:00
|
|
|
wxCHECK_RET( m_bufdata->m_data, _T("invalid wxMemoryBuffer") );
|
|
|
|
|
|
|
|
m_bufdata->ResizeIfNeeded(m_bufdata->m_len + 1);
|
|
|
|
*(((char*)m_bufdata->m_data) + m_bufdata->m_len) = data;
|
|
|
|
m_bufdata->m_len += 1;
|
|
|
|
}
|
|
|
|
|
2005-09-24 21:28:15 +00:00
|
|
|
void AppendData(const void *data, size_t len)
|
2002-12-04 14:11:26 +00:00
|
|
|
{
|
|
|
|
memcpy(GetAppendBuf(len), data, len);
|
|
|
|
UngetAppendBuf(len);
|
2002-03-13 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 14:11:26 +00:00
|
|
|
operator const char *() const { return (const char*)GetData(); }
|
|
|
|
|
2002-03-13 22:05:05 +00:00
|
|
|
private:
|
2002-12-04 14:11:26 +00:00
|
|
|
wxMemoryBufferData* m_bufdata;
|
2002-03-13 22:05:05 +00:00
|
|
|
};
|
|
|
|
|
1999-04-12 09:57:22 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// template class for any kind of data
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
#endif // _WX_BUFFER_H
|