blob: 060c25387df580d3df6f32597132eacbbaaf5f77 [file] [log] [blame]
Bert Vermeulenb2c19612011-12-04 10:33:02 +01001/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
Bert Vermeulen4fadb122012-01-22 03:29:22 +01005 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
Bert Vermeulenb2c19612011-12-04 10:33:02 +01006 *
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
Uwe Hermann73191412012-01-03 19:56:01 +010021#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
Bert Vermeulen451680f2012-01-15 03:58:27 +010022#include "sigrokdecode-internal.h"
Bert Vermeulen15969942012-01-07 02:50:14 +010023#include "config.h"
Bert Vermeulenb2c19612011-12-04 10:33:02 +010024
25
26/**
Bert Vermeulen451680f2012-01-15 03:58:27 +010027 * Get the value of a python object's attribute, returned as a newly
28 * allocated char *.
Bert Vermeulenb2c19612011-12-04 10:33:02 +010029 *
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010030 * @param py_obj The object to probe.
Bert Vermeulen451680f2012-01-15 03:58:27 +010031 * @param attr Name of the attribute to retrieve.
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010032 * @param outstr ptr to char * storage to be filled in.
Bert Vermeulenb2c19612011-12-04 10:33:02 +010033 *
34 * @return SRD_OK upon success, a (negative) error code otherwise.
35 * The 'outstr' argument points to a malloc()ed string upon success.
36 */
Bert Vermeulen451680f2012-01-15 03:58:27 +010037int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
Bert Vermeulenb2c19612011-12-04 10:33:02 +010038{
Bert Vermeulen451680f2012-01-15 03:58:27 +010039 PyObject *py_str;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010040 int ret;
41
Bert Vermeulen451680f2012-01-15 03:58:27 +010042 if (!PyObject_HasAttrString(py_obj, attr)) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010043 srd_dbg("srd: %s object has no attribute '%s'.",
44 Py_TYPE(py_obj)->tp_name, attr);
Bert Vermeulen451680f2012-01-15 03:58:27 +010045 return SRD_ERR_PYTHON;
46 }
47
48 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
49 /* TODO: report exception message/traceback to err/dbg */
50 PyErr_Clear();
51 return SRD_ERR_PYTHON;
52 }
53
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010054 if (!PyUnicode_Check(py_str)) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010055 srd_dbg("srd: %s attribute should be a string, but is a %s.",
56 attr, Py_TYPE(py_str)->tp_name);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010057 Py_DecRef(py_str);
58 return SRD_ERR_PYTHON;
59 }
60
Bert Vermeulen451680f2012-01-15 03:58:27 +010061 ret = py_str_as_str(py_str, outstr);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010062 Py_DecRef(py_str);
Bert Vermeulen451680f2012-01-15 03:58:27 +010063
64 return ret;
65}
66
67
68/**
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010069 * 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 attr Key of the item to retrieve.
74 * @param outstr ptr 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 */
79int py_dictitem_as_str(PyObject *py_obj, const char *key, char **outstr)
80{
81 PyObject *py_value;
82 int ret;
83
84 if (!PyDict_Check(py_obj)) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010085 srd_dbg("srd: Object is a %s, not a dictionary.", Py_TYPE(py_obj)->tp_name);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010086 return SRD_ERR_PYTHON;
87 }
88
89 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010090 srd_dbg("srd: Dictionary has no attribute '%s'.", key);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010091 return SRD_ERR_PYTHON;
92 }
93
94 if (!PyUnicode_Check(py_value)) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +010095 srd_dbg("srd: Dictionary value for %s should be a string, but is a %s.",
96 key, Py_TYPE(py_value)->tp_name);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010097 return SRD_ERR_PYTHON;
98 }
99
100 ret = py_str_as_str(py_value, outstr);
101
102 return SRD_OK;
103}
104
105
106/**
Bert Vermeulen451680f2012-01-15 03:58:27 +0100107 * Get the value of a python unicode string object, returned as a newly
108 * allocated char *.
109 *
110 * @param py_str The unicode string object.
111 * @param outstr ptr to char * storage to be filled in.
112 *
113 * @return SRD_OK upon success, a (negative) error code otherwise.
114 * The 'outstr' argument points to a malloc()ed string upon success.
115 */
116int py_str_as_str(PyObject *py_str, char **outstr)
117{
118 PyObject *py_encstr;
119 int ret;
120 char *str;
121
122 py_encstr = NULL;
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100123 str = NULL;
124 ret = SRD_OK;
125
Bert Vermeulen451680f2012-01-15 03:58:27 +0100126 if (!PyUnicode_Check(py_str)) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100127 srd_dbg("srd: object is a %s, not a string object", Py_TYPE(py_str)->tp_name);
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100128 ret = SRD_ERR_PYTHON;
129 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100130 }
131
Bert Vermeulen5f802ec2011-12-27 22:15:53 +0100132 if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100133 ret = SRD_ERR_PYTHON;
134 goto err_out;
Bert Vermeulen5f802ec2011-12-27 22:15:53 +0100135 }
136 if (!(str = PyBytes_AS_STRING(py_encstr))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100137 ret = SRD_ERR_PYTHON;
138 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100139 }
140
141 if (!(*outstr = g_strdup(str))) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100142 srd_dbg("srd: malloc failed");
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100143 ret = SRD_ERR_MALLOC;
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100144 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100145 }
146
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100147err_out:
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100148 if (py_encstr)
149 Py_XDECREF(py_encstr);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100150
Bert Vermeulen451680f2012-01-15 03:58:27 +0100151 if (PyErr_Occurred()) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100152 srd_dbg("srd: string conversion failed");
Bert Vermeulen451680f2012-01-15 03:58:27 +0100153 /* TODO: dump exception to srd_dbg */
154 PyErr_Clear();
155 }
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100156
157 return ret;
158}
159
Bert Vermeulen451680f2012-01-15 03:58:27 +0100160
Bert Vermeulen15969942012-01-07 02:50:14 +0100161/**
162 * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded
163 * char * array. The caller must free each string when finished.
Bert Vermeulen451680f2012-01-15 03:58:27 +0100164 *
165 * @param py_strlist The list object.
166 * @param outstr ptr to char ** storage to be filled in.
167 *
168 * @return SRD_OK upon success, a (negative) error code otherwise.
169 * The 'outstr' argument points to a malloc()ed char ** upon success.
Bert Vermeulen15969942012-01-07 02:50:14 +0100170 */
171int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
172{
173 PyObject *py_str;
174 int list_len, i;
175 char **out, *str;
176
177 list_len = PyList_Size(py_strlist);
178 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1))))
179 return SRD_ERR_MALLOC;
180 for (i = 0; i < list_len; i++) {
Bert Vermeulen451680f2012-01-15 03:58:27 +0100181 if (!(py_str = PyUnicode_AsEncodedString(
182 PyList_GetItem(py_strlist, i), "utf-8", NULL)))
Bert Vermeulen15969942012-01-07 02:50:14 +0100183 return SRD_ERR_PYTHON;
184 if (!(str = PyBytes_AS_STRING(py_str)))
185 return SRD_ERR_PYTHON;
186 out[i] = g_strdup(str);
187 }
188 out[i] = NULL;
189 *outstr = out;
190
191 return SRD_OK;
192}
193