|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Name: csharp_strings.i |
| 4 | + * Project: GDAL CSharp Interface |
| 5 | + * Purpose: Typemaps for C# marshalling to/from UTF-8. |
| 6 | + * Authors: Tamas Szekeres, szekerest@gmail.com |
| 7 | + * Michael Bucari-Tovo, mbucari1@gmail.com |
| 8 | + * |
| 9 | + ****************************************************************************** |
| 10 | + * Copyright (c) 2007, Tamas Szekeres; 2026, Michael Bucari-Tovo |
| 11 | + * |
| 12 | + * SPDX-License-Identifier: MIT |
| 13 | + *****************************************************************************/ |
| 14 | + |
| 15 | + |
| 16 | +%insert(runtime) %{ |
| 17 | +#include <stdlib.h> |
| 18 | +#include <string.h> |
| 19 | +#include <stdio.h> |
| 20 | +%} |
| 21 | + |
| 22 | + |
| 23 | +/****************************************************************************** |
| 24 | + * Marshaler for NULL terminated UTF-8 encoded strings * |
| 25 | + ******************************************************************************/ |
| 26 | + |
| 27 | +%insert(runtime) %{ |
| 28 | +/* Callback for returning strings to C# without leaking memory */ |
| 29 | +typedef char * (SWIGSTDCALL* CSharpUtf8StringHelperCallback)(const char *); |
| 30 | +static CSharpUtf8StringHelperCallback SWIG_csharp_string_callback = NULL; |
| 31 | +%} |
| 32 | + |
| 33 | +%pragma(csharp) imclasscode=%{ |
| 34 | + public class Utf8StringHelper { |
| 35 | + |
| 36 | + public delegate System.IntPtr Utf8StringDelegate(System.IntPtr message); |
| 37 | + |
| 38 | + static Utf8StringDelegate stringDelegate = new Utf8StringDelegate($modulePINVOKE.Utf8UnmanagedToLengthPrefixedUtf16Unmanaged); |
| 39 | + |
| 40 | + [global::System.Runtime.InteropServices.DllImport("$dllimport", EntryPoint="RegisterUtf8StringCallback_$module")] |
| 41 | + public static extern void RegisterUtf8StringCallback_$module(Utf8StringDelegate stringDelegate); |
| 42 | + |
| 43 | + static Utf8StringHelper() { |
| 44 | + RegisterUtf8StringCallback_$module(stringDelegate); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static readonly Utf8StringHelper utf8StringHelper = new Utf8StringHelper(); |
| 49 | + |
| 50 | + public unsafe static IntPtr Utf8UnmanagedToLengthPrefixedUtf16Unmanaged(IntPtr pUtf8Bts) |
| 51 | + { |
| 52 | + if (pUtf8Bts == IntPtr.Zero) |
| 53 | + return IntPtr.Zero; |
| 54 | + |
| 55 | + byte* pStringUtf8 = (byte*)pUtf8Bts; |
| 56 | + int len = 0; |
| 57 | + while (pStringUtf8[len] != 0) len++; |
| 58 | + |
| 59 | + var charCount = System.Text.Encoding.UTF8.GetCharCount(pStringUtf8, len); |
| 60 | + var bstrDest = Marshal.AllocHGlobal(sizeof(int) + charCount * sizeof(char)); |
| 61 | + *(int*)bstrDest = charCount; |
| 62 | + |
| 63 | + System.Text.Encoding.UTF8.GetChars(pStringUtf8, len, (char*)IntPtr.Add(bstrDest, sizeof(int)), charCount); |
| 64 | + return bstrDest; |
| 65 | + } |
| 66 | + |
| 67 | + public unsafe static string LengthPrefixedUtf16UnmanagedToString(IntPtr pBstr) |
| 68 | + { |
| 69 | + if (pBstr == IntPtr.Zero) |
| 70 | + return null; |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + int charCount = *(int*)pBstr; |
| 75 | + if (charCount < 0) |
| 76 | + return null; |
| 77 | + int charStart = sizeof(int) / sizeof(char); |
| 78 | + return new string((char*)pBstr, charStart, charCount); |
| 79 | + } |
| 80 | + finally |
| 81 | + { |
| 82 | + Marshal.FreeHGlobal(pBstr); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public unsafe static IntPtr StringToUtf8Unmanaged(string str) |
| 87 | + { |
| 88 | + if (str == null) |
| 89 | + return IntPtr.Zero; |
| 90 | + |
| 91 | + int byteCount = System.Text.Encoding.UTF8.GetByteCount(str); |
| 92 | + IntPtr unmanagedString = Marshal.AllocHGlobal(byteCount + 1); |
| 93 | + |
| 94 | + byte* ptr = (byte*)unmanagedString.ToPointer(); |
| 95 | + fixed (char *pStr = str) |
| 96 | + { |
| 97 | + System.Text.Encoding.UTF8.GetBytes(pStr, str.Length, ptr, byteCount); |
| 98 | + // null-terminate |
| 99 | + ptr[byteCount] = 0; |
| 100 | + } |
| 101 | + |
| 102 | + return unmanagedString; |
| 103 | + } |
| 104 | +%} |
| 105 | + |
| 106 | +%insert(runtime) %{ |
| 107 | +#ifdef __cplusplus |
| 108 | +extern "C" |
| 109 | +#endif |
| 110 | +SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringHelperCallback callback) { |
| 111 | + SWIG_csharp_string_callback = callback; |
| 112 | +} |
| 113 | +%} |
| 114 | + |
| 115 | +/****************************************************************************** |
| 116 | + * Apply typemaps for all SWIG string types and for const char *utf8_string * |
| 117 | + ******************************************************************************/ |
| 118 | + |
| 119 | +/* Changing csin and imtype typemaps for string types defined by SWIG in csharp.swg */ |
| 120 | + |
| 121 | +%typemap(imtype) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) "IntPtr" |
| 122 | + |
| 123 | +%typemap(in) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) %{ |
| 124 | + $1 = ($1_ltype)$input; |
| 125 | +%} |
| 126 | + |
| 127 | +%typemap(out) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) |
| 128 | +%{ |
| 129 | + $result = SWIG_csharp_string_callback((const char *)$1); |
| 130 | +%} |
| 131 | + |
| 132 | +%typemap(csin) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) "$modulePINVOKE.StringToUtf8Unmanaged($csinput)" |
| 133 | + |
| 134 | +%typemap(csout, excode=SWIGEXCODE) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) |
| 135 | + { |
| 136 | + /* %typemap(csout) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) */ |
| 137 | + IntPtr cPtr = $imcall; |
| 138 | + string ret = $modulePINVOKE.LengthPrefixedUtf16UnmanagedToString(cPtr); |
| 139 | + $excode |
| 140 | + return ret; |
| 141 | + } |
| 142 | + |
| 143 | +%typemap(csvarout, excode=SWIGEXCODE2) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) %{ |
| 144 | + get { |
| 145 | + /* %typemap(csvarout) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) */ |
| 146 | + IntPtr cPtr = $imcall; |
| 147 | + string ret = $modulePINVOKE.LengthPrefixedUtf16UnmanagedToString(cPtr); |
| 148 | + $excode |
| 149 | + return ret; |
| 150 | + } %} |
0 commit comments