Repository navigation
Expand file tree
/
Copy pathstream_input.cpp
More file actions
162 lines (135 loc) · 4.18 KB
/
Copy pathstream_input.cpp
File metadata and controls
162 lines (135 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//--------------------------------------------------------------------------
static PyObject* wxPyGetMethod(PyObject* py, char* name)
{
if (!PyObject_HasAttrString(py, name))
return NULL;
PyObject* o = PyObject_GetAttrString(py, name);
if (!PyMethod_Check(o) && !PyCFunction_Check(o)) {
Py_DECREF(o);
return NULL;
}
return o;
}
#define wxPyBlock_t_default PyGILState_UNLOCKED
// This class can wrap a Python file-like object and allow it to be used
// as a wxInputStream.
class wxPyInputStream : public wxInputStream
{
public:
// Make sure there is at least a read method
static bool Check(PyObject* fileObj)
{
PyObject* method = wxPyGetMethod(fileObj, "read");
bool rval = method != NULL;
Py_XDECREF(method);
return rval;
}
wxPyInputStream(PyObject* fileObj, bool block=true)
{
m_block = block;
wxPyThreadBlocker blocker(m_block);
m_read = wxPyGetMethod(fileObj, "read");
m_seek = wxPyGetMethod(fileObj, "seek");
m_tell = wxPyGetMethod(fileObj, "tell");
}
virtual ~wxPyInputStream()
{
wxPyThreadBlocker blocker(m_block);
Py_XDECREF(m_read);
Py_XDECREF(m_seek);
Py_XDECREF(m_tell);
}
wxPyInputStream(const wxPyInputStream& other)
{
wxPyThreadBlocker blocker;
m_read = other.m_read;
m_seek = other.m_seek;
m_tell = other.m_tell;
m_block = other.m_block;
Py_INCREF(m_read);
Py_INCREF(m_seek);
Py_INCREF(m_tell);
}
protected:
// implement base class virtuals
wxFileOffset GetLength() const
{
wxPyInputStream* self = (wxPyInputStream*)this; // cast off const
if (m_seek && m_tell) {
wxFileOffset temp = self->OnSysTell();
wxFileOffset ret = self->OnSysSeek(0, wxFromEnd);
self->OnSysSeek(temp, wxFromStart);
return ret;
}
else
return wxInvalidOffset;
}
size_t OnSysRead(void *buffer, size_t bufsize)
{
if (bufsize == 0)
return 0;
wxPyThreadBlocker blocker;
PyObject* arglist = Py_BuildValue("(i)", bufsize);
PyObject* result = PyEval_CallObject(m_read, arglist);
Py_DECREF(arglist);
size_t o = 0;
if ((result != NULL) && PyBytes_Check(result)) {
o = PyBytes_Size(result);
if (o == 0)
m_lasterror = wxSTREAM_EOF;
if (o > bufsize)
o = bufsize;
memcpy((char*)buffer, PyBytes_AsString(result), o); // strings only, not unicode...
Py_DECREF(result);
}
else
m_lasterror = wxSTREAM_READ_ERROR;
return o;
}
size_t OnSysWrite(const void *buffer, size_t bufsize)
{
m_lasterror = wxSTREAM_WRITE_ERROR;
return 0;
}
wxFileOffset OnSysSeek(wxFileOffset off, wxSeekMode mode)
{
wxPyThreadBlocker blocker;
PyObject* arglist = PyTuple_New(2);
if (sizeof(wxFileOffset) > sizeof(long))
// wxFileOffset is a 64-bit value...
PyTuple_SET_ITEM(arglist, 0, PyLong_FromLongLong(off));
else
PyTuple_SET_ITEM(arglist, 0, wxPyInt_FromLong(off));
PyTuple_SET_ITEM(arglist, 1, wxPyInt_FromLong(mode));
PyObject* result = PyEval_CallObject(m_seek, arglist);
Py_DECREF(arglist);
Py_XDECREF(result);
return OnSysTell();
}
wxFileOffset OnSysTell() const
{
wxPyThreadBlocker blocker;
PyObject* arglist = Py_BuildValue("()");
PyObject* result = PyEval_CallObject(m_tell, arglist);
Py_DECREF(arglist);
wxFileOffset o = 0;
if (result != NULL) {
if (PyLong_Check(result))
o = PyLong_AsLongLong(result);
else
o = wxPyInt_AsLong(result);
Py_DECREF(result);
};
return o;
}
bool IsSeekable() const
{
return (m_seek != NULL);
}
private:
PyObject* m_read;
PyObject* m_seek;
PyObject* m_tell;
bool m_block;
};
//--------------------------------------------------------------------------