Skip to content

Commit

Permalink
Add UTF8 marshaler to MapScript C# (#5275)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7bc388d)
  • Loading branch information
szekerest committed May 19, 2016
1 parent b509261 commit 8771b59
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions mapscript/csharp/swig_csharp_extensions.i
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,58 @@
%pragma(csharp) imclasscode=%{
static $imclassname() {
}

public class UTF8Marshaler : ICustomMarshaler {
static UTF8Marshaler static_instance;

public IntPtr MarshalManagedToNative(object managedObj) {
if (managedObj == null)
return IntPtr.Zero;
if (!(managedObj is string))
throw new MarshalDirectiveException(
"UTF8Marshaler must be used on a string.");

// not null terminated
byte[] strbuf = System.Text.Encoding.UTF8.GetBytes((string)managedObj);
IntPtr buffer = Marshal.AllocHGlobal(strbuf.Length + 1);
Marshal.Copy(strbuf, 0, buffer, strbuf.Length);

// write the terminating null
Marshal.WriteByte(buffer, strbuf.Length, 0);
return buffer;
}

public object MarshalNativeToManaged(IntPtr pNativeData) {
int len = Marshal.PtrToStringAnsi(pNativeData).Length;
byte[] utf8data = new byte[len];
Marshal.Copy(pNativeData, utf8data, 0, len);
return System.Text.Encoding.UTF8.GetString(utf8data);
}

public void CleanUpNativeData(IntPtr pNativeData) {
Marshal.FreeHGlobal(pNativeData);
}

public void CleanUpManagedData(object managedObj) {
}

public int GetNativeDataSize() {
return -1;
}

public static ICustomMarshaler GetInstance(string cookie) {
if (static_instance == null) {
return static_instance = new UTF8Marshaler();
}
return static_instance;
}
}
%}

%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]",
outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]")
char *, char *&, char[ANY], char[] "string"

%typemap(csout, excode=SWIGEXCODE) SWIGTYPE {
/* %typemap(csout, excode=SWIGEXCODE) SWIGTYPE */
$&csclassname ret = new $&csclassname($imcall, true, null);$excode
Expand Down

0 comments on commit 8771b59

Please sign in to comment.