Skip to content

Commit b083c4d

Browse files
committed
Refactor GsApi methods
1 parent a9c3428 commit b083c4d

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

Libraries/Generating/Sources/Internal/GsApi.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
namespace Cube.Pdf.Ghostscript;
2020

2121
using System;
22+
using System.Collections.Generic;
23+
using System.Linq;
2224
using System.Runtime.InteropServices;
2325
using Cube.FileSystem;
2426
using Cube.Text.Extensions;
@@ -71,30 +73,22 @@ public static GsInformation Information
7173
/// </summary>
7274
///
7375
/* --------------------------------------------------------------------- */
74-
public static void Invoke(string[] args, string tmp) => SetTemp(tmp, () =>
76+
public static void Invoke(string[] raw, string tmp) => SetTemp(tmp, () =>
7577
{
7678
_ = NativeMethods.NewInstance(out var core, IntPtr.Zero);
7779
if (core == IntPtr.Zero) throw new GsApiException(GsApiStatus.UnknownError, "gsapi_new_instance");
78-
79-
IntPtr[] utf8argv = new IntPtr[args.Length];
80-
for (int i = 0; i < utf8argv.Length; i++)
81-
{
82-
utf8argv[i] = NativeUtf8FromString(args[i]);
83-
}
80+
var args = new List<IntPtr>();
8481

8582
try
8683
{
84+
foreach (var e in raw) args.Add(ToUtf8(e));
8785
NativeMethods.SetArgEncoding(core, 1 /*GS_ARG_ENCODING_UTF8*/ );
88-
var code = NativeMethods.InitWithArgs(core, utf8argv.Length, utf8argv);
86+
var code = NativeMethods.InitWithArgs(core, args.Count, args.ToArray());
8987
if (code < 0 && code != (int)GsApiStatus.Quit && code != (int)GsApiStatus.Info) throw new GsApiException(code);
9088
}
9189
finally
9290
{
93-
for (int i = 0; i < utf8argv.Length; i++)
94-
{
95-
Marshal.FreeHGlobal(utf8argv[i]);
96-
}
97-
91+
foreach (var e in args) Marshal.FreeHGlobal(e);
9892
_ = NativeMethods.Exit(core);
9993
NativeMethods.DeleteInstance(core);
10094
}
@@ -103,24 +97,31 @@ public static void Invoke(string[] args, string tmp) => SetTemp(tmp, () =>
10397
#endregion
10498

10599
#region Implementations
100+
106101
/* --------------------------------------------------------------------- */
107102
///
108-
/// NativeUtf8FromString
103+
/// ToUtf8
109104
///
110105
/// <summary>
111-
/// Convert string to nativeUTF8 ptr. *remember to clean up with a call to Marshal.FreeHGlobal.*
106+
/// Convert string to Native UTF-8 pointer.
112107
/// </summary>
113108
///
109+
/// <remarks>
110+
/// Remember to clean up with a call to Marshal.FreeHGlobal.
111+
/// </remarks>
112+
///
114113
/* --------------------------------------------------------------------- */
115-
public static IntPtr NativeUtf8FromString(string managedString)
114+
private static IntPtr ToUtf8(string src)
116115
{
117-
int len = System.Text.Encoding.UTF8.GetByteCount(managedString);
118-
byte[] buffer = new byte[len + 1]; // null-terminator allocated
119-
System.Text.Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
120-
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
121-
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
122-
return nativeUtf8;
116+
var bytes = System.Text.Encoding.UTF8.GetByteCount(src);
117+
var buffer = new byte[bytes + 1]; // Null-terminator allocated
118+
System.Text.Encoding.UTF8.GetBytes(src, 0, src.Length, buffer, 0);
119+
120+
var dest = Marshal.AllocHGlobal(buffer.Length);
121+
Marshal.Copy(buffer, 0, dest, buffer.Length);
122+
return dest;
123123
}
124+
124125
/* --------------------------------------------------------------------- */
125126
///
126127
/// SetTemp

Libraries/Generating/Sources/Internal/GsApiNative.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal static class NativeMethods
6363
/// SetArgEncoding
6464
///
6565
/// <summary>
66-
/// Sets encoding of arguments supplied via the gsapi interface
66+
/// Sets encoding of arguments supplied via the Ghostscript API.
6767
/// </summary>
6868
///
6969
/* --------------------------------------------------------------------- */

0 commit comments

Comments
 (0)