-// wxInputStream which operates on a Python file-like object
-class wxPyCBInputStream : public wxInputStream {
-protected:
- PyObject* read;
- PyObject* seek;
- PyObject* tell;
- PyObject* py;
-
- virtual size_t OnSysRead(void *buffer, size_t bufsize) {
- if (bufsize == 0)
- return 0;
-
- bool doSave = wxPyRestoreThread();
- PyObject* arglist = Py_BuildValue("(i)", bufsize);
- PyObject* result = PyEval_CallObject(read, arglist);
- Py_DECREF(arglist);
-
- size_t o = 0;
- if ((result != NULL) && PyString_Check(result)) {
- o = PyString_Size(result);
- if (o == 0)
- m_lasterror = wxSTREAM_EOF;
- if (o > bufsize)
- o = bufsize;
- strncpy((char*)buffer, PyString_AsString(result), o);
- Py_DECREF(result);
-
- }
- else
- m_lasterror = wxSTREAM_READ_ERROR;
- wxPySaveThread(doSave);
- m_lastcount = o;
- return o;
- }
-
- virtual size_t OnSysWrite(const void *buffer, size_t bufsize){
- m_lasterror = wxSTREAM_WRITE_ERROR;
- return 0;
- }
-
- virtual off_t OnSysSeek(off_t off, wxSeekMode mode){
- bool doSave = wxPyRestoreThread();
- PyObject*arglist = Py_BuildValue("(ii)", off, mode);
- PyObject*result = PyEval_CallObject(seek, arglist);
- Py_DECREF(arglist);
- Py_XDECREF(result);
- wxPySaveThread(doSave);
- return OnSysTell();
- }
-
- virtual off_t OnSysTell() const{
- bool doSave = wxPyRestoreThread();
- PyObject* arglist = Py_BuildValue("()");
- PyObject* result = PyEval_CallObject(tell, arglist);
- Py_DECREF(arglist);
- off_t o = 0;
- if (result != NULL) {
- o = PyInt_AsLong(result);
- Py_DECREF(result);
- };
- wxPySaveThread(doSave);
- return o;
- }
-
- wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t)
- : py(p), read(r), seek(s), tell(t)
- {}
-
-public:
- ~wxPyCBInputStream() {
- bool doSave = wxPyRestoreThread();
- Py_XDECREF(py);
- Py_XDECREF(read);
- Py_XDECREF(seek);
- Py_XDECREF(tell);
- wxPySaveThread(doSave);
- }
-
- virtual size_t GetSize() {
- if (seek && tell) {
- off_t temp = OnSysTell();
- off_t ret = OnSysSeek(0, wxFromEnd);
- OnSysSeek(temp, wxFromStart);
- return ret;
- }
- else
- return 0;
- }
-
- static wxPyCBInputStream* create(PyObject *py) {
- PyObject* read;
- PyObject* seek;
- PyObject* tell;
-
- if (!PyInstance_Check(py) && !PyFile_Check(py)) {
- PyErr_SetString(PyExc_TypeError, "Not a file-like object");
- Py_XDECREF(py);
- return NULL;
- }
- read = getMethod(py, "read");
- seek = getMethod(py, "seek");
- tell = getMethod(py, "tell");
-
- if (!read) {
- PyErr_SetString(PyExc_TypeError, "Not a file-like object");
- Py_XDECREF(py);
- Py_XDECREF(read);
- Py_XDECREF(seek);
- Py_XDECREF(tell);
- return NULL;
- }
- return new wxPyCBInputStream(py, read, seek, tell);
- }
-
- static PyObject* getMethod(PyObject* py, char* name) {
- if (!PyObject_HasAttrString(py, name))
- return NULL;
- PyObject* o = PyObject_GetAttrString(py, name);
- if (!PyMethod_Check(o) && !PyCFunction_Check(o))
- return NULL;
- return o;
- }
-
-protected:
-
-};
-
-%} // End of the C++
-//----------------------------------------------------------------------
-
-
-// block threads for wxPyInputStream **** WHY?
-%except(python) {
- $function
-}
-
-
-// wxStringPtrList* to python list of strings typemap
-%typemap(python, out) wxStringPtrList* {