home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
utils
/
prntutil.zip
/
LINES.C
< prev
next >
Wrap
Text File
|
1987-04-25
|
10KB
|
243 lines
/**************************************************************************
* *
* LINES 2.0 - by Mike Callahan - April 25, 1987 *
* National Weather Service *
* 1020 Standiford Lane *
* Louisville, KY 40213 *
* *
* This program intelligently converts '+', '-', '|' *
* '=', '"', '*', and '#' into line drawings. *
* *
* This version for IBM-PC using Ecosoft C-88. This *
* code is in the Public Domain and can be copied if *
* credit is given to the author. *
* *
* How the program works: *
* In the input file, the user marks horizonal single lines by -, *
* horizontal double lines by =, vertcal single lines by |, vertical *
* double lines by ", and single line connecting intersections by +, *
* double line connecting intersections by #, and non-connecting *
* intersections by *. *
* For each mark in the input file, the program looks at all adjecent *
* positions ie. above, below, left, and right. It notes if it found *
* it found a connecting mark in each direction and then looks up the *
* IBM line drawing character in a table. If if finds does not a NUL *
* program copies that character to the output file, otherwise it *
* copies the mark to the output file. *
* The user will first use an editor or word processor to create the *
* input file containing the marks, and then change the marks to line *
* drawing characters using LINES. *
* *
* Revisions: *
* 4/22/87 - 2.0 - Add support for double line characters. *
* *
**************************************************************************/
/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
/* defines */
#define COLUMNS 134 /* largest input line length */
#define and && /* helps in reducing coding errors */
#define or ||
#define is ==
#define is_not !=
/* prototypes */
void convert(char *, char *, char *, char *);
/* the program */
int main(argc, argv)
int argc;
char **argv;
{
register char *ap, *bp, *mp, *tp;
char buff1[COLUMNS], buff2[COLUMNS], buff3[COLUMNS], lines[COLUMNS],
*temp = "delete.me";
int last_pass = FALSE;
FILE *inp, *outp;
/* process arguments */
if (argc < 2)
{
puts(" To use this program, type in the following command line:");
puts(" lines input output\n");
puts(" Where: input - is the name of the input file.");
puts(" output - is the name of the output file. If");
puts(" omitted, the program will write over");
puts(" the input file.");
puts("\n");
exit(-1);
}
/* open input and output files into workspace, if outfile file was not */
/* given use delete.me as a temporary file */
if ((inp = fopen(argv[1], "r")) is NULL)
{
puts("I CANNOT FIND THE INPUT FILE!");
exit(-1);
}
if (argc is 3)
temp = argv[2];
if ((outp = fopen(temp, "w")) is NULL)
{
puts("I CANNOT CREATE THE OUTPUT FILE!");
exit(-1);
}
printf("Converting %s to %s.\n", argv[1], temp);
/* set up for converting the first line */
memset(buff1, ' ', COLUMNS);
*buff2 = ' ';
fgets(buff2+1, COLUMNS-1, inp);
ap = buff1;
mp = buff2;
bp = buff3;
/* convert text into line drawing characters */
while (last_pass is FALSE)
{
memset(bp, ' ', COLUMNS); /* blank bottom buffer */
if ((last_pass = feof(inp)) is FALSE)
fgets(bp+1, COLUMNS-1, inp); /* read next line */
convert(ap+1, mp+1, bp+1, lines); /* convert one line */
fputs(lines, outp); /* write convered line */
tp = ap; /* rotate pointers */
ap = mp;
mp = bp;
bp = tp;
}
fclose(inp);
fclose(outp);
/* check to see if user did not give an output file name */
if (argc is 2)
{
printf("Deleting %s.\n", argv[1]);
unlink(argv[1]);
printf("Renaming %s to %s.\n", temp, argv[1]);
rename(temp, argv[1]);
}
}
/*--------------------------convert - CMC - 042287-----------------------*/
void convert(ap, mp, bp, cp)
char *ap, *mp, *bp, *cp;
/**************************************************************************
* ACTION: Scan three lines of text, and convert the middle line to line *
* drawing characters. *
* INPUT: ap - the above line of characters *
* mp - the middle line of characters *
* bp - the below line of characters *
* OUTPUT: cp - the converted middle line *
**************************************************************************/
{
static char
table[] = /* conversion table to IBM line drawing characters */
{
NUL, NUL, NUL, NUL,'\304', NUL, NUL, NUL,'\315', NUL,
'\332','\325','\277','\302', NUL,'\270', NUL,'\321', NUL,'\326',
'\311','\267','\322', NUL,'\273', NUL,'\313', NUL,'\300','\324',
'\331','\301', NUL,'\276', NUL,'\317','\263','\303','\306','\264',
'\305', NUL,'\265', NUL,'\330', NUL, NUL, NUL, NUL, NUL,
NUL, NUL, NUL, NUL, NUL,'\323','\310','\275','\320', NUL,
'\274', NUL,'\312', NUL, NUL, NUL, NUL, NUL, NUL, NUL,
NUL, NUL,'\272','\307','\314','\266','\327', NUL,'\271', NUL,
'\316'
};
char *rp, *lp;
int temp, above, below, left, right;
for (; *mp; ap++, mp++, bp++, cp++)
{
above = below = left = right = 0;
lp = mp - 1;
rp = mp + 1;
switch (*mp) /* look at marks */
{
case '-': /* single horizontal lines */
if (strchr("+#*-", *lp) or strchr("+#*-", *rp))
left = right = 1;
break;
case '=': /* double horizontal lines */
if (strchr("+#*=", *lp) or strchr("+#*=", *rp))
left = right = 2;
break;
case '|': /* single vertical lines */
if (strchr("+#*|", *ap) or strchr("+#*|", *bp))
above = below = 1;
break;
case '\"': /* double vertical lines */
if (strchr("+#*\"", *ap) or strchr("+#*\"", *bp))
above = below = 2;
break;
case '*': /* non-connecting intersections */
if (*lp is '-')
left = 1;
else if (*lp is '=')
left = 2;
if (*rp is '-')
right = 1;
else if (*rp is '=')
right = 2;
if (*ap is '|')
above = 1;
else if (*ap is '\"')
above = 2;
if (*bp is '|')
below = 1;
else if (*bp is '\"')
below = 2;
break;
case '+': /* connecting intersections */
case '#':
if (*lp is '+' or *lp is '-')
left = 1;
else if (*lp is '#' or *lp is '=')
left = 2;
if (*rp is '+' or *rp is '-')
right = 1;
else if (*rp is '#' or *rp is '=')
right = 2;
if (*ap is '+' or *ap is '|')
above = 1;
else if (*ap is '#' or *ap is '\"')
above = 2;
if (*bp is '+' or *bp is '|')
below = 1;
else if (*bp is '#' or *bp is '\"')
below = 2;
break;
}
/* look up the needed char and see what the IBM line char is */
temp = above*27 + below*9 + left*3 + right;
if ((*cp = table[temp]) is NUL) /* no conversion */
*cp = *mp;
}
*cp = NUL; /* mark end */
}
/****************************END OF LINES.C*****************************/