home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 09print / pr_cpy.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.6 KB  |  143 lines

  1. /*
  2.  *    pr_cpy -- copy input stream to output stream
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <local\std.h>
  8. #include <local\printer.h>
  9. #include <sys\types.h>
  10. #include <sys\stat.h>
  11. #include <time.h>
  12. #include "print.h"
  13.  
  14. extern PRINT pcnf;
  15. extern char Pagelist[MAXLINE];
  16. extern long Highest;
  17.  
  18. int
  19. pr_cpy(fin, fout, fname)
  20. FILE *fin;
  21. FILE *fout;
  22. char *fname;
  23. {
  24.     int errcount = 0;
  25.     unsigned int p_line;    /* page-relative line number */
  26.     long f_line;        /* file-relative line number */
  27.     long f_page;        /* file-relative page number */
  28.     int lnlen;        /* line length */
  29.     char line[MAXLINE];    /* input line buffer */
  30.     struct stat tbuf;    /* file information */
  31.     long ltime;        /* date and time */
  32.     FILE *fnull, *fx;    /* additional output file pointers */
  33.  
  34.     extern void mkslist(char *);    /* make a selection list */
  35.     extern int selected(long);    /* is item in the list? */
  36.     extern int spaces(int, FILE *);    /* emit string of spaces */
  37.     extern int setfont(int, FILE *);/* set printer font type */
  38.     extern int clrprnt(FILE *);    /* clear special fonts */
  39.     extern int lines(int, FILE *);    /* emit string of blank lines */
  40.     static int fit(int, int);    /* will line fit on page? */
  41.     extern int pr_line(char *, FILE *, unsigned int);
  42.  
  43.     /* install page selection list, if any */
  44.     if (Pagelist[0] != '\0') {
  45.         /* open the NUL device for dumping output */
  46.         if ((fnull = fopen("NUL", "w")) == NULL) {
  47.             perror("Error opening NUL device");
  48.             exit(1);
  49.         }
  50.         mkslist(Pagelist);
  51.     }
  52.     else
  53.         Highest = BIGGEST;
  54.  
  55.     /* get date and time stamp */
  56.     if (*fname == '\0')
  57.         /* using stdin -- use today's date and time */
  58.         ltime = time(NULL);
  59.     else {
  60.         if (stat(fname, &tbuf) == -1)
  61.             return (-1);
  62.         /* use file's modification time */
  63.         ltime = tbuf.st_mtime;
  64.     }
  65.     p_line = 0;
  66.     f_line = 1;
  67.     f_page = 1;
  68.     while ((lnlen = pr_getln(line, MAXLINE, fin)) > 0 ) {
  69.         /* if formfeed or no room for line, eject page */
  70.         if (line[0] == '\f' || !fit(lnlen, p_line)) {
  71.             /* to top of next page */
  72.             if (pcnf.p_ff == 0)
  73.                 lines(pcnf.p_len - p_line, fx);
  74.             else
  75.                 fputc('\f', fx);
  76.             p_line = 0;
  77.         }
  78.  
  79.         /* if at top of page, print the header */
  80.         if (p_line == 0) {
  81.             if (f_page > Highest)
  82.                 break;
  83.             fx = selected(f_page) ? fout : fnull;
  84.             p_line += lines(pcnf.p_top1, fx);
  85.             if (pcnf.p_mode != 0)
  86.                 setfont(EMPHASIZED, fx);
  87.             spaces(pcnf.p_lmarg, fx);
  88.             if (*pcnf.p_hdr != '\0') 
  89.                 fprintf(fx, "%s  ", pcnf.p_hdr);
  90.             else if (*fname != '\0')
  91.                 fprintf(fx, "%s  ", strupr(fname));
  92.             fprintf(fx, "Page %u  ", f_page++);
  93.             fputs(ctime(<ime), fx);
  94.             ++p_line;
  95.             if (pcnf.p_mode != 0)
  96.                 setfont(pcnf.p_font, fx);
  97.             p_line += lines(pcnf.p_top2, fx);
  98.         }
  99.  
  100.         /* OK to output the line */
  101.         if (line[0] != '\f')
  102.             p_line += pr_line(line, fx, f_line++);
  103.     }
  104.     if (ferror(fin) != 0)
  105.         ++errcount;
  106.     if (p_line > 0 && p_line < pcnf.p_len)
  107.         if (pcnf.p_ff == 0)
  108.             lines(pcnf.p_len - p_line, fx);
  109.         else
  110.             fputc('\f', fx);
  111.  
  112.     if (pcnf.p_mode != 0)
  113.         clrprnt(fx);
  114.     return (errcount);
  115. }
  116.  
  117. /*
  118.  *    fit -- return non-zero value if enough physical
  119.  *    lines are available on the current page to take
  120.  *    the current logical line of text
  121.  */
  122.  
  123. #define NFLDWIDTH  8    /* width of number field */
  124.  
  125. static int
  126. fit(len, ln)
  127. int len, ln;
  128. {
  129.     int need, left;    /* physical lines */
  130.     int cols;    /* columns of actual output */
  131.     int lw;        /* displayable line width */
  132.  
  133.     /* total need (columns -> physical lines) */
  134.     cols = len + (pcnf.p_lnum > 0 ? NFLDWIDTH : 0);
  135.     lw = pcnf.p_wid - pcnf.p_lmarg - pcnf.p_rmarg;
  136.     need = 1 + cols / lw;
  137.  
  138.     /* lines remaining on page */
  139.     left = pcnf.p_len - ln - pcnf.p_btm;
  140.  
  141.     return (need <= left ? 1 : 0);
  142. }
  143.