📄 gvpgs.c
字号:
}
gs_addmess("\n");
fclose(optfile);
}
infile = fopen(filename, "rb");
if (infile == (FILE *)NULL) {
gs_addmess(usage);
sprintf(gsarg, "Can't find file '%s'\n", filename);
gs_addmess(gsarg);
return FALSE;
}
/* find length of file */
fseek(infile, 0L, SEEK_END);
lsize = ftell(infile);
lsize = lsize / 100; /* to get percent values */
if (lsize <= 0)
lsize = 1;
fseek(infile, 0L, SEEK_SET);
ldone = 0;
/* build options list */
strcpy(gsarg, "@");
strcat(gsarg, option);
gsarg[strlen(gsarg) + 1] = '\0';
return TRUE;
}
/*********************************************************************/
/* stdio functions */
static int GSDLLCALL
gsdll_stdin(void *instance, char *buf, int len)
{
char mess[MAXSTR];
sprintf(mess,"stdin callback not supported: %p %d\n", buf, len);
gs_addmess(mess);
return 0; /* EOF */
}
static int GSDLLCALL
gsdll_stdout(void *instance, const char *str, int len)
{
gs_addmess_count(str, len);
return len;
}
static int GSDLLCALL
gsdll_stderr(void *instance, const char *str, int len)
{
gs_addmess_count(str, len);
return len;
}
/* Poll the caller for cooperative multitasking. */
/* If this function is NULL, polling is not needed */
int GSDLLCALL gsdll_poll(void *handle)
{
return 0;
}
/*********************************************************************/
void
gs_clear_gsdll(void)
{
gsdll.hmodule = (HMODULE)NULL;
gsdll.revision_number = 0;
gsdll.minst = NULL;
gsdll.revision = NULL;
gsdll.new_instance = NULL;
gsdll.delete_instance = NULL;
gsdll.set_stdio = NULL;
gsdll.set_display_callback = NULL;
gsdll.set_poll = NULL;
gsdll.run_string_begin = NULL;
gsdll.run_string_continue = NULL;
gsdll.run_string_end = NULL;
gsdll.exit = NULL;
}
/* free GS DLL */
/* This should only be called when gsdll_execute_cont has returned */
/* TRUE means no error */
BOOL
gs_free_dll(void)
{
char buf[MAXSTR];
APIRET rc = 0;
int code;
if (gsdll.hmodule == (HMODULE)NULL)
return TRUE;
if (gsdll.exit && gsdll.minst) {
code = gsdll.exit(gsdll.minst);
#ifdef DEBUG
sprintf(buf,"gsapi_exit returns %d\n", code);
gs_addmess(buf);
#endif
}
if (gsdll.minst && gsdll.delete_instance)
gsdll.delete_instance(gsdll.minst);
if (gsdll.hmodule) {
rc = DosFreeModule(gsdll.hmodule);
#ifdef DEBUG
sprintf(buf,"DosFreeModule returns %ld\n", rc);
gs_addmess(buf);
#endif
}
gs_clear_gsdll();
return !rc;
}
/* Cleanup when DLL couldn't be loaded */
void
gs_load_dll_cleanup(void)
{
char buf[MAXSTR];
gs_free_dll();
gs_clear_gsdll();
sprintf(buf, "Can't load DLL %s", gsdllname);
message_box(buf, 0);
}
/* load GS DLL if not already loaded */
/* return TRUE if OK */
BOOL
gs_load_dll(void)
{
APIRET rc;
char buf[MAXSTR+40];
const char *shortname;
const char *dllname;
gsapi_revision_t rv;
if (gsdll.hmodule)
return TRUE;
gs_clear_gsdll();
dllname = gsdllname;
sprintf(buf, "Trying to load %s\n", dllname);
if (debug)
gs_addmess(buf);
memset(buf, 0, sizeof(buf));
rc = DosLoadModule((PBYTE)buf, sizeof(buf), (PCSZ)dllname, &gsdll.hmodule);
if (rc) {
/* failed */
/* try once more - which bug are we dodging? */
rc = DosLoadModule((PBYTE)buf, sizeof(buf), (PCSZ)dllname, &gsdll.hmodule);
}
if (rc) {
/* failed */
/* try once more, this time on system search path */
if ((shortname = strrchr((char *)gsdllname, '\\')) == (const char *)NULL)
shortname = gsdllname;
dllname = shortname;
sprintf(buf, "Trying to load %s\n", dllname);
if (debug)
gs_addmess(buf);
rc = DosLoadModule((PBYTE)buf, sizeof(buf), (PCSZ)dllname, &gsdll.hmodule);
}
if (rc) {
sprintf(buf, "Can't load Ghostscript DLL %s \nDosLoadModule rc = %ld\n", gsdllname, rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if (debug)
gs_addmess("Loaded Ghostscript DLL\n");
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_REVISION",
(PFN *) (&gsdll.revision))) != 0) {
sprintf(buf, "Can't find GSAPI_REVISION, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
/* check DLL version */
if (gsdll.revision(&rv, sizeof(rv)) != 0) {
sprintf(buf, "Unable to identify Ghostscript DLL revision - it must be newer than needed.\n");
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
gsdll.revision_number = rv.revision;
if ( (gsdll.revision_number < GS_REVISION_MIN) ||
(gsdll.revision_number > GS_REVISION_MAX) ) {
sprintf(buf, "\nWrong version of DLL found.\n\
Found version %ld\n Need version %ld - %ld\n",
gsdll.revision_number,
(long)GS_REVISION_MIN, (long)GS_REVISION_MAX);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_NEW_INSTANCE",
(PFN *) (&gsdll.new_instance))) != 0) {
sprintf(buf, "Can't find GSAPI_NEW_INSTANCE, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_DELETE_INSTANCE",
(PFN *) (&gsdll.delete_instance))) != 0) {
sprintf(buf, "Can't find GSAPI_DELETE_INSTANCE, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_SET_STDIO",
(PFN *) (&gsdll.set_stdio))) != 0) {
sprintf(buf, "Can't find GSAPI_SET_STDIO, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_SET_DISPLAY_CALLBACK",
(PFN *) (&gsdll.set_display_callback))) != 0) {
sprintf(buf, "Can't find GSAPI_SET_DISPLAY_CALLBACK, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_SET_POLL",
(PFN *) (&gsdll.set_poll))) != 0) {
sprintf(buf, "Can't find GSAPI_SET_POLL, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0,
(PCSZ)"GSAPI_INIT_WITH_ARGS",
(PFN *) (&gsdll.init_with_args))) != 0) {
sprintf(buf, "Can't find GSAPI_INIT_WITH_ARGS, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_RUN_STRING_BEGIN",
(PFN *) (&gsdll.run_string_begin))) != 0) {
sprintf(buf, "Can't find GSAPI_RUN_STRING_BEGIN, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_RUN_STRING_CONTINUE",
(PFN *) (&gsdll.run_string_continue))) != 0) {
sprintf(buf, "Can't find GSAPI_RUN_STRING_CONTINUE, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_RUN_STRING_END",
(PFN *) (&gsdll.run_string_end))) != 0) {
sprintf(buf, "Can't find GSAPI_RUN_STRING_END, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
if ((rc = DosQueryProcAddr(gsdll.hmodule, 0, (PCSZ)"GSAPI_EXIT",
(PFN *) (&gsdll.exit))) != 0) {
sprintf(buf, "Can't find GSAPI_EXIT, rc = %ld\n", rc);
gs_addmess(buf);
gs_load_dll_cleanup();
return FALSE;
}
return TRUE;
}
/* Thread which loads Ghostscript DLL and sends input file to DLL */
void
gs_thread(void *arg)
{
char buf[MAXSTR];
int len;
int code;
HAB hab;
int gs_argc;
char *gs_argv[3];
int exit_code;
hab = WinInitialize(0);
if (!gs_load_dll())
return;
gs_argv[0] = gsdllname;
gs_argv[1] = gsarg;
gs_argv[2] = NULL;
gs_argc = 2;
code = gsdll.new_instance(&gsdll.minst, NULL);
if (code) {
sprintf(buf,"gsapi_new_instance returns %d\n", code);
gs_addmess(buf);
}
if (!code) {
gsdll.set_stdio(gsdll.minst, gsdll_stdin, gsdll_stdout, gsdll_stderr);
gsdll.set_poll(gsdll.minst, gsdll_poll);
code = gsdll.init_with_args(gsdll.minst, gs_argc, gs_argv);
if (debug) {
sprintf(buf,"gsapi_init_with_args returns %d\n", code);
gs_addmess(buf);
}
}
if (!code) {
code = gsdll.run_string_begin(gsdll.minst, 0, &exit_code);
if (!code) {
while ((len = fread(buf, 1, sizeof(buf), infile)) != 0) {
code = gsdll.run_string_continue(gsdll.minst, buf, len,
0, &exit_code);
ldone += len;
if (pcdone != (int)(ldone / lsize)) {
pcdone = (int)(ldone / lsize);
WinPostMsg(hwnd_client, WM_PCUPDATE, (MPARAM)pcdone, 0);
}
if (code == e_NeedInput)
code = 0;
if (code) {
sprintf(buf, "gsdll_execute_cont returns %d\n", code);
gs_addmess(buf);
break;
}
}
if (!code)
gsdll.run_string_end(gsdll.minst, 0, &exit_code);
}
fclose(infile);
}
gs_free_dll();
WinTerminate(hab);
/* tell main thread to shut down */
if ((code == 0) && (!debug))
WinPostMsg(hwnd_client, WM_QUIT, 0, 0);
else {
WinSetWindowPos(hwnd_frame, HWND_TOP, 0, 0, 0, 0,
SWP_ACTIVATE | SWP_SHOW | SWP_RESTORE);
}
gstid = 0;
}
/* About Dialog Box */
MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
switch(msg) {
case WM_INITDLG:
WinSetWindowText( WinWindowFromID(hwnd, ABOUT_VERSION),
(PCSZ)GSVIEW_DOT_VERSION );
break;
case WM_COMMAND:
switch(SHORT1FROMMP(mp1)) {
case DID_OK:
WinDismissDlg(hwnd, TRUE);
return (MRESULT)TRUE;
}
break;
}
return WinDefDlgProc(hwnd, msg, mp1, mp2);
}
void
show_about(void)
{
WinDlgBox(HWND_DESKTOP, hwnd_frame, AboutDlgProc, 0, IDD_ABOUT, 0);
}
void
saveas(void)
{
FILEDLG FileDlg;
FILE *f;
memset(&FileDlg, 0, sizeof(FILEDLG));
FileDlg.cbSize = sizeof(FILEDLG);
FileDlg.fl = FDS_CENTER | FDS_SAVEAS_DIALOG;
WinFileDlg(HWND_DESKTOP, hwnd_frame, &FileDlg);
if (FileDlg.lReturn == DID_OK) {
if ((f = fopen(FileDlg.szFullFile, "wb")) == (FILE *)NULL) {
message_box("Can't create file", 0);
return;
}
fwrite(twbuf, 1, twend, f);
fclose(f);
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -