home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / POSTSCPT / GSVIEW / SRC / GVWPIPE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-17  |  7.8 KB  |  277 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvwpipe.c */
  19. /* pipe support for Windows GSview */
  20. #include "gvwin.h"
  21.  
  22. /* routines to be visible outside this module */
  23. extern void pipeinit();
  24. extern FILE *pipeopen(void);    /* open pipe for first time */
  25. extern void pipeclose(void);    /* finished with pipe, close & delete temp files */
  26. extern void pipereset(void);    /* pipe is empty, do cleanup */
  27. extern void piperequest(void);    /* request from gswin for pipe data */
  28. extern void pipeflush(void);    /* start sending data through pipe */
  29. extern BOOL is_pipe_done(void);    /* true if pipe has just been reset */
  30.  
  31. /* internal to this module */
  32.  
  33. /* imitation pipes using SHAREABLE GLOBAL MEMORY */
  34. #ifdef __WIN32__
  35. #define PIPE_DATASIZE 16380    /* maximum block size */
  36. #else
  37. #define PIPE_DATASIZE 4092    /* maximum block size */
  38. #endif
  39. /* Data is passed to gswin in a global shareable memory block.
  40.  * The global handle is passed in lParam and the byte count
  41.  * is stored in the first word of the global memory block.
  42.  * The maximum number of bytes passed is PIPE_DATASIZE.
  43.  * EOF is signified by count = 0 (hglobal must still be valid)
  44.  */
  45. /* In gsview 1.0, Ghostscript 2.6.1 and earlier, 
  46.  * The global handle was passed in the LOWORD of lParam 
  47.  * and the HIWORD contained the byte count.
  48.  * This was changed in gsview 1.1, Ghostscript 3.0 so that a
  49.  * 32 bit handle could be used for Win32  */
  50. /* In gsview 1.2, Windows NT or 95 uses a memory mapped file */
  51.  
  52. char pipe_name[MAXSTR];        /* pipe filename */
  53. FILE *pipe_file;        /* pipe file */
  54. fpos_t pipe_wpos;
  55. fpos_t pipe_rpos;
  56. BOOL pipe_empty;
  57. int piperead(char *buf, int size);
  58. char *pipebuf;
  59. HANDLE pipe_hmapfile;
  60. LPBYTE pipe_mapptr;
  61.  
  62. /* this is called before gswin is started */
  63. /* so we can tell when we get the first piperequest */
  64. void
  65. pipeinit(void)
  66. {
  67.     pipe_empty = FALSE;    /* so we wait for first request */
  68. }
  69.  
  70. FILE *
  71. pipeopen(void)
  72. {
  73.     if (pipe_file != (FILE *)NULL) {
  74.         fclose(pipe_file);
  75.         pipe_file = (FILE *)NULL;
  76.         unlink(pipe_name);
  77.         pipe_name[0] = '\0';
  78.     }
  79. #ifdef __WIN32__
  80.     if (is_winnt || is_win95) {
  81.         char buf[64];
  82.         if (pipe_hmapfile != 0) {
  83.         if (pipe_mapptr != NULL)
  84.             UnmapViewOfFile(pipe_mapptr);
  85.         pipe_mapptr = NULL;
  86.         CloseHandle(pipe_hmapfile);
  87.         pipe_hmapfile = 0;
  88.         }
  89.         sprintf(buf,"gsview_%d", hwndimg);
  90.         pipe_hmapfile = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, 
  91.                 PIPE_DATASIZE+sizeof(WORD), buf);
  92.         pipe_mapptr = MapViewOfFile(pipe_hmapfile, FILE_MAP_WRITE, 0, 0, 0);
  93.         if (pipe_mapptr == NULL) {
  94.         message_box("Can't memory map file",0);
  95.         return NULL;
  96.         }
  97.     }
  98. #endif
  99.     if ((pipe_file = gp_open_scratch_file(szScratch, pipe_name, "w+b")) == (FILE *)NULL) {
  100.         gserror(IDS_PIPE_EOPEN, NULL, NULL, SOUND_ERROR);
  101.         unlink(pipe_name);
  102.         pipe_name[0] = '\0';
  103.         return (FILE *)NULL;
  104.     }
  105.     pipebuf = malloc(PIPE_DATASIZE);
  106.     if (pipebuf == (char *)NULL) {
  107.         gserror(IDS_PIPE_EMEM, NULL, NULL, SOUND_ERROR);
  108.         fclose(pipe_file);
  109.         return (FILE *)NULL;
  110.     }
  111.     pipereset();
  112.     return pipe_file;
  113. }
  114.  
  115. void
  116. pipeclose(void)
  117. {
  118. HGLOBAL hglobal;
  119. LPBYTE lpb;
  120.     if (pipebuf != (char *)NULL) {
  121.         free(pipebuf);
  122.         pipebuf = (char *)NULL;
  123.     }
  124.     if (pipe_file != (FILE *)NULL) {
  125.         fclose(pipe_file);
  126.         pipe_file = (FILE *)NULL;
  127.         unlink(pipe_name);
  128.         pipe_name[0] = '\0';
  129.     }
  130.     if (hwndtext != (HWND)NULL) {
  131. #if !defined(__WIN32__) && defined(GS261)
  132.       if (option.gsversion == IDM_GS261) {
  133.         /* send an EOF (zero length block) */
  134.         hglobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, 1);
  135.         if (hglobal == (HGLOBAL)NULL) {
  136.             gserror(IDS_PIPE_EMEM, NULL, NULL, SOUND_ERROR);
  137.             return;
  138.         }
  139.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, MAKELPARAM(hglobal,0));
  140.       }
  141.       else {
  142. #endif
  143. #ifdef __WIN32__
  144.         if (is_winnt || is_win95) {
  145.         /* send an EOF (zero length block) */
  146.         *((WORD *)pipe_mapptr) = 0;
  147.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, (LPARAM)hwndimg);
  148.         }
  149.         else
  150. #endif
  151.         {
  152.         /* send an EOF (zero length block) */
  153.         hglobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(WORD));
  154.         if (hglobal == (HGLOBAL)NULL) {
  155.             gserror(IDS_PIPE_EMEM, NULL, NULL, SOUND_ERROR);
  156.             return;
  157.         }
  158.         lpb = GlobalLock(hglobal);
  159.         *((WORD FAR *)lpb) = 0;
  160.         GlobalUnlock(hglobal);
  161.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, (LPARAM)hglobal);
  162.         }
  163. #if !defined(__WIN32__) && defined(GS261)
  164.       }
  165. #endif
  166.     }
  167.     pipe_empty = TRUE;
  168. }
  169.  
  170. /* rpos and wpos are now empty so reset the file */
  171. void
  172. pipereset(void)
  173. {
  174.     if ( (pipe_file == (FILE *)NULL) || (pipe_name[0] == '\0') )
  175.         return;
  176.     if ((pipe_file = freopen(pipe_name, "w+b", pipe_file)) == (FILE *)NULL) {
  177.         gserror(IDS_PIPE_EOPEN, NULL, NULL, SOUND_ERROR);
  178.         unlink(pipe_name);
  179.         pipe_name[0] = '\0';
  180.         return;
  181.     }
  182.     fgetpos(pipe_file, &pipe_rpos);
  183.     fgetpos(pipe_file, &pipe_wpos);
  184.     pipe_empty = TRUE;
  185.     info_wait(IDS_NOWAIT);
  186. }
  187.  
  188. /* give another block of data to gswin */
  189. /* called from WndImgProc */
  190. void
  191. piperequest(void)
  192. {
  193. HGLOBAL hglobal;
  194. LPBYTE lpb;
  195. UINT count;
  196.     if (pipe_file == (FILE *)NULL) {
  197.         pipe_empty = TRUE;
  198.         return;
  199.     }
  200.  
  201.     count = piperead(pipebuf, PIPE_DATASIZE);
  202.     if (count==0)
  203.         return; 
  204.  
  205. #ifdef __WIN32__
  206.     if (is_winnt || is_win95) {
  207.         memcpy(pipe_mapptr+sizeof(WORD), pipebuf, count);
  208.         *((WORD *)pipe_mapptr) = (WORD)count;
  209.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, (LPARAM)hwndimg);
  210.     }
  211.     else
  212. #endif
  213.      {
  214.         hglobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, PIPE_DATASIZE + sizeof(WORD));
  215.         if (hglobal == (HGLOBAL)NULL) {
  216.         gserror(IDS_PIPE_EMEM, NULL, NULL, SOUND_ERROR);
  217.         return;
  218.         }
  219. #if !defined(__WIN32__) && defined(GS261)
  220.       if (option.gsversion == IDM_GS261) {
  221.         lpb = GlobalLock(hglobal);
  222.         _fmemcpy(lpb, pipebuf, count);
  223.         GlobalUnlock(hglobal);
  224.         /* we may be processing SendMessage so use PostMessage to avoid lockups */
  225.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, MAKELPARAM(hglobal,count));
  226.       }
  227.       else {
  228. #endif
  229.         lpb = GlobalLock(hglobal);
  230.         *((WORD FAR *)lpb) = (WORD)count;
  231.         _fmemcpy(lpb+sizeof(WORD), pipebuf, count);
  232.         GlobalUnlock(hglobal);
  233.         /* we may be processing SendMessage so use PostMessage to avoid lockups */
  234.         PostMessage(hwndtext, WM_GSVIEW, PIPE_DATA, (LPARAM)hglobal);
  235. #if !defined(__WIN32__) && defined(GS261)
  236.       }
  237. #endif
  238.     }
  239. }
  240.  
  241. /* write pipe_file to pipe */
  242. void
  243. pipeflush(void)
  244. {
  245.     if (pipe_empty) {
  246.         pipe_empty = FALSE;
  247.         piperequest();    /* repeat the request */
  248.     }
  249.     info_wait(IDS_WAIT);
  250. }
  251.  
  252. /* true  if pipereset was last called */
  253. /* false if pipeflush was last called */
  254. int
  255. is_pipe_done(void)
  256. {
  257.     return pipe_empty;
  258. }
  259.  
  260. /* read a block from pipe */
  261. /* return count of characters read */
  262. /* reset pipe if empty */
  263. int
  264. piperead(char *buf, int size)
  265. {
  266. int rcount;
  267.     fflush(pipe_file);
  268.     fgetpos(pipe_file,&pipe_wpos);
  269.     fsetpos(pipe_file,&pipe_rpos);
  270.     rcount = fread(buf, 1, size, pipe_file);
  271.     fgetpos(pipe_file,&pipe_rpos);
  272.     fsetpos(pipe_file,&pipe_wpos);
  273.     if (rcount == 0)
  274.        pipereset();
  275.     return rcount;
  276. }
  277.