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* one = PyInt_FromLong(1);
71 PyObject_SetAttrString($target, "thisown", one);
80 //----------------------------------------------------------------------
83 // definitions of wxStringPtrList and wxPyInputStream
84 #include <wx/listimpl.cpp>
85 WX_DEFINE_LIST(wxStringPtrList);
88 void wxPyInputStream::close() {
92 void wxPyInputStream::flush() {
96 bool wxPyInputStream::eof() {
103 wxPyInputStream::~wxPyInputStream() {
107 wxString* wxPyInputStream::read(int size) {
109 const int BUFSIZE = 1024;
111 // check if we have a real wxInputStream to work with
113 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
119 char * buf = new char[BUFSIZE];
133 wxPy_BEGIN_ALLOW_THREADS;
134 while (! wxi->Eof()) {
135 wxi->Read(buf, BUFSIZE);
136 //*s += wxString(buf, wxi->LastRead());
137 s->Append(buf, wxi->LastRead());
140 wxPy_END_ALLOW_THREADS;
143 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
145 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
149 } else { // Read only size number of characters
157 wxPy_BEGIN_ALLOW_THREADS;
158 wxi->Read(s->GetWriteBuf(size+1), size);
159 s->UngetWriteBuf(wxi->LastRead());
160 wxPy_END_ALLOW_THREADS;
163 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
165 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
173 wxString* wxPyInputStream::readline (int size) {
174 // check if we have a real wxInputStream to work with
176 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
183 wxString* s = new wxString;
189 // read until \n or byte limit reached
190 wxPy_BEGIN_ALLOW_THREADS;
191 for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) {
192 *s += ch = wxi->GetC();
194 wxPy_END_ALLOW_THREADS;
197 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
199 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
206 wxStringPtrList* wxPyInputStream::readlines (int sizehint) {
207 // check if we have a real wxInputStream to work with
209 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
214 wxStringPtrList* l = new wxStringPtrList();
220 // read sizehint bytes or until EOF
221 wxPy_BEGIN_ALLOW_THREADS;
223 for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) {
224 wxString* s = readline();
226 l->DeleteContents(TRUE);
233 wxPy_END_ALLOW_THREADS;
236 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
237 l->DeleteContents(TRUE);
239 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
246 void wxPyInputStream::seek(int offset, int whence) {
248 wxi->SeekI(offset, wxSeekMode(whence));
251 int wxPyInputStream::tell(){
258 // wxInputStream which operates on a Python file-like object
259 class wxPyCBInputStream : public wxInputStream {
266 virtual size_t OnSysRead(void *buffer, size_t bufsize) {
270 bool doSave = wxPyRestoreThread();
271 PyObject* arglist = Py_BuildValue("(i)", bufsize);
272 PyObject* result = PyEval_CallObject(read, arglist);
276 if ((result != NULL) && PyString_Check(result)) {
277 o = PyString_Size(result);
279 m_lasterror = wxSTREAM_EOF;
282 strncpy((char*)buffer, PyString_AsString(result), o);
287 m_lasterror = wxSTREAM_READ_ERROR;
288 wxPySaveThread(doSave);
293 virtual size_t OnSysWrite(const void *buffer, size_t bufsize){
294 m_lasterror = wxSTREAM_WRITE_ERROR;
298 virtual off_t OnSysSeek(off_t off, wxSeekMode mode){
299 bool doSave = wxPyRestoreThread();
300 PyObject*arglist = Py_BuildValue("(ii)", off, mode);
301 PyObject*result = PyEval_CallObject(seek, arglist);
304 wxPySaveThread(doSave);
308 virtual off_t OnSysTell() const{
309 bool doSave = wxPyRestoreThread();
310 PyObject* arglist = Py_BuildValue("()");
311 PyObject* result = PyEval_CallObject(tell, arglist);
314 if (result != NULL) {
315 o = PyInt_AsLong(result);
318 wxPySaveThread(doSave);
322 wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t)
323 : py(p), read(r), seek(s), tell(t)
327 ~wxPyCBInputStream() {
328 bool doSave = wxPyRestoreThread();
333 wxPySaveThread(doSave);
336 virtual size_t GetSize() {
338 off_t temp = OnSysTell();
339 off_t ret = OnSysSeek(0, wxFromEnd);
340 OnSysSeek(temp, wxFromStart);
347 static wxPyCBInputStream* create(PyObject *py) {
352 if (!PyInstance_Check(py) && !PyFile_Check(py)) {
353 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
357 read = getMethod(py, "read");
358 seek = getMethod(py, "seek");
359 tell = getMethod(py, "tell");
362 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
369 return new wxPyCBInputStream(py, read, seek, tell);
372 static PyObject* getMethod(PyObject* py, char* name) {
373 if (!PyObject_HasAttrString(py, name))
375 PyObject* o = PyObject_GetAttrString(py, name);
376 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
388 //----------------------------------------------------------------------
391 // block threads for wxPyInputStream **** WHY?
397 // wxStringPtrList* to python list of strings typemap
398 %typemap(python, out) wxStringPtrList* {
400 $target = PyList_New($source->GetCount());
401 wxStringPtrList::Node *node = $source->GetFirst();
402 for (int i=0; node; i++) {
403 wxString *s = node->GetData();
404 PyList_SetItem($target, i, PyString_FromStringAndSize(s->c_str(), s->Len()));
405 node = node->GetNext();
415 // transport exceptions via %target=0
416 %typemap(python, out) wxPyInputStream* {
419 SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p");
420 $target = Py_BuildValue("s",_ptemp);
427 // transport exceptions via %target=0
428 %typemap(python, out) wxString* {
430 $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
439 %name(wxInputStream) class wxPyInputStream {
442 wxPyInputStream(PyObject* p) {
443 wxInputStream* wxi = wxPyCBInputStream::create(p);
445 return new wxPyInputStream(wxi);
453 wxString* read(int size=-1);
454 wxString* readline(int size=-1);
455 wxStringPtrList* readlines(int sizehint=-1);
456 void seek(int offset, int whence=0);
461 void truncate(int size=-1);
462 void write(wxString data);
463 void writelines(wxStringPtrList);
469 // TODO: make a more fully implemented file interface...
470 class wxOutputStream {
475 wxString* read(int size=-1);
476 wxString* readline(int size=-1);
477 wxStringPtrList* readlines(int sizehint=-1);
478 void seek(int offset, int whence=0);
482 void truncate(int size=-1);
483 void write(wxString data);
484 void writelines(wxStringPtrList);
488 void write(const wxString& str) {
489 self->Write(str.c_str(), str.Length());
495 // restore except and typemaps
496 %typemap(python,out) wxStringPtrList*;
497 %typemap(python,out) wxPyInputStream*;
498 %typemap(python, out) wxString* {
499 $target = PyString_FromStringAndSize($source->c_str(), $source->Len());
502 wxPy_BEGIN_ALLOW_THREADS;
504 wxPy_END_ALLOW_THREADS;