home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / cmanual-3.0.lha / CManual / Amiga / Tools / Pager / Pager.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  5KB  |  148 lines

  1. /* Program Pager.c -- print text file on 2 sides of paper
  2.  
  3. Programmer: Dick Taylor, 99 Valley View Rd, Glastonbury CT 06033 USA
  4. Date:       April 1991
  5.  
  6. History:  I wrote this program to print Anders Bjerin's C Manual from 
  7. the Fred Fish disk collection.
  8.  
  9. Preparation:  I make a separate run of Pager for each chapter.  In CLI 
  10. I use JOIN to combine the chapter text with all examples for that chapter,
  11. to make one text file in ram:.  If you wish to improve the readability
  12. of this JOIN'ed file, you could edit it to add a few blank lines between
  13. the examples.
  14.  
  15. To run:  Use CLI to start Pager.  At start of execution, you are prompted
  16. to enter--
  17.     1) Input file name
  18.     2) Number of lines printed per page.  I use 60 for 8 1/2 x 11 inch pages.
  19.     3) First page number.  Obviously start with 1 for chapter 1.  For
  20.         following chapters, I recommend that you always use an odd number,
  21.         so that the page numbers will appear on the outside page corner.
  22.         This means that sometimes you will need to skip an even number
  23.         following the end of the previous chapter.
  24.  
  25. On the first pass, Pager prints the first page and every second following
  26. page--usually the odd page numbers, while the second page and every second
  27. following are saved in a temporary file in ram:--usually the even numbers.
  28. At the end of this step, the program pauses and prompts you to take out the
  29. printer paper, turn it over, position it to start printing on the back of
  30. the first page.  Then you start it running again.
  31.  
  32. Assumption:  Line 1 of the text file is the chapter title.  This will be
  33.         printed at the top of each page, along with the page number.
  34. */
  35.  
  36. #include <stdio.h>
  37. #include <exec/types.h>
  38.  
  39. #define LINESIZE  81
  40. #define TITLESIZE 25
  41.  
  42. main ()
  43. {
  44.  int  line_ct = 0, PageNum, FirstPageNum, i, NumLinesPage;
  45.  FILE *in_file, *printer, *temp_file, *fopen();
  46.  char buff[LINESIZE], in_name[25], title[TITLESIZE];
  47.  char static TempName[] = "ram:Even#Pages";
  48.  char SwPrinter = TRUE;        /* Printer switch, T=printer, F=temp*/
  49.  
  50.  
  51.  printf ("Enter input file name:\n");
  52.  scanf (" %24s", in_name);
  53.  in_file = fopen(in_name, "r");
  54.  if (in_file == NULL)
  55.     {printf ("Cannot open input file, terminate\n"); exit(1); }
  56.  
  57.  printf ("Enter number of lines per printed page:\n");
  58.  printf ("(Use 60 for 8 1/2 x 11 inch page size.)\n");
  59.  scanf (" %d", &NumLinesPage);
  60.  
  61.  printf ("Enter page number of first page to be printed:\n");
  62.  scanf (" %d", &PageNum);
  63.  FirstPageNum = PageNum;
  64.  
  65.  printer = fopen("prt:", "w");
  66.  if (printer == NULL)
  67.     {printf ("Cannot open printer, terminate\n"); exit(2); }
  68.  
  69.  temp_file = fopen(TempName, "w");
  70.  if (temp_file == NULL)
  71.     {printf ("Cannot open temp file for write, terminate\n"); exit(3); }
  72.  
  73.  fgets(title,TITLESIZE,in_file);    /* Read title = 1st line of input file*/
  74.  
  75.  for (i=0; i<25; ++i)        /* Remove 'newline' from title[] */
  76.     if (title[i] == '\n') title[i] = ' ';
  77.  
  78.  fprintf(printer,"%27c %-24s %18c Page %2d\n\n",' ',title,' ',PageNum);
  79.  
  80.  fgets(buff,LINESIZE,in_file);  /* Read a line of input */
  81.  
  82.  while (! feof(in_file))    /* Loop to read input file */
  83.  {
  84.     line_ct += 1;            /* Increm line count */
  85.     if (line_ct >= NumLinesPage || buff[0] == '\f')
  86.     {
  87.         line_ct = 0;
  88.         PageNum += 1;        /* Increm page count */
  89.         if (SwPrinter)
  90.         {
  91.             SwPrinter = FALSE;    /* Set switch to temp file */
  92.             if (PageNum > FirstPageNum + 1)
  93.                 fprintf(temp_file,"\fPage %2d %19c %-24s\n\n",PageNum,' ',title);
  94.                                 /* Page eject,print page no. & blank line */
  95.             else
  96.                 fprintf(temp_file,"Page %2d %19c %-24s\n\n",PageNum,' ',title);
  97.                     /* page 2 -- No page eject, Print page no. & blank line */
  98.         }
  99.         else
  100.         {
  101.             SwPrinter = TRUE;
  102.             fprintf(printer,"\f%27c %-24s %18c Page %2d\n\n",' ',title,' ',PageNum);
  103.                             /* print page no. & blank line */
  104.         }
  105.     }
  106.  
  107.         /* If have a form feed char, then replace with a blank char, */
  108.         /* because form feed already used with title line. */
  109.     if (buff[0] == '\f')
  110.          buff[0] = ' ';
  111.  
  112.     if (SwPrinter) fputs (buff,printer);    /* Send line to printer */
  113.     else fputs (buff,temp_file);            /* Send line to temp file */
  114.  
  115.     fgets(buff,LINESIZE,in_file);  /* Read a line of input */
  116.  }
  117.  
  118.  fprintf(printer,"\f");        /* Final page eject on first part --odd */
  119.                                     /* page no's-- to keep printer sychronized*/
  120.  fclose(in_file);
  121.  printf ("Completed read of input file\n");
  122.  fclose(temp_file);
  123.  
  124.  printf ("\nPause to reverse printer paper.\n");
  125.  printf ("Remove paper from printer, turn it over, and position\n");
  126.  printf ("   it so that next page will print on back of first page.\n");
  127.  printf ("When ready to continue, press 1 (or any number), and RETURN.\n");
  128.  scanf (" %d",&i);
  129.  
  130.  temp_file = fopen (TempName, "r");
  131.  if (temp_file == NULL)
  132.     {printf ("Cannot open temp file for read, terminate\n"); exit(4); }
  133.  
  134.  fgets (buff, LINESIZE, temp_file);
  135.  
  136.  while (! feof(temp_file))        /* Read-print loop for temp file--even #'s*/
  137.  {
  138.     fputs(buff, printer);
  139.     fgets(buff, LINESIZE, temp_file);
  140.  }
  141.  
  142.  fprintf(printer,"\f");        /* Final page eject on second part --even */
  143.                                     /* page no's-- to keep printer sychronized*/
  144.  fclose (temp_file);
  145.  fclose (printer);
  146.  printf ("Finish execution\n");
  147. }
  148.