home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 443.lha / dj_v1.0 / dj.c < prev    next >
C/C++ Source or Header  |  1990-12-02  |  6KB  |  281 lines

  1. /*
  2.  *  DJ    -    Print files to Deskjet+ 2 pages side-by-side landscape mode.
  3.  *
  4.  *    DJ will output files to a HP Deskjet+ printer connected to the PAR:
  5.  *    device.  It prints 2 pages side-by-side by putting the printer into
  6.  *    tiny print landscape mode.  Each page has a header which include
  7.  *    file modification time (or current time if redirected input),
  8.  *    file name and page #.
  9.  *
  10.  *    DJ [file1] [file2 ... fileN] OR
  11.  *    DJ <redirected file
  12.  *
  13.  *         DJ.C an Amiga page formatter for HP Deskjet Plus printer.
  14.  *
  15.  *                  Copywrite (c) 1990 Jeff Kunzelman
  16.  *                        CIS:    76224,102
  17.  *
  18.  *   This software is freely redistributable, as long as all code,
  19.  *     comments, and documentation are included unaltered, and no other
  20.  *   conditions are imposed on its redistribution.
  21.  *
  22.  *   This software is provided 'AS IS' if it doesn't work very well,
  23.  *   sorry.
  24.  *
  25.  *     Compiled and linked with Lattice/SAS Ver 5.10   'lc -L dj.c'
  26.  *     Ignore the character const warnings.
  27.  *
  28.  *    Modification History:
  29.  *
  30.  *    16-Oct-90    Jeff
  31.  *    Ver 1.0 1st release.
  32.  */
  33. #include <stdio.h>
  34. #include <dos.h>
  35. #include <time.h>
  36.  
  37. typedef int        bool;
  38. #define IS_NIL(x)     ((bool)((x) == NULL))
  39. #define EOS                '\0'
  40.  
  41. #define    MAXPAGES          2
  42. #define    MAXROWS           90
  43. #define    MAXLINE              170
  44.  
  45. #define    LHS                0
  46. #define    RHS                1
  47.  
  48. #define PAGE_END_COL    95
  49. #define MIDDLE_COL        (PAGE_END_COL + 5)
  50. #define    LAST_COL        (MIDDLE_COL*2)
  51.  
  52. #define UNDERWIDTH        80
  53. #define    SEPCHAR            179
  54. #define UNDERCHAR        196
  55.  
  56. #define TABWIDTH        4        /* Must be 4, 8, 16 ... */
  57.  
  58. static char *eosPtr    = "";
  59.  
  60. static void
  61. initPrinter(FILE *outHandle)
  62. {
  63.     fprintf(outHandle, "\033&l1O");            /* landscape             */
  64.     fprintf(outHandle, "\033&l4C");
  65.     fprintf(outHandle, "\033(s20hs6V\r");
  66. }
  67.  
  68. static void
  69. resetPrinter(FILE *outHandle)
  70. {
  71.     fprintf(outHandle, "\033E");
  72. }
  73.  
  74. static void
  75. printLines(FILE *outHandle, char *lhsPtr, char *rhsPtr, bool doSep)
  76. {
  77.     int        tabCol;
  78.     int        col;
  79.     int        loc;
  80.     char    c;
  81.  
  82.     /*
  83.      *  Reset current tab location and column pointer.
  84.      *  Output the line expanding tabs as we go.
  85.      */
  86.     for (tabCol = -1, loc = col = 0; col < PAGE_END_COL;) {
  87.         c = lhsPtr[loc];
  88.         if (c == '\n' || c == EOS)
  89.             break;
  90.         else if (c == '\t') {
  91.             if (tabCol == -1)
  92.                 tabCol = TABWIDTH - (col & (TABWIDTH - 1));
  93.             else if (tabCol) {
  94.                 fputc(' ', outHandle);
  95.                 col++;
  96.                 tabCol--;
  97.             } else {
  98.                 tabCol = -1;
  99.                 loc++;
  100.             }
  101.         } else {
  102.             fputc(c, outHandle);
  103.             col++;
  104.             loc++;
  105.         }
  106.     }
  107.  
  108.     /*
  109.      *  Check for RHS text, skip separator and line if none.
  110.      */
  111.     if (*rhsPtr != EOS) {
  112.         /*  
  113.          *  Skip to center column. 
  114.          */
  115.         for (; col < MIDDLE_COL; col++) 
  116.             fputc(' ', outHandle);
  117.  
  118.         /*  
  119.          *  Print separator if requested. 
  120.          */
  121.         fputc(doSep ? SEPCHAR : ' ', outHandle);
  122.  
  123.         /*  
  124.          *  Print an nice comfy gap 
  125.          */
  126.         fprintf(outHandle, "    ");
  127.  
  128.         /*
  129.          *  Output the line expanding tabs as we go.
  130.          */
  131.         for (tabCol = -1, loc = col = 0; col < PAGE_END_COL;) {
  132.             c = rhsPtr[loc];
  133.             if (c == '\n' || c == EOS)
  134.                 break;
  135.             else if (c == '\t') {
  136.                 if (tabCol == -1)
  137.                     tabCol = TABWIDTH - (col & (TABWIDTH - 1));
  138.                 else if (tabCol) {
  139.                     fputc(' ', outHandle);
  140.                     col++;
  141.                     tabCol--;
  142.                 } else {
  143.                     tabCol = -1;
  144.                     loc++;
  145.                 }
  146.             } else {
  147.                 fputc(c, outHandle);
  148.                 col++;
  149.                 loc++;
  150.             }
  151.         }
  152.     }
  153.     fprintf(outHandle, "\n\r");
  154. }
  155.  
  156. static void
  157. printFile(register FILE *inHandle, register FILE *outHandle, 
  158.           char *name, char *fTime)
  159. {
  160.     static char pageBuffer[MAXPAGES][MAXROWS][MAXLINE+1];
  161.     static char titleBuffer[MAXPAGES][MAXLINE+1];
  162.  
  163.     register     int        bufPage;
  164.     register     int        line;
  165.     register     bool    eof;
  166.     int                    page = 1;
  167.     int                    col;
  168.     bool                haveRHS;
  169.  
  170.     do {
  171.         /*
  172.          *  Read lines upto a full page buffer worth.
  173.          */
  174.         for (eof = FALSE, bufPage = 0; bufPage < MAXPAGES && !eof; bufPage++)
  175.         {
  176.             /*
  177.              *  Load a page worth of text.
  178.              */
  179.             for (line = 0; line < MAXROWS && !eof; line++) 
  180.                 eof =  IS_NIL(fgets(pageBuffer[bufPage][line],  
  181.                               MAXLINE, inHandle));
  182.             /*
  183.              *  Terminate empty part of this page buffer.
  184.              */
  185.             for (; line < MAXROWS; line++)
  186.                 pageBuffer[bufPage][line][0] = EOS;
  187.         }
  188.         haveRHS = bufPage > 1;
  189.  
  190.         /*
  191.          *  Terminate empty part of next page if we hit EOF.
  192.          */
  193.         for (; bufPage < MAXPAGES; bufPage++)
  194.             for (line = 0; line < MAXROWS; line++)
  195.                 pageBuffer[bufPage][line][0] = EOS;
  196.  
  197.         /*
  198.          *  Output page title text header line.
  199.          */
  200.         sprintf(titleBuffer[LHS], "%s   File: %-39sPage %3d", 
  201.                 fTime, name, page);
  202.         sprintf(titleBuffer[RHS], "%s   File: %-39sPage %3d", 
  203.                 fTime, name, page + 1);
  204.         printLines(outHandle, titleBuffer[LHS],
  205.                    haveRHS ? titleBuffer[RHS] : eosPtr, FALSE);
  206.  
  207.         /*
  208.          *  Add in header division underline.
  209.          */
  210.         for (col = 0; col < UNDERWIDTH; col++)
  211.             titleBuffer[LHS][col] = UNDERCHAR;
  212.         titleBuffer[LHS][col] = EOS;
  213.         printLines(outHandle, titleBuffer[LHS], 
  214.                    haveRHS ? titleBuffer[LHS] : eosPtr,  FALSE);
  215.         fprintf(outHandle, "\n\n\r");
  216.  
  217.         /*
  218.          *  Output page text contents.
  219.          */
  220.         for (line = 0;  line < MAXROWS && pageBuffer[LHS][line] != EOS; 
  221.              line++) {
  222.             printLines(outHandle, pageBuffer[LHS][line],
  223.                        pageBuffer[RHS][line], TRUE);
  224.         }
  225.  
  226.         /*
  227.          *  Eject page and advance page count.
  228.          */
  229.         fputc('\f', outHandle);
  230.         page += 2;
  231.     } while (!eof);
  232. }
  233.  
  234. static char *
  235. fileTime(long time)
  236. {
  237.     return asctime(gmtime(&time));
  238. }
  239.  
  240. static char *
  241. sysTime(long time)
  242. {
  243.     return ctime(&time);
  244. }
  245.  
  246. void
  247. main(int argc, char **argv)
  248. {
  249.     FILE        *fh;
  250.     FILE        *oh;
  251.     char           *timeString;
  252.  
  253.     printf("\x9b1mDJ Ver 1.0\x9b0m (c) 1990 Jeff Kunzelman.\n");
  254.  
  255.     if (IS_NIL(oh = fopen("par:", "w"))) {
  256.         fprintf(stderr, "Cannot open PAR: device for output.\n");
  257.         exit(1);
  258.     }
  259.  
  260.     initPrinter(oh);
  261.  
  262.     if (argc == 1) {
  263.         timeString = sysTime(time(NULL));
  264.         timeString[strlen(timeString) - 1] = EOS; /* strip \n from string */
  265.         printFile(stdin, oh, "Standard Input", timeString);
  266.     } else for (argc--, argv++; argc; argc--, argv++) {
  267.         fh = fopen(*argv, "r");
  268.         if (!IS_NIL(fh)) {
  269.             timeString = fileTime(getft(*argv));
  270.             timeString[strlen(timeString) - 1] = EOS; 
  271.             printf("Printing: %s...\n", *argv);
  272.             printFile(fh, oh, *argv, timeString); 
  273.             fclose(fh);
  274.         } else
  275.             fprintf(stderr, "Cannot open %s.\n", *argv);
  276.     }
  277.  
  278.     resetPrinter(oh);
  279.     exit(0);
  280. }
  281.