home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / A4PRINT.ZIP / P2.C < prev    next >
C/C++ Source or Header  |  1990-09-27  |  6KB  |  262 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define LPP 126
  6. #define MAXL 86
  7. #define INDENT 8
  8. #define HALF LPP/2
  9.  
  10. void
  11.     clearBuffers(void),
  12.     printBuffers(int),
  13.     usage(char *);
  14. unsigned short
  15.     fillBuffers(void);
  16. char
  17.     *indent(int);
  18. int
  19.     print(char *);    
  20. long
  21.     lineNumber = 0;
  22. int
  23.     tof = 1,
  24.     c,
  25.     t,
  26.     numbering = 0,
  27.     col = 0,
  28.     tabint = 8,
  29.     filesPrinted = 0;
  30. FILE
  31.     *text,
  32.     *lpt1 = NULL;
  33. char
  34.     line[LPP][MAXL+INDENT+1];
  35.  
  36. /*──────────────────────────────────────────────────────────────────────────────
  37.  
  38.                                     main
  39.                                     
  40. ──────────────────────────────────────────────────────────────────────────────*/
  41.  
  42. void main(int argc, char *argv[])
  43. {
  44.     short int c;
  45.     for (c = 1; c < argc; ++c)
  46.         if (*argv[c] == '/')
  47.             if (isdigit(*(argv[c]+1)))
  48.             {
  49.                 sscanf(argv[c]+1,"%d",&t);
  50.                 if (t > 0)
  51.                     tabint = t;
  52.             }
  53.             else
  54.             {
  55.                 if (tolower(*(argv[c]+1)) == 'n')
  56.                     numbering = !numbering;
  57.                 else
  58.                     if (tolower(*(argv[c]+1)) == 'h')
  59.                     {
  60.                         usage(argv[0]);
  61.                         exit(1);
  62.                     }
  63.             }
  64.         else
  65.             filesPrinted += print(argv[c]);
  66.  
  67.     if (lpt1 != NULL)
  68.         fputs("\033&l0O",lpt1);
  69.     if (filesPrinted == 0)
  70.     {
  71.         usage(argv[0]);
  72.         exit(1);
  73.     }
  74.     exit(0);
  75. }
  76.  
  77. /*──────────────────────────────────────────────────────────────────────────────
  78.  
  79.                                     usage
  80.                                     
  81. ──────────────────────────────────────────────────────────────────────────────*/
  82.  
  83. void usage(char *program)
  84. {
  85.     char
  86.         drive[_MAX_DRIVE],
  87.         dir[_MAX_DIR],
  88.         fname[_MAX_FNAME],
  89.         ext[_MAX_EXT];
  90.     _splitpath(program,drive,dir,fname,ext);
  91.     printf("\nUsage:\n\t%s [/n] [/t] FileName\n\n", fname);
  92.     printf("where FileName is the name of the input file (wild cards allowed)\n");
  93.     printf("and the [optional] /n toggles line numbering (default=OFF).\n");
  94.     printf("The t in the [optional] /t represents the tab expansion interval.  If\n");
  95.     printf("not specified then it defaults to 8.\n\n");
  96.     printf("More than one file name may be specified and line numbering may be\n");
  97.     printf("toggled and the tab interval changed several times.  For example:\n");
  98.     printf("\n\t%s dd.man /n font.txt /4 *.c *.h /8 /n *.doc\n\n",program);
  99.     printf("prints DD.MAN without line numbers and with 8-column tabs then prints\n");
  100.     printf("FONT.TXT with line numbers, then sets tab spacing to 4 before printing\n");
  101.     printf("all the .C and .H files (with line numbers) and finally prints all the\n");
  102.     printf(".DOC files after resetting tab spacing to 8 and turning off line numbers.");
  103. }
  104.  
  105. /*──────────────────────────────────────────────────────────────────────────────
  106.  
  107.                                      print
  108.  
  109. ──────────────────────────────────────────────────────────────────────────────*/
  110.  
  111. int print(char *fileName)
  112. {
  113.     short int
  114.         n;
  115.     if ((text=fopen(fileName,"r")) == NULL)
  116.     {
  117.         printf("Could not open input file %s\n",strupr(fileName));
  118.         return 0;
  119.     }
  120.     fseek(text,0L,SEEK_END);
  121.     if (ftell(text) == 0)
  122.     {
  123.         fclose(text);
  124.         printf("Zero-length file %s not printed\n",fileName);
  125.         return 1;
  126.     }
  127.     rewind(text);
  128.     if (lpt1 == NULL)
  129.     {
  130.         lpt1 = fopen("LPT1","w");
  131. //               landscape    9 lpi      7 point    16.6 cpi
  132.         fputs("\033&l1O\033\&l5.3333C\033(s7V\033(s16.6H",lpt1);
  133.     }
  134.     while (!feof(text))
  135.     {
  136.         clearBuffers();
  137.         if (n=fillBuffers())
  138.             printBuffers(n);
  139.     }
  140.     fclose(text);
  141.     return 1;
  142. }
  143.  
  144. /*──────────────────────────────────────────────────────────────────────────────
  145.  
  146.                                 clearBuffers
  147.  
  148. ──────────────────────────────────────────────────────────────────────────────*/
  149.  
  150. void clearBuffers(void)
  151. {
  152.     unsigned short int
  153.         x = LPP;
  154.     while (x--)
  155.         line[x][0] = '\0';
  156. }
  157.  
  158. /*──────────────────────────────────────────────────────────────────────────────
  159.  
  160.                                 fillBuffers
  161.  
  162. ──────────────────────────────────────────────────────────────────────────────*/
  163.  
  164. unsigned short fillBuffers(void)
  165. {
  166.     unsigned short int
  167.         lines = 0,
  168.         filling = 1;
  169.     char
  170.         *p;
  171.  
  172.     p = indent(0);
  173.     while (filling)
  174.         switch(c=fgetc(text))
  175.         {
  176. case EOF:
  177. case 014:
  178.             *p = '\0';
  179.             if (lines < HALF)
  180.             {
  181.                 lines = HALF;
  182.                 p = indent(lines);
  183.             }
  184.             else
  185.                 filling = 0;
  186.             break;
  187. case '\n':
  188.             *p = '\0';
  189.             col = 0;
  190.             if (++lines >= LPP)
  191.                 filling = 0;
  192.             else
  193.                 p = indent(lines);
  194.             break;
  195. case '\t':
  196.             for (t=col/tabint*tabint+tabint; col < t; ++col)
  197.                 if (col < MAXL)
  198.                     *p++ = ' ';
  199.             break;
  200. case 0xCD:
  201.             c = '=';
  202.             goto others;
  203. case 0xC4:
  204.             c = '-';
  205. default:
  206. others:
  207.             if (col++ < MAXL)
  208.                 *p++ = (char)c;
  209.         }
  210.     return lines;
  211. }
  212.  
  213. /*──────────────────────────────────────────────────────────────────────────────
  214.  
  215.                                     indent
  216.  
  217. ──────────────────────────────────────────────────────────────────────────────*/
  218.  
  219. char *indent(int l)
  220. {
  221.     memset(&line[l][0],' ',INDENT);
  222.     if (numbering)
  223.     {
  224.         if (INDENT < 8)
  225.         {
  226.             sprintf(&line[l][0],"%5ld: ",++lineNumber % 100000L);
  227.             return &line[l][strlen(line[l])];
  228.         }
  229.         else
  230.             sprintf(&line[l][INDENT-7],"%5ld: ",++lineNumber % 100000L);
  231.     }
  232.     return &line[l][INDENT];
  233. }
  234.  
  235. /*──────────────────────────────────────────────────────────────────────────────
  236.  
  237.                                 printBuffers
  238.  
  239. ──────────────────────────────────────────────────────────────────────────────*/
  240.  
  241. void printBuffers(int lines)
  242. {
  243.     int l;
  244.     if (lines)
  245.     {
  246.         if (!tof)
  247.         {
  248.             fputc(014,lpt1);
  249.             tof = 1;
  250.         }
  251.         fputs("\n",lpt1);
  252.         for (l=0; (l < HALF) && (l < lines); ++l)
  253.         {
  254.             fputs(line[l],lpt1);
  255.             fputc('\n',lpt1);
  256.         }
  257.         for (l=HALF; l<lines; ++l)
  258.             fprintf(lpt1,"\033&a%dR\033&a%dC%s", l-HALF+1, 96, line[l]);
  259.     }
  260.     tof = 0;
  261. }
  262.