]> sigrok.org Git - libsigrokdecode.git/blame - util.c
tca6408a: Cosmetics.
[libsigrokdecode.git] / util.c
CommitLineData
b2c19614 1/*
50bd5d25 2 * This file is part of the libsigrokdecode project.
b2c19614
BV
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
4fadb128 5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
b2c19614
BV
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
f6c7eade
MC
21#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "libsigrokdecode.h"
15969949 23#include "config.h"
b2c19614 24
b2c19614 25/**
511e2123 26 * Get the value of a Python object's attribute, returned as a newly
451680f1 27 * allocated char *.
b2c19614 28 *
8b4bbd2a 29 * @param py_obj The object to probe.
451680f1 30 * @param attr Name of the attribute to retrieve.
8b4bbd2a 31 * @param outstr ptr to char * storage to be filled in.
b2c19614
BV
32 *
33 * @return SRD_OK upon success, a (negative) error code otherwise.
34 * The 'outstr' argument points to a malloc()ed string upon success.
57790bc8
UH
35 *
36 * @private
b2c19614 37 */
abeeed8b
UH
38SRD_PRIV int py_attr_as_str(const PyObject *py_obj, const char *attr,
39 char **outstr)
b2c19614 40{
451680f1 41 PyObject *py_str;
b2c19614
BV
42 int ret;
43
abeeed8b 44 if (!PyObject_HasAttrString((PyObject *)py_obj, attr)) {
7a1712c4
UH
45 srd_dbg("%s object has no attribute '%s'.",
46 Py_TYPE(py_obj)->tp_name, attr);
451680f1
BV
47 return SRD_ERR_PYTHON;
48 }
49
abeeed8b 50 if (!(py_str = PyObject_GetAttrString((PyObject *)py_obj, attr))) {
aafeeaea 51 srd_exception_catch("");
451680f1
BV
52 return SRD_ERR_PYTHON;
53 }
54
d42fc6ee 55 if (!PyUnicode_Check(py_str)) {
7a1712c4
UH
56 srd_dbg("%s attribute should be a string, but is a %s.",
57 attr, Py_TYPE(py_str)->tp_name);
d42fc6ee
BV
58 Py_DecRef(py_str);
59 return SRD_ERR_PYTHON;
60 }
61
451680f1 62 ret = py_str_as_str(py_str, outstr);
d42fc6ee 63 Py_DecRef(py_str);
451680f1
BV
64
65 return ret;
66}
67
d42fc6ee 68/**
511e2123 69 * Get the value of a Python dictionary item, returned as a newly
d42fc6ee
BV
70 * allocated char *.
71 *
72 * @param py_obj The dictionary to probe.
29590b14
UH
73 * @param key Key of the item to retrieve.
74 * @param outstr Pointer to char * storage to be filled in.
d42fc6ee
BV
75 *
76 * @return SRD_OK upon success, a (negative) error code otherwise.
77 * The 'outstr' argument points to a malloc()ed string upon success.
57790bc8
UH
78 *
79 * @private
d42fc6ee 80 */
abeeed8b 81SRD_PRIV int py_dictitem_as_str(const PyObject *py_obj, const char *key,
55c3c5f4 82 char **outstr)
d42fc6ee
BV
83{
84 PyObject *py_value;
85 int ret;
86
abeeed8b 87 if (!PyDict_Check((PyObject *)py_obj)) {
c9bfccc6 88 srd_dbg("Object is a %s, not a dictionary.",
abeeed8b 89 Py_TYPE((PyObject *)py_obj)->tp_name);
d42fc6ee
BV
90 return SRD_ERR_PYTHON;
91 }
92
abeeed8b 93 if (!(py_value = PyDict_GetItemString((PyObject *)py_obj, key))) {
7a1712c4 94 srd_dbg("Dictionary has no attribute '%s'.", key);
d42fc6ee
BV
95 return SRD_ERR_PYTHON;
96 }
97
98 if (!PyUnicode_Check(py_value)) {
c9bfccc6
UH
99 srd_dbg("Dictionary value for %s should be a string, but is "
100 "a %s.", key, Py_TYPE(py_value)->tp_name);
d42fc6ee
BV
101 return SRD_ERR_PYTHON;
102 }
103
104 ret = py_str_as_str(py_value, outstr);
105
708b3b84 106 return ret;
d42fc6ee
BV
107}
108
451680f1 109/**
511e2123 110 * Get the value of a Python unicode string object, returned as a newly
451680f1
BV
111 * allocated char *.
112 *
113 * @param py_str The unicode string object.
114 * @param outstr ptr to char * storage to be filled in.
115 *
116 * @return SRD_OK upon success, a (negative) error code otherwise.
117 * The 'outstr' argument points to a malloc()ed string upon success.
57790bc8
UH
118 *
119 * @private
451680f1 120 */
abeeed8b 121SRD_PRIV int py_str_as_str(const PyObject *py_str, char **outstr)
451680f1
BV
122{
123 PyObject *py_encstr;
124 int ret;
125 char *str;
126
127 py_encstr = NULL;
8b4bbd2a
BV
128 str = NULL;
129 ret = SRD_OK;
130
abeeed8b 131 if (!PyUnicode_Check((PyObject *)py_str)) {
c9bfccc6 132 srd_dbg("Object is a %s, not a string object.",
abeeed8b 133 Py_TYPE((PyObject *)py_str)->tp_name);
8b4bbd2a
BV
134 ret = SRD_ERR_PYTHON;
135 goto err_out;
b2c19614
BV
136 }
137
abeeed8b
UH
138 if (!(py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str,
139 "utf-8", NULL))) {
8b4bbd2a
BV
140 ret = SRD_ERR_PYTHON;
141 goto err_out;
5f802ec6
BV
142 }
143 if (!(str = PyBytes_AS_STRING(py_encstr))) {
8b4bbd2a
BV
144 ret = SRD_ERR_PYTHON;
145 goto err_out;
b2c19614
BV
146 }
147
148 if (!(*outstr = g_strdup(str))) {
a61ece20 149 srd_dbg("Failed to g_malloc() outstr.");
b2c19614 150 ret = SRD_ERR_MALLOC;
8b4bbd2a 151 goto err_out;
b2c19614
BV
152 }
153
8b4bbd2a 154err_out:
8b4bbd2a
BV
155 if (py_encstr)
156 Py_XDECREF(py_encstr);
b2c19614 157
451680f1 158 if (PyErr_Occurred()) {
aafeeaea 159 srd_exception_catch("string conversion failed");
451680f1 160 }
b2c19614
BV
161
162 return ret;
163}
164
15969949 165/**
511e2123 166 * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded
56bf4c20 167 * char * array. The caller must g_free() each string when finished.
451680f1
BV
168 *
169 * @param py_strlist The list object.
170 * @param outstr ptr to char ** storage to be filled in.
171 *
172 * @return SRD_OK upon success, a (negative) error code otherwise.
a61ece20 173 * The 'outstr' argument points to a g_malloc()ed char** upon success.
57790bc8
UH
174 *
175 * @private
15969949 176 */
62a2b15c 177SRD_PRIV int py_strseq_to_char(const PyObject *py_strseq, char ***outstr)
15969949
BV
178{
179 PyObject *py_str;
180 int list_len, i;
181 char **out, *str;
182
62a2b15c 183 list_len = PySequence_Size((PyObject *)py_strseq);
a61ece20
UH
184 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
185 srd_err("Failed to g_malloc() 'out'.");
15969949 186 return SRD_ERR_MALLOC;
a61ece20 187 }
15969949 188 for (i = 0; i < list_len; i++) {
451680f1 189 if (!(py_str = PyUnicode_AsEncodedString(
62a2b15c 190 PySequence_GetItem((PyObject *)py_strseq, i), "utf-8", NULL)))
15969949
BV
191 return SRD_ERR_PYTHON;
192 if (!(str = PyBytes_AS_STRING(py_str)))
193 return SRD_ERR_PYTHON;
194 out[i] = g_strdup(str);
195 }
196 out[i] = NULL;
197 *outstr = out;
198
199 return SRD_OK;
200}