PageRenderTime 28ms CodeModel.GetById 9ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1.3.35/Examples/python/varargs/example.i

#
Swig | 65 lines | 34 code | 13 blank | 18 comment | 0 complexity | 50884c55f96e066f7d5b8d62f5167625 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1/* File : example.i */
 2%module example
 3
 4%{
 5#include <stdarg.h>
 6%}
 7
 8/* This example illustrates SWIG's handling of varargs functions.  
 9   By default, variable length arguments are simply ignored.  This
10   is generally appropriate for wrapping I/O functions like printf.
11   You can simply format a string in the scripting language, and
12   pass it directly */
13
14int printf(const char *fmt, ...);
15
16/* Since passing a format string might be dangerous.  Here is a slightly
17   different way of wrapping a printf style function */
18
19#if 1
20/* Replace ... with char *.   */
21%varargs(char *) fprintf;
22
23/* Ignore the format string, but set it to %s */
24%typemap(in,numinputs=0) const char *fmt {
25	$1 = "%s";
26}
27#else
28/* An alternative approach using typemaps */
29%typemap(in) (const char *fmt, ...) {
30   $1 = "%s";
31   $2 = (void *) PyString_AsString($input);
32}
33#endif
34
35/* Typemap just to make the example work */
36%typemap(in) FILE * "$1 = PyFile_AsFile($input);";
37
38int fprintf(FILE *, const char *fmt, ...);
39
40/* Here is somewhat different example.  A variable length argument
41   function that takes a NULL-terminated list of arguments. We
42   can use a slightly different form of %varargs that specifies
43   a default value and a maximum number of arguments.
44 */
45
46/* Maximum of 20 arguments with default value NULL */
47
48%varargs(20, char *x = NULL) printv;
49
50%inline %{
51void printv(char *s, ...) {
52    va_list ap;
53    char *x;
54    fputs(s,stdout);
55    fputc(' ',stdout);
56    va_start(ap, s);
57    while ((x = va_arg(ap, char *))) {
58      fputs(x,stdout);
59      fputc(' ',stdout);
60    }
61    va_end(ap);
62    fputc('\n',stdout);
63}
64%}
65