1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: SWIG definitions of the wxFileSystem family of classes
5 // Author: Joerg Baumann
7 // Created: 25-Sept-2000
9 // Copyright: (c) 2000 by Joerg Baumann
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
17 #include <wx/stream.h>
21 //----------------------------------------------------------------------
24 %include my_typemaps.i
26 // Import some definitions of other classes, etc.
29 %pragma(python) code = "import wx"
30 %pragma(python) code = "import string"
32 //----------------------------------------------------------------------
33 // typemaps for wxInputStream
34 %typemap(python,in) wxInputStream *stream {
35 if (PyInstance_Check($source)) {
37 if (SWIG_GetPtrObj($source, (void **) &ptr,"_wxPyInputStream_p")) {
38 PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p.");
43 PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p.");
49 // typemaps for wxInputStream
50 %typemap(python,out) wxInputStream* {
51 wxPyInputStream * _ptr = NULL;
54 _ptr = new wxPyInputStream($source);
58 SWIG_MakePtr(swigptr, _ptr, "_wxPyInputStream_p");
60 PyObject* classobj = PyDict_GetItemString(wxPython_dict, "wxInputStreamPtr");
65 PyObject* arg = Py_BuildValue("(s)", swigptr);
66 $target = PyInstance_New(classobj, arg, NULL);
70 PyObject_SetAttrString($target, "thisown", PyInt_FromLong(1));
78 //----------------------------------------------------------------------
81 // definitions of wxStringPtrList and wxPyInputStream
82 #include <wx/listimpl.cpp>
83 WX_DEFINE_LIST(wxStringPtrList);
86 void wxPyInputStream::close() {
90 void wxPyInputStream::flush() {
94 bool wxPyInputStream::eof() {
101 wxPyInputStream::~wxPyInputStream() {
105 wxString* wxPyInputStream::read(int size) {
107 const int BUFSIZE = 1024;
109 // check if we have a real wxInputStream to work with
111 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
117 char * buf = new char[BUFSIZE];
131 wxPy_BEGIN_ALLOW_THREADS;
132 while (! wxi->Eof()) {
133 wxi->Read(buf, BUFSIZE);
134 //*s += wxString(buf, wxi->LastRead());
135 s->Append(buf, wxi->LastRead());
138 wxPy_END_ALLOW_THREADS;
141 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
143 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
147 } else { // Read only size number of characters
155 wxPy_BEGIN_ALLOW_THREADS;
156 wxi->Read(s->GetWriteBuf(size+1), size);
157 s->UngetWriteBuf(wxi->LastRead());
158 wxPy_END_ALLOW_THREADS;
161 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
163 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
171 wxString* wxPyInputStream::readline (int size) {
172 // check if we have a real wxInputStream to work with
174 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
181 wxString* s = new wxString;
187 // read until \n or byte limit reached
188 wxPy_BEGIN_ALLOW_THREADS;
189 for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) {
190 *s += ch = wxi->GetC();
192 wxPy_END_ALLOW_THREADS;
195 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
197 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
204 wxStringPtrList* wxPyInputStream::readlines (int sizehint) {
205 // check if we have a real wxInputStream to work with
207 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
212 wxStringPtrList* l = new wxStringPtrList();
218 // read sizehint bytes or until EOF
219 wxPy_BEGIN_ALLOW_THREADS;
221 for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) {
222 wxString* s = readline();
224 l->DeleteContents(TRUE);
231 wxPy_END_ALLOW_THREADS;
234 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
235 l->DeleteContents(TRUE);
237 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
244 void wxPyInputStream::seek(int offset, int whence) {
246 wxi->SeekI(offset, wxSeekMode(whence));
249 int wxPyInputStream::tell(){
256 // wxInputStream which operates on a Python file-like object
257 class wxPyCBInputStream : public wxInputStream {
264 virtual size_t OnSysRead(void *buffer, size_t bufsize) {
268 bool doSave = wxPyRestoreThread();
269 PyObject* arglist = Py_BuildValue("(i)", bufsize);
270 PyObject* result = PyEval_CallObject(read, arglist);
274 if ((result != NULL) && PyString_Check(result)) {
275 o = PyString_Size(result);
277 m_lasterror = wxSTREAM_EOF;
280 strncpy((char*)buffer, PyString_AsString(result), o);
285 m_lasterror = wxSTREAM_READ_ERROR;
286 wxPySaveThread(doSave);
291 virtual size_t OnSysWrite(const void *buffer, size_t bufsize){
292 m_lasterror = wxSTREAM_WRITE_ERROR;
296 virtual off_t OnSysSeek(off_t off, wxSeekMode mode){
297 bool doSave = wxPyRestoreThread();
298 PyObject*arglist = Py_BuildValue("(ii)", off, mode);
299 PyObject*result = PyEval_CallObject(seek, arglist);
302 wxPySaveThread(doSave);
306 virtual off_t OnSysTell() const{
307 bool doSave = wxPyRestoreThread();
308 PyObject* arglist = Py_BuildValue("()");
309 PyObject* result = PyEval_CallObject(tell, arglist);
312 if (result != NULL) {
313 o = PyInt_AsLong(result);
316 wxPySaveThread(doSave);
320 wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t)
321 : py(p), read(r), seek(s), tell(t)
325 ~wxPyCBInputStream() {
326 bool doSave = wxPyRestoreThread();
331 wxPySaveThread(doSave);
334 virtual size_t GetSize() {
336 off_t temp = OnSysTell();
337 off_t ret = OnSysSeek(0, wxFromEnd);
338 OnSysSeek(temp, wxFromStart);
345 static wxPyCBInputStream* create(PyObject *py) {
350 if (!PyInstance_Check(py) && !PyFile_Check(py)) {
351 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
355 read = getMethod(py, "read");
356 seek = getMethod(py, "seek");
357 tell = getMethod(py, "tell");
360 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
367 return new wxPyCBInputStream(py, read, seek, tell);
370 static PyObject* getMethod(PyObject* py, char* name) {
371 if (!PyObject_HasAttrString(py, name))
373 PyObject* o = PyObject_GetAttrString(py, name);
374 if (!PyMethod_Check(o) && !PyCFunction_Check(o))
384 //----------------------------------------------------------------------
387 // block threads for wxPyInputStream **** WHY?
393 // wxStringPtrList* to python list of strings typemap
394 %typemap(python, out) wxStringPtrList* {
396 $target = PyList_New($source->GetCount());
397 wxStringPtrList::Node *node = $source->GetFirst();
398 for (int i=0; node; i++) {
399 wxString *s = node->GetData();
400 PyList_SetItem($target, i, PyString_FromStringAndSize(s->c_str(), s->Len()));
401 node = node->GetNext();
411 // transport exceptions via %target=0
412 %typemap(python, out) wxPyInputStream* {
415 SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p");
416 $target = Py_BuildValue("s",_ptemp);
423 // transport exceptions via %target=0
424 %typemap(python, out) wxString* {
426 $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
435 %name(wxInputStream) class wxPyInputStream {
438 wxPyInputStream(PyObject* p) {
439 wxInputStream* wxi = wxPyCBInputStream::create(p);
441 return new wxPyInputStream(wxi);
449 wxString* read(int size=-1);
450 wxString* readline(int size=-1);
451 wxStringPtrList* readlines(int sizehint=-1);
452 void seek(int offset, int whence=0);
457 void truncate(int size=-1);
458 void write(wxString data);
459 void writelines(wxStringPtrList);
465 // TODO: make a more fully implemented file interface...
466 class wxOutputStream {
471 wxString* read(int size=-1);
472 wxString* readline(int size=-1);
473 wxStringPtrList* readlines(int sizehint=-1);
474 void seek(int offset, int whence=0);
478 void truncate(int size=-1);
479 void write(wxString data);
480 void writelines(wxStringPtrList);
484 void write(const wxString& str) {
485 self->Write(str.c_str(), str.Length());
491 // restore except and typemaps
492 %typemap(python,out) wxStringPtrList*;
493 %typemap(python,out) wxPyInputStream*;
494 %typemap(python, out) wxString* {
495 $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
498 wxPy_BEGIN_ALLOW_THREADS;
500 wxPy_END_ALLOW_THREADS;