fixes for char[], const char[], and cleaning wchar_t

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5930 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2004-05-27 11:25:31 +00:00
commit d0c59e6529
6 changed files with 294 additions and 453 deletions

View file

@ -1,11 +1,8 @@
/* ------------------------------------------------------------
* utility methods for wstrings handling
* utility methods for wchar_t strings
* ------------------------------------------------------------ */
%types(wchar_t *);
%{
#include <wchar.h>
%}
%fragment("SWIG_AsWCharPtrAndSize","header") %{
SWIGSTATIC(int)
SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize)
@ -149,226 +146,11 @@ SWIG_FromWCharArray(const wchar_t * carray, size_t size)
* The plain wchar_t * handling
* ------------------------------------------------------------ */
/* in */
%typemap_pystring(wchar_t,
SWIG_AsWCharPtr,
SWIG_AsWCharPtrAndSize,
SWIG_FromWCharPtr,
SWIG_AsNewWCharPtr,
SWIG_AsWCharArray,
SWIG_FromWCharArray);
%typemap(in,fragment="SWIG_AsWCharPtr")
wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const
"if (!SWIG_AsWCharPtr($input, (wchar_t **)&$1)) SWIG_fail;";
%typemap(in,fragment="SWIG_AsWCharPtr")
wchar_t const*&, wchar_t *const&, wchar_t const *const &
{
$*ltype temp;
if (!SWIG_AsWCharPtr($input, (wchar_t **)&temp)) SWIG_fail;
$1 = &temp;
}
/* out */
%typemap(out,fragment="SWIG_FromWCharPtr")
wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const
"$result = SWIG_FromWCharPtr($1);";
%typemap(out,fragment="SWIG_FromWCharPtr")
wchar_t *const &, wchar_t const* &, wchar_t const *const &
"$result = SWIG_FromWCharPtr(*$1);";
/* varin */
%typemap(varin,fragment="SWIG_AsNewWCharPtr") wchar_t *
{
wchar_t *cptr = 0;
if (!SWIG_AsNewWCharPtr($input, &cptr)) {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
if ($1) swig_delete_array($1);
$1 = cptr;
}
%typemap(varin,fragment="SWIG_AsNewWCharPtr",
warning="451:Setting const wchar_t * variable may leak memory")
const wchar_t *
{
wchar_t *cptr;
if (!SWIG_AsNewWCharPtr($input, &cptr)) {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
$1 = cptr;
}
/* varout */
%typemap(varout,fragment="SWIG_FromWCharPtr")
wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const
"$result = SWIG_FromWCharPtr($1);";
/* constant */
%typemap(constcode,fragment="SWIG_FromWCharPtr")
wchar_t *, wchar_t const*, wchar_t * const, wchar_t const* const
"PyDict_SetItemString(d,\"$symname\", SWIG_FromWCharPtr($value));";
/* directorin */
%typemap(directorin,fragment="SWIG_FromWCharPtr")
wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const,
wchar_t const *&, wchar_t *const &, wchar_t const *const &
"$input = SWIG_NewPointerObj((wchar_t *)($1_name), $descriptor(wchar_t *), 0);"
/* "$input = SWIG_FromWCharPtr($1_name);"; */
/* directorout */
%typemap(directorout,fragment="SWIG_AsWCharPtr")
wchar_t *, wchar_t const*, wchar_t *const, wchar_t const* const
"if (!SWIG_AsWCharPtr($input, (wchar_t **) &$result)) {
Swig::DirectorTypeMismatchException(\"Error converting Python object into wchar_t *\");
}";
%typemap(directorout,fragment="SWIG_AsWCharPtr")
wchar_t const *&, wchar_t *const &, wchar_t const *const &
{
wchar_t * temp;
if (!SWIG_AsWCharPtr($input, &temp)) {
Swig::DirectorTypeMismatchException("Error converting Python object into wchar_t *");
}
$result = ($1_ltype) &temp;
}
/* typecheck */
%typemap(typecheck,precedence=SWIG_TYPECHECK_UNISTRING,
fragment="SWIG_AsWCharPtr")
wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const,
wchar_t const*&, wchar_t *const&, wchar_t const *const &
"$1 = SWIG_AsWCharPtr($input, (wchar_t **)(0));";
/* throws */
%typemap(throws,fragment="SWIG_FromWCharPtr")
wchar_t *, wchar_t const*, wchar_t * const, wchar_t const* const
{
PyErr_SetObject(PyExc_RuntimeError, SWIG_FromWCharPtr($1));
SWIG_fail;
}
/* ------------------------------------------------------------
* Fix size wcharacter array wchar_t[ANY] handling
* ------------------------------------------------------------ */
/* memberin and globalin typemaps */
%typemap(memberin) wchar_t [ANY]
{
if ($input) memcpy($1,$input,$1_dim0*sizeof(wchar_t));
else memset($1,0,$1_dim0*sizeof(wchar_t));
}
%typemap(globalin) wchar_t [ANY]
{
if ($input) memcpy($1,$input,$1_dim0*sizeof(wchar_t));
else memset($1,0,$1_dim0*sizeof(wchar_t));
}
/* in */
%typemap(in,fragment="SWIG_AsWCharArray")
wchar_t [ANY], const wchar_t [ANY]
{
wchar_t temp[$1_dim0];
if (!SWIG_AsWCharArray($input, temp, $1_dim0)) SWIG_fail;
$1 = temp;
}
/* out */
%typemap(out,fragment="SWIG_FromWCharArray")
wchar_t [ANY], const wchar_t [ANY]
"$result = SWIG_FromWCharArray($1, $1_dim0);";
/* varin */
%typemap(varin,fragment="SWIG_AsWCharArray")
wchar_t [ANY]
{
if (!SWIG_AsWCharArray($input, $1, $1_dim0)) {
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "C variable '$name ($1_ltype)'");
return 1;
}
}
/* varout */
%typemap(varout,fragment="SWIG_FromWCharArray")
wchar_t [ANY], const wchar_t [ANY]
"$result = SWIG_FromWCharArray($1, $1_dim0);";
/* constants */
%typemap(constcode,fragment="SWIG_FromWCharArray")
wchar_t [ANY], const wchar_t [ANY]
"PyDict_SetItemString(d,\"$symname\", SWIG_FromWCharArray($value, $value_dim0));";
/* directorin */
%typemap(directorin,fragment="SWIG_FromWCharArray")
wchar_t [ANY], const wchar_t [ANY]
"$input = SWIG_FromWCharArray($1_name, $1_dim0);";
/* directorout */
%typemap(directorout,fragment="SWIG_AsWCharArray")
wchar_t [ANY], const wchar_t [ANY] (wchar_t temp[$result_dim0])
{
if (!SWIG_AsWCharArray($input, temp, $result_dim0)) {
Swig::DirectorTypeMismatchException("Error converting Python object into wchar_t[$result_dim0]");
}
$result = temp;
}
/* typecheck */
%typemap(typecheck,precedence=SWIG_TYPECHECK_UNISTRING,
fragment="SWIG_AsWCharArray")
wchar_t [ANY], const wchar_t[ANY]
{
return SWIG_AsWCharArray($input, (wchar_t **)0, (size_t *)0);
}
/* throw */
%typemap(throws,fragment="SWIG_FromWCharArray")
wchar_t[ANY], const wchar_t[ANY] {
PyErr_SetObject(PyExc_RuntimeError, SWIG_FromWCharArray($1,$1_dim0));
SWIG_fail;
}
/* ------------------------------------------------------------
* --- String & length ---
* ------------------------------------------------------------ */
/* Here len doesn't include the '0' terminator */
%typemap(in, fragment="SWIG_AsWCharPtrAndSize")
(wchar_t *STRING, int LENGTH) (wchar_t *buf, size_t size)
{
int res = SWIG_AsWCharPtrAndSize($input, &buf, &size);
if (!res) SWIG_fail;
$1 = ($1_ltype) buf;
$2 = ($2_ltype) size - 1;
}
/* Here size includes the '0' terminator */
%typemap(in,fragment="SWIG_AsWCharPtrAndSize")
(wchar_t *STRING, int SIZE) (wchar_t *buf, size_t size)
{
if (!SWIG_AsWCharPtrAndSize($input, &buf, &size)) SWIG_fail;
$1 = ($1_ltype) buf;
$2 = ($2_ltype) size;
}