]> git.saurik.com Git - wxWidgets.git/blame - wxPython/src/streams.i
corrections to dynamic loading for Darwin
[wxWidgets.git] / wxPython / src / streams.i
CommitLineData
c368d904
RD
1/////////////////////////////////////////////////////////////////////////////
2// Name: streams.i
3// Purpose: SWIG definitions of the wxFileSystem family of classes
4//
5// Author: Joerg Baumann
6//
7// Created: 25-Sept-2000
8// RCS-ID: $Id$
9// Copyright: (c) 2000 by Joerg Baumann
10// Licence: wxWindows license
11/////////////////////////////////////////////////////////////////////////////
12
13%module streams
14
15%{
16#include "helpers.h"
17#include <wx/stream.h>
18#include <wx/list.h>
19%}
20
21//----------------------------------------------------------------------
22
23%include typemaps.i
24%include my_typemaps.i
25
26// Import some definitions of other classes, etc.
27%import _defs.i
28
29%pragma(python) code = "import wx"
30%pragma(python) code = "import string"
31
32//----------------------------------------------------------------------
33// typemaps for wxInputStream
34%typemap(python,in) wxInputStream *stream {
35 if (PyInstance_Check($source)) {
36 wxPyInputStream* ptr;
37 if (SWIG_GetPtrObj($source, (void **) &ptr,"_wxPyInputStream_p")) {
38 PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p.");
39 return NULL;
40 }
41 $target = ptr->wxi;
42 } else {
43 PyErr_SetString(PyExc_TypeError,"Expected _wxInputStream_p.");
44 return NULL;
45 }
46}
47
48
49// typemaps for wxInputStream
50%typemap(python,out) wxInputStream* {
51 wxPyInputStream * _ptr = NULL;
52
53 if ($source) {
54 _ptr = new wxPyInputStream($source);
55 }
9416aa89 56 $target = wxPyConstructObject(_ptr, "wxInputStream", TRUE);
c368d904
RD
57}
58
59//----------------------------------------------------------------------
60
61%{ // C++
62// definitions of wxStringPtrList and wxPyInputStream
63#include <wx/listimpl.cpp>
64WX_DEFINE_LIST(wxStringPtrList);
65
66
67void wxPyInputStream::close() {
68 /* do nothing */
69}
70
71void wxPyInputStream::flush() {
72 /*do nothing*/
73}
74
75bool wxPyInputStream::eof() {
76 if (wxi)
77 return wxi->Eof();
78 else
79 return TRUE;
80}
81
82wxPyInputStream::~wxPyInputStream() {
83 /*do nothing*/
84}
85
86wxString* wxPyInputStream::read(int size) {
87 wxString* s = NULL;
88 const int BUFSIZE = 1024;
89
90 // check if we have a real wxInputStream to work with
91 if (!wxi) {
92 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
93 return NULL;
94 }
95
96 if (size < 0) {
97 // init buffers
98 char * buf = new char[BUFSIZE];
99 if (!buf) {
100 PyErr_NoMemory();
101 return NULL;
102 }
103
104 s = new wxString();
105 if (!s) {
106 delete buf;
107 PyErr_NoMemory();
108 return NULL;
109 }
110
111 // read until EOF
c368d904
RD
112 while (! wxi->Eof()) {
113 wxi->Read(buf, BUFSIZE);
114 //*s += wxString(buf, wxi->LastRead());
115 s->Append(buf, wxi->LastRead());
116 }
117 delete buf;
c368d904
RD
118
119 // error check
120 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
121 delete s;
122 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
123 return NULL;
124 }
125
126 } else { // Read only size number of characters
127 s = new wxString;
128 if (!s) {
129 PyErr_NoMemory();
130 return NULL;
131 }
132
133 // read size bytes
c368d904
RD
134 wxi->Read(s->GetWriteBuf(size+1), size);
135 s->UngetWriteBuf(wxi->LastRead());
c368d904
RD
136
137 // error check
138 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
139 delete s;
140 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
141 return NULL;
142 }
143 }
144 return s;
145}
146
147
148wxString* wxPyInputStream::readline (int size) {
149 // check if we have a real wxInputStream to work with
150 if (!wxi) {
151 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
152 return NULL;
153 }
154
155 // init buffer
156 int i;
157 char ch;
158 wxString* s = new wxString;
159 if (!s) {
160 PyErr_NoMemory();
161 return NULL;
162 }
163
164 // read until \n or byte limit reached
c368d904
RD
165 for (i=ch=0; (ch != '\n') && (!wxi->Eof()) && ((size < 0) || (i < size)); i++) {
166 *s += ch = wxi->GetC();
167 }
c368d904
RD
168
169 // errorcheck
170 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
171 delete s;
172 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
173 return NULL;
174 }
175 return s;
176}
177
178
179wxStringPtrList* wxPyInputStream::readlines (int sizehint) {
180 // check if we have a real wxInputStream to work with
181 if (!wxi) {
182 PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below");
183 return NULL;
184 }
185
186 // init list
187 wxStringPtrList* l = new wxStringPtrList();
188 if (!l) {
189 PyErr_NoMemory();
190 return NULL;
191 }
192
193 // read sizehint bytes or until EOF
c368d904
RD
194 int i;
195 for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) {
196 wxString* s = readline();
197 if (s == NULL) {
198 l->DeleteContents(TRUE);
199 l->Clear();
200 return NULL;
201 }
202 l->Append(s);
203 i = i + s->Length();
204 }
c368d904
RD
205
206 // error check
207 if (wxi->LastError() == wxSTREAM_READ_ERROR) {
208 l->DeleteContents(TRUE);
209 l->Clear();
210 PyErr_SetString(PyExc_IOError,"IOError in wxInputStream");
211 return NULL;
212 }
213 return l;
214}
215
216
217void wxPyInputStream::seek(int offset, int whence) {
218 if (wxi)
219 wxi->SeekI(offset, wxSeekMode(whence));
220}
221
222int wxPyInputStream::tell(){
223 if (wxi)
224 return wxi->TellI();
225}
226
227
228
229// wxInputStream which operates on a Python file-like object
230class wxPyCBInputStream : public wxInputStream {
231protected:
232 PyObject* read;
233 PyObject* seek;
234 PyObject* tell;
235 PyObject* py;
236
237 virtual size_t OnSysRead(void *buffer, size_t bufsize) {
238 if (bufsize == 0)
239 return 0;
240
4268f798 241 wxPyBeginBlockThreads();
c368d904
RD
242 PyObject* arglist = Py_BuildValue("(i)", bufsize);
243 PyObject* result = PyEval_CallObject(read, arglist);
244 Py_DECREF(arglist);
245
246 size_t o = 0;
247 if ((result != NULL) && PyString_Check(result)) {
248 o = PyString_Size(result);
249 if (o == 0)
250 m_lasterror = wxSTREAM_EOF;
251 if (o > bufsize)
252 o = bufsize;
253 strncpy((char*)buffer, PyString_AsString(result), o);
254 Py_DECREF(result);
255
256 }
257 else
258 m_lasterror = wxSTREAM_READ_ERROR;
4268f798 259 wxPyEndBlockThreads();
c368d904
RD
260 m_lastcount = o;
261 return o;
262 }
263
264 virtual size_t OnSysWrite(const void *buffer, size_t bufsize){
265 m_lasterror = wxSTREAM_WRITE_ERROR;
266 return 0;
267 }
268
269 virtual off_t OnSysSeek(off_t off, wxSeekMode mode){
4268f798 270 wxPyBeginBlockThreads();
c368d904
RD
271 PyObject*arglist = Py_BuildValue("(ii)", off, mode);
272 PyObject*result = PyEval_CallObject(seek, arglist);
273 Py_DECREF(arglist);
274 Py_XDECREF(result);
4268f798 275 wxPyEndBlockThreads();
c368d904
RD
276 return OnSysTell();
277 }
278
279 virtual off_t OnSysTell() const{
4268f798 280 wxPyBeginBlockThreads();
c368d904
RD
281 PyObject* arglist = Py_BuildValue("()");
282 PyObject* result = PyEval_CallObject(tell, arglist);
283 Py_DECREF(arglist);
284 off_t o = 0;
285 if (result != NULL) {
286 o = PyInt_AsLong(result);
287 Py_DECREF(result);
288 };
4268f798 289 wxPyEndBlockThreads();
c368d904
RD
290 return o;
291 }
292
293 wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t)
294 : py(p), read(r), seek(s), tell(t)
295 {}
296
297public:
298 ~wxPyCBInputStream() {
4268f798 299 wxPyBeginBlockThreads();
c368d904
RD
300 Py_XDECREF(py);
301 Py_XDECREF(read);
302 Py_XDECREF(seek);
303 Py_XDECREF(tell);
4268f798 304 wxPyEndBlockThreads();
c368d904
RD
305 }
306
307 virtual size_t GetSize() {
308 if (seek && tell) {
309 off_t temp = OnSysTell();
310 off_t ret = OnSysSeek(0, wxFromEnd);
311 OnSysSeek(temp, wxFromStart);
312 return ret;
313 }
314 else
315 return 0;
316 }
317
318 static wxPyCBInputStream* create(PyObject *py) {
319 PyObject* read;
320 PyObject* seek;
321 PyObject* tell;
322
323 if (!PyInstance_Check(py) && !PyFile_Check(py)) {
324 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
325 Py_XDECREF(py);
326 return NULL;
327 }
328 read = getMethod(py, "read");
329 seek = getMethod(py, "seek");
330 tell = getMethod(py, "tell");
331
332 if (!read) {
333 PyErr_SetString(PyExc_TypeError, "Not a file-like object");
334 Py_XDECREF(py);
335 Py_XDECREF(read);
336 Py_XDECREF(seek);
337 Py_XDECREF(tell);
338 return NULL;
339 }
340 return new wxPyCBInputStream(py, read, seek, tell);
341 }
342
343 static PyObject* getMethod(PyObject* py, char* name) {
344 if (!PyObject_HasAttrString(py, name))
345 return NULL;
346 PyObject* o = PyObject_GetAttrString(py, name);
de20db99
RD
347 if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
348 Py_DECREF(o);
c368d904 349 return NULL;
de20db99 350 }
c368d904
RD
351 return o;
352 }
353
354protected:
355
356};
357
358%} // End of the C++
359//----------------------------------------------------------------------
360
361
c368d904
RD
362// wxStringPtrList* to python list of strings typemap
363%typemap(python, out) wxStringPtrList* {
364 if ($source) {
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();
371 delete s;
372 }
373 delete $source;
374 }
375 else
376 $target=0;
377}
378
379
c368d904
RD
380%typemap(python, out) wxPyInputStream* {
381 char _ptemp[128];
382 if ($source) {
383 SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p");
384 $target = Py_BuildValue("s",_ptemp);
385 }
386 else
387 $target=0;
388}
389
390
c368d904
RD
391
392
393%name(wxInputStream) class wxPyInputStream {
394public:
395 %addmethods {
396 wxPyInputStream(PyObject* p) {
397 wxInputStream* wxi = wxPyCBInputStream::create(p);
398 if (wxi)
399 return new wxPyInputStream(wxi);
400 else
401 return NULL;
402 }
403 }
404 void close();
405 void flush();
406 bool eof();
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);
411 int tell();
412 /*
413 bool isatty();
414 int fileno();
415 void truncate(int size=-1);
416 void write(wxString data);
417 void writelines(wxStringPtrList);
418 */
419}
420
421
422
423// TODO: make a more fully implemented file interface...
424class wxOutputStream {
425public:
426 /*
427 void close();
428 void flush();
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);
433 int tell();
434 bool isatty();
435 int fileno();
436 void truncate(int size=-1);
437 void write(wxString data);
438 void writelines(wxStringPtrList);
439 */
440
441 %addmethods {
442 void write(const wxString& str) {
443 self->Write(str.c_str(), str.Length());
444 }
445 }
446};
447
448
449// restore except and typemaps
450%typemap(python,out) wxStringPtrList*;
451%typemap(python,out) wxPyInputStream*;
c368d904
RD
452
453
9416aa89
RD
454//----------------------------------------------------------------------
455
456%init %{
457 wxPyPtrTypeMap_Add("wxInputStream", "wxPyInputStream");
458%}
459
460//----------------------------------------------------------------------