wxOutputStream wrapers and typemaps
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@46278 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
#define __PYISTREAM__
|
#define __PYISTREAM__
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
// Handling of wxInputStreams by Joerg Baumann
|
// Handling of wxInputStreams by Joerg Baumann
|
||||||
// See stream.i for implementations
|
// See stream.i for implementations
|
||||||
|
|
||||||
@@ -34,19 +34,22 @@ public:
|
|||||||
void close();
|
void close();
|
||||||
void flush();
|
void flush();
|
||||||
bool eof();
|
bool eof();
|
||||||
PyObject* read(int size=-1);
|
|
||||||
PyObject* readline(int size=-1);
|
|
||||||
PyObject* readlines(int sizehint=-1);
|
|
||||||
void seek(int offset, int whence=0);
|
void seek(int offset, int whence=0);
|
||||||
int tell();
|
int tell();
|
||||||
|
|
||||||
/* do these later?
|
PyObject* read(int size=-1);
|
||||||
bool isatty();
|
PyObject* readline(int size=-1);
|
||||||
int fileno();
|
PyObject* readlines(int sizehint=-1);
|
||||||
void truncate(int size=-1);
|
|
||||||
void write(wxString data);
|
// do these later?
|
||||||
void writelines(wxStringPtrList);
|
//bool isatty();
|
||||||
*/
|
//int fileno();
|
||||||
|
//void truncate(int size=-1);
|
||||||
|
//PyObject* next();
|
||||||
|
|
||||||
|
// It's an input stream, can't write to it.
|
||||||
|
//void write(wxString data);
|
||||||
|
//void writelines(wxStringPtrList);
|
||||||
|
|
||||||
// wxInputStream methods that may come in handy...
|
// wxInputStream methods that may come in handy...
|
||||||
|
|
||||||
@@ -86,14 +89,82 @@ protected:
|
|||||||
virtual wxFileOffset OnSysSeek(wxFileOffset off, wxSeekMode mode);
|
virtual wxFileOffset OnSysSeek(wxFileOffset off, wxSeekMode mode);
|
||||||
virtual wxFileOffset OnSysTell() const;
|
virtual wxFileOffset OnSysTell() const;
|
||||||
|
|
||||||
// helper
|
|
||||||
static PyObject* getMethod(PyObject* py, char* name);
|
|
||||||
|
|
||||||
PyObject* m_read;
|
PyObject* m_read;
|
||||||
PyObject* m_seek;
|
PyObject* m_seek;
|
||||||
PyObject* m_tell;
|
PyObject* m_tell;
|
||||||
bool m_block;
|
bool m_block;
|
||||||
};
|
};
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// C++ class wxPyOutputStream to act as base for python class wxOutputStream
|
||||||
|
// You can use it in python like a python file object.
|
||||||
|
class wxPyOutputStream {
|
||||||
|
public:
|
||||||
|
// underlying wxOutputStream
|
||||||
|
wxOutputStream* m_wxos;
|
||||||
|
|
||||||
|
public:
|
||||||
|
wxPyOutputStream(wxOutputStream* wxos) : m_wxos(wxos) {}
|
||||||
|
~wxPyOutputStream();
|
||||||
|
|
||||||
|
void close();
|
||||||
|
void flush();
|
||||||
|
bool eof();
|
||||||
|
void seek(int offset, int whence=0);
|
||||||
|
int tell();
|
||||||
|
|
||||||
|
// it's an output stream, can't read from it.
|
||||||
|
//PyObject* read(int size=-1);
|
||||||
|
//PyObject* readline(int size=-1);
|
||||||
|
//PyObject* readlines(int sizehint=-1);
|
||||||
|
|
||||||
|
// do these later?
|
||||||
|
//bool isatty();
|
||||||
|
//int fileno();
|
||||||
|
//void truncate(int size=-1);
|
||||||
|
|
||||||
|
void write(PyObject* data);
|
||||||
|
//void writelines(wxStringArray& arr);
|
||||||
|
|
||||||
|
|
||||||
|
// wxOutputStream methods that may come in handy...
|
||||||
|
void PutC(char c) { if (m_wxos) m_wxos->PutC(c); }
|
||||||
|
size_t LastWrite() { if (m_wxos) return m_wxos->LastWrite(); }
|
||||||
|
unsigned long SeekO(unsigned long pos, wxSeekMode mode = wxFromStart)
|
||||||
|
{ if (m_wxos) return m_wxos->SeekO(pos, mode); else return 0; }
|
||||||
|
unsigned long TellO() { if (m_wxos) return m_wxos->TellO(); else return 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// This is a wxOutputStream that wraps a Python file-like
|
||||||
|
// object and calls the Python methods as needed.
|
||||||
|
class wxPyCBOutputStream : public wxOutputStream {
|
||||||
|
public:
|
||||||
|
~wxPyCBOutputStream();
|
||||||
|
virtual wxFileOffset GetLength() const;
|
||||||
|
|
||||||
|
// factory function
|
||||||
|
static wxPyCBOutputStream* create(PyObject *py, bool block=true);
|
||||||
|
|
||||||
|
wxPyCBOutputStream(const wxPyCBOutputStream& other);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// can only be created via the factory
|
||||||
|
wxPyCBOutputStream(PyObject *w, PyObject *s, PyObject *t, bool block);
|
||||||
|
|
||||||
|
// wxStreamBase methods
|
||||||
|
virtual size_t OnSysRead(void *buffer, size_t bufsize);
|
||||||
|
virtual size_t OnSysWrite(const void *buffer, size_t bufsize);
|
||||||
|
virtual wxFileOffset OnSysSeek(wxFileOffset off, wxSeekMode mode);
|
||||||
|
virtual wxFileOffset OnSysTell() const;
|
||||||
|
|
||||||
|
PyObject* m_write;
|
||||||
|
PyObject* m_seek;
|
||||||
|
PyObject* m_tell;
|
||||||
|
bool m_block;
|
||||||
|
};
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
#endif
|
#endif
|
||||||
|
@@ -123,6 +123,8 @@ inline wxPyCoreAPI* wxPyGetCoreAPIPtr()
|
|||||||
#define wxPoint2D_LIST_helper(a,b) (wxPyGetCoreAPIPtr()->p_wxPoint2D_LIST_helper(a, b))
|
#define wxPoint2D_LIST_helper(a,b) (wxPyGetCoreAPIPtr()->p_wxPoint2D_LIST_helper(a, b))
|
||||||
#define wxRect2D_helper(a,b) (wxPyGetCoreAPIPtr()->p_wxRect2D_helper(a,b))
|
#define wxRect2D_helper(a,b) (wxPyGetCoreAPIPtr()->p_wxRect2D_helper(a,b))
|
||||||
|
|
||||||
|
#define wxPyCBOutputStream_create(a, b) (wxPyGetCoreAPIPtr()->p_wxPyCBOutputStream_create(a, b))
|
||||||
|
#define wxPyCBOutputStream_copy(a) (wxPyGetCoreAPIPtr()->p_wxPyCBOutputStream_copy(a))
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
#endif
|
#endif
|
||||||
|
@@ -352,6 +352,7 @@ class wxPyClientData;
|
|||||||
class wxPyUserData;
|
class wxPyUserData;
|
||||||
class wxPyOORClientData;
|
class wxPyOORClientData;
|
||||||
class wxPyCBInputStream;
|
class wxPyCBInputStream;
|
||||||
|
class wxPyCBOutputStream;
|
||||||
|
|
||||||
void wxPyClientData_dtor(wxPyClientData* self);
|
void wxPyClientData_dtor(wxPyClientData* self);
|
||||||
void wxPyUserData_dtor(wxPyUserData* self);
|
void wxPyUserData_dtor(wxPyUserData* self);
|
||||||
@@ -359,6 +360,9 @@ void wxPyOORClientData_dtor(wxPyOORClientData* self);
|
|||||||
wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block);
|
wxPyCBInputStream* wxPyCBInputStream_create(PyObject *py, bool block);
|
||||||
wxPyCBInputStream* wxPyCBInputStream_copy(wxPyCBInputStream* other);
|
wxPyCBInputStream* wxPyCBInputStream_copy(wxPyCBInputStream* other);
|
||||||
|
|
||||||
|
wxPyCBOutputStream* wxPyCBOutputStream_create(PyObject *py, bool block);
|
||||||
|
wxPyCBOutputStream* wxPyCBOutputStream_copy(wxPyCBOutputStream* other);
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
// Export a C API in a struct. Other modules will be able to load this from
|
// Export a C API in a struct. Other modules will be able to load this from
|
||||||
// the wx.core module and will then have safe access to these functions, even if
|
// the wx.core module and will then have safe access to these functions, even if
|
||||||
@@ -435,6 +439,9 @@ struct wxPyCoreAPI {
|
|||||||
wxPoint2D* (*p_wxPoint2D_LIST_helper)(PyObject* source, size_t* npoints);
|
wxPoint2D* (*p_wxPoint2D_LIST_helper)(PyObject* source, size_t* npoints);
|
||||||
bool (*p_wxRect2D_helper)(PyObject* source, wxRect2D** obj);
|
bool (*p_wxRect2D_helper)(PyObject* source, wxRect2D** obj);
|
||||||
|
|
||||||
|
wxPyCBOutputStream* (*p_wxPyCBOutputStream_create)(PyObject *py, bool block);
|
||||||
|
wxPyCBOutputStream* (*p_wxPyCBOutputStream_copy)(wxPyCBOutputStream* other);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@@ -241,6 +241,8 @@ static wxPyCoreAPI API = {
|
|||||||
wxPoint2D_LIST_helper,
|
wxPoint2D_LIST_helper,
|
||||||
wxRect2D_helper,
|
wxRect2D_helper,
|
||||||
|
|
||||||
|
wxPyCBOutputStream_create,
|
||||||
|
wxPyCBOutputStream_copy,
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -61,18 +61,43 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%typemap(out) wxInputStream* {
|
%typemap(out) wxInputStream* {
|
||||||
wxPyInputStream * _ptr = NULL;
|
wxPyInputStream * _ptr = NULL;
|
||||||
|
if ($1)
|
||||||
if ($1) {
|
|
||||||
_ptr = new wxPyInputStream($1);
|
_ptr = new wxPyInputStream($1);
|
||||||
}
|
|
||||||
$result = wxPyConstructObject(_ptr, wxT("wxPyInputStream"), $owner);
|
$result = wxPyConstructObject(_ptr, wxT("wxPyInputStream"), $owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
// Typemaps for wxOutputStream. We only need in by reference and out by
|
||||||
|
// pointer in this one.
|
||||||
|
|
||||||
|
|
||||||
|
%typemap(in) wxOutputStream& (wxPyOutputStream* temp, bool created) {
|
||||||
|
if (wxPyConvertSwigPtr($input, (void **)&temp, wxT("wxPyOutputStream"))) {
|
||||||
|
$1 = temp->m_wxos;
|
||||||
|
created = false;
|
||||||
|
} else {
|
||||||
|
PyErr_Clear(); // clear the failure of the wxPyConvert above
|
||||||
|
$1 = wxPyCBOutputStream_create($input, false);
|
||||||
|
if ($1 == NULL) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Expected wx.OutputStream or Python file-like object.");
|
||||||
|
SWIG_fail;
|
||||||
|
}
|
||||||
|
created = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
%typemap(freearg) wxOutputStream& { if (created$argnum) delete $1; }
|
||||||
|
|
||||||
|
|
||||||
|
%typemap(out) wxOutputStream* {
|
||||||
|
wxPyOutputStream * _ptr = NULL;
|
||||||
|
if ($1)
|
||||||
|
_ptr = new wxPyOutputStream($1);
|
||||||
|
$result = wxPyConstructObject(_ptr, wxT("wxPyOutputStream"), $owner);
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
enum wxSeekMode
|
enum wxSeekMode
|
||||||
@@ -107,14 +132,6 @@ public:
|
|||||||
void seek(int offset, int whence=0);
|
void seek(int offset, int whence=0);
|
||||||
int tell();
|
int tell();
|
||||||
|
|
||||||
/*
|
|
||||||
bool isatty();
|
|
||||||
int fileno();
|
|
||||||
void truncate(int size=-1);
|
|
||||||
void write(wxString data);
|
|
||||||
void writelines(wxStringPtrList);
|
|
||||||
*/
|
|
||||||
|
|
||||||
char Peek();
|
char Peek();
|
||||||
char GetC();
|
char GetC();
|
||||||
size_t LastRead();
|
size_t LastRead();
|
||||||
@@ -128,43 +145,41 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: make a more fully implemented file interface...
|
|
||||||
class wxOutputStream {
|
%rename(OutputStream) wxPyOutputStream;
|
||||||
|
class wxPyOutputStream
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
/*
|
%extend {
|
||||||
|
wxPyOutputStream(PyObject* p) {
|
||||||
|
wxOutputStream* wxis = wxPyCBOutputStream::create(p);
|
||||||
|
if (wxis)
|
||||||
|
return new wxPyOutputStream(wxis);
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
~wxPyOutputStream();
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
void flush();
|
void flush();
|
||||||
wxString* read(int size=-1);
|
bool eof();
|
||||||
wxString* readline(int size=-1);
|
|
||||||
wxStringPtrList* readlines(int sizehint=-1);
|
|
||||||
void seek(int offset, int whence=0);
|
void seek(int offset, int whence=0);
|
||||||
int tell();
|
int tell();
|
||||||
bool isatty();
|
|
||||||
int fileno();
|
|
||||||
void truncate(int size=-1);
|
|
||||||
void write(wxString data);
|
|
||||||
void writelines(wxStringPtrList);
|
|
||||||
*/
|
|
||||||
|
|
||||||
%extend {
|
void write(PyObject* data);
|
||||||
void write(PyObject* obj) {
|
//void writelines(wxStringArray& arr);
|
||||||
// We use only strings for the streams, not unicode
|
|
||||||
PyObject* str = PyObject_Str(obj);
|
void PutC(char c);
|
||||||
if (! str) {
|
size_t LastWrite();
|
||||||
PyErr_SetString(PyExc_TypeError, "Unable to convert to string");
|
unsigned long SeekO(unsigned long pos, wxSeekMode mode = wxFromStart);
|
||||||
return;
|
unsigned long TellO();
|
||||||
}
|
|
||||||
self->Write(PyString_AS_STRING(str),
|
|
||||||
PyString_GET_SIZE(str));
|
|
||||||
Py_DECREF(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
size_t LastWrite() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
%init %{
|
%init %{
|
||||||
wxPyPtrTypeMap_Add("wxInputStream", "wxPyInputStream");
|
wxPyPtrTypeMap_Add("wxInputStream", "wxPyInputStream");
|
||||||
|
wxPyPtrTypeMap_Add("wxOutputStream", "wxPyOutputStream");
|
||||||
%}
|
%}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user