configure.ac: Bump package version to 0.3.1.
[libsigrokdecode/gsi.git] / util.c
blob1efaa843a603988bece2f59a9680ef06d71c08c8
1 /*
2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "libsigrokdecode-internal.h"
23 #include "config.h"
25 /**
26 * Get the value of a Python object's attribute, returned as a newly
27 * allocated char *.
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.
33 * @return SRD_OK upon success, a (negative) error code otherwise.
34 * The 'outstr' argument points to a malloc()ed string upon success.
36 * @private
38 SRD_PRIV int py_attr_as_str(const PyObject *py_obj, const char *attr,
39 char **outstr)
41 PyObject *py_str;
42 int ret;
44 if (!PyObject_HasAttrString((PyObject *)py_obj, attr)) {
45 srd_dbg("%s object has no attribute '%s'.",
46 Py_TYPE(py_obj)->tp_name, attr);
47 return SRD_ERR_PYTHON;
50 if (!(py_str = PyObject_GetAttrString((PyObject *)py_obj, attr))) {
51 srd_exception_catch("");
52 return SRD_ERR_PYTHON;
55 if (!PyUnicode_Check(py_str)) {
56 srd_dbg("%s attribute should be a string, but is a %s.",
57 attr, Py_TYPE(py_str)->tp_name);
58 Py_DecRef(py_str);
59 return SRD_ERR_PYTHON;
62 ret = py_str_as_str(py_str, outstr);
63 Py_DecRef(py_str);
65 return ret;
68 /**
69 * Get the value of a Python dictionary item, returned as a newly
70 * allocated char *.
72 * @param py_obj The dictionary to probe.
73 * @param key Key of the item to retrieve.
74 * @param outstr Pointer to char * storage to be filled in.
76 * @return SRD_OK upon success, a (negative) error code otherwise.
77 * The 'outstr' argument points to a malloc()ed string upon success.
79 * @private
81 SRD_PRIV int py_dictitem_as_str(const PyObject *py_obj, const char *key,
82 char **outstr)
84 PyObject *py_value;
85 int ret;
87 if (!PyDict_Check((PyObject *)py_obj)) {
88 srd_dbg("Object is a %s, not a dictionary.",
89 Py_TYPE((PyObject *)py_obj)->tp_name);
90 return SRD_ERR_PYTHON;
93 if (!(py_value = PyDict_GetItemString((PyObject *)py_obj, key))) {
94 srd_dbg("Dictionary has no attribute '%s'.", key);
95 return SRD_ERR_PYTHON;
98 if (!PyUnicode_Check(py_value)) {
99 srd_dbg("Dictionary value for %s should be a string, but is "
100 "a %s.", key, Py_TYPE(py_value)->tp_name);
101 return SRD_ERR_PYTHON;
104 ret = py_str_as_str(py_value, outstr);
106 return ret;
110 * Get the value of a Python unicode string object, returned as a newly
111 * allocated char *.
113 * @param py_str The unicode string object.
114 * @param outstr ptr to char * storage to be filled in.
116 * @return SRD_OK upon success, a (negative) error code otherwise.
117 * The 'outstr' argument points to a malloc()ed string upon success.
119 * @private
121 SRD_PRIV int py_str_as_str(const PyObject *py_str, char **outstr)
123 PyObject *py_encstr;
124 int ret;
125 char *str;
127 py_encstr = NULL;
128 str = NULL;
129 ret = SRD_OK;
131 if (!PyUnicode_Check((PyObject *)py_str)) {
132 srd_dbg("Object is a %s, not a string object.",
133 Py_TYPE((PyObject *)py_str)->tp_name);
134 ret = SRD_ERR_PYTHON;
135 goto err_out;
138 if (!(py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str,
139 "utf-8", NULL))) {
140 ret = SRD_ERR_PYTHON;
141 goto err_out;
143 if (!(str = PyBytes_AS_STRING(py_encstr))) {
144 ret = SRD_ERR_PYTHON;
145 goto err_out;
148 if (!(*outstr = g_strdup(str))) {
149 srd_dbg("Failed to g_malloc() outstr.");
150 ret = SRD_ERR_MALLOC;
151 goto err_out;
154 err_out:
155 if (py_encstr)
156 Py_XDECREF(py_encstr);
158 if (PyErr_Occurred()) {
159 srd_exception_catch("string conversion failed");
162 return ret;
166 * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded
167 * char * array. The caller must g_free() each string when finished.
169 * @param py_strlist The list object.
170 * @param outstr ptr to char ** storage to be filled in.
172 * @return SRD_OK upon success, a (negative) error code otherwise.
173 * The 'outstr' argument points to a g_malloc()ed char** upon success.
175 * @private
177 SRD_PRIV int py_strseq_to_char(const PyObject *py_strseq, char ***outstr)
179 PyObject *py_str;
180 int list_len, i;
181 char **out, *str;
183 list_len = PySequence_Size((PyObject *)py_strseq);
184 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
185 srd_err("Failed to g_malloc() 'out'.");
186 return SRD_ERR_MALLOC;
188 for (i = 0; i < list_len; i++) {
189 if (!(py_str = PyUnicode_AsEncodedString(
190 PySequence_GetItem((PyObject *)py_strseq, i), "utf-8", NULL)))
191 return SRD_ERR_PYTHON;
192 if (!(str = PyBytes_AS_STRING(py_str)))
193 return SRD_ERR_PYTHON;
194 out[i] = g_strdup(str);
196 out[i] = NULL;
197 *outstr = out;
199 return SRD_OK;