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