Add support for wchar_t * and std::wstring Unicode strings on Linux
Initial contributions for Linux provided in issue #1233, modified to work on both Windows and Linux. Dual support is possible by detecting the sizeof wchar_t which is different on each of these systems.
This commit is contained in:
parent
05580ff1e7
commit
ed42422d1c
8 changed files with 327 additions and 46 deletions
|
|
@ -15,6 +15,28 @@
|
|||
#include <string>
|
||||
%}
|
||||
|
||||
%fragment("Swig_csharp_UTF16ToWString", "header") %{
|
||||
/* For converting from .NET UTF16 (2 byte unicode) strings. wchar_t is 2 bytes on Windows, 4 bytes on Linux. */
|
||||
static std::wstring Swig_csharp_UTF16ToWString(const wchar_t *str) {
|
||||
if (sizeof(wchar_t) == 2) {
|
||||
return std::wstring(str);
|
||||
} else {
|
||||
const unsigned short *pBegin((const unsigned short *)(str));
|
||||
const unsigned short *ptr(pBegin);
|
||||
|
||||
while (*ptr != 0)
|
||||
++ptr;
|
||||
|
||||
std::wstring result;
|
||||
result.reserve(ptr - pBegin);
|
||||
while(pBegin != ptr)
|
||||
result.push_back(*pBegin++);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
|
||||
%naturalvar wstring;
|
||||
|
|
@ -31,22 +53,22 @@ class wstring;
|
|||
%typemap(csdirectorin) wstring "$iminput"
|
||||
%typemap(csdirectorout) wstring "$cscall"
|
||||
|
||||
%typemap(in, canthrow=1) wstring
|
||||
%typemap(in, canthrow=1, fragment="Swig_csharp_UTF16ToWString") wstring
|
||||
%{ if (!$input) {
|
||||
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
|
||||
return $null;
|
||||
}
|
||||
$1.assign($input); %}
|
||||
%typemap(out) wstring %{ $result = SWIG_csharp_wstring_callback($1.c_str()); %}
|
||||
$1 = Swig_csharp_UTF16ToWString($input); %}
|
||||
%typemap(out) wstring %{ $result = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %}
|
||||
|
||||
%typemap(directorout, canthrow=1) wstring
|
||||
%typemap(directorout, canthrow=1) wstring
|
||||
%{ if (!$input) {
|
||||
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
|
||||
return $null;
|
||||
}
|
||||
$result.assign($input); %}
|
||||
|
||||
%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %}
|
||||
%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %}
|
||||
|
||||
%typemap(csin) wstring "$csinput"
|
||||
%typemap(csout, excode=SWIGEXCODE) wstring {
|
||||
|
|
@ -72,14 +94,14 @@ class wstring;
|
|||
%typemap(csdirectorin) const wstring & "$iminput"
|
||||
%typemap(csdirectorout) const wstring & "$cscall"
|
||||
|
||||
%typemap(in, canthrow=1) const wstring &
|
||||
%typemap(in, canthrow=1, fragment="Swig_csharp_UTF16ToWString") const wstring &
|
||||
%{ if (!$input) {
|
||||
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
|
||||
return $null;
|
||||
}
|
||||
std::wstring $1_str($input);
|
||||
std::wstring $1_str(Swig_csharp_UTF16ToWString($input));
|
||||
$1 = &$1_str; %}
|
||||
%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_callback($1->c_str()); %}
|
||||
%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_with_length_callback($1->c_str(), (int)$1->size()); %}
|
||||
|
||||
%typemap(csin) const wstring & "$csinput"
|
||||
%typemap(csout, excode=SWIGEXCODE) const wstring & {
|
||||
|
|
@ -97,7 +119,7 @@ class wstring;
|
|||
$1_str = $input;
|
||||
$result = &$1_str; %}
|
||||
|
||||
%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %}
|
||||
%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1->size()); %}
|
||||
|
||||
%typemap(csvarin, excode=SWIGEXCODE2) const wstring & %{
|
||||
set {
|
||||
|
|
|
|||
|
|
@ -11,28 +11,49 @@
|
|||
#if !defined(SWIG_CSHARP_NO_WSTRING_HELPER)
|
||||
#if !defined(SWIG_CSHARP_WSTRING_HELPER_)
|
||||
#define SWIG_CSHARP_WSTRING_HELPER_
|
||||
|
||||
%fragment("<wchar.h>"); // TODO: %fragment("<wchar.h", "runtime");
|
||||
|
||||
%insert(runtime) %{
|
||||
/* Callback for returning strings to C# without leaking memory */
|
||||
typedef void * (SWIGSTDCALL* SWIG_CSharpWStringHelperCallback)(const wchar_t *);
|
||||
static SWIG_CSharpWStringHelperCallback SWIG_csharp_wstring_callback = NULL;
|
||||
typedef void * (SWIGSTDCALL* SWIG_CSharpWStringHelperCallback)(const wchar_t *, int length);
|
||||
static SWIG_CSharpWStringHelperCallback SWIG_csharp_wstring_with_length_callback = NULL;
|
||||
%}
|
||||
|
||||
%insert(header) %{
|
||||
static void * SWIG_csharp_wstring_callback(const wchar_t *s) {
|
||||
return SWIG_csharp_wstring_with_length_callback(s, (int)wcslen(s));
|
||||
}
|
||||
%}
|
||||
|
||||
|
||||
%pragma(csharp) imclasscode=%{
|
||||
protected class SWIGWStringHelper {
|
||||
|
||||
[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]
|
||||
public delegate string SWIGWStringDelegate(global::System.IntPtr message);
|
||||
static SWIGWStringDelegate wstringDelegate = new SWIGWStringDelegate(CreateWString);
|
||||
public delegate string SWIGWStringDelegate(global::System.IntPtr message, int length);
|
||||
static SWIGWStringDelegate wstringUTF16Delegate = new SWIGWStringDelegate(CreateWStringFromUTF16);
|
||||
static SWIGWStringDelegate wstringUTF32Delegate = new SWIGWStringDelegate(CreateWStringFromUTF32);
|
||||
|
||||
[global::System.Runtime.InteropServices.DllImport("$dllimport", EntryPoint="SWIGRegisterWStringCallback_$module")]
|
||||
public static extern void SWIGRegisterWStringCallback_$module(SWIGWStringDelegate wstringDelegate);
|
||||
public static extern void SWIGRegisterWStringCallback_$module(SWIGWStringDelegate wstringUTF16Delegate, SWIGWStringDelegate wstringUTF32Delegate);
|
||||
|
||||
static string CreateWString([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString) {
|
||||
return global::System.Runtime.InteropServices.Marshal.PtrToStringUni(cString);
|
||||
static string CreateWStringFromUTF16([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
|
||||
return global::System.Runtime.InteropServices.Marshal.PtrToStringUni(cString, length);
|
||||
}
|
||||
|
||||
public static string CreateWStringFromUTF32([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
|
||||
if (length == 0)
|
||||
return string.Empty;
|
||||
|
||||
byte[] buffer = new byte[length * 4];
|
||||
global::System.Runtime.InteropServices.Marshal.Copy(cString, buffer, 0, buffer.Length);
|
||||
byte[] utf8buffer = global::System.Text.Encoding.Convert(global::System.Text.Encoding.UTF32, global::System.Text.Encoding.UTF8, buffer);
|
||||
return global::System.Text.Encoding.Default.GetString(utf8buffer);
|
||||
}
|
||||
|
||||
static SWIGWStringHelper() {
|
||||
SWIGRegisterWStringCallback_$module(wstringDelegate);
|
||||
SWIGRegisterWStringCallback_$module(wstringUTF16Delegate, wstringUTF32Delegate);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,8 +64,8 @@ static SWIG_CSharpWStringHelperCallback SWIG_csharp_wstring_callback = NULL;
|
|||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
#endif
|
||||
SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStringHelperCallback callback) {
|
||||
SWIG_csharp_wstring_callback = callback;
|
||||
SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStringHelperCallback callback_utf16, SWIG_CSharpWStringHelperCallback callback_utf32) {
|
||||
SWIG_csharp_wstring_with_length_callback = sizeof(wchar_t) == 2 ? callback_utf16 : callback_utf32;
|
||||
}
|
||||
%}
|
||||
#endif // SWIG_CSHARP_WSTRING_HELPER_
|
||||
|
|
@ -77,13 +98,60 @@ SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStri
|
|||
%typemap(typecheck) wchar_t = char;
|
||||
|
||||
// wchar_t *
|
||||
%typemap(ctype) wchar_t * "wchar_t *"
|
||||
%typemap(imtype, inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]", out="global::System.IntPtr" ) wchar_t * "string"
|
||||
|
||||
%fragment("Swig_csharp_UTF16ToWCharPtr", "header") %{
|
||||
/* For converting from .NET UTF16 (2 byte unicode) strings. wchar_t is 2 bytes on Windows, 4 bytes on Linux. */
|
||||
static wchar_t * Swig_csharp_UTF16ToWCharPtr(const wchar_t *str) {
|
||||
if (sizeof(wchar_t) == 2) {
|
||||
return (wchar_t *)str;
|
||||
} else {
|
||||
wchar_t *result = 0;
|
||||
|
||||
if (str) {
|
||||
const unsigned short *pBegin((const unsigned short *)(str));
|
||||
const unsigned short *pEnd(pBegin);
|
||||
wchar_t *ptr = 0;
|
||||
|
||||
while (*pEnd != 0)
|
||||
++pEnd;
|
||||
|
||||
#ifdef __cplusplus
|
||||
result = ptr = new wchar_t[pEnd - pBegin + 1];
|
||||
#else
|
||||
result = ptr = (wchar_t *)malloc(sizeof(wchar_t) * (pEnd - pBegin + 1));
|
||||
#endif
|
||||
while(pBegin != pEnd)
|
||||
*ptr++ = *pBegin++;
|
||||
*ptr++ = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
||||
%fragment("Swig_csharp_UTF16ToWCharPtrFree", "header") %{
|
||||
static void Swig_csharp_UTF16ToWCharPtrFree(wchar_t *str) {
|
||||
if (sizeof(wchar_t) != 2) {
|
||||
#ifdef __cplusplus
|
||||
delete [] str;
|
||||
#else
|
||||
free(str);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(ctype, out="void *") wchar_t * "wchar_t *"
|
||||
%typemap(imtype,
|
||||
inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
|
||||
outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]"
|
||||
) wchar_t * "string"
|
||||
%typemap(cstype) wchar_t * "string"
|
||||
|
||||
%typemap(csin) wchar_t * "$csinput"
|
||||
%typemap(csout, excode=SWIGEXCODE) wchar_t * {
|
||||
string ret = global::System.Runtime.InteropServices.Marshal.PtrToStringUni($imcall);$excode
|
||||
string ret = $imcall;$excode
|
||||
return ret;
|
||||
}
|
||||
%typemap(csvarin, excode=SWIGEXCODE2) wchar_t * %{
|
||||
|
|
@ -92,12 +160,100 @@ SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStri
|
|||
} %}
|
||||
%typemap(csvarout, excode=SWIGEXCODE2) wchar_t * %{
|
||||
get {
|
||||
string ret = global::System.Runtime.InteropServices.Marshal.PtrToStringUni($imcall);$excode
|
||||
string ret = $imcall;$excode
|
||||
return ret;
|
||||
} %}
|
||||
|
||||
%typemap(in) wchar_t * %{ $1 = ($1_ltype)$input; %}
|
||||
%typemap(out) wchar_t * %{ $result = (wchar_t *)$1; %}
|
||||
%typemap(in, fragment="Swig_csharp_UTF16ToWCharPtr") wchar_t *
|
||||
%{ $1 = Swig_csharp_UTF16ToWCharPtr($input); %}
|
||||
|
||||
%typemap(out) wchar_t * %{ $result = $1 ? SWIG_csharp_wstring_callback((wchar_t *)$1) : 0; %}
|
||||
|
||||
%typemap(freearg, fragment="Swig_csharp_UTF16ToWCharPtrFree") wchar_t *
|
||||
%{ Swig_csharp_UTF16ToWCharPtrFree($1); %}
|
||||
|
||||
%typemap(typecheck) wchar_t * = char *;
|
||||
|
||||
|
||||
/* Default typemap for handling wchar_t * members (based on char * in swig.swg) */
|
||||
|
||||
#ifdef __cplusplus
|
||||
%typemap(memberin,fragment="<wchar.h>") wchar_t * {
|
||||
delete [] $1;
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
%typemap(memberin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
%typemap(globalin,fragment="<wchar.h>") wchar_t * {
|
||||
delete [] $1;
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
%typemap(globalin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
#else
|
||||
%typemap(memberin,fragment="<wchar.h>") wchar_t * {
|
||||
free($1);
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
%typemap(memberin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
%typemap(globalin,fragment="<wchar.h>") wchar_t * {
|
||||
free($1);
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
%typemap(globalin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
|
||||
if ($input && sizeof(wchar_t) == 2) {
|
||||
$1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
|
||||
wcscpy((wchar_t *)$1, (const wchar_t *)$input);
|
||||
} else {
|
||||
$1 = $input;
|
||||
$input = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue