home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_02 / wpipe.c < prev    next >
C/C++ Source or Header  |  1991-03-17  |  3KB  |  199 lines

  1. /* wpipe.c
  2.  *
  3.  *    wpipefin () - this function 'pipes' the contents of a file
  4.  *            as input to the program, bypassing the keyboard
  5.  *
  6.  *    use: setup a file with input data.
  7.  *         call wpipefin() to install the pipe.
  8.  *         subsequent calls to wgetc() or wreadc() will access the pipe.
  9.  *         the pipe removes itself when completed
  10.  *
  11.  *     wpipefout () - this function traps keystrokes into a file
  12.  *             continues to trap keystrokes
  13.  *             until stopcode is pressed
  14.  *
  15.  * NOTE: mouse use causes value of wmouse to be saved/restored
  16.  *
  17.  *
  18.  */
  19. #include "wsys.h"
  20.  
  21.  
  22. static  int         reader (void);
  23. static  void W_NEAR   getnext (void);
  24. static  int         in_installed =0;
  25. static  WMOUSE      nextmouse;
  26. static  int             nextch;
  27. static  void        (*callhome)(void) = NULL;
  28. static    FILE        *inf = NULL;
  29.  
  30.  
  31.  
  32.  
  33. int  wpipefin ( char *filename, void (*u_callhome)(void) )
  34.     {
  35.  
  36.     if ( in_installed )
  37.         {
  38.         return (-2);
  39.         }
  40.  
  41.     inf = fopen ( filename, "rb" );
  42.     if ( ! inf )
  43.         {
  44.         return (-1);
  45.         }
  46.  
  47.     getnext();
  48.  
  49.     if ( ! nextch )
  50.         {
  51.         return (-1);    /* empty file or mouse error */
  52.         }
  53.  
  54.     wpipein = reader;
  55.     callhome = u_callhome;
  56.     in_installed = 1;
  57.  
  58.  
  59.  
  60.     return (0);    /* wpipefin */
  61.     }
  62.  
  63.  
  64.  
  65. static  int  reader (void)
  66.     {
  67.     int pipeval;
  68.  
  69.     pipeval =  nextch ;
  70.  
  71.     if ( pipeval == MOUSE )
  72.         {
  73.         memcpy ( &wmouse, &nextmouse, sizeof(WMOUSE) );
  74.         }
  75.  
  76.     /* note that the pipe has to read one keystroke ahead
  77.      * so that it knows when to un-install itself
  78.      */
  79.     getnext();
  80.  
  81.  
  82.  
  83.     return     (pipeval);
  84.     }
  85.  
  86.  
  87.  
  88. static void W_NEAR getnext (void)
  89.     {
  90.     int n;
  91.     if (1 == ( n= fread ( &nextch, sizeof (int), 1, inf))  )
  92.         {
  93.         /* successfull read
  94.          */
  95.         if ( nextch == MOUSE )
  96.             {
  97.             n=fread ( &nextmouse, sizeof (WMOUSE), 1, inf);
  98.             }
  99.         }
  100.  
  101.     if ( n != 1 )
  102.         {
  103.         /* end of file */
  104.         nextch = 0;
  105.         fclose (inf);
  106.         wpipein = NULL;
  107.         in_installed =0;
  108.  
  109.         /* inform calling routine that end of pipe is reached
  110.          */
  111.         if ( callhome )
  112.             {
  113.             (*callhome)();
  114.             }
  115.  
  116.         }
  117.  
  118.  
  119.  
  120.     return; /* getnext */
  121.     }
  122.  
  123.  
  124.     /*------------------  input pipe -----------------*/
  125.  
  126.  
  127.     /*------------------  output pipe -----------------*/
  128.  
  129.  
  130. static void writer (int x);
  131. static int  stopcode = 0;
  132. static FILE *outf;
  133. static int out_installed =0;
  134.  
  135.  
  136.  
  137. void wpipefout ( char *filename, int ustopcode )
  138.     {
  139.     if ( out_installed )
  140.         {
  141.         return;
  142.         }
  143.  
  144.     if ( NULL == (outf= fopen (filename, "wb") ) )
  145.         {
  146.         werror ('W',"WMACRO - output file error");
  147.         }
  148.     stopcode = ustopcode;
  149.  
  150.     wpipeout = writer;
  151.  
  152.     out_installed = 1;
  153.  
  154.     return; /* wpipefout */
  155.     }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. static void writer (int key )
  162.     {
  163.     int n;
  164.  
  165.     if ( key == stopcode )
  166.         {
  167.         /* un-install the pipe
  168.          */
  169.         wpipeout = NULL;
  170.         out_installed = 0;
  171.         fclose (outf);
  172.         }
  173.     else
  174.         {
  175.         if ( 1 == (n=fwrite (&key, sizeof(int), 1, outf) ) )
  176.             {
  177.             if ( key == MOUSE )
  178.                 {
  179.                 n=fwrite (&wmouse, sizeof(WMOUSE), 1, outf);
  180.                 }
  181.             }
  182.         if ( n != 1 )
  183.             {
  184.             /* file write error */
  185.             werror ('W',"WMACRO - output file error");
  186.             }
  187.  
  188.         }
  189.  
  190.     return;    /* writer */
  191.     }
  192.  
  193.  
  194.  
  195.  
  196. /*--------------------- END of WPIPE.C ---------------------*/
  197.  
  198.  
  199.