]>
Commit | Line | Data |
---|---|---|
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 | } | |
56 | if (_ptr) { | |
57 | char swigptr[64]; | |
58 | SWIG_MakePtr(swigptr, _ptr, "_wxPyInputStream_p"); | |
59 | ||
60 | PyObject* classobj = PyDict_GetItemString(wxPython_dict, "wxInputStreamPtr"); | |
61 | if (! classobj) { | |
62 | Py_INCREF(Py_None); | |
63 | $target = Py_None; | |
64 | } else { | |
65 | PyObject* arg = Py_BuildValue("(s)", swigptr); | |
66 | $target = PyInstance_New(classobj, arg, NULL); | |
67 | Py_DECREF(arg); | |
68 | ||
69 | // set ThisOwn | |
70 | PyObject_SetAttrString($target, "thisown", PyInt_FromLong(1)); | |
71 | } | |
72 | } else { | |
73 | Py_INCREF(Py_None); | |
74 | $target = Py_None; | |
75 | } | |
76 | } | |
77 | ||
78 | //---------------------------------------------------------------------- | |
79 | ||
80 | %{ // C++ | |
81 | // definitions of wxStringPtrList and wxPyInputStream | |
82 | #include <wx/listimpl.cpp> | |
83 | WX_DEFINE_LIST(wxStringPtrList); | |
84 | ||
85 | ||
86 | void wxPyInputStream::close() { | |
87 | /* do nothing */ | |
88 | } | |
89 | ||
90 | void wxPyInputStream::flush() { | |
91 | /*do nothing*/ | |
92 | } | |
93 | ||
94 | bool wxPyInputStream::eof() { | |
95 | if (wxi) | |
96 | return wxi->Eof(); | |
97 | else | |
98 | return TRUE; | |
99 | } | |
100 | ||
101 | wxPyInputStream::~wxPyInputStream() { | |
102 | /*do nothing*/ | |
103 | } | |
104 | ||
105 | wxString* wxPyInputStream::read(int size) { | |
106 | wxString* s = NULL; | |
107 | const int BUFSIZE = 1024; | |
108 | ||
109 | // check if we have a real wxInputStream to work with | |
110 | if (!wxi) { | |
111 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); | |
112 | return NULL; | |
113 | } | |
114 | ||
115 | if (size < 0) { | |
116 | // init buffers | |
117 | char * buf = new char[BUFSIZE]; | |
118 | if (!buf) { | |
119 | PyErr_NoMemory(); | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | s = new wxString(); | |
124 | if (!s) { | |
125 | delete buf; | |
126 | PyErr_NoMemory(); | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | // read until EOF | |
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()); | |
136 | } | |
137 | delete buf; | |
138 | wxPy_END_ALLOW_THREADS; | |
139 | ||
140 | // error check | |
141 | if (wxi->LastError() == wxSTREAM_READ_ERROR) { | |
142 | delete s; | |
143 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
144 | return NULL; | |
145 | } | |
146 | ||
147 | } else { // Read only size number of characters | |
148 | s = new wxString; | |
149 | if (!s) { | |
150 | PyErr_NoMemory(); | |
151 | return NULL; | |
152 | } | |
153 | ||
154 | // read size bytes | |
155 | wxPy_BEGIN_ALLOW_THREADS; | |
156 | wxi->Read(s->GetWriteBuf(size+1), size); | |
157 | s->UngetWriteBuf(wxi->LastRead()); | |
158 | wxPy_END_ALLOW_THREADS; | |
159 | ||
160 | // error check | |
161 | if (wxi->LastError() == wxSTREAM_READ_ERROR) { | |
162 | delete s; | |
163 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
164 | return NULL; | |
165 | } | |
166 | } | |
167 | return s; | |
168 | } | |
169 | ||
170 | ||
171 | wxString* wxPyInputStream::readline (int size) { | |
172 | // check if we have a real wxInputStream to work with | |
173 | if (!wxi) { | |
174 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); | |
175 | return NULL; | |
176 | } | |
177 | ||
178 | // init buffer | |
179 | int i; | |
180 | char ch; | |
181 | wxString* s = new wxString; | |
182 | if (!s) { | |
183 | PyErr_NoMemory(); | |
184 | return NULL; | |
185 | } | |
186 | ||
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(); | |
191 | } | |
192 | wxPy_END_ALLOW_THREADS; | |
193 | ||
194 | // errorcheck | |
195 | if (wxi->LastError() == wxSTREAM_READ_ERROR) { | |
196 | delete s; | |
197 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
198 | return NULL; | |
199 | } | |
200 | return s; | |
201 | } | |
202 | ||
203 | ||
204 | wxStringPtrList* wxPyInputStream::readlines (int sizehint) { | |
205 | // check if we have a real wxInputStream to work with | |
206 | if (!wxi) { | |
207 | PyErr_SetString(PyExc_IOError,"no valid C-wxInputStream below"); | |
208 | return NULL; | |
209 | } | |
210 | ||
211 | // init list | |
212 | wxStringPtrList* l = new wxStringPtrList(); | |
213 | if (!l) { | |
214 | PyErr_NoMemory(); | |
215 | return NULL; | |
216 | } | |
217 | ||
218 | // read sizehint bytes or until EOF | |
219 | wxPy_BEGIN_ALLOW_THREADS; | |
220 | int i; | |
221 | for (i=0; (!wxi->Eof()) && ((sizehint < 0) || (i < sizehint));) { | |
222 | wxString* s = readline(); | |
223 | if (s == NULL) { | |
224 | l->DeleteContents(TRUE); | |
225 | l->Clear(); | |
226 | return NULL; | |
227 | } | |
228 | l->Append(s); | |
229 | i = i + s->Length(); | |
230 | } | |
231 | wxPy_END_ALLOW_THREADS; | |
232 | ||
233 | // error check | |
234 | if (wxi->LastError() == wxSTREAM_READ_ERROR) { | |
235 | l->DeleteContents(TRUE); | |
236 | l->Clear(); | |
237 | PyErr_SetString(PyExc_IOError,"IOError in wxInputStream"); | |
238 | return NULL; | |
239 | } | |
240 | return l; | |
241 | } | |
242 | ||
243 | ||
244 | void wxPyInputStream::seek(int offset, int whence) { | |
245 | if (wxi) | |
246 | wxi->SeekI(offset, wxSeekMode(whence)); | |
247 | } | |
248 | ||
249 | int wxPyInputStream::tell(){ | |
250 | if (wxi) | |
251 | return wxi->TellI(); | |
252 | } | |
253 | ||
254 | ||
255 | ||
256 | // wxInputStream which operates on a Python file-like object | |
257 | class wxPyCBInputStream : public wxInputStream { | |
258 | protected: | |
259 | PyObject* read; | |
260 | PyObject* seek; | |
261 | PyObject* tell; | |
262 | PyObject* py; | |
263 | ||
264 | virtual size_t OnSysRead(void *buffer, size_t bufsize) { | |
265 | if (bufsize == 0) | |
266 | return 0; | |
267 | ||
268 | bool doSave = wxPyRestoreThread(); | |
269 | PyObject* arglist = Py_BuildValue("(i)", bufsize); | |
270 | PyObject* result = PyEval_CallObject(read, arglist); | |
271 | Py_DECREF(arglist); | |
272 | ||
273 | size_t o = 0; | |
274 | if ((result != NULL) && PyString_Check(result)) { | |
275 | o = PyString_Size(result); | |
276 | if (o == 0) | |
277 | m_lasterror = wxSTREAM_EOF; | |
278 | if (o > bufsize) | |
279 | o = bufsize; | |
280 | strncpy((char*)buffer, PyString_AsString(result), o); | |
281 | Py_DECREF(result); | |
282 | ||
283 | } | |
284 | else | |
285 | m_lasterror = wxSTREAM_READ_ERROR; | |
286 | wxPySaveThread(doSave); | |
287 | m_lastcount = o; | |
288 | return o; | |
289 | } | |
290 | ||
291 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize){ | |
292 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
293 | return 0; | |
294 | } | |
295 | ||
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); | |
300 | Py_DECREF(arglist); | |
301 | Py_XDECREF(result); | |
302 | wxPySaveThread(doSave); | |
303 | return OnSysTell(); | |
304 | } | |
305 | ||
306 | virtual off_t OnSysTell() const{ | |
307 | bool doSave = wxPyRestoreThread(); | |
308 | PyObject* arglist = Py_BuildValue("()"); | |
309 | PyObject* result = PyEval_CallObject(tell, arglist); | |
310 | Py_DECREF(arglist); | |
311 | off_t o = 0; | |
312 | if (result != NULL) { | |
313 | o = PyInt_AsLong(result); | |
314 | Py_DECREF(result); | |
315 | }; | |
316 | wxPySaveThread(doSave); | |
317 | return o; | |
318 | } | |
319 | ||
320 | wxPyCBInputStream(PyObject *p, PyObject *r, PyObject *s, PyObject *t) | |
321 | : py(p), read(r), seek(s), tell(t) | |
322 | {} | |
323 | ||
324 | public: | |
325 | ~wxPyCBInputStream() { | |
326 | bool doSave = wxPyRestoreThread(); | |
327 | Py_XDECREF(py); | |
328 | Py_XDECREF(read); | |
329 | Py_XDECREF(seek); | |
330 | Py_XDECREF(tell); | |
331 | wxPySaveThread(doSave); | |
332 | } | |
333 | ||
334 | virtual size_t GetSize() { | |
335 | if (seek && tell) { | |
336 | off_t temp = OnSysTell(); | |
337 | off_t ret = OnSysSeek(0, wxFromEnd); | |
338 | OnSysSeek(temp, wxFromStart); | |
339 | return ret; | |
340 | } | |
341 | else | |
342 | return 0; | |
343 | } | |
344 | ||
345 | static wxPyCBInputStream* create(PyObject *py) { | |
346 | PyObject* read; | |
347 | PyObject* seek; | |
348 | PyObject* tell; | |
349 | ||
350 | if (!PyInstance_Check(py) && !PyFile_Check(py)) { | |
351 | PyErr_SetString(PyExc_TypeError, "Not a file-like object"); | |
352 | Py_XDECREF(py); | |
353 | return NULL; | |
354 | } | |
355 | read = getMethod(py, "read"); | |
356 | seek = getMethod(py, "seek"); | |
357 | tell = getMethod(py, "tell"); | |
358 | ||
359 | if (!read) { | |
360 | PyErr_SetString(PyExc_TypeError, "Not a file-like object"); | |
361 | Py_XDECREF(py); | |
362 | Py_XDECREF(read); | |
363 | Py_XDECREF(seek); | |
364 | Py_XDECREF(tell); | |
365 | return NULL; | |
366 | } | |
367 | return new wxPyCBInputStream(py, read, seek, tell); | |
368 | } | |
369 | ||
370 | static PyObject* getMethod(PyObject* py, char* name) { | |
371 | if (!PyObject_HasAttrString(py, name)) | |
372 | return NULL; | |
373 | PyObject* o = PyObject_GetAttrString(py, name); | |
374 | if (!PyMethod_Check(o) && !PyCFunction_Check(o)) | |
375 | return NULL; | |
376 | return o; | |
377 | } | |
378 | ||
379 | protected: | |
380 | ||
381 | }; | |
382 | ||
383 | %} // End of the C++ | |
384 | //---------------------------------------------------------------------- | |
385 | ||
386 | ||
387 | // block threads for wxPyInputStream **** WHY? | |
388 | %except(python) { | |
389 | $function | |
390 | } | |
391 | ||
392 | ||
393 | // wxStringPtrList* to python list of strings typemap | |
394 | %typemap(python, out) wxStringPtrList* { | |
395 | if ($source) { | |
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(); | |
402 | delete s; | |
403 | } | |
404 | delete $source; | |
405 | } | |
406 | else | |
407 | $target=0; | |
408 | } | |
409 | ||
410 | ||
411 | // transport exceptions via %target=0 | |
412 | %typemap(python, out) wxPyInputStream* { | |
413 | char _ptemp[128]; | |
414 | if ($source) { | |
415 | SWIG_MakePtr(_ptemp, (char *) $source,"_wxPyInputStream_p"); | |
416 | $target = Py_BuildValue("s",_ptemp); | |
417 | } | |
418 | else | |
419 | $target=0; | |
420 | } | |
421 | ||
422 | ||
423 | // transport exceptions via %target=0 | |
424 | %typemap(python, out) wxString* { | |
425 | if ($source) { | |
426 | $target = PyString_FromStringAndSize($source->c_str(), $source->Len()); | |
427 | delete $source; | |
428 | } | |
429 | else | |
430 | $target=0; | |
431 | } | |
432 | ||
433 | ||
434 | ||
435 | %name(wxInputStream) class wxPyInputStream { | |
436 | public: | |
437 | %addmethods { | |
438 | wxPyInputStream(PyObject* p) { | |
439 | wxInputStream* wxi = wxPyCBInputStream::create(p); | |
440 | if (wxi) | |
441 | return new wxPyInputStream(wxi); | |
442 | else | |
443 | return NULL; | |
444 | } | |
445 | } | |
446 | void close(); | |
447 | void flush(); | |
448 | bool eof(); | |
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); | |
453 | int tell(); | |
454 | /* | |
455 | bool isatty(); | |
456 | int fileno(); | |
457 | void truncate(int size=-1); | |
458 | void write(wxString data); | |
459 | void writelines(wxStringPtrList); | |
460 | */ | |
461 | } | |
462 | ||
463 | ||
464 | ||
465 | // TODO: make a more fully implemented file interface... | |
466 | class wxOutputStream { | |
467 | public: | |
468 | /* | |
469 | void close(); | |
470 | void flush(); | |
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); | |
475 | int tell(); | |
476 | bool isatty(); | |
477 | int fileno(); | |
478 | void truncate(int size=-1); | |
479 | void write(wxString data); | |
480 | void writelines(wxStringPtrList); | |
481 | */ | |
482 | ||
483 | %addmethods { | |
484 | void write(const wxString& str) { | |
485 | self->Write(str.c_str(), str.Length()); | |
486 | } | |
487 | } | |
488 | }; | |
489 | ||
490 | ||
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()); | |
496 | } | |
497 | %except(python) { | |
498 | wxPy_BEGIN_ALLOW_THREADS; | |
499 | $function | |
500 | wxPy_END_ALLOW_THREADS; | |
501 | } | |
502 | ||
503 |