]> sigrok.org Git - libsigrokdecode.git/blob - util.c
6f80ff98688ad3457d262c71b5f566db86c281c1
[libsigrokdecode.git] / util.c
1 /*
2  * This file is part of the libsigrokdecode project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23
24 /**
25  * Get the value of a Python object's attribute, returned as a newly
26  * allocated char *.
27  *
28  * @param[in] py_obj The object to probe.
29  * @param[in] attr Name of the attribute to retrieve.
30  * @param[out] outstr ptr to char * storage to be filled in.
31  *
32  * @return SRD_OK upon success, a (negative) error code otherwise.
33  *         The 'outstr' argument points to a g_malloc()ed string upon success.
34  *
35  * @private
36  */
37 SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
38 {
39         PyObject *py_str;
40         int ret;
41
42         if (!PyObject_HasAttrString(py_obj, attr)) {
43                 srd_dbg("Object has no attribute '%s'.", attr);
44                 return SRD_ERR_PYTHON;
45         }
46
47         if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
48                 srd_exception_catch("Failed to get attribute '%s'", attr);
49                 return SRD_ERR_PYTHON;
50         }
51
52         ret = py_str_as_str(py_str, outstr);
53         Py_DECREF(py_str);
54
55         return ret;
56 }
57
58 /**
59  * Get the value of a Python dictionary item, returned as a newly
60  * allocated char *.
61  *
62  * @param[in] py_obj The dictionary to probe.
63  * @param[in] key Key of the item to retrieve.
64  * @param[out] outstr Pointer to char * storage to be filled in.
65  *
66  * @return SRD_OK upon success, a (negative) error code otherwise.
67  *         The 'outstr' argument points to a g_malloc()ed string upon success.
68  *
69  * @private
70  */
71 SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
72                                 char **outstr)
73 {
74         PyObject *py_value;
75
76         if (!PyDict_Check(py_obj)) {
77                 srd_dbg("Object is not a dictionary.");
78                 return SRD_ERR_PYTHON;
79         }
80
81         if (!(py_value = PyDict_GetItemString(py_obj, key))) {
82                 srd_dbg("Dictionary has no attribute '%s'.", key);
83                 return SRD_ERR_PYTHON;
84         }
85
86         return py_str_as_str(py_value, outstr);
87 }
88
89 /**
90  * Get the value of a Python unicode string object, returned as a newly
91  * allocated char *.
92  *
93  * @param[in] py_str The unicode string object.
94  * @param[out] outstr ptr to char * storage to be filled in.
95  *
96  * @return SRD_OK upon success, a (negative) error code otherwise.
97  *         The 'outstr' argument points to a g_malloc()ed string upon success.
98  *
99  * @private
100  */
101 SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
102 {
103         PyObject *py_bytes;
104         char *str;
105
106         if (!PyUnicode_Check(py_str)) {
107                 srd_dbg("Object is not a string object.");
108                 return SRD_ERR_PYTHON;
109         }
110
111         py_bytes = PyUnicode_AsUTF8String(py_str);
112         if (py_bytes) {
113                 str = g_strdup(PyBytes_AsString(py_bytes));
114                 Py_DECREF(py_bytes);
115                 if (str) {
116                         *outstr = str;
117                         return SRD_OK;
118                 }
119         }
120         srd_exception_catch("Failed to extract string");
121
122         return SRD_ERR_PYTHON;
123 }
124
125 /**
126  * Convert a Python list of unicode strings to a C string vector.
127  * On success, a pointer to a newly allocated NULL-terminated array of
128  * allocated C strings is written to @a out_strv. The caller must g_free()
129  * each string and the array itself.
130  *
131  * @param[in] py_strseq The sequence object.
132  * @param[out] out_strv Address of string vector to be filled in.
133  *
134  * @return SRD_OK upon success, a (negative) error code otherwise.
135  *
136  * @private
137  */
138 SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
139 {
140         PyObject *py_item, *py_bytes;
141         char **strv, *str;
142         ssize_t seq_len, i;
143
144         if (!PySequence_Check(py_strseq)) {
145                 srd_err("Object does not provide sequence protocol.");
146                 return SRD_ERR_PYTHON;
147         }
148
149         seq_len = PySequence_Size(py_strseq);
150         if (seq_len < 0) {
151                 srd_exception_catch("Failed to obtain sequence size");
152                 return SRD_ERR_PYTHON;
153         }
154
155         strv = g_try_new0(char *, seq_len + 1);
156         if (!strv) {
157                 srd_err("Failed to allocate result string vector.");
158                 return SRD_ERR_MALLOC;
159         }
160
161         for (i = 0; i < seq_len; i++) {
162                 py_item = PySequence_GetItem(py_strseq, i);
163                 if (!py_item)
164                         goto err_out;
165
166                 if (!PyUnicode_Check(py_item)) {
167                         Py_DECREF(py_item);
168                         goto err_out;
169                 }
170                 py_bytes = PyUnicode_AsUTF8String(py_item);
171                 Py_DECREF(py_item);
172                 if (!py_bytes)
173                         goto err_out;
174
175                 str = g_strdup(PyBytes_AsString(py_bytes));
176                 Py_DECREF(py_bytes);
177                 if (!str)
178                         goto err_out;
179
180                 strv[i] = str;
181         }
182         *out_strv = strv;
183
184         return SRD_OK;
185
186 err_out:
187         g_strfreev(strv);
188         srd_exception_catch("Failed to obtain string item");
189
190         return SRD_ERR_PYTHON;
191 }
192
193 /**
194  * Convert a Python scalar object to a GLib variant.
195  * Supported variant types are string, int64 and double.
196  *
197  * @param[in] py_obj The Python object. Must not be NULL.
198  * @return A floating reference to a new variant, or NULL on failure.
199  */
200 SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
201 {
202         GVariant *var = NULL;
203
204         if (PyUnicode_Check(py_obj)) { /* string */
205                 PyObject *py_bytes;
206                 const char *str;
207
208                 py_bytes = PyUnicode_AsUTF8String(py_obj);
209                 if (py_bytes) {
210                         str = PyBytes_AsString(py_bytes);
211                         if (str)
212                                 var = g_variant_new_string(str);
213                         Py_DECREF(py_bytes);
214                 }
215                 if (!var)
216                         srd_exception_catch("Failed to extract string value");
217
218         } else if (PyLong_Check(py_obj)) { /* integer */
219                 int64_t val;
220
221                 val = PyLong_AsLongLong(py_obj);
222                 if (!PyErr_Occurred())
223                         var = g_variant_new_int64(val);
224                 else
225                         srd_exception_catch("Failed to extract integer value");
226
227         } else if (PyFloat_Check(py_obj)) { /* float */
228                 double val;
229
230                 val = PyFloat_AsDouble(py_obj);
231                 if (!PyErr_Occurred())
232                         var = g_variant_new_double(val);
233                 else
234                         srd_exception_catch("Failed to extract float value");
235
236         } else {
237                 srd_err("Failed to extract value of unsupported type.");
238         }
239
240         return var;
241 }