blob: df433d719f6bb08ad26262883ec5acd7434ed366 [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>
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
Uwe Hermann73191412012-01-03 19:56:01 +010021#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
Bert Vermeulen15969942012-01-07 02:50:14 +010022#include "config.h"
Bert Vermeulenb2c19612011-12-04 10:33:02 +010023
24
25/**
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010026 * Helper function to get the value of a python object's attribute,
27 * returned as a newly 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.
30 * @param key Name of the attribute to retrieve.
31 * @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 */
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010036int h_str(PyObject *py_obj, const char *key, char **outstr)
Bert Vermeulenb2c19612011-12-04 10:33:02 +010037{
Bert Vermeulen5f802ec2011-12-27 22:15:53 +010038 PyObject *py_str, *py_encstr;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010039 char *str;
40 int ret;
41
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010042 py_str = py_encstr = NULL;
43 str = NULL;
44 ret = SRD_OK;
45
46 if (!(py_str = PyObject_GetAttrString(py_obj, (char *)key))) {
47 /* TODO: log level 4 debug message */
48 ret = SRD_ERR_PYTHON;
49 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010050 }
51
Bert Vermeulen5f802ec2011-12-27 22:15:53 +010052 if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010053 /* TODO: log level 4 debug message */
54 ret = SRD_ERR_PYTHON;
55 goto err_out;
Bert Vermeulen5f802ec2011-12-27 22:15:53 +010056 }
57 if (!(str = PyBytes_AS_STRING(py_encstr))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010058 /* TODO: log level 4 debug message */
59 ret = SRD_ERR_PYTHON;
60 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010061 }
62
63 if (!(*outstr = g_strdup(str))) {
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010064 /* TODO: log level 4 debug message */
Bert Vermeulenb2c19612011-12-04 10:33:02 +010065 ret = SRD_ERR_MALLOC;
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010066 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010067 }
68
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010069err_out:
70 if (py_str)
71 Py_XDECREF(py_str);
72 if (py_encstr)
73 Py_XDECREF(py_encstr);
Bert Vermeulenb2c19612011-12-04 10:33:02 +010074
75 if (PyErr_Occurred())
Bert Vermeulen15969942012-01-07 02:50:14 +010076 /* TODO: log level 4 debug message */
Bert Vermeulen8b4bbd22011-12-28 13:40:23 +010077 PyErr_Print();
Bert Vermeulenb2c19612011-12-04 10:33:02 +010078
79 return ret;
80}
81
Bert Vermeulen15969942012-01-07 02:50:14 +010082/**
83 * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded
84 * char * array. The caller must free each string when finished.
85 */
86int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
87{
88 PyObject *py_str;
89 int list_len, i;
90 char **out, *str;
91
92 list_len = PyList_Size(py_strlist);
93 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1))))
94 return SRD_ERR_MALLOC;
95 for (i = 0; i < list_len; i++) {
96 if (!(py_str = PyUnicode_AsEncodedString(PyList_GetItem(py_strlist, i), "utf-8", NULL)))
97 return SRD_ERR_PYTHON;
98 if (!(str = PyBytes_AS_STRING(py_str)))
99 return SRD_ERR_PYTHON;
100 out[i] = g_strdup(str);
101 }
102 out[i] = NULL;
103 *outstr = out;
104
105 return SRD_OK;
106}
107