home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / utils / prntutil.zip / LINES.C < prev    next >
Text File  |  1987-04-25  |  10KB  |  243 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. *             LINES 2.0 - by Mike Callahan - April 25, 1987               *
  4. *                         National Weather Service                        *
  5. *                         1020 Standiford Lane                            *
  6. *                         Louisville, KY  40213                           *
  7. *                                                                         *
  8. *             This program intelligently converts '+', '-', '|'           *
  9. *             '=', '"', '*', and '#' into line drawings.                  *
  10. *                                                                         *
  11. *             This version for IBM-PC using Ecosoft C-88.  This           *
  12. *             code is in the Public Domain and can be copied if           *
  13. *             credit is given to the author.                              *
  14. *                                                                         *
  15. *   How the program works:                                                *
  16. *   In the input file, the user marks horizonal single lines by -,        *
  17. *   horizontal double lines by =, vertcal single lines by |, vertical     *
  18. *   double lines by ", and single line connecting intersections by +,     *
  19. *   double line connecting intersections by #, and non-connecting         *
  20. *   intersections by *.                                                   *
  21. *   For each mark in the input file, the program looks at all adjecent    *
  22. *   positions ie. above, below, left, and right.  It notes if it found    *
  23. *   it found a connecting mark in each direction and then looks up the    *
  24. *   IBM line drawing character in a table.  If if finds does not a NUL    *
  25. *   program copies that character to the output file, otherwise it        *
  26. *   copies the mark to the output file.                                   *
  27. *   The user will first use an editor or word processor to create the     *
  28. *   input file containing the marks, and then change the marks to line    *
  29. *   drawing characters using LINES.                                       *
  30. *                                                                         *
  31. *   Revisions:                                                            *
  32. *   4/22/87 - 2.0 - Add support for double line characters.               *
  33. *                                                                         *
  34. **************************************************************************/
  35.  
  36. /* includes */
  37.  
  38. #include  <stdio.h>
  39. #include  <stdlib.h>
  40. #include  <memory.h>
  41. #include  <string.h>
  42.  
  43. /* defines */
  44.  
  45. #define   COLUMNS      134            /* largest input line length */
  46.  
  47. #define   and          &&             /* helps in reducing coding errors */
  48. #define   or           ||
  49. #define   is           ==
  50. #define   is_not       !=
  51.  
  52. /* prototypes */
  53.  
  54. void convert(char *, char *, char *, char *);
  55.  
  56. /* the program */
  57.  
  58. int main(argc, argv)
  59.     int    argc;
  60.     char   **argv;
  61. {
  62.     register char  *ap, *bp, *mp, *tp;
  63.     char   buff1[COLUMNS], buff2[COLUMNS], buff3[COLUMNS], lines[COLUMNS],
  64.            *temp = "delete.me";
  65.     int    last_pass = FALSE;
  66.     FILE   *inp, *outp;
  67.  
  68.     /* process arguments */
  69.  
  70.     if (argc < 2)
  71.        {
  72.        puts("   To use this program, type in the following command line:");
  73.        puts("         lines input output\n");
  74.        puts("         Where: input  - is the name of the input file.");
  75.        puts("                output - is the name of the output file.  If");
  76.        puts("                         omitted, the program will write over");
  77.        puts("                         the input file.");
  78.        puts("\n");
  79.        exit(-1);
  80.        }
  81.  
  82.     /* open input and output files into workspace, if outfile file was not */
  83.     /* given use delete.me as a temporary file                             */
  84.  
  85.     if ((inp = fopen(argv[1], "r")) is NULL)
  86.        {
  87.        puts("I CANNOT FIND THE INPUT FILE!");
  88.        exit(-1);
  89.        }
  90.     if (argc is 3)
  91.        temp = argv[2];
  92.     if ((outp = fopen(temp, "w")) is NULL)
  93.        {
  94.        puts("I CANNOT CREATE THE OUTPUT FILE!");
  95.        exit(-1);
  96.        }
  97.     printf("Converting %s to %s.\n", argv[1], temp);
  98.  
  99.     /* set up for converting the first line */
  100.  
  101.     memset(buff1, ' ', COLUMNS);
  102.     *buff2 = ' ';
  103.     fgets(buff2+1, COLUMNS-1, inp);
  104.     ap = buff1;
  105.     mp = buff2;
  106.     bp = buff3;
  107.  
  108.     /* convert text into line drawing characters */
  109.  
  110.     while (last_pass is FALSE)
  111.        {
  112.        memset(bp, ' ', COLUMNS);                  /* blank bottom buffer */
  113.        if ((last_pass = feof(inp)) is FALSE)
  114.           fgets(bp+1, COLUMNS-1, inp);            /* read next line */
  115.        convert(ap+1, mp+1, bp+1, lines);          /* convert one line */
  116.        fputs(lines, outp);                        /* write convered line */
  117.        tp = ap;                                   /* rotate pointers */
  118.        ap = mp;
  119.        mp = bp;
  120.        bp = tp;
  121.        }
  122.     fclose(inp);
  123.     fclose(outp);
  124.  
  125.     /* check to see if user did not give an output file name */
  126.  
  127.     if (argc is 2)
  128.        {
  129.        printf("Deleting %s.\n", argv[1]);
  130.        unlink(argv[1]);
  131.        printf("Renaming %s to %s.\n", temp, argv[1]);
  132.        rename(temp, argv[1]);
  133.        }
  134. }
  135.  
  136. /*--------------------------convert - CMC - 042287-----------------------*/
  137.  
  138. void convert(ap, mp, bp, cp)
  139.     char    *ap, *mp, *bp, *cp;
  140. /**************************************************************************
  141. * ACTION: Scan three lines of text, and convert the middle line to line   *
  142. *         drawing characters.                                             *
  143. * INPUT:  ap - the above line of characters                               *
  144. *         mp - the middle line of characters                              *
  145. *         bp - the below line of characters                               *
  146. * OUTPUT: cp - the converted middle line                                  *
  147. **************************************************************************/
  148. {
  149.     static char
  150.        table[] =        /* conversion table to IBM line drawing characters */
  151.        {
  152.           NUL,   NUL,   NUL,   NUL,'\304',   NUL,   NUL,   NUL,'\315',   NUL,
  153.        '\332','\325','\277','\302',   NUL,'\270',   NUL,'\321',   NUL,'\326',
  154.        '\311','\267','\322',   NUL,'\273',   NUL,'\313',   NUL,'\300','\324',
  155.        '\331','\301',   NUL,'\276',   NUL,'\317','\263','\303','\306','\264',
  156.        '\305',   NUL,'\265',   NUL,'\330',   NUL,   NUL,   NUL,   NUL,   NUL,
  157.           NUL,   NUL,   NUL,   NUL,   NUL,'\323','\310','\275','\320',   NUL,
  158.        '\274',   NUL,'\312',   NUL,   NUL,   NUL,   NUL,   NUL,   NUL,   NUL,
  159.           NUL,   NUL,'\272','\307','\314','\266','\327',   NUL,'\271',   NUL,
  160.        '\316'
  161.        };
  162.     char   *rp, *lp;
  163.     int    temp, above, below, left, right;
  164.  
  165.     for (; *mp; ap++, mp++, bp++, cp++)
  166.         {
  167.  
  168.         above = below = left = right = 0;
  169.         lp = mp - 1;
  170.         rp = mp + 1;
  171.         switch (*mp)                   /* look at marks */
  172.                {
  173.                case '-':               /* single horizontal lines */
  174.                     if (strchr("+#*-", *lp) or strchr("+#*-", *rp))
  175.                        left = right = 1;
  176.                     break;
  177.  
  178.                case '=':               /* double horizontal lines */
  179.                     if (strchr("+#*=", *lp) or strchr("+#*=", *rp))
  180.                        left = right = 2;
  181.                     break;
  182.  
  183.                case '|':               /* single vertical lines */
  184.                     if (strchr("+#*|", *ap) or strchr("+#*|", *bp))
  185.                        above = below = 1;
  186.                     break;
  187.  
  188.                case '\"':              /* double vertical lines */
  189.                     if (strchr("+#*\"", *ap) or strchr("+#*\"", *bp))
  190.                        above = below = 2;
  191.                     break;
  192.  
  193.                case '*':               /* non-connecting intersections */
  194.                     if (*lp is '-')
  195.                        left = 1;
  196.                     else if (*lp is '=')
  197.                        left = 2;
  198.                     if (*rp is '-')
  199.                        right = 1;
  200.                     else if (*rp is '=')
  201.                        right = 2;
  202.                     if (*ap is '|')
  203.                        above = 1;
  204.                     else if (*ap is '\"')
  205.                        above = 2;
  206.                     if (*bp is '|')
  207.                        below = 1;
  208.                     else if (*bp is '\"')
  209.                        below = 2;
  210.                     break;
  211.  
  212.                case '+':               /* connecting intersections */
  213.                case '#':
  214.                     if (*lp is '+' or *lp is '-')
  215.                        left = 1;
  216.                     else if (*lp is '#' or *lp is '=')
  217.                        left = 2;
  218.                     if (*rp is '+' or *rp is '-')
  219.                        right = 1;
  220.                     else if (*rp is '#' or *rp is '=')
  221.                        right = 2;
  222.                     if (*ap is '+' or *ap is '|')
  223.                        above = 1;
  224.                     else if (*ap is '#' or *ap is '\"')
  225.                        above = 2;
  226.                     if (*bp is '+' or *bp is '|')
  227.                        below = 1;
  228.                     else if (*bp is '#' or *bp is '\"')
  229.                        below = 2;
  230.                     break;
  231.                }
  232.  
  233.         /* look up the needed char and see what the IBM line char is */
  234.  
  235.         temp = above*27 + below*9 + left*3 + right;
  236.         if ((*cp = table[temp]) is NUL)               /* no conversion */
  237.            *cp = *mp;
  238.         }
  239.     *cp = NUL;                                        /* mark end */
  240. }
  241.  
  242. /****************************END OF LINES.C*****************************/
  243.