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

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