]> sigrok.org Git - libsigrokdecode.git/blame_incremental - util.c
Doxygen: Mark private functions/variables properly.
[libsigrokdecode.git] / util.c
... / ...
CommitLineData
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 * @private
37 */
38SRD_PRIV int py_attr_as_str(const PyObject *py_obj, const char *attr,
39 char **outstr)
40{
41 PyObject *py_str;
42 int ret;
43
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;
48 }
49
50 if (!(py_str = PyObject_GetAttrString((PyObject *)py_obj, attr))) {
51 srd_exception_catch("");
52 return SRD_ERR_PYTHON;
53 }
54
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;
60 }
61
62 ret = py_str_as_str(py_str, outstr);
63 Py_DecRef(py_str);
64
65 return ret;
66}
67
68/**
69 * Get the value of a Python dictionary item, returned as a newly
70 * allocated char *.
71 *
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.
75 *
76 * @return SRD_OK upon success, a (negative) error code otherwise.
77 * The 'outstr' argument points to a malloc()ed string upon success.
78 *
79 * @private
80 */
81SRD_PRIV int py_dictitem_as_str(const PyObject *py_obj, const char *key,
82 char **outstr)
83{
84 PyObject *py_value;
85 int ret;
86
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;
91 }
92
93 if (!(py_value = PyDict_GetItemString((PyObject *)py_obj, key))) {
94 srd_dbg("Dictionary has no attribute '%s'.", key);
95 return SRD_ERR_PYTHON;
96 }
97
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;
102 }
103
104 ret = py_str_as_str(py_value, outstr);
105
106 return ret;
107}
108
109/**
110 * Get the value of a Python unicode string object, returned as a newly
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.
118 *
119 * @private
120 */
121SRD_PRIV int py_str_as_str(const PyObject *py_str, char **outstr)
122{
123 PyObject *py_encstr;
124 int ret;
125 char *str;
126
127 py_encstr = NULL;
128 str = NULL;
129 ret = SRD_OK;
130
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;
136 }
137
138 if (!(py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str,
139 "utf-8", NULL))) {
140 ret = SRD_ERR_PYTHON;
141 goto err_out;
142 }
143 if (!(str = PyBytes_AS_STRING(py_encstr))) {
144 ret = SRD_ERR_PYTHON;
145 goto err_out;
146 }
147
148 if (!(*outstr = g_strdup(str))) {
149 srd_dbg("Failed to g_malloc() outstr.");
150 ret = SRD_ERR_MALLOC;
151 goto err_out;
152 }
153
154err_out:
155 if (py_encstr)
156 Py_XDECREF(py_encstr);
157
158 if (PyErr_Occurred()) {
159 srd_exception_catch("string conversion failed");
160 }
161
162 return ret;
163}
164
165/**
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.
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.
173 * The 'outstr' argument points to a g_malloc()ed char** upon success.
174 *
175 * @private
176 */
177SRD_PRIV int py_strlist_to_char(const PyObject *py_strlist, char ***outstr)
178{
179 PyObject *py_str;
180 int list_len, i;
181 char **out, *str;
182
183 list_len = PyList_Size((PyObject *)py_strlist);
184 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
185 srd_err("Failed to g_malloc() 'out'.");
186 return SRD_ERR_MALLOC;
187 }
188 for (i = 0; i < list_len; i++) {
189 if (!(py_str = PyUnicode_AsEncodedString(
190 PyList_GetItem((PyObject *)py_strlist, 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);
195 }
196 out[i] = NULL;
197 *outstr = out;
198
199 return SRD_OK;
200}