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
This commit is contained in:
parent
e64517f6a7
commit
2e2617edce
11 changed files with 602 additions and 214 deletions
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue