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 while (! wxi->Eof()) {
113 wxi->Read(buf, BUFSIZE);
114 //*s += wxString(buf, wxi->LastRead());
115 s->Append(buf, wxi->LastRead());
120 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
122 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
126 } else { // Read only size number of characters
134 wxi->Read(s->GetWriteBuf(size+1), size);
135 s->UngetWriteBuf(wxi->LastRead());
138 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
140 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
148 wxString* wxPyInputStream::readline (int size) {
149 // check if we have a real wxInputStream to work with
151 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
158 wxString* s = new wxString;
164 // read until \n or byte limit reached
165 for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) {
166 *s += ch = wxi->GetC();
170 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
172 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
179 wxStringPtrList* wxPyInputStream::readlines (int sizehint) {
180 // check if we have a real wxInputStream to work with
182 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
187 wxStringPtrList* l = new wxStringPtrList();
193 // read sizehint bytes or until EOF
195 for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) {
196 wxString* s = readline();
198 l->DeleteContents(TRUE);
207 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
208 l->DeleteContents(TRUE);
210 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
217 void wxPyInputStream::seek(int offset, int whence) {
219 wxi->SeekI(offset, wxSeekMode(whence));
222 int wxPyInputStream::tell(){
229 // wxInputStream which operates on a Python file-like object
230 class wxPyCBInputStream : public wxInputStream {
237 virtual size_t OnSysRead(void *buffer, size_t bufsize) {
241 wxPyBeginBlockThreads();
242 PyObject* arglist = Py_BuildValue("(i)", bufsize);
243 PyObject* result = PyEval_CallObject(read, arglist);
247 if ((result != NULL) && PyString_Check(result)) {
248 o = PyString_Size(result);
250 m_lasterror = wxSTREAM_EOF;
253 strncpy((char*)buffer, PyString_AsString(result), o);
258 m_lasterror = wxSTREAM_READ_ERROR;
259 wxPyEndBlockThreads();
264 virtual size_t OnSysWrite(const void *buffer, size_t bufsize){
265 m_lasterror = wxSTREAM_WRITE_ERROR;
269 virtual off_t OnSysSeek(off_t off, wxSeekMode mode){
270 wxPyBeginBlockThreads();
271 PyObject*arglist = Py_BuildValue("(ii)", off, mode);
272 PyObject*result = PyEval_CallObject(seek, arglist);
275 wxPyEndBlockThreads();
279 virtual off_t OnSysTell() const{
280 wxPyBeginBlockThreads();
281 PyObject* arglist = Py_BuildValue("()");
282 PyObject* result = PyEval_CallObject(tell, arglist);
285 if (result != NULL) {
286 o = PyInt_AsLong(result);
289 wxPyEndBlockThreads();
293 wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t)
294 : py(p), read(r), seek(s), tell(t)
298 ~wxPyCBInputStream() {
299 wxPyBeginBlockThreads();
304 wxPyEndBlockThreads();
307 virtual size_t GetSize() {
309 off_t temp = OnSysTell();
310 off_t ret = OnSysSeek(0, wxFromEnd);
311 OnSysSeek(temp, wxFromStart);
318 static wxPyCBInputStream* create(PyObject *py) {
323 if (!PyInstance_Check(py) && !PyFile_Check(py)) {
324 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
328 read = getMethod(py, "read");
329 seek = getMethod(py, "seek");
330 tell = getMethod(py, "tell");
333 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
340 return new wxPyCBInputStream(py, read, seek, tell);
343 static PyObject* getMethod(PyObject* py, char* name) {
344 if (!PyObject_HasAttrString(py, name))
346 PyObject* o = PyObject_GetAttrString(py, name);
347 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
359 //----------------------------------------------------------------------
362 // wxStringPtrList* to python list of strings typemap
363 %typemap(python, out) wxStringPtrList* {
365 $target = PyList_New($source->GetCount());
366 wxStringPtrList::Node *node = $source->GetFirst();
367 for (int i=0; node; i++) {
368 wxString *s = node->GetData();
369 PyList_SetItem($target, i, PyString_FromStringAndSize(s->c_str(), s->Len()));
370 node = node->GetNext();
380 %typemap(python, out) wxPyInputStream* {
383 SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p");
384 $target = Py_BuildValue("s",_ptemp);
393 %name(wxInputStream) class wxPyInputStream {
396 wxPyInputStream(PyObject* p) {
397 wxInputStream* wxi = wxPyCBInputStream::create(p);
399 return new wxPyInputStream(wxi);
407 wxString* read(int size=-1);
408 wxString* readline(int size=-1);
409 wxStringPtrList* readlines(int sizehint=-1);
410 void seek(int offset, int whence=0);
415 void truncate(int size=-1);
416 void write(wxString data);
417 void writelines(wxStringPtrList);
423 // TODO: make a more fully implemented file interface...
424 class wxOutputStream {
429 wxString* read(int size=-1);
430 wxString* readline(int size=-1);
431 wxStringPtrList* readlines(int sizehint=-1);
432 void seek(int offset, int whence=0);
436 void truncate(int size=-1);
437 void write(wxString data);
438 void writelines(wxStringPtrList);
442 void write(const wxString& str) {
443 self->Write(str.c_str(), str.Length());
449 // restore except and typemaps
450 %typemap(python,out) wxStringPtrList*;
451 %typemap(python,out) wxPyInputStream*;
454 //----------------------------------------------------------------------
457 wxPyPtrTypeMap_Add("wxInputStream", "wxPyInputStream");
460 //----------------------------------------------------------------------