summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorBert Vermeulen <bert@biot.com>2012-01-07 02:50:14 +0100
committerBert Vermeulen <bert@biot.com>2012-01-07 02:54:43 +0100
commit159699490ea4bf2495e99dcd5fb18b240d7499df (patch)
tree9e6f497a2b078e25918fd478d9b0cc41b86bb8da /util.c
parent721501bf4267db27bd6b848c8a0d4b5016b1f1c0 (diff)
downloadlibsigrokdecode-159699490ea4bf2495e99dcd5fb18b240d7499df.tar.gz
libsigrokdecode-159699490ea4bf2495e99dcd5fb18b240d7499df.zip
convert data coming in from a PD to C structs
This is in preparation for passing annotation data back to the calling frontend, and python data up to the next protocol in the stack.
Diffstat (limited to 'util.c')
-rw-r--r--util.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/util.c b/util.c
index 6164aaa..df433d7 100644
--- a/util.c
+++ b/util.c
@@ -18,8 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "config.h"
#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
+#include "config.h"
/**
@@ -73,8 +73,35 @@ err_out:
Py_XDECREF(py_encstr);
if (PyErr_Occurred())
+ /* TODO: log level 4 debug message */
PyErr_Print();
return ret;
}
+/**
+ * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded
+ * char * array. The caller must free each string when finished.
+ */
+int py_strlist_to_char(PyObject *py_strlist, char ***outstr)
+{
+ PyObject *py_str;
+ int list_len, i;
+ char **out, *str;
+
+ list_len = PyList_Size(py_strlist);
+ if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1))))
+ return SRD_ERR_MALLOC;
+ for (i = 0; i < list_len; i++) {
+ if (!(py_str = PyUnicode_AsEncodedString(PyList_GetItem(py_strlist, i), "utf-8", NULL)))
+ return SRD_ERR_PYTHON;
+ if (!(str = PyBytes_AS_STRING(py_str)))
+ return SRD_ERR_PYTHON;
+ out[i] = g_strdup(str);
+ }
+ out[i] = NULL;
+ *outstr = out;
+
+ return SRD_OK;
+}
+