|
From: Pengkui L. <pen...@gm...> - 2012-06-22 00:03:28
|
Suppose I have a C function strsplit() that returns char**. How should I write the interface file such that I can get a list of strings in Python? I realize that this page contains doc on converting python list to char**, but I would like to know the other way around.. http://www.swig.org/Doc1.3/Python.html#Python_nn59 #include <stdio.h> #include <string.h> #include <stddef.h> /* Split an input string 'instr', using a set of given delimiters, to an array of strings of at most 'maxparts' parts. */ char **strsplit (const char *instr, const char *delimiters, size_t maxparts) { char **tokens = (char **) calloc (maxparts, sizeof(char *)); char *_instr = strdup (instr); size_t tokenmaxsize = strlen (_instr) + 1; char *pstr = strtok (_instr, delimiters); int i; for (i=0; pstr != NULL; i++) { tokens[i] = (char *) malloc (tokenmaxsize * sizeof(char)); strcpy (tokens[i], pstr); pstr = strtok (NULL, delimiters); } free (_instr); return tokens; } (I know I could use str split() in Python for this particular example.) Thanks, Pengkui |