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);
56 $target = wxPyConstructObject(_ptr, "wxInputStream", TRUE);
59 //----------------------------------------------------------------------
62 // definitions of wxStringPtrList and wxPyInputStream
63 #include <wx/listimpl.cpp>
64 WX_DEFINE_LIST(wxStringPtrList);
67 void wxPyInputStream::close() {
71 void wxPyInputStream::flush() {
75 bool wxPyInputStream::eof() {
82 wxPyInputStream::~wxPyInputStream() {
86 wxString* wxPyInputStream::read(int size) {
88 const int BUFSIZE = 1024;
90 // check if we have a real wxInputStream to work with
92 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
98 char * buf = new char[BUFSIZE];
112 wxPy_BEGIN_ALLOW_THREADS;
113 while (! wxi->Eof()) {
114 wxi->Read(buf, BUFSIZE);
115 //*s += wxString(buf, wxi->LastRead());
116 s->Append(buf, wxi->LastRead());
119 wxPy_END_ALLOW_THREADS;
122 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
124 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
128 } else { // Read only size number of characters
136 wxPy_BEGIN_ALLOW_THREADS;
137 wxi->Read(s->GetWriteBuf(size+1), size);
138 s->UngetWriteBuf(wxi->LastRead());
139 wxPy_END_ALLOW_THREADS;
142 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
144 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
152 wxString* wxPyInputStream::readline (int size) {
153 // check if we have a real wxInputStream to work with
155 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
162 wxString* s = new wxString;
168 // read until \n or byte limit reached
169 wxPy_BEGIN_ALLOW_THREADS;
170 for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) {
171 *s += ch = wxi->GetC();
173 wxPy_END_ALLOW_THREADS;
176 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
178 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
185 wxStringPtrList* wxPyInputStream::readlines (int sizehint) {
186 // check if we have a real wxInputStream to work with
188 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
193 wxStringPtrList* l = new wxStringPtrList();
199 // read sizehint bytes or until EOF
200 wxPy_BEGIN_ALLOW_THREADS;
202 for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) {
203 wxString* s = readline();
205 l->DeleteContents(TRUE);
212 wxPy_END_ALLOW_THREADS;
215 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
216 l->DeleteContents(TRUE);
218 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
225 void wxPyInputStream::seek(int offset, int whence) {
227 wxi->SeekI(offset, wxSeekMode(whence));
230 int wxPyInputStream::tell(){
237 // wxInputStream which operates on a Python file-like object
238 class wxPyCBInputStream : public wxInputStream {
245 virtual size_t OnSysRead(void *buffer, size_t bufsize) {
249 bool doSave = wxPyRestoreThread();
250 PyObject* arglist = Py_BuildValue("(i)", bufsize);
251 PyObject* result = PyEval_CallObject(read, arglist);
255 if ((result != NULL) && PyString_Check(result)) {
256 o = PyString_Size(result);
258 m_lasterror = wxSTREAM_EOF;
261 strncpy((char*)buffer, PyString_AsString(result), o);
266 m_lasterror = wxSTREAM_READ_ERROR;
267 wxPySaveThread(doSave);
272 virtual size_t OnSysWrite(const void *buffer, size_t bufsize){
273 m_lasterror = wxSTREAM_WRITE_ERROR;
277 virtual off_t OnSysSeek(off_t off, wxSeekMode mode){
278 bool doSave = wxPyRestoreThread();
279 PyObject*arglist = Py_BuildValue("(ii)", off, mode);
280 PyObject*result = PyEval_CallObject(seek, arglist);
283 wxPySaveThread(doSave);
287 virtual off_t OnSysTell() const{
288 bool doSave = wxPyRestoreThread();
289 PyObject* arglist = Py_BuildValue("()");
290 PyObject* result = PyEval_CallObject(tell, arglist);
293 if (result != NULL) {
294 o = PyInt_AsLong(result);
297 wxPySaveThread(doSave);
301 wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t)
302 : py(p), read(r), seek(s), tell(t)
306 ~wxPyCBInputStream() {
307 bool doSave = wxPyRestoreThread();
312 wxPySaveThread(doSave);
315 virtual size_t GetSize() {
317 off_t temp = OnSysTell();
318 off_t ret = OnSysSeek(0, wxFromEnd);
319 OnSysSeek(temp, wxFromStart);
326 static wxPyCBInputStream* create(PyObject *py) {
331 if (!PyInstance_Check(py) && !PyFile_Check(py)) {
332 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
336 read = getMethod(py, "read");
337 seek = getMethod(py, "seek");
338 tell = getMethod(py, "tell");
341 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
348 return new wxPyCBInputStream(py, read, seek, tell);
351 static PyObject* getMethod(PyObject* py, char* name) {
352 if (!PyObject_HasAttrString(py, name))
354 PyObject* o = PyObject_GetAttrString(py, name);
355 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
367 //----------------------------------------------------------------------
370 // block threads for wxPyInputStream **** WHY?
376 // wxStringPtrList* to python list of strings typemap
377 %typemap(python, out) wxStringPtrList* {
379 $target = PyList_New($source->GetCount());
380 wxStringPtrList::Node *node = $source->GetFirst();
381 for (int i=0; node; i++) {
382 wxString *s = node->GetData();
383 PyList_SetItem($target, i, PyString_FromStringAndSize(s->c_str(), s->Len()));
384 node = node->GetNext();
394 // transport exceptions via %target=0
395 %typemap(python, out) wxPyInputStream* {
398 SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p");
399 $target = Py_BuildValue("s",_ptemp);
406 // transport exceptions via %target=0
407 %typemap(python, out) wxString* {
409 $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
418 %name(wxInputStream) class wxPyInputStream {
421 wxPyInputStream(PyObject* p) {
422 wxInputStream* wxi = wxPyCBInputStream::create(p);
424 return new wxPyInputStream(wxi);
432 wxString* read(int size=-1);
433 wxString* readline(int size=-1);
434 wxStringPtrList* readlines(int sizehint=-1);
435 void seek(int offset, int whence=0);
440 void truncate(int size=-1);
441 void write(wxString data);
442 void writelines(wxStringPtrList);
448 // TODO: make a more fully implemented file interface...
449 class wxOutputStream {
454 wxString* read(int size=-1);
455 wxString* readline(int size=-1);
456 wxStringPtrList* readlines(int sizehint=-1);
457 void seek(int offset, int whence=0);
461 void truncate(int size=-1);
462 void write(wxString data);
463 void writelines(wxStringPtrList);
467 void write(const wxString& str) {
468 self->Write(str.c_str(), str.Length());
474 // restore except and typemaps
475 %typemap(python,out) wxStringPtrList*;
476 %typemap(python,out) wxPyInputStream*;
477 %typemap(python, out) wxString* {
478 $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
481 wxPy_BEGIN_ALLOW_THREADS;
483 wxPy_END_ALLOW_THREADS;
487 //----------------------------------------------------------------------
490 wxPyPtrTypeMap_Add("wxInputStream", "wxPyInputStream");
493 //----------------------------------------------------------------------