]> sigrok.org Git - libsigrokdecode.git/blame - util.c
srd: simplified error checking, in preparation for more of it
[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>
5 * Copyright (C) 2011 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
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
BV
42 if (!PyObject_HasAttrString(py_obj, attr)) {
43 srd_dbg("object has no attribute '%s'", attr);
44 return SRD_ERR_PYTHON;
45 }
46
47 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
48 /* TODO: report exception message/traceback to err/dbg */
49 PyErr_Clear();
50 return SRD_ERR_PYTHON;
51 }
52
53 ret = py_str_as_str(py_str, outstr);
54 Py_XDECREF(py_str);
55
56 return ret;
57}
58
59
60/**
61 * Get the value of a python unicode string object, returned as a newly
62 * allocated char *.
63 *
64 * @param py_str The unicode string object.
65 * @param outstr ptr to char * storage to be filled in.
66 *
67 * @return SRD_OK upon success, a (negative) error code otherwise.
68 * The 'outstr' argument points to a malloc()ed string upon success.
69 */
70int py_str_as_str(PyObject *py_str, char **outstr)
71{
72 PyObject *py_encstr;
73 int ret;
74 char *str;
75
76 py_encstr = NULL;
8b4bbd2a
BV
77 str = NULL;
78 ret = SRD_OK;
79
451680f1
BV
80 if (!PyUnicode_Check(py_str)) {
81 srd_dbg("not a string object");
8b4bbd2a
BV
82 ret = SRD_ERR_PYTHON;
83 goto err_out;
b2c19614
BV
84 }
85
5f802ec6 86 if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) {
8b4bbd2a
BV
87 ret = SRD_ERR_PYTHON;
88 goto err_out;
5f802ec6
BV
89 }
90 if (!(str = PyBytes_AS_STRING(py_encstr))) {
8b4bbd2a
BV
91 ret = SRD_ERR_PYTHON;
92 goto err_out;
b2c19614
BV
93 }
94
95 if (!(*outstr = g_strdup(str))) {
451680f1 96 srd_dbg("malloc failed");
b2c19614 97 ret = SRD_ERR_MALLOC;
8b4bbd2a 98 goto err_out;
b2c19614
BV
99 }
100
8b4bbd2a 101err_out:
8b4bbd2a
BV
102 if (py_encstr)
103 Py_XDECREF(py_encstr);
b2c19614 104
451680f1
BV
105 if (PyErr_Occurred()) {
106 srd_dbg("string conversion failed");
107 /* TODO: dump exception to srd_dbg */
108 PyErr_Clear();
109 }
b2c19614
BV
110
111 return ret;
112}
113
451680f1 114
15969949
BV
115/**
116 * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded
117 * char * array. The caller must free each string when finished.
451680f1
BV
118 *
119 * @param py_strlist The list object.
120 * @param outstr ptr to char ** storage to be filled in.
121 *
122 * @return SRD_OK upon success, a (negative) error code otherwise.
123 * The 'outstr' argument points to a malloc()ed char ** upon success.
15969949
BV
124 */
125int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
126{
127 PyObject *py_str;
128 int list_len, i;
129 char **out, *str;
130
131 list_len = PyList_Size(py_strlist);
132 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1))))
133 return SRD_ERR_MALLOC;
134 for (i = 0; i < list_len; i++) {
451680f1
BV
135 if (!(py_str = PyUnicode_AsEncodedString(
136 PyList_GetItem(py_strlist, i), "utf-8", NULL)))
15969949
BV
137 return SRD_ERR_PYTHON;
138 if (!(str = PyBytes_AS_STRING(py_str)))
139 return SRD_ERR_PYTHON;
140 out[i] = g_strdup(str);
141 }
142 out[i] = NULL;
143 *outstr = out;
144
145 return SRD_OK;
146}
147