blob: cd77fc10481836aa3c9057e0d06a3360cecd9135 [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
Bert Vermeulenb2c19612011-12-04 10:33:02 +010025/**
Uwe Hermann511e2122012-02-10 09:29:38 +010026 * Get the value of a Python object's attribute, returned as a newly
Bert Vermeulen451680f2012-01-15 03:58:27 +010027 * allocated char *.
Bert Vermeulenb2c19612011-12-04 10:33:02 +010028 *
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010029 * @param py_obj The object to probe.
Bert Vermeulen451680f2012-01-15 03:58:27 +010030 * @param attr Name of the attribute to retrieve.
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010031 * @param outstr ptr to char * storage to be filled in.
Bert Vermeulenb2c19612011-12-04 10:33:02 +010032 *
33 * @return SRD_OK upon success, a (negative) error code otherwise.
34 * The 'outstr' argument points to a malloc()ed string upon success.
35 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010036SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
Bert Vermeulenb2c19612011-12-04 10:33:02 +010037{
Bert Vermeulen451680f2012-01-15 03:58:27 +010038 PyObject *py_str;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010039 int ret;
40
Bert Vermeulen451680f2012-01-15 03:58:27 +010041 if (!PyObject_HasAttrString(py_obj, attr)) {
Uwe Hermann7a1712c2012-01-26 01:15:10 +010042 srd_dbg("%s object has no attribute '%s'.",
43 Py_TYPE(py_obj)->tp_name, attr);
Bert Vermeulen451680f2012-01-15 03:58:27 +010044 return SRD_ERR_PYTHON;
45 }
46
47 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
Bert Vermeulen2086c682012-01-23 04:51:33 +010048 catch_exception("");
Bert Vermeulen451680f2012-01-15 03:58:27 +010049 return SRD_ERR_PYTHON;
50 }
51
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010052 if (!PyUnicode_Check(py_str)) {
Uwe Hermann7a1712c2012-01-26 01:15:10 +010053 srd_dbg("%s attribute should be a string, but is a %s.",
54 attr, Py_TYPE(py_str)->tp_name);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010055 Py_DecRef(py_str);
56 return SRD_ERR_PYTHON;
57 }
58
Bert Vermeulen451680f2012-01-15 03:58:27 +010059 ret = py_str_as_str(py_str, outstr);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010060 Py_DecRef(py_str);
Bert Vermeulen451680f2012-01-15 03:58:27 +010061
62 return ret;
63}
64
Bert Vermeulen451680f2012-01-15 03:58:27 +010065/**
Uwe Hermann511e2122012-02-10 09:29:38 +010066 * Get the value of a Python dictionary item, returned as a newly
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010067 * 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 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010076SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
77 char **outstr)
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010078{
79 PyObject *py_value;
80 int ret;
81
82 if (!PyDict_Check(py_obj)) {
Uwe Hermannc9bfccc2012-02-08 22:39:30 +010083 srd_dbg("Object is a %s, not a dictionary.",
84 Py_TYPE(py_obj)->tp_name);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010085 return SRD_ERR_PYTHON;
86 }
87
88 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
Uwe Hermann7a1712c2012-01-26 01:15:10 +010089 srd_dbg("Dictionary has no attribute '%s'.", key);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010090 return SRD_ERR_PYTHON;
91 }
92
93 if (!PyUnicode_Check(py_value)) {
Uwe Hermannc9bfccc2012-02-08 22:39:30 +010094 srd_dbg("Dictionary value for %s should be a string, but is "
95 "a %s.", key, Py_TYPE(py_value)->tp_name);
Bert Vermeulend42fc6e2012-01-20 22:08:22 +010096 return SRD_ERR_PYTHON;
97 }
98
99 ret = py_str_as_str(py_value, outstr);
100
101 return SRD_OK;
102}
103
Bert Vermeulend42fc6e2012-01-20 22:08:22 +0100104/**
Uwe Hermann511e2122012-02-10 09:29:38 +0100105 * Get the value of a Python unicode string object, returned as a newly
Bert Vermeulen451680f2012-01-15 03:58:27 +0100106 * 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 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100114SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100115{
116 PyObject *py_encstr;
117 int ret;
118 char *str;
119
120 py_encstr = NULL;
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100121 str = NULL;
122 ret = SRD_OK;
123
Bert Vermeulen451680f2012-01-15 03:58:27 +0100124 if (!PyUnicode_Check(py_str)) {
Uwe Hermannc9bfccc2012-02-08 22:39:30 +0100125 srd_dbg("Object is a %s, not a string object.",
126 Py_TYPE(py_str)->tp_name);
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100127 ret = SRD_ERR_PYTHON;
128 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100129 }
130
Bert Vermeulen5f802ec2011-12-27 22:15:53 +0100131 if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100132 ret = SRD_ERR_PYTHON;
133 goto err_out;
Bert Vermeulen5f802ec2011-12-27 22:15:53 +0100134 }
135 if (!(str = PyBytes_AS_STRING(py_encstr))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100136 ret = SRD_ERR_PYTHON;
137 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100138 }
139
140 if (!(*outstr = g_strdup(str))) {
Uwe Hermanna61ece22012-02-10 00:06:58 +0100141 srd_dbg("Failed to g_malloc() outstr.");
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100142 ret = SRD_ERR_MALLOC;
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100143 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100144 }
145
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100146err_out:
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +0100147 if (py_encstr)
148 Py_XDECREF(py_encstr);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100149
Bert Vermeulen451680f2012-01-15 03:58:27 +0100150 if (PyErr_Occurred()) {
Bert Vermeulen2086c682012-01-23 04:51:33 +0100151 catch_exception("string conversion failed");
Bert Vermeulen451680f2012-01-15 03:58:27 +0100152 }
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100153
154 return ret;
155}
156
Bert Vermeulen15969942012-01-07 02:50:14 +0100157/**
Uwe Hermann511e2122012-02-10 09:29:38 +0100158 * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded
Uwe Hermann56bf4c22012-02-11 20:06:46 +0100159 * char * array. The caller must g_free() each string when finished.
Bert Vermeulen451680f2012-01-15 03:58:27 +0100160 *
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.
Uwe Hermanna61ece22012-02-10 00:06:58 +0100165 * The 'outstr' argument points to a g_malloc()ed char** upon success.
Bert Vermeulen15969942012-01-07 02:50:14 +0100166 */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +0100167SRD_PRIV int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
Bert Vermeulen15969942012-01-07 02:50:14 +0100168{
169 PyObject *py_str;
170 int list_len, i;
171 char **out, *str;
172
173 list_len = PyList_Size(py_strlist);
Uwe Hermanna61ece22012-02-10 00:06:58 +0100174 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
175 srd_err("Failed to g_malloc() 'out'.");
Bert Vermeulen15969942012-01-07 02:50:14 +0100176 return SRD_ERR_MALLOC;
Uwe Hermanna61ece22012-02-10 00:06:58 +0100177 }
Bert Vermeulen15969942012-01-07 02:50:14 +0100178 for (i = 0; i < list_len; i++) {
Bert Vermeulen451680f2012-01-15 03:58:27 +0100179 if (!(py_str = PyUnicode_AsEncodedString(
Uwe Hermannc9bfccc2012-02-08 22:39:30 +0100180 PyList_GetItem(py_strlist, i), "utf-8", NULL)))
Bert Vermeulen15969942012-01-07 02:50:14 +0100181 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}