]> sigrok.org Git - libsigrokdecode.git/blame - util.c
srd: Drop unused ARRAY_SIZE/ARRAY_AND_SIZE for now.
[libsigrokdecode.git] / util.c
CommitLineData
b2c19614
BV
1/*
2 * This file is part of the sigrok project.
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
73191416 21#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
451680f1 22#include "sigrokdecode-internal.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.
35 */
55c3c5f4 36SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
b2c19614 37{
451680f1 38 PyObject *py_str;
b2c19614
BV
39 int ret;
40
451680f1 41 if (!PyObject_HasAttrString(py_obj, attr)) {
7a1712c4
UH
42 srd_dbg("%s object has no attribute '%s'.",
43 Py_TYPE(py_obj)->tp_name, attr);
451680f1
BV
44 return SRD_ERR_PYTHON;
45 }
46
47 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
2086c684 48 catch_exception("");
451680f1
BV
49 return SRD_ERR_PYTHON;
50 }
51
d42fc6ee 52 if (!PyUnicode_Check(py_str)) {
7a1712c4
UH
53 srd_dbg("%s attribute should be a string, but is a %s.",
54 attr, Py_TYPE(py_str)->tp_name);
d42fc6ee
BV
55 Py_DecRef(py_str);
56 return SRD_ERR_PYTHON;
57 }
58
451680f1 59 ret = py_str_as_str(py_str, outstr);
d42fc6ee 60 Py_DecRef(py_str);
451680f1
BV
61
62 return ret;
63}
64
d42fc6ee 65/**
511e2123 66 * Get the value of a Python dictionary item, returned as a newly
d42fc6ee
BV
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 */
55c3c5f4
UH
76SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
77 char **outstr)
d42fc6ee
BV
78{
79 PyObject *py_value;
80 int ret;
81
82 if (!PyDict_Check(py_obj)) {
c9bfccc6
UH
83 srd_dbg("Object is a %s, not a dictionary.",
84 Py_TYPE(py_obj)->tp_name);
d42fc6ee
BV
85 return SRD_ERR_PYTHON;
86 }
87
88 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
7a1712c4 89 srd_dbg("Dictionary has no attribute '%s'.", key);
d42fc6ee
BV
90 return SRD_ERR_PYTHON;
91 }
92
93 if (!PyUnicode_Check(py_value)) {
c9bfccc6
UH
94 srd_dbg("Dictionary value for %s should be a string, but is "
95 "a %s.", key, Py_TYPE(py_value)->tp_name);
d42fc6ee
BV
96 return SRD_ERR_PYTHON;
97 }
98
99 ret = py_str_as_str(py_value, outstr);
100
708b3b84 101 return ret;
d42fc6ee
BV
102}
103
451680f1 104/**
511e2123 105 * Get the value of a Python unicode string object, returned as a newly
451680f1
BV
106 * allocated char *.
107 *
108 * @param py_str The unicode string object.
109 * @param outstr ptr to char * storage to be filled in.
110 *
111 * @return SRD_OK upon success, a (negative) error code otherwise.
112 * The 'outstr' argument points to a malloc()ed string upon success.
113 */
55c3c5f4 114SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
451680f1
BV
115{
116 PyObject *py_encstr;
117 int ret;
118 char *str;
119
120 py_encstr = NULL;
8b4bbd2a
BV
121 str = NULL;
122 ret = SRD_OK;
123
451680f1 124 if (!PyUnicode_Check(py_str)) {
c9bfccc6
UH
125 srd_dbg("Object is a %s, not a string object.",
126 Py_TYPE(py_str)->tp_name);
8b4bbd2a
BV
127 ret = SRD_ERR_PYTHON;
128 goto err_out;
b2c19614
BV
129 }
130
5f802ec6 131 if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) {
8b4bbd2a
BV
132 ret = SRD_ERR_PYTHON;
133 goto err_out;
5f802ec6
BV
134 }
135 if (!(str = PyBytes_AS_STRING(py_encstr))) {
8b4bbd2a
BV
136 ret = SRD_ERR_PYTHON;
137 goto err_out;
b2c19614
BV
138 }
139
140 if (!(*outstr = g_strdup(str))) {
a61ece20 141 srd_dbg("Failed to g_malloc() outstr.");
b2c19614 142 ret = SRD_ERR_MALLOC;
8b4bbd2a 143 goto err_out;
b2c19614
BV
144 }
145
8b4bbd2a 146err_out:
8b4bbd2a
BV
147 if (py_encstr)
148 Py_XDECREF(py_encstr);
b2c19614 149
451680f1 150 if (PyErr_Occurred()) {
2086c684 151 catch_exception("string conversion failed");
451680f1 152 }
b2c19614
BV
153
154 return ret;
155}
156
15969949 157/**
511e2123 158 * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded
56bf4c20 159 * char * array. The caller must g_free() each string when finished.
451680f1
BV
160 *
161 * @param py_strlist The list object.
162 * @param outstr ptr to char ** storage to be filled in.
163 *
164 * @return SRD_OK upon success, a (negative) error code otherwise.
a61ece20 165 * The 'outstr' argument points to a g_malloc()ed char** upon success.
15969949 166 */
55c3c5f4 167SRD_PRIV int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
15969949
BV
168{
169 PyObject *py_str;
170 int list_len, i;
171 char **out, *str;
172
173 list_len = PyList_Size(py_strlist);
a61ece20
UH
174 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
175 srd_err("Failed to g_malloc() 'out'.");
15969949 176 return SRD_ERR_MALLOC;
a61ece20 177 }
15969949 178 for (i = 0; i < list_len; i++) {
451680f1 179 if (!(py_str = PyUnicode_AsEncodedString(
c9bfccc6 180 PyList_GetItem(py_strlist, i), "utf-8", NULL)))
15969949
BV
181 return SRD_ERR_PYTHON;
182 if (!(str = PyBytes_AS_STRING(py_str)))
183 return SRD_ERR_PYTHON;
184 out[i] = g_strdup(str);
185 }
186 out[i] = NULL;
187 *outstr = out;
188
189 return SRD_OK;
190}