Imported Upstream version 2.28.6
[platform/upstream/pygobject2.git] / gio / ginputstream.override
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygobject - Python bindings for GObject
3  * Copyright (C) 2008  Johan Dahlin
4  *
5  *   ginputstream.override: module overrides for GInputStream
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22 %%
23 headers
24 #define BUFSIZE 8192
25
26 %%
27 override g_input_stream_read kwargs
28 static PyObject *
29 _wrap_g_input_stream_read(PyGObject *self, PyObject *args, PyObject *kwargs)
30 {
31     static char *kwlist[] = { "count", "cancellable", NULL };
32     PyGObject *pycancellable = NULL;
33     PyObject *v;
34     GCancellable *cancellable;
35     long count = -1;
36     GError *error = NULL;
37     size_t bytesread, buffersize, chunksize;
38
39     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
40                                      "|lO:InputStream.read",
41                                      kwlist, &count,
42                                      &pycancellable))
43         return NULL;
44
45     buffersize = (count < 0 ? BUFSIZE : count);
46
47     if (!pygio_check_cancellable(pycancellable, &cancellable))
48         return NULL;
49
50     v = PyString_FromStringAndSize((char *)NULL, buffersize);
51     if (v == NULL)
52         return NULL;
53
54     bytesread = 0;
55     for (;;)
56         {
57             pyg_begin_allow_threads;
58             errno = 0;
59             chunksize = g_input_stream_read(G_INPUT_STREAM(self->obj),
60                                             PyString_AS_STRING((PyStringObject *)v) + bytesread,
61                                             buffersize - bytesread, cancellable,
62                                             &error);
63             pyg_end_allow_threads;
64
65             if (pyg_error_check(&error)) {
66                 Py_DECREF(v);
67                 return NULL;
68             }
69             if (chunksize == 0) {
70                 /* End of file. */
71                 break;
72             }
73
74             bytesread += chunksize;
75             if (bytesread < buffersize) {
76                 /* g_input_stream_read() decided to not read full buffer.  We
77                  * then return early too, even if 'count' is less than 0.
78                  */
79                 break;
80             }
81
82             if (count < 0) {
83                 buffersize += BUFSIZE;
84                 if (_PyString_Resize(&v, buffersize) < 0)
85                     return NULL;
86             }
87             else {
88                 /* Got what was requested. */
89                 break;
90             }
91         }
92
93     if (bytesread != buffersize)
94         _PyString_Resize(&v, bytesread);
95
96     return v;
97 }
98 %%
99 override g_input_stream_read_all kwargs
100 static PyObject *
101 _wrap_g_input_stream_read_all(PyGObject *self, PyObject *args, PyObject *kwargs)
102 {
103     static char *kwlist[] = { "count", "cancellable", NULL };
104     PyGObject *pycancellable = NULL;
105     PyObject *v;
106     GCancellable *cancellable;
107     long count = -1;
108     GError *error = NULL;
109     size_t bytesread, buffersize, chunksize;
110
111     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
112                                      "|lO:InputStream.read",
113                                      kwlist, &count,
114                                      &pycancellable))
115         return NULL;
116
117     buffersize = (count < 0 ? BUFSIZE : count);
118
119     if (!pygio_check_cancellable(pycancellable, &cancellable))
120         return NULL;
121
122     v = PyString_FromStringAndSize((char *)NULL, buffersize);
123     if (v == NULL)
124         return NULL;
125
126     bytesread = 0;
127     for (;;)
128         {
129             pyg_begin_allow_threads;
130             errno = 0;
131             g_input_stream_read_all(G_INPUT_STREAM(self->obj),
132                                     PyString_AS_STRING((PyStringObject *)v) + bytesread,
133                                     buffersize - bytesread,
134                                     &chunksize,
135                                     cancellable, &error);
136             pyg_end_allow_threads;
137
138             if (pyg_error_check(&error)) {
139                 Py_DECREF(v);
140                 return NULL;
141             }
142
143             bytesread += chunksize;
144             if (bytesread < buffersize || chunksize == 0) {
145                 /* End of file. */
146                 break;
147             }
148
149             if (count < 0) {
150                 buffersize += BUFSIZE;
151                 if (_PyString_Resize(&v, buffersize) < 0)
152                     return NULL;
153             }
154             else {
155                 /* Got what was requested. */
156                 break;
157             }
158         }
159
160     if (bytesread != buffersize)
161         _PyString_Resize(&v, bytesread);
162
163     return v;
164 }
165 %%
166 override g_input_stream_read_async kwargs
167 static PyObject *
168 _wrap_g_input_stream_read_async(PyGObject *self,
169                                 PyObject *args,
170                                 PyObject *kwargs)
171 {
172     static char *kwlist[] = { "count", "callback", "io_priority",
173                               "cancellable", "user_data", NULL };
174     long count = -1;
175     int io_priority = G_PRIORITY_DEFAULT;
176     PyGObject *pycancellable = NULL;
177     GCancellable *cancellable;
178     PyGIONotify *notify;
179
180     notify = pygio_notify_new();
181
182     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
183                                      "lO|iOO:InputStream.read_async",
184                                      kwlist,
185                                      &count,
186                                      &notify->callback,
187                                      &io_priority,
188                                      &pycancellable,
189                                      &notify->data))
190         goto error;
191
192     if (!pygio_notify_callback_is_valid(notify))
193         goto error;
194
195     if (!pygio_check_cancellable(pycancellable, &cancellable))
196         goto error;
197
198     if (!pygio_notify_allocate_buffer(notify, count))
199         goto error;
200
201     pygio_notify_reference_callback(notify);
202     pygio_notify_attach_to_result(notify);
203
204     g_input_stream_read_async(G_INPUT_STREAM(self->obj),
205                               notify->buffer,
206                               notify->buffer_size,
207                               io_priority,
208                               cancellable,
209                               (GAsyncReadyCallback) async_result_callback_marshal,
210                               notify);
211
212     Py_INCREF(Py_None);
213     return Py_None;
214
215  error:
216     pygio_notify_free(notify);
217     return NULL;
218 }
219 %%
220 override g_input_stream_read_finish kwargs
221 static PyObject *
222 _wrap_g_input_stream_read_finish(PyGObject *self,
223                                  PyObject *args,
224                                  PyObject *kwargs)
225 {
226     static char *kwlist[] = { "result", NULL };
227     PyGObject *result;
228     GError *error = NULL;
229     Py_ssize_t bytesread;
230     PyGIONotify *notify;
231
232     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
233                                      "O!:gio.InputStream.read_finish",
234                                      kwlist, &PyGAsyncResult_Type, &result))
235         return NULL;
236
237     bytesread = g_input_stream_read_finish(G_INPUT_STREAM(self->obj),
238                                            G_ASYNC_RESULT(result->obj), &error);
239
240     if (pyg_error_check(&error))
241         return NULL;
242
243     if (bytesread == 0)
244         return PyString_FromString("");
245
246     notify = pygio_notify_get_attached(result);
247     return PyString_FromStringAndSize(notify->buffer, bytesread);
248 }
249 %%
250 override g_input_stream_close_async kwargs
251 static PyObject *
252 _wrap_g_input_stream_close_async(PyGObject *self,
253                                  PyObject *args,
254                                  PyObject *kwargs)
255 {
256     static char *kwlist[] = { "callback", "io_priority", "cancellable",
257                               "user_data", NULL };
258     int io_priority = G_PRIORITY_DEFAULT;
259     PyGObject *pycancellable = NULL;
260     GCancellable *cancellable;
261     PyGIONotify *notify;
262
263     notify = pygio_notify_new();
264
265     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
266                                      "O|iOO:InputStream.close_async",
267                                      kwlist,
268                                      &notify->callback,
269                                      &io_priority,
270                                      &pycancellable,
271                                      &notify->data))
272         goto error;
273
274     if (!pygio_notify_callback_is_valid(notify))
275         goto error;
276
277     if (!pygio_check_cancellable(pycancellable, &cancellable))
278         goto error;
279
280     pygio_notify_reference_callback(notify);
281
282     g_input_stream_close_async(G_INPUT_STREAM(self->obj),
283                                io_priority,
284                                cancellable,
285                                (GAsyncReadyCallback)async_result_callback_marshal,
286                                notify);
287
288     Py_INCREF(Py_None);
289     return Py_None;
290
291  error:
292     pygio_notify_free(notify);
293     return NULL;
294 }
295 %%
296 override g_input_stream_skip_async kwargs
297 static PyObject *
298 _wrap_g_input_stream_skip_async(PyGObject *self,
299                                 PyObject *args,
300                                 PyObject *kwargs)
301 {
302     static char *kwlist[] = { "count", "callback", "io_priority",
303                               "cancellable", "user_data", NULL };
304     long count = -1;
305     int io_priority = G_PRIORITY_DEFAULT;
306     PyGObject *pycancellable = NULL;
307     GCancellable *cancellable;
308     PyGIONotify *notify;
309
310     notify = pygio_notify_new();
311
312     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
313                                      "lO|iOO:InputStream.skip_async",
314                                      kwlist,
315                                      &count,
316                                      &notify->callback,
317                                      &io_priority,
318                                      &pycancellable,
319                                      &notify->data))
320         goto error;
321
322     if (!pygio_notify_callback_is_valid(notify))
323         goto error;
324
325     if (!pygio_check_cancellable(pycancellable, &cancellable))
326         goto error;
327
328     pygio_notify_reference_callback(notify);
329     
330
331     g_input_stream_skip_async(G_INPUT_STREAM(self->obj),
332                               count,
333                               io_priority,
334                               cancellable,
335                               (GAsyncReadyCallback) async_result_callback_marshal,
336                               notify);
337
338     Py_INCREF(Py_None);
339     return Py_None;
340
341  error:
342     pygio_notify_free(notify);
343     return NULL;
344 }