示例#1
0
        /* Close the display device and delete the instance */
        public gsParamState_t DisplayDeviceClose()
        {
            int            code       = 0;
            gsParamState_t out_params = new gsParamState_t();

            out_params.result = GS_Result_t.gsOK;

            try
            {
                int code1 = GSAPI.gsapi_exit(dispInstance);
                if ((code == 0) || (code == gsConstants.E_QUIT))
                {
                    code = code1;
                }

                GSAPI.gsapi_delete_instance(dispInstance);
                dispInstance = IntPtr.Zero;
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
                out_params.result = GS_Result_t.gsFAILED;
            }
            return(out_params);
        }
示例#2
0
        /* Processing with gsapi_run_string for callback progress */
        private void gsBytesAsync(object data)
        {
            List <object>  genericlist    = data as List <object>;
            gsParamState_t Params         = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = Params;
            int            num_params     = Params.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            GCHandle       argPtrsStable  = new GCHandle();

            Byte[] Buffer = new Byte[gsConstants.GS_READ_BUFFER];

            int        code     = 0;
            int        exitcode = 0;
            var        Feed     = new GCHandle();
            var        FeedPtr  = new IntPtr();
            FileStream fs       = null;
            bool       cleanup  = true;

            try
            {
                /* Open the file */
                fs = new FileStream(Params.inputfile, FileMode.Open);
                var len = (int)fs.Length;

                code = GSAPI.gsapi_new_instance(out gsInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_new_instance error");
                }
                code = GSAPI.gsapi_set_stdio(gsInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_set_stdio error");
                }
                code = GSAPI.gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_set_arg_encoding error");
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((Params.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + Params.args[k];
                }

                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_init_with_args error");
                }

                /* Pin data buffer */
                Feed    = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
                FeedPtr = Feed.AddrOfPinnedObject();

                /* Now start feeding the input piece meal and do a call back
                 * with our progress */
                if (code == 0)
                {
                    int    count;
                    double perc;
                    int    total = 0;

                    GSAPI.gsapi_run_string_begin(gsInstance, 0, ref exitcode);
                    if (exitcode < 0)
                    {
                        code = exitcode;
                        throw new GhostscriptException("gsBytesAsync: gsapi_run_string_begin error");
                    }

                    while ((count = fs.Read(Buffer, 0, gsConstants.GS_READ_BUFFER)) > 0)
                    {
                        GSAPI.gsapi_run_string_continue(gsInstance, FeedPtr, count, 0, ref exitcode);
                        if (exitcode < 0)
                        {
                            code = exitcode;
                            throw new GhostscriptException("gsBytesAsync: gsapi_run_string_continue error");
                        }

                        total = total + count;
                        perc  = 100.0 * (double)total / (double)len;
                        gsProgressChanged(Params, (int)perc);
                    }
                    GSAPI.gsapi_run_string_end(gsInstance, 0, ref exitcode);
                    if (exitcode < 0)
                    {
                        code = exitcode;
                        throw new GhostscriptException("gsBytesAsync: gsapi_run_string_end error");
                    }
                }
            }
            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            catch (Exception except)
            {
                /* Could be a file io issue */
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            finally
            {
                if (cleanup)
                {
                    fs.Close();

                    /* Free pinned items */
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();
                    Feed.Free();

                    /* gs clean up */
                    int code1 = GSAPI.gsapi_exit(gsInstance);
                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        code = code1;
                    }

                    GSAPI.gsapi_delete_instance(gsInstance);
                    Params.return_code = code;

                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        Params.result = GS_Result_t.gsOK;
                        Result        = Params;
                    }
                    else
                    {
                        Params.result = GS_Result_t.gsFAILED;
                        Result        = Params;
                    }
                    gsInstance = IntPtr.Zero;
                }
            }
            gsCompleted(Result);
            return;
        }
示例#3
0
        /* Process command line with gsapi_init_with_args */
        private void gsFileAsync(object data)
        {
            List <object>  genericlist    = data as List <object>;
            gsParamState_t Params         = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = Params;
            int            num_params     = Params.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            GCHandle       argPtrsStable  = new GCHandle();
            int            code           = 0;
            bool           cleanup        = true;

            try
            {
                code = GSAPI.gsapi_new_instance(out gsInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_new_instance error");
                }
                code = GSAPI.gsapi_set_stdio(gsInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_set_stdio error");
                }
                code = GSAPI.gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_set_arg_encoding error");
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((Params.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + Params.args[k];
                }

                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_init_with_args error");
                }
            }
            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            finally
            {
                if (cleanup)
                {
                    /* All the pinned items need to be freed so the GC can do its job */
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();

                    int code1 = GSAPI.gsapi_exit(gsInstance);
                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        code = code1;
                    }

                    GSAPI.gsapi_delete_instance(gsInstance);
                    Params.return_code = code;

                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        Params.result = GS_Result_t.gsOK;
                        Result        = Params;
                    }
                    else
                    {
                        Params.result = GS_Result_t.gsFAILED;
                        Result        = Params;
                    }
                    gsInstance = IntPtr.Zero;
                }
            }

            /* Completed. */
            gsCompleted(Result);
            return;
        }