1/* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygobject - Python bindings for GObject
3 * Copyright (C) 2008  Johan Dahlin
4 * Copyright (C) 2009  Gian Mario Tagliaretti
5 *
6 *   gmemoryinputstream.override: module overrides for GMemoryInputStream
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 * USA
22 */
23%%
24override g_memory_input_stream_add_data kwargs
25static PyObject *
26_wrap_g_memory_input_stream_add_data(PyGObject *self,
27                                     PyObject *args,
28                                     PyObject *kwargs)
29{
30    static char *kwlist[] = { "data", NULL };
31    PyObject *data;
32
33    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:gio.MemoryInputStream.add_data",
34                                     kwlist, &data))
35        return NULL;
36
37    if (data != Py_None) {
38        char *copy;
39        int length;
40
41        if (!PyString_Check(data)) {
42            PyErr_SetString(PyExc_TypeError, "data must be a string or None");
43            return NULL;
44        }
45
46        length = PyString_Size(data);
47        copy = g_malloc(length);
48        memcpy(copy, PyString_AsString(data), length);
49
50        g_memory_input_stream_add_data(G_MEMORY_INPUT_STREAM(self->obj),
51                                       copy, length, (GDestroyNotify) g_free);
52    }
53
54    Py_INCREF(Py_None);
55    return Py_None;
56}
57%%
58override g_memory_input_stream_new_from_data kwargs
59static PyObject *
60_wrap_g_memory_input_stream_new_from_data(PyGObject *self,
61                                          PyObject *args,
62                                          PyObject *kwargs)
63{
64    static char *kwlist[] = { "data", NULL };
65    PyObject *data;
66    GInputStream *stream = NULL;
67
68    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
69                                     "O:gio.memory_input_stream_new_from_data",
70                                     kwlist, &data))
71        return NULL;
72
73    if (data != Py_None) {
74        char *copy;
75        int length;
76
77        if (!PyString_Check(data)) {
78            PyErr_SetString(PyExc_TypeError, "data must be a string or None");
79            return NULL;
80        }
81
82        length = PyString_Size(data);
83        copy = g_malloc(length);
84        memcpy(copy, PyString_AsString(data), length);
85
86        stream = g_memory_input_stream_new_from_data(copy, length,
87                                                      (GDestroyNotify) g_free);
88    }
89
90    return pygobject_new((GObject *)stream);
91}
92