From 2e2617edce0a869a2a071ea6a1001c8fce2f66a5 Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Tue, 6 Sep 2005 06:22:10 +0000 Subject: [PATCH] normalize cstring.i to use fragments, and add cwstring.i as a subproduct git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7406 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/test-suite/li_cstring.i | 35 ++- SWIG/Examples/test-suite/li_cwstring.i | 97 +++++++ SWIG/Examples/test-suite/python/Makefile.in | 2 + .../test-suite/python/li_cstring_runme.py | 21 ++ .../test-suite/python/li_cwstring_runme.py | 26 ++ SWIG/Lib/python/cstrbase.swg | 254 ++++++++++++++++++ SWIG/Lib/python/cstring.i | 211 +++------------ SWIG/Lib/python/cwstring.i | 137 ++++++++++ SWIG/Lib/python/pymacros.swg | 20 +- SWIG/Lib/python/pystrbase.swg | 7 +- SWIG/Source/CParse/parser.y | 6 +- 11 files changed, 602 insertions(+), 214 deletions(-) create mode 100644 SWIG/Examples/test-suite/li_cwstring.i create mode 100644 SWIG/Examples/test-suite/python/li_cwstring_runme.py create mode 100644 SWIG/Lib/python/cstrbase.swg create mode 100644 SWIG/Lib/python/cwstring.i diff --git a/SWIG/Examples/test-suite/li_cstring.i b/SWIG/Examples/test-suite/li_cstring.i index 480ddf351..104992b18 100644 --- a/SWIG/Examples/test-suite/li_cstring.i +++ b/SWIG/Examples/test-suite/li_cstring.i @@ -6,17 +6,18 @@ %cstring_input_binary(char *in, int n); %cstring_bounded_output(char *out1, 512); -%cstring_chunk_output(char *out2, 128); +%cstring_chunk_output(char *out2, 64); %cstring_bounded_mutable(char *out3, 512); %cstring_mutable(char *out4, 32); %cstring_output_maxsize(char *out5, int max); %cstring_output_withsize(char *out6, int *size); + #ifdef __cplusplus -%cstring_output_allocate(char **out7, delete [] $1); -%cstring_output_allocate_size(char **out8, int *size, delete [] $1); +%cstring_output_allocate(char **out7, delete [] *$1); +%cstring_output_allocate_size(char **out8, int *size, delete [] *$1); #else -%cstring_output_allocate(char **out7, free($1)); -%cstring_output_allocate_size(char **out8, int *size, free($1)); +%cstring_output_allocate(char **out7, free(*$1)); +%cstring_output_allocate_size(char **out8, int *size, free(*$1)); #endif %inline %{ @@ -38,8 +39,8 @@ void test1(char *out1) { void test2(char *out2) { int i; - for (i = 0; i < 128; i++) { - *out2 = (char) i; + for (i = 0; i < 64; i++) { + *out2 = (char) i + 32; out2++; } } @@ -74,29 +75,23 @@ void test7(char **out7) { #else *out7 = malloc(64); #endif + (*out7)[0] = 0; strcat(*out7,"Hello world!"); } -void test8(char **out8, int *sz) { +void test8(char **out8, int *size) { int i; #ifdef __cplusplus - *out8 = new char[128]; + *out8 = new char[64]; #else - *out8 = malloc(128); + *out8 = malloc(64); #endif - for (i = 0; i < 128; i++) { - *out8[i] = (char) i; + for (i = 0; i < 64; i++) { + (*out8)[i] = (char) i+32; } + *size = 64; } %} #endif - - - - - - - - diff --git a/SWIG/Examples/test-suite/li_cwstring.i b/SWIG/Examples/test-suite/li_cwstring.i new file mode 100644 index 000000000..3bd1a7508 --- /dev/null +++ b/SWIG/Examples/test-suite/li_cwstring.i @@ -0,0 +1,97 @@ +%module li_cwstring + +%include "cwstring.i" + +#ifndef _CSTRING_UNIMPL + +%cwstring_input_binary(wchar_t *in, int n); +%cwstring_bounded_output(wchar_t *out1, 512); +%cwstring_chunk_output(wchar_t *out2, 64); +%cwstring_bounded_mutable(wchar_t *out3, 512); +%cwstring_mutable(wchar_t *out4, 32); +%cwstring_output_maxsize(wchar_t *out5, int max); +%cwstring_output_withsize(wchar_t *out6, int *size); + +#ifdef __cplusplus +%cwstring_output_allocate(wchar_t **out7, delete [] *$1); +%cwstring_output_allocate_size(wchar_t **out8, int *size, delete [] *$1); +#else +%cwstring_output_allocate(wchar_t **out7, free(*$1)); +%cwstring_output_allocate_size(wchar_t **out8, int *size, free(*$1)); +#endif + +%inline %{ + +int count(wchar_t *in, int n, wchar_t c) { + int r = 0; + while (n > 0) { + if (*in == c) { + r++; + } + in++; + } + return r; +} + +void test1(wchar_t *out1) { + wcscpy(out1,L"Hello World"); +} + +void test2(wchar_t *out2) { + int i; + for (i = 0; i < 64; i++) { + *out2 = (wchar_t) i + 32; + out2++; + } +} + +void test3(wchar_t *out3) { + wcscat(out3,L"-suffix"); +} + +void test4(wchar_t *out4) { + wcscat(out4,L"-suffix"); +} + +void test5(wchar_t *out5, int max) { + int i; + for (i = 0; i < max; i++) { + out5[i] = 'x'; + } + out5[max]='\0'; +} + +void test6(wchar_t *out6, int *size) { + int i; + for (i = 0; i < (*size/2); i++) { + out6[i] = 'x'; + } + *size = (*size/2); +} + +void test7(wchar_t **out7) { +#ifdef __cplusplus + *out7 = new wchar_t[64]; +#else + *out7 = malloc(64*sizeof(wchar_t)); +#endif + (*out7)[0] = 0; + wcscat(*out7,L"Hello world!"); +} + +void test8(wchar_t **out8, int *size) { + int i; +#ifdef __cplusplus + *out8 = new wchar_t[64]; +#else + *out8 = malloc(64*sizeof(wchar_t)); +#endif + for (i = 0; i < 64; i++) { + (*out8)[i] = (wchar_t) i + 32; + } + *size = 64; +} + +%} + +#endif diff --git a/SWIG/Examples/test-suite/python/Makefile.in b/SWIG/Examples/test-suite/python/Makefile.in index c68e4a858..e6faa2bbc 100644 --- a/SWIG/Examples/test-suite/python/Makefile.in +++ b/SWIG/Examples/test-suite/python/Makefile.in @@ -25,6 +25,7 @@ CPP_TEST_CASES += \ inplaceadd \ kwargs \ li_cstring \ + li_cwstring \ li_std_except \ li_std_vectora \ li_std_map \ @@ -41,6 +42,7 @@ CPP_TEST_CASES += \ C_TEST_CASES += \ file_test \ li_cstring \ + li_cwstring \ nondynamic # diff --git a/SWIG/Examples/test-suite/python/li_cstring_runme.py b/SWIG/Examples/test-suite/python/li_cstring_runme.py index 3c9a13f21..ae937fb67 100644 --- a/SWIG/Examples/test-suite/python/li_cstring_runme.py +++ b/SWIG/Examples/test-suite/python/li_cstring_runme.py @@ -1,5 +1,26 @@ from li_cstring import * +if test1() != "Hello World": + raise RuntimeError +if test2() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_": + raise RuntimeError + +if test3("hello") != "hello-suffix": + raise RuntimeError + +if test4("hello") != "hello-suffix": + raise RuntimeError + if test5(4) != 'xxxx': raise RuntimeError + +if test6(10) != 'xxxxx': + raise RuntimeError + +if test7() !="Hello world!": + raise RuntimeError + +if test8() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_": + raise RuntimeError + diff --git a/SWIG/Examples/test-suite/python/li_cwstring_runme.py b/SWIG/Examples/test-suite/python/li_cwstring_runme.py new file mode 100644 index 000000000..b1a140612 --- /dev/null +++ b/SWIG/Examples/test-suite/python/li_cwstring_runme.py @@ -0,0 +1,26 @@ +from li_cwstring import * + +if test1() != "Hello World": + raise RuntimeError + +if test2() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_": + raise RuntimeError + +if test3("hello") != "hello-suffix": + raise RuntimeError + +if test4("hello") != "hello-suffix": + raise RuntimeError + +if test5(4) != 'xxxx': + raise RuntimeError + +if test6(10) != 'xxxxx': + raise RuntimeError + +if test7() !="Hello world!": + raise RuntimeError + +if test8() != " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_": + raise RuntimeError + diff --git a/SWIG/Lib/python/cstrbase.swg b/SWIG/Lib/python/cstrbase.swg new file mode 100644 index 000000000..6eb38da31 --- /dev/null +++ b/SWIG/Lib/python/cstrbase.swg @@ -0,0 +1,254 @@ +/* + * cstring.i + * $Header$ + * + * Author(s): David Beazley (beazley@cs.uchicago.edu) + * + * This file provides typemaps and macros for dealing with various forms + * of C character string handling. The primary use of this module + * is in returning character data that has been allocated or changed in + * some way. + */ + +%include + +%define %typemap_cstrings(Name, Char, + SWIG_AsCharPtr, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtr, + SWIG_FromCharArray) + +/* %cstring_input_binary(TYPEMAP, SIZE) + * + * Macro makes a function accept binary string data along with + * a size. For example: + * + * %cstring_input_binary(Char *buff, int size); + * void foo(Char *buff, int size) { + * } + * + */ + +#define Name ## _input_binary(TYPEMAP, SIZE) \ +%typemap(in, fragment=#SWIG_AsCharPtrAndSize) (TYPEMAP, SIZE) \ + (Char *buf, size_t size) \ +{ \ + SWIG_AsCharPtrAndSize($input, &buf, &size); \ + if (SWIG_arg_fail($argnum)) SWIG_fail; \ + $1 = ($1_ltype) buf; \ + $2 = ($2_ltype) size; \ +} + + +/* + * %cstring_bounded_output(TYPEMAP, MAX) + * + * This macro is used to return a NULL-terminated output string of + * some maximum length. For example: + * + * %cstring_bounded_output(Char *outx, 512); + * void foo(Char *outx) { + * sprintf(outx,"blah blah\n"); + * } + * + */ + +#define Name ## _bounded_output(TYPEMAP,MAX) \ +%typemap(in,numinputs=0) TYPEMAP(Char temp[MAX+1]) \ + "$1 = ($1_ltype) temp;"; \ + \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr ) TYPEMAP \ + "$1[MAX] = 0; $result = t_output_helper($result, SWIG_FromCharPtr($1));"; + + + +/* + * %cstring_chunk_output(TYPEMAP, SIZE) + * + * This macro is used to return a chunk of binary string data. + * Embedded NULLs are okay. For example: + * + * %cstring_chunk_output(Char *outx, 512); + * void foo(Char *outx) { + * memmove(outx, somedata, 512); + * } + * + */ + +#define Name ## _chunk_output(TYPEMAP,SIZE) \ +%typemap(in,numinputs=0) TYPEMAP(Char temp[SIZE]) \ + "$1 = ($1_ltype) temp;"; \ + \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray) TYPEMAP \ + "$result = t_output_helper($result, SWIG_FromCharArray($1,SIZE));"; + + +/* + * %cstring_bounded_mutable(TYPEMAP, SIZE) + * + * This macro is used to wrap a string that's going to mutate. + * + * %cstring_bounded_mutable(Char *in, 512); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + + +#define Name ## _bounded_mutable(TYPEMAP,MAX) \ +%typemap(in,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP(Char temp[MAX+1]) { \ + Char *t = 0; size_t n; \ + SWIG_AsCharPtrAndSize($input, &t, &n); \ + if (SWIG_arg_fail($argnum)) SWIG_fail; \ + if ( n > (size_t)MAX ) n = (size_t)MAX; \ + memcpy(temp, t, sizeof(Char)*n); \ + temp[n] = 0; \ + $1 = ($1_ltype) temp; \ +} \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP \ + "$1[MAX] = 0; $result = t_output_helper($result, SWIG_FromCharPtr($1));"; + + + +/* + * %cstring_mutable(TYPEMAP [, expansion]) + * + * This macro is used to wrap a string that will mutate in place. + * It may change size up to a user-defined expansion. + * + * %cstring_mutable(Char *in); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +#define Name ## _mutable(TYPEMAP,EXP...) \ +%typemap(in,fragment=#SWIG_AsCharPtrAndSize) TYPEMAP { \ + const size_t expansion = strlen(#EXP) ? atoi(#EXP) : 0; \ + Char* t = 0; size_t n = 0; \ + SWIG_AsCharPtrAndSize($input, &t, &n); \ + if (SWIG_arg_fail($argnum)) SWIG_fail; \ + $1 = SWIG_new_array(n+1+expansion, $*1_ltype); \ + memcpy($1,t,sizeof(Char)*n); \ + $1[n] = 0; \ +} \ + \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP { \ + $result = t_output_helper($result,SWIG_FromCharPtr($1));\ + SWIG_delete_array($1); \ +} + + +/* + * %cstring_output_maxsize(TYPEMAP, SIZE) + * + * This macro returns data in a string of some user-defined size. + * + * %cstring_output_maxsize(Char *outx, int max) { + * void foo(Char *outx, int max) { + * sprintf(outx,"blah blah\n"); + * } + */ + +#define Name ## _output_maxsize(TYPEMAP, SIZE) \ +%typemap(in,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) { \ + $2 = ($2_ltype) SWIG_As(unsigned long)($input); \ + if (SWIG_arg_fail($argnum)) SWIG_fail; \ + $1 = SWIG_new_array($2+1, $*1_ltype); \ +} \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) (TYPEMAP,SIZE) { \ + $result = t_output_helper($result,SWIG_FromCharPtr($1)); \ + SWIG_delete_array($1); \ +} + + + +/* + * %cstring_output_withsize(TYPEMAP, SIZE) + * + * This macro is used to return Character data along with a size + * parameter. + * + * %cstring_output_maxsize(Char *outx, int *max) { + * void foo(Char *outx, int *max) { + * sprintf(outx,"blah blah\n"); + * *max = strlen(outx); + * } + */ + +#define Name ## _output_withsize(TYPEMAP, SIZE) \ +%typemap(in,fragment=SWIG_As_frag(unsigned long)) (TYPEMAP, SIZE) { \ + size_t n = SWIG_As(unsigned long)($input); \ + if (SWIG_arg_fail($argnum)) SWIG_fail; \ + $1 = SWIG_new_array(n+1, $*1_ltype); \ + $2 = SWIG_new($*2_ltype); \ + *$2 = n; \ +} \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray) (TYPEMAP,SIZE) { \ + $result = t_output_helper($result, SWIG_FromCharArray($1,*$2)); \ + SWIG_delete_array($1); \ + SWIG_delete($2); \ +} + + +/* + * %cstring_output_allocate(TYPEMAP, RELEASE) + * + * This macro is used to return Character data that was + * allocated with new or malloc. + * + * %cstring_output_allocated(Char **outx, free($1)); + * void foo(Char **outx) { + * *outx = (Char *) malloc(512); + * sprintf(outx,"blah blah\n"); + * } + */ + +#define Name ## _output_allocate(TYPEMAP, RELEASE) \ +%typemap(in,numinputs=0) TYPEMAP($*1_ltype temp = 0) \ + "$1 = &temp;"; \ + \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharPtr) TYPEMAP { \ + if (*$1) { \ + $result = t_output_helper($result,SWIG_FromCharPtr(*$1));\ + RELEASE; \ + } \ +} \ + + +/* + * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) + * + * This macro is used to return Character data that was + * allocated with new or malloc. + * + * %cstring_output_allocated(Char **outx, int *sz, free($1)); + * void foo(Char **outx, int *sz) { + * *outx = (Char *) malloc(512); + * sprintf(outx,"blah blah\n"); + * *sz = strlen(outx); + * } + */ + +#define Name ## _output_allocate_size(TYPEMAP, SIZE, RELEASE) \ +%typemap(in,numinputs=0) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) \ + "$1 = &temp; $2 = &tempn;"; \ + \ +%typemap(argout,fragment="t_output_helper," #SWIG_FromCharArray)(TYPEMAP,SIZE) { \ + if (*$1) { \ + $result = t_output_helper($result,SWIG_FromCharArray(*$1,*$2)); \ + RELEASE; \ + } \ +} \ + + +%enddef + diff --git a/SWIG/Lib/python/cstring.i b/SWIG/Lib/python/cstring.i index dc592768c..2a9a5be53 100644 --- a/SWIG/Lib/python/cstring.i +++ b/SWIG/Lib/python/cstring.i @@ -10,72 +10,45 @@ * some way. */ -%include - -/* %cstring_input_binary(TYPEMAP, SIZE) +/* + * %cstring_input_binary(TYPEMAP, SIZE) * * Macro makes a function accept binary string data along with * a size. */ -%define %cstring_input_binary(TYPEMAP, SIZE) -%apply (char *STRING, int LENGTH) { (TYPEMAP, SIZE) }; -%enddef - /* * %cstring_bounded_output(TYPEMAP, MAX) * * This macro is used to return a NULL-terminated output string of * some maximum length. For example: * - * %cstring_bounded_output(char *outx, 512); - * void foo(char *outx) { + * %cstring_bounded_output(Char *outx, 512); + * void foo(Char *outx) { * sprintf(outx,"blah blah\n"); * } * */ -%define %cstring_bounded_output(TYPEMAP,MAX) -%typemap(in,numinputs=0) TYPEMAP(char temp[MAX+1]) { - $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper") TYPEMAP { - PyObject *o; - $1[MAX] = 0; - o = PyString_FromString($1); - $result = t_output_helper($result,o); -} -%enddef - /* * %cstring_chunk_output(TYPEMAP, SIZE) * * This macro is used to return a chunk of binary string data. * Embedded NULLs are okay. For example: * - * %cstring_chunk_output(char *outx, 512); - * void foo(char *outx) { + * %cstring_chunk_output(Char *outx, 512); + * void foo(Char *outx) { * memmove(outx, somedata, 512); * } * */ -%define %cstring_chunk_output(TYPEMAP,SIZE) -%typemap(in,numinputs=0) TYPEMAP(char temp[SIZE]) { - $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper") TYPEMAP { - PyObject *o = PyString_FromStringAndSize($1,SIZE); - $result = t_output_helper($result,o); -} -%enddef - /* * %cstring_bounded_mutable(TYPEMAP, SIZE) * * This macro is used to wrap a string that's going to mutate. * - * %cstring_bounded_mutable(char *in, 512); + * %cstring_bounded_mutable(Char *in, 512); * void foo(in *x) { * while (*x) { * *x = toupper(*x); @@ -85,29 +58,13 @@ * */ - -%define %cstring_bounded_mutable(TYPEMAP,MAX) -%typemap(in) TYPEMAP(char temp[MAX+1]) { - char *t = PyString_AsString($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; - strncpy(temp,t,MAX); - $1 = ($1_ltype) temp; -} -%typemap(argout,fragment="t_output_helper") TYPEMAP { - PyObject *o; - $1[MAX] = 0; - o = PyString_FromString($1); - $result = t_output_helper($result,o); -} -%enddef - /* * %cstring_mutable(TYPEMAP [, expansion]) * * This macro is used to wrap a string that will mutate in place. * It may change size up to a user-defined expansion. * - * %cstring_mutable(char *in); + * %cstring_mutable(Char *in); * void foo(in *x) { * while (*x) { * *x = toupper(*x); @@ -117,172 +74,64 @@ * */ -%define %cstring_mutable(TYPEMAP,...) -%typemap(in) TYPEMAP { - char *t = PyString_AsString($input); - int n = PyString_Size($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; - $1 = ($1_ltype) t; -#if #__VA_ARGS__ == "" -#ifdef __cplusplus - $1 = ($1_ltype) new char[n+1]; -#else - $1 = ($1_ltype) malloc(n+1); -#endif -#else -#ifdef __cplusplus - $1 = ($1_ltype) new char[n+1+__VA_ARGS__]; -#else - $1 = ($1_ltype) malloc(n+1+__VA_ARGS__); -#endif -#endif - memmove($1,t,n); - $1[n] = 0; -} - -%typemap(argout,fragment="t_output_helper") TYPEMAP { - PyObject *o; - o = PyString_FromString($1); - $result = t_output_helper($result,o); -#ifdef __cplusplus - delete[] $1; -#else - free($1); -#endif -} -%enddef - /* * %cstring_output_maxsize(TYPEMAP, SIZE) * * This macro returns data in a string of some user-defined size. * - * %cstring_output_maxsize(char *outx, int max) { - * void foo(char *outx, int max) { + * %cstring_output_maxsize(Char *outx, int max) { + * void foo(Char *outx, int max) { * sprintf(outx,"blah blah\n"); * } */ -%define %cstring_output_maxsize(TYPEMAP, SIZE) -%typemap(in) (TYPEMAP, SIZE) { - $2 = PyInt_AsLong($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; -#ifdef __cplusplus - $1 = ($1_ltype) new char[$2+1]; -#else - $1 = ($1_ltype) malloc($2+1); -#endif -} -%typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) { - PyObject *o; - o = PyString_FromString($1); - $result = t_output_helper($result,o); -#ifdef __cplusplus - delete [] $1; -#else - free($1); -#endif -} -%enddef - /* * %cstring_output_withsize(TYPEMAP, SIZE) * - * This macro is used to return character data along with a size + * This macro is used to return Character data along with a size * parameter. * - * %cstring_output_maxsize(char *outx, int *max) { - * void foo(char *outx, int *max) { + * %cstring_output_maxsize(Char *outx, int *max) { + * void foo(Char *outx, int *max) { * sprintf(outx,"blah blah\n"); * *max = strlen(outx); * } */ - -%define %cstring_output_withsize(TYPEMAP, SIZE) -%typemap(in) (TYPEMAP, SIZE) { - int n = PyInt_AsLong($input); - if (SWIG_arg_fail($argnum)) SWIG_fail; -#ifdef __cplusplus - $1 = ($1_ltype) new char[n+1]; - $2 = ($2_ltype) new $*2_ltype; -#else - $1 = ($1_ltype) malloc(n+1); - $2 = ($2_ltype) malloc(sizeof($*2_ltype)); -#endif - *$2 = n; -} -%typemap(argout,fragment="t_output_helper") (TYPEMAP,SIZE) { - PyObject *o; - o = PyString_FromStringAndSize($1,*$2); - $result = t_output_helper($result,o); -#ifdef __cplusplus - delete [] $1; - delete $2; -#else - free($1); - free($2); -#endif -} -%enddef - + /* * %cstring_output_allocate(TYPEMAP, RELEASE) * - * This macro is used to return character data that was + * This macro is used to return Character data that was * allocated with new or malloc. * - * %cstring_output_allocated(char **outx, free($1)); - * void foo(char **outx) { - * *outx = (char *) malloc(512); + * %cstring_output_allocated(Char **outx, free($1)); + * void foo(Char **outx) { + * *outx = (Char *) malloc(512); * sprintf(outx,"blah blah\n"); * } */ -%define %cstring_output_allocate(TYPEMAP, RELEASE) -%typemap(in,numinputs=0) TYPEMAP($*1_ltype temp = 0) { - $1 = &temp; -} - -%typemap(argout,fragment="t_output_helper") TYPEMAP { - if (*$1) { - PyObject *o = PyString_FromString(*$1); - RELEASE; - $result = t_output_helper($result,o); - } -} -%enddef - /* * %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) * - * This macro is used to return character data that was + * This macro is used to return Character data that was * allocated with new or malloc. * - * %cstring_output_allocated(char **outx, int *sz, free($1)); - * void foo(char **outx, int *sz) { - * *outx = (char *) malloc(512); + * %cstring_output_allocated(Char **outx, int *sz, free($1)); + * void foo(Char **outx, int *sz) { + * *outx = (Char *) malloc(512); * sprintf(outx,"blah blah\n"); * *sz = strlen(outx); * } */ -%define %cstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) -%typemap(in,numinputs=0) (TYPEMAP, SIZE) ($*1_ltype temp = 0, $*2_ltype tempn) { - $1 = &temp; - $2 = &tempn; -} - -%typemap(argout,fragment="t_output_helper")(TYPEMAP,SIZE) { - if (*$1) { - PyObject *o = PyString_FromStringAndSize(*$1,*$2); - RELEASE; - $result = t_output_helper($result,o); - } -} -%enddef - - - - +%include +%include +%typemap_cstrings(%cstring, + char, + SWIG_AsCharPtr, + SWIG_AsCharPtrAndSize, + SWIG_FromCharPtr, + SWIG_FromCharArray); diff --git a/SWIG/Lib/python/cwstring.i b/SWIG/Lib/python/cwstring.i new file mode 100644 index 000000000..b9fe6cb04 --- /dev/null +++ b/SWIG/Lib/python/cwstring.i @@ -0,0 +1,137 @@ +/* + * cstring.i + * $Header$ + * + + * This file provides typemaps and macros for dealing with various forms + * of C character string handling. The primary use of this module + * is in returning character data that has been allocated or changed in + * some way. + */ + +/* + * %cwstring_input_binary(TYPEMAP, SIZE) + * + * Macro makes a function accept binary string data along with + * a size. + */ + +/* + * %cwstring_bounded_output(TYPEMAP, MAX) + * + * This macro is used to return a NULL-terminated output string of + * some maximum length. For example: + * + * %cwstring_bounded_output(wchar_t *outx, 512); + * void foo(wchar_t *outx) { + * sprintf(outx,"blah blah\n"); + * } + * + */ + +/* + * %cwstring_chunk_output(TYPEMAP, SIZE) + * + * This macro is used to return a chunk of binary string data. + * Embedded NULLs are okay. For example: + * + * %cwstring_chunk_output(wchar_t *outx, 512); + * void foo(wchar_t *outx) { + * memmove(outx, somedata, 512); + * } + * + */ + +/* + * %cwstring_bounded_mutable(TYPEMAP, SIZE) + * + * This macro is used to wrap a string that's going to mutate. + * + * %cwstring_bounded_mutable(wchar_t *in, 512); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +/* + * %cwstring_mutable(TYPEMAP [, expansion]) + * + * This macro is used to wrap a string that will mutate in place. + * It may change size up to a user-defined expansion. + * + * %cwstring_mutable(wchar_t *in); + * void foo(in *x) { + * while (*x) { + * *x = toupper(*x); + * x++; + * } + * } + * + */ + +/* + * %cwstring_output_maxsize(TYPEMAP, SIZE) + * + * This macro returns data in a string of some user-defined size. + * + * %cwstring_output_maxsize(wchar_t *outx, int max) { + * void foo(wchar_t *outx, int max) { + * sprintf(outx,"blah blah\n"); + * } + */ + +/* + * %cwstring_output_withsize(TYPEMAP, SIZE) + * + * This macro is used to return wchar_tacter data along with a size + * parameter. + * + * %cwstring_output_maxsize(wchar_t *outx, int *max) { + * void foo(wchar_t *outx, int *max) { + * sprintf(outx,"blah blah\n"); + * *max = strlen(outx); + * } + */ + +/* + * %cwstring_output_allocate(TYPEMAP, RELEASE) + * + * This macro is used to return wchar_tacter data that was + * allocated with new or malloc. + * + * %cwstring_output_allocated(wchar_t **outx, free($1)); + * void foo(wchar_t **outx) { + * *outx = (wchar_t *) malloc(512); + * sprintf(outx,"blah blah\n"); + * } + */ + +/* + * %cwstring_output_allocate_size(TYPEMAP, SIZE, RELEASE) + * + * This macro is used to return wchar_tacter data that was + * allocated with new or malloc. + * + * %cwstring_output_allocated(wchar_t **outx, int *sz, free($1)); + * void foo(wchar_t **outx, int *sz) { + * *outx = (wchar_t *) malloc(512); + * sprintf(outx,"blah blah\n"); + * *sz = strlen(outx); + * } + */ + + +%include +%include + +%typemap_cstrings(%cwstring, + wchar_t, + SWIG_AsWCharPtr, + SWIG_AsWCharPtrAndSize, + SWIG_FromWCharPtr, + SWIG_FromWCharArray); + diff --git a/SWIG/Lib/python/pymacros.swg b/SWIG/Lib/python/pymacros.swg index c4d264083..fff615664 100644 --- a/SWIG/Lib/python/pymacros.swg +++ b/SWIG/Lib/python/pymacros.swg @@ -39,24 +39,30 @@ #endif #endif -#if defined(__cplusplus) && defined(SWIG_CPLUSPLUS_CAST) +#if defined(__cplusplus) +#define SWIG_new(Type...) (new Type) +#define SWIG_new_copy(ptr,Type...) (new Type(*ptr)) #define SWIG_new_array(size,Type...) (new Type[(size)]) #define SWIG_delete(cptr) delete cptr #define SWIG_delete_array(cptr) delete[] cptr -#define SWIG_const_cast(a,Type...) const_cast(a) -#define SWIG_static_cast(a,Type...) static_cast(a) -#define SWIG_reinterpret_cast(a,Type...) reinterpret_cast(a) -#define SWIG_new_copy(ptr,Type...) (new Type(*ptr)) -#define SWIG_numeric_cast(a,Type...) static_cast(a) #else /* C case */ +#define SWIG_new(Type...) ((Type*)malloc(sizeof(Type))) +#define SWIG_new_copy(ptr,Type...) ((Type*)memcpy(malloc(sizeof(Type)),ptr,sizeof(Type))) #define SWIG_new_array(size,Type...) ((Type*) malloc((size)*sizeof(Type))) #define SWIG_delete(cptr) free((char*)cptr) #define SWIG_delete_array(cptr) free((char*)cptr) +#endif /* __cplusplus */ + +#if defined(__cplusplus) && defined(SWIG_CPLUSPLUS_CAST) +#define SWIG_const_cast(a,Type...) const_cast(a) +#define SWIG_static_cast(a,Type...) static_cast(a) +#define SWIG_reinterpret_cast(a,Type...) reinterpret_cast(a) +#define SWIG_numeric_cast(a,Type...) static_cast(a) +#else /* C case */ #define SWIG_const_cast(a,Type...) (Type)(a) #define SWIG_static_cast(a,Type...) (Type)(a) #define SWIG_reinterpret_cast(a,Type...) (Type)(a) #define SWIG_numeric_cast(a,Type...) (Type)(a) -#define SWIG_new_copy(ptr,Type...) ((Type*)memcpy(malloc(sizeof(Type)),ptr,sizeof(Type))) #endif /* __cplusplus */ diff --git a/SWIG/Lib/python/pystrbase.swg b/SWIG/Lib/python/pystrbase.swg index fbd63e9cb..c4f057401 100644 --- a/SWIG/Lib/python/pystrbase.swg +++ b/SWIG/Lib/python/pystrbase.swg @@ -311,8 +311,8 @@ %typemap(in, fragment=#SWIG_AsCharPtrAndSize) (Char *STRING, int LENGTH) (Char *buf, size_t size) { - int res = SWIG_AsCharPtrAndSize($input, &buf, &size); - if (!res) {SWIG_arg_fail($argnum);SWIG_fail;} + SWIG_AsCharPtrAndSize($input, &buf, &size); + if (SWIG_arg_fail($argnum)) SWIG_fail; $1 = ($1_ltype) buf; $2 = ($2_ltype) size - 1; } @@ -321,7 +321,8 @@ %typemap(in,fragment=#SWIG_AsCharPtrAndSize) (Char *STRING, int SIZE) (Char *buf, size_t size) { - if (!SWIG_AsCharPtrAndSize($input, &buf, &size)) {SWIG_arg_fail($argnum);SWIG_fail;} + SWIG_AsCharPtrAndSize($input, &buf, &size); + if (SWIG_arg_fail($argnum)) SWIG_fail; $1 = ($1_ltype) buf; $2 = ($2_ltype) size; } diff --git a/SWIG/Source/CParse/parser.y b/SWIG/Source/CParse/parser.y index b0309c746..ace366281 100644 --- a/SWIG/Source/CParse/parser.y +++ b/SWIG/Source/CParse/parser.y @@ -1428,7 +1428,7 @@ declaration : swig_directive { $$ = $1; } if (!Swig_error_count()) { static int last_error_line = -1; if (last_error_line != cparse_line) { - Swig_error(cparse_file, cparse_line,"Syntax error in input.\n"); + Swig_error(cparse_file, cparse_line,"Syntax error in input(1).\n"); last_error_line = cparse_line; skip_decl(); } @@ -2831,7 +2831,7 @@ c_constructor_decl : storage_class type LPAREN parms RPAREN ctor_end { } } if (err) { - Swig_error(cparse_file,cparse_line,"Syntax error in input.\n"); + Swig_error(cparse_file,cparse_line,"Syntax error in input(2).\n"); } } ; @@ -3597,7 +3597,7 @@ cpp_members : cpp_member cpp_members { { static int last_error_line = -1; if (last_error_line != cparse_line) { - Swig_error(cparse_file, cparse_line,"Syntax error in input.\n"); + Swig_error(cparse_file, cparse_line,"Syntax error in input(3).\n"); last_error_line = cparse_line; } }