1919namespace Cube . Pdf . Ghostscript ;
2020
2121using System ;
22+ using System . Collections . Generic ;
23+ using System . Linq ;
2224using System . Runtime . InteropServices ;
2325using Cube . FileSystem ;
2426using 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
0 commit comments