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