normalize cstring.i to use fragments, and add cwstring.i as a subproduct

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7406 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-09-06 06:22:10 +00:00
commit c1416a59a5
11 changed files with 602 additions and 214 deletions

254
Lib/python/cstrbase.swg Normal file
View file

@ -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 <pytuplehlp.swg>
%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

View file

@ -10,72 +10,45 @@
* some way.
*/
%include <pytuplehlp.swg>
/* %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 <pystrings.swg>
%include <cstrbase.swg>
%typemap_cstrings(%cstring,
char,
SWIG_AsCharPtr,
SWIG_AsCharPtrAndSize,
SWIG_FromCharPtr,
SWIG_FromCharArray);

137
Lib/python/cwstring.i Normal file
View file

@ -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 <pywstrings.swg>
%include <cstrbase.swg>
%typemap_cstrings(%cwstring,
wchar_t,
SWIG_AsWCharPtr,
SWIG_AsWCharPtrAndSize,
SWIG_FromWCharPtr,
SWIG_FromWCharArray);

View file

@ -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<Type >(a)
#define SWIG_static_cast(a,Type...) static_cast<Type >(a)
#define SWIG_reinterpret_cast(a,Type...) reinterpret_cast<Type >(a)
#define SWIG_new_copy(ptr,Type...) (new Type(*ptr))
#define SWIG_numeric_cast(a,Type...) static_cast<Type >(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<Type >(a)
#define SWIG_static_cast(a,Type...) static_cast<Type >(a)
#define SWIG_reinterpret_cast(a,Type...) reinterpret_cast<Type >(a)
#define SWIG_numeric_cast(a,Type...) static_cast<Type >(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 */

View file

@ -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;
}