From 567c73731b3a2aa3f836ea0724462587c6405a49 Mon Sep 17 00:00:00 2001
From: Olly Betts
-
-
A common problem in many C programs is the processing of command line arguments, which are usually passed in an array of NULL terminated -strings. The following SWIG interface file allows a Python list -object to be used as a char ** object. +strings. SWIG provides typemaps which allow passing a Python list +or tuple - see +argcargv.i.
--%module argv - -// This tells SWIG to treat char ** as a special case -%typemap(in) char ** { - /* Check if is a list */ - if (PyList_Check($input)) { - int size = PyList_Size($input); - int i = 0; - $1 = (char **) malloc((size+1)*sizeof(char *)); - for (i = 0; i < size; i++) { - PyObject *o = PyList_GetItem($input, i); - if (PyString_Check(o)) { - $1[i] = PyString_AsString(PyList_GetItem($input, i)); - } else { - PyErr_SetString(PyExc_TypeError, "list must contain strings"); - SWIG_fail; - } - } - $1[i] = 0; - } else { - PyErr_SetString(PyExc_TypeError, "not a list"); - SWIG_fail; - } -} - -// This cleans up the char ** array we malloc'd before the function call -%typemap(freearg) char ** { - free((char *) $1); -} - -// Now a test function -%inline %{ -int print_args(char **argv) { - int i = 0; - while (argv[i]) { - printf("argv[%d] = %s\n", i, argv[i]); - i++; - } - return i; -} -%} - -
-When this module is compiled, the wrapped C function now operates as -follows : -
- -->>> from argv import * ->>> print_args(["Dave", "Mike", "Mary", "Jane", "John"]) -argv[0] = Dave -argv[1] = Mike -argv[2] = Mary -argv[3] = Jane -argv[4] = John -5 -
-In the example, two different typemaps are used. The "in" typemap is -used to receive an input argument and convert it to a C array. Since dynamic -memory allocation is used to allocate memory for the array, the -"freearg" typemap is used to later release this memory after the execution of -the C function. -