]> sigrok.org Git - libsigrokdecode.git/blob - util.c
060c25387df580d3df6f32597132eacbbaaf5f77
[libsigrokdecode.git] / util.c
1 /*
2  * This file is part of the sigrok 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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "sigrokdecode-internal.h"
23 #include "config.h"
24
25
26 /**
27  * Get the value of a python object's attribute, returned as a newly
28  * allocated char *.
29  *
30  * @param py_obj The object to probe.
31  * @param attr Name of the attribute to retrieve.
32  * @param outstr ptr to char * storage to be filled in.
33  *
34  * @return SRD_OK upon success, a (negative) error code otherwise.
35  *         The 'outstr' argument points to a malloc()ed string upon success.
36  */
37 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("srd: %s object has no attribute '%s'.",
44                                 Py_TYPE(py_obj)->tp_name, attr);
45                 return SRD_ERR_PYTHON;
46         }
47
48         if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
49                 /* TODO: report exception message/traceback to err/dbg */
50                 PyErr_Clear();
51                 return SRD_ERR_PYTHON;
52         }
53
54         if (!PyUnicode_Check(py_str)) {
55                 srd_dbg("srd: %s attribute should be a string, but is a %s.",
56                                 attr, Py_TYPE(py_str)->tp_name);
57                 Py_DecRef(py_str);
58                 return SRD_ERR_PYTHON;
59         }
60
61         ret = py_str_as_str(py_str, outstr);
62         Py_DecRef(py_str);
63
64         return ret;
65 }
66
67
68 /**
69  * Get the value of a python dictionary item, returned as a newly
70  * allocated char *.
71  *
72  * @param py_obj The dictionary to probe.
73  * @param attr Key of the item to retrieve.
74  * @param outstr ptr to char * storage to be filled in.
75  *
76  * @return SRD_OK upon success, a (negative) error code otherwise.
77  *         The 'outstr' argument points to a malloc()ed string upon success.
78  */
79 int py_dictitem_as_str(PyObject *py_obj, const char *key, char **outstr)
80 {
81         PyObject *py_value;
82         int ret;
83
84         if (!PyDict_Check(py_obj)) {
85                 srd_dbg("srd: Object is a %s, not a dictionary.", Py_TYPE(py_obj)->tp_name);
86                 return SRD_ERR_PYTHON;
87         }
88
89         if (!(py_value = PyDict_GetItemString(py_obj, key))) {
90                 srd_dbg("srd: Dictionary has no attribute '%s'.", key);
91                 return SRD_ERR_PYTHON;
92         }
93
94         if (!PyUnicode_Check(py_value)) {
95                 srd_dbg("srd: Dictionary value for %s should be a string, but is a %s.",
96                                 key, Py_TYPE(py_value)->tp_name);
97                 return SRD_ERR_PYTHON;
98         }
99
100         ret = py_str_as_str(py_value, outstr);
101
102         return SRD_OK;
103 }
104
105
106 /**
107  * Get the value of a python unicode string object, returned as a newly
108  * allocated char *.
109  *
110  * @param py_str The unicode string object.
111  * @param outstr ptr to char * storage to be filled in.
112  *
113  * @return SRD_OK upon success, a (negative) error code otherwise.
114  *         The 'outstr' argument points to a malloc()ed string upon success.
115  */
116 int py_str_as_str(PyObject *py_str, char **outstr)
117 {
118         PyObject *py_encstr;
119         int ret;
120         char *str;
121
122         py_encstr = NULL;
123         str = NULL;
124         ret = SRD_OK;
125
126         if (!PyUnicode_Check(py_str)) {
127                 srd_dbg("srd: object is a %s, not a string object", Py_TYPE(py_str)->tp_name);
128                 ret = SRD_ERR_PYTHON;
129                 goto err_out;
130         }
131
132         if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) {
133                 ret = SRD_ERR_PYTHON;
134                 goto err_out;
135         }
136         if (!(str = PyBytes_AS_STRING(py_encstr))) {
137                 ret = SRD_ERR_PYTHON;
138                 goto err_out;
139         }
140
141         if (!(*outstr = g_strdup(str))) {
142                 srd_dbg("srd: malloc failed");
143                 ret = SRD_ERR_MALLOC;
144                 goto err_out;
145         }
146
147 err_out:
148         if (py_encstr)
149                 Py_XDECREF(py_encstr);
150
151         if (PyErr_Occurred()) {
152                 srd_dbg("srd: string conversion failed");
153                 /* TODO: dump exception to srd_dbg */
154                 PyErr_Clear();
155         }
156
157         return ret;
158 }
159
160
161 /**
162  * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded
163  * char * array. The caller must free each string when finished.
164  *
165  * @param py_strlist The list object.
166  * @param outstr ptr to char ** storage to be filled in.
167  *
168  * @return SRD_OK upon success, a (negative) error code otherwise.
169  *         The 'outstr' argument points to a malloc()ed char ** upon success.
170  */
171 int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
172 {
173         PyObject *py_str;
174         int list_len, i;
175         char **out, *str;
176
177         list_len = PyList_Size(py_strlist);
178         if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1))))
179                 return SRD_ERR_MALLOC;
180         for (i = 0; i < list_len; i++) {
181                 if (!(py_str = PyUnicode_AsEncodedString(
182                                 PyList_GetItem(py_strlist, i), "utf-8", NULL)))
183                         return SRD_ERR_PYTHON;
184                 if (!(str = PyBytes_AS_STRING(py_str)))
185                         return SRD_ERR_PYTHON;
186                 out[i] = g_strdup(str);
187         }
188         out[i] = NULL;
189         *outstr = out;
190
191         return SRD_OK;
192 }
193