+// wxPyInputStream and wxPyCBInputStream methods
+
+
+void wxPyInputStream::close() {
+ /* do nothing for now */
+}
+
+void wxPyInputStream::flush() {
+ /* do nothing for now */
+}
+
+bool wxPyInputStream::eof() {
+ if (m_wxis)
+ return m_wxis->Eof();
+ else
+ return TRUE;
+}
+
+wxPyInputStream::~wxPyInputStream() {
+ /* do nothing */
+}
+
+
+
+
+PyObject* wxPyInputStream::read(int size) {
+ PyObject* obj = NULL;
+ wxMemoryBuffer buf;
+ const int BUFSIZE = 1024;
+
+ // check if we have a real wxInputStream to work with
+ if (!m_wxis) {
+ PyErr_SetString(PyExc_IOError, "no valid C-wxInputStream");
+ return NULL;
+ }
+
+ if (size < 0) {
+ // read until EOF
+ while (! m_wxis->Eof()) {
+ m_wxis->Read(buf.GetAppendBuf(BUFSIZE), BUFSIZE);
+ buf.UngetAppendBuf(m_wxis->LastRead());
+ }
+
+ } else { // Read only size number of characters
+ m_wxis->Read(buf.GetWriteBuf(size), size);
+ buf.UngetWriteBuf(m_wxis->LastRead());
+ }
+
+ // error check
+ if (m_wxis->LastError() == wxSTREAM_READ_ERROR) {
+ PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
+ }
+ else {
+ // We use only strings for the streams, not unicode
+ obj = PyString_FromStringAndSize(buf, buf.GetDataLen());
+ }
+ return obj;
+}
+
+
+PyObject* wxPyInputStream::readline(int size) {
+ PyObject* obj = NULL;
+ wxMemoryBuffer buf;
+ int i;
+ char ch;
+
+ // check if we have a real wxInputStream to work with
+ if (!m_wxis) {
+ PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream");
+ return NULL;
+ }
+
+ // read until \n or byte limit reached
+ for (i=ch=0; (ch != '\n') && (!m_wxis->Eof()) && ((size < 0) || (i < size)); i++) {
+ ch = m_wxis->GetC();
+ buf.AppendByte(ch);
+ }
+
+ // errorcheck
+ if (m_wxis->LastError() == wxSTREAM_READ_ERROR) {
+ PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
+ }
+ else {
+ // We use only strings for the streams, not unicode
+ obj = PyString_FromStringAndSize((char*)buf.GetData(), buf.GetDataLen());
+ }
+ return obj;
+}
+
+
+PyObject* wxPyInputStream::readlines(int sizehint) {
+ PyObject* pylist;
+
+ // check if we have a real wxInputStream to work with
+ if (!m_wxis) {
+ PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
+ return NULL;
+ }
+
+ // init list
+ pylist = PyList_New(0);
+ if (!pylist) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+
+ // read sizehint bytes or until EOF
+ int i;
+ for (i=0; (!m_wxis->Eof()) && ((sizehint < 0) || (i < sizehint));) {
+ PyObject* s = this->readline();
+ if (s == NULL) {
+ Py_DECREF(pylist);
+ return NULL;
+ }
+ PyList_Append(pylist, s);
+ i += PyString_Size(s);
+ }
+
+ // error check
+ if (m_wxis->LastError() == wxSTREAM_READ_ERROR) {
+ Py_DECREF(pylist);
+ PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
+ return NULL;
+ }
+
+ return pylist;
+}
+
+
+void wxPyInputStream::seek(int offset, int whence) {
+ if (m_wxis)
+ m_wxis->SeekI(offset, wxSeekMode(whence));
+}
+
+int wxPyInputStream::tell(){
+ if (m_wxis)
+ return m_wxis->TellI();
+ else return 0;
+}
+
+
+
+
+wxPyCBInputStream::wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block)
+ : wxInputStream(), m_read(r), m_seek(s), m_tell(t), m_block(block)
+{}
+
+
+wxPyCBInputStream::~wxPyCBInputStream() {
+ if (m_block) wxPyBeginBlockThreads();
+ Py_XDECREF(m_read);
+ Py_XDECREF(m_seek);
+ Py_XDECREF(m_tell);
+ if (m_block) wxPyEndBlockThreads();
+}
+
+
+wxPyCBInputStream* wxPyCBInputStream::create(PyObject *py, bool block) {
+ if (block) wxPyBeginBlockThreads();
+
+ PyObject* read = getMethod(py, "read");
+ PyObject* seek = getMethod(py, "seek");
+ PyObject* tell = getMethod(py, "tell");
+
+ if (!read) {
+ PyErr_SetString(PyExc_TypeError, "Not a file-like object");
+ Py_XDECREF(read);
+ Py_XDECREF(seek);
+ Py_XDECREF(tell);
+ if (block) wxPyEndBlockThreads();
+ return NULL;
+ }
+
+ if (block) wxPyEndBlockThreads();
+ return new wxPyCBInputStream(read, seek, tell, block);
+}