]>
Commit | Line | Data |
---|---|---|
cbf60e09 RD |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: pyistream.h | |
3 | // Purpose: Classes for managing wxInputStream <--> Python streams | |
4 | // | |
5 | // Author: Robin Dunn | |
6 | // | |
7 | // Created: 25-Sept-2000 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2000 by Total Control Software | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifndef __PYISTREAM__ | |
14 | #define __PYISTREAM__ | |
15 | ||
16 | ||
17 | //---------------------------------------------------------------------- | |
18 | // Handling of wxInputStreams by Joerg Baumann | |
19 | // See stream.i for implementations | |
20 | ||
cbf60e09 RD |
21 | |
22 | // C++ class wxPyInputStream to act as base for python class wxInputStream | |
23 | // You can use it in python like a python file object. | |
24 | class wxPyInputStream { | |
25 | public: | |
26 | // underlying wxInputStream | |
27 | wxInputStream* m_wxis; | |
28 | ||
29 | public: | |
30 | wxPyInputStream(wxInputStream* wxis) : m_wxis(wxis) {} | |
31 | ~wxPyInputStream(); | |
32 | ||
33 | // python file object interface for input files (most of it) | |
34 | void close(); | |
35 | void flush(); | |
36 | bool eof(); | |
a541c325 RD |
37 | PyObject* read(int size=-1); |
38 | PyObject* readline(int size=-1); | |
39 | PyObject* readlines(int sizehint=-1); | |
cbf60e09 RD |
40 | void seek(int offset, int whence=0); |
41 | int tell(); | |
42 | ||
43 | /* do these later? | |
44 | bool isatty(); | |
45 | int fileno(); | |
46 | void truncate(int size=-1); | |
47 | void write(wxString data); | |
48 | void writelines(wxStringPtrList); | |
49 | */ | |
1e4a197e RD |
50 | |
51 | // wxInputStream methods that may come in handy... | |
52 | ||
53 | char Peek() { if (m_wxis) return m_wxis->Peek(); else return -1; } | |
54 | char GetC() { if (m_wxis) return m_wxis->GetC(); else return -1; } | |
55 | size_t LastRead() { if (m_wxis) return m_wxis->LastRead(); else return 0; } | |
dd9f7fea RD |
56 | bool CanRead() { if (m_wxis) return m_wxis->CanRead(); else return False; } |
57 | bool Eof() { if (m_wxis) return m_wxis->Eof(); else return False; } | |
58 | bool Ungetch(char c) { if (m_wxis) return m_wxis->Ungetch(c); else return False; } | |
1e4a197e RD |
59 | |
60 | unsigned long SeekI(unsigned long pos, wxSeekMode mode) | |
61 | { if (m_wxis) return m_wxis->SeekI(pos, mode); else return 0; } | |
62 | unsigned long TellI() { if (m_wxis) return m_wxis->TellI(); else return 0; } | |
cbf60e09 RD |
63 | }; |
64 | ||
65 | ||
66 | ||
67 | // This is a wxInputStream that wraps a Python file-like | |
68 | // object and calls the Python methods as needed. | |
69 | class wxPyCBInputStream : public wxInputStream { | |
70 | public: | |
71 | ~wxPyCBInputStream(); | |
72 | virtual size_t GetSize() const; | |
73 | ||
74 | // factory function | |
dd9f7fea | 75 | static wxPyCBInputStream* create(PyObject *py, bool block=True); |
cbf60e09 RD |
76 | |
77 | protected: | |
78 | // can only be created via the factory | |
79 | wxPyCBInputStream(PyObject *r, PyObject *s, PyObject *t, bool block); | |
80 | ||
81 | // wxStreamBase methods | |
82 | virtual size_t OnSysRead(void *buffer, size_t bufsize); | |
83 | virtual size_t OnSysWrite(const void *buffer, size_t bufsize); | |
84 | virtual off_t OnSysSeek(off_t off, wxSeekMode mode); | |
85 | virtual off_t OnSysTell() const; | |
86 | ||
87 | // helper | |
88 | static PyObject* getMethod(PyObject* py, char* name); | |
89 | ||
90 | PyObject* m_read; | |
91 | PyObject* m_seek; | |
92 | PyObject* m_tell; | |
93 | bool m_block; | |
94 | }; | |
95 | ||
96 | //---------------------------------------------------------------------- | |
97 | #endif |