]> sigrok.org Git - libsigrokdecode.git/blame - util.c
autogen.sh: aclocal support for Windows XP/Vista/7.
[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
BV
24
25
26/**
451680f1
BV
27 * Get the value of a python object's attribute, returned as a newly
28 * allocated char *.
b2c19614 29 *
8b4bbd2a 30 * @param py_obj The object to probe.
451680f1 31 * @param attr Name of the attribute to retrieve.
8b4bbd2a 32 * @param outstr ptr to char * storage to be filled in.
b2c19614
BV
33 *
34 * @return SRD_OK upon success, a (negative) error code otherwise.
35 * The 'outstr' argument points to a malloc()ed string upon success.
36 */
451680f1 37int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
b2c19614 38{
451680f1 39 PyObject *py_str;
b2c19614
BV
40 int ret;
41
451680f1 42 if (!PyObject_HasAttrString(py_obj, attr)) {
7a1712c4
UH
43 srd_dbg("%s object has no attribute '%s'.",
44 Py_TYPE(py_obj)->tp_name, attr);
451680f1
BV
45 return SRD_ERR_PYTHON;
46 }
47
48 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
2086c684 49 catch_exception("");
451680f1
BV
50 return SRD_ERR_PYTHON;
51 }
52
d42fc6ee 53 if (!PyUnicode_Check(py_str)) {
7a1712c4
UH
54 srd_dbg("%s attribute should be a string, but is a %s.",
55 attr, Py_TYPE(py_str)->tp_name);
d42fc6ee
BV
56 Py_DecRef(py_str);
57 return SRD_ERR_PYTHON;
58 }
59
451680f1 60 ret = py_str_as_str(py_str, outstr);
d42fc6ee 61 Py_DecRef(py_str);
451680f1
BV
62
63 return ret;
64}
65
66
d42fc6ee
BV
67/**
68 * Get the value of a python dictionary item, returned as a newly
69 * allocated char *.
70 *
71 * @param py_obj The dictionary to probe.
72 * @param attr Key of the item to retrieve.
73 * @param outstr ptr to char * storage to be filled in.
74 *
75 * @return SRD_OK upon success, a (negative) error code otherwise.
76 * The 'outstr' argument points to a malloc()ed string upon success.
77 */
78int py_dictitem_as_str(PyObject *py_obj, const char *key, char **outstr)
79{
80 PyObject *py_value;
81 int ret;
82
83 if (!PyDict_Check(py_obj)) {
7a1712c4 84 srd_dbg("Object is a %s, not a dictionary.", 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)) {
7a1712c4
UH
94 srd_dbg("Dictionary value for %s should be a string, but is a %s.",
95 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
101 return SRD_OK;
102}
103
104
451680f1
BV
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 */
115int py_str_as_str(PyObject *py_str, char **outstr)
116{
117 PyObject *py_encstr;
118 int ret;
119 char *str;
120
121 py_encstr = NULL;
8b4bbd2a
BV
122 str = NULL;
123 ret = SRD_OK;
124
451680f1 125 if (!PyUnicode_Check(py_str)) {
7a1712c4 126 srd_dbg("Object is a %s, not a string object.", 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))) {
7a1712c4 141 srd_dbg("outstr malloc failed");
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
451680f1 157
15969949
BV
158/**
159 * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded
160 * char * array. The caller must free each string when finished.
451680f1
BV
161 *
162 * @param py_strlist The list object.
163 * @param outstr ptr to char ** storage to be filled in.
164 *
165 * @return SRD_OK upon success, a (negative) error code otherwise.
166 * The 'outstr' argument points to a malloc()ed char ** upon success.
15969949
BV
167 */
168int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
169{
170 PyObject *py_str;
171 int list_len, i;
172 char **out, *str;
173
174 list_len = PyList_Size(py_strlist);
175 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1))))
176 return SRD_ERR_MALLOC;
177 for (i = 0; i < list_len; i++) {
451680f1
BV
178 if (!(py_str = PyUnicode_AsEncodedString(
179 PyList_GetItem(py_strlist, i), "utf-8", NULL)))
15969949
BV
180 return SRD_ERR_PYTHON;
181 if (!(str = PyBytes_AS_STRING(py_str)))
182 return SRD_ERR_PYTHON;
183 out[i] = g_strdup(str);
184 }
185 out[i] = NULL;
186 *outstr = out;
187
188 return SRD_OK;
189}
190