home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / 015n / bakprint.c < prev    next >
Text File  |  1985-05-13  |  6KB  |  181 lines

  1.  
  2. /*
  3.  
  4.     BAKPRINT.C    Vers 1.0    By Craig Derouen  May 12,1985
  5.  
  6.  
  7.  Bakprint is a little printer utility that is most useful from printing
  8.  documents and other text files that have 'backspaces' inserted in the
  9.  text. These backspaces are inserted in the text to implement text 
  10.  formatting such as undserscore or bold print. There are many printers out
  11.  there such as my OKI 92A that do not accept a backspace character. This
  12.  program is designed to filter out the backspaces and convert the format
  13.  to a readable one accepted by most any printer. This program was done out
  14.  of necessity because of all the un-forgiving PD author's who put out text
  15.  files with backspaces in them! Do they think we all have Epson FX-80's!!
  16.  
  17.  The program works by reading a line of text into a buffer. When it encounters
  18.  a backspace, it will set a flag and save the next character and its position
  19.  for later manipulation. The line buffer is scanned up to a cr-lf set. Then
  20.  the line is printed out to the printer WITHOUT a linefeed. A cr is also
  21.  sent to the printer which will return the printer head back to the start of
  22.  the line, then the backspace/chars are output at their respective positions.
  23.  Spaces are inserted to produce a full line. The printer is advanced to the
  24.  next line and the process starts over. BAKPRINT will read in a file
  25.  specified. To read in a file from backprint type the following:
  26.  
  27.     BAKPRINT (pathname) [(output pathname)]
  28.  
  29.  Wild cards are not accepted at this time. Pathname is any legal DOS path
  30.  name. If no output pathname is specified, the output goes to the standard
  31.  printer output. If the input pathname can not be opened, the filter mode
  32.  will be set. If the output file already exists it will be re-written. If
  33.  the output name is illegal or can not be opened, output goes to printer.
  34.  
  35.  As an additional feature this program will convert XENIX text files 
  36.  (terminated with cr only) to a cr-lf terminator accepted by MS-DOS. Also 
  37.  tabs are expanded, again because most printers can not accept tabs!
  38.  
  39. */
  40.  
  41. #define vers 1.00
  42.  
  43. #include "stdio.h"
  44. #include "ctype.h"
  45.  
  46. #define maxlen  132     /* Maximum length of line from input file */
  47. #define true 1
  48. #define false 0
  49.  
  50. #define lf    0x0a
  51. #define cr  0x0d
  52. #define bsp 0x08
  53. #define tab 0x07
  54. #define blank 0x20
  55.  
  56. #define tabpos 8        /* Spaces between tabs */
  57.  
  58. FILE *infile,*outfile;
  59. int _fmode = 0x8000;    /* Raw mode */
  60. int testchar,outcount,backcount,line;
  61. char outline[maxlen+1];
  62. char bkspline[maxlen*2];
  63. char testline[maxlen];
  64. int linecount=0,charpos=0,backflag,notcon=false;
  65.  
  66. main (argc,argv)
  67. int argc;
  68. char *argv[];
  69.  
  70. {
  71.     if (argc < 2) {        /* No files were specified so make us a filter */
  72.        infile = fopen(stdin,"r");
  73.        outfile = fopen(stdout,"w");
  74.     }
  75.      else if ((infile = fopen(argv[1],"r")) == NULL ) {
  76.        printf ("\007Sorry,bad filename\n");
  77.        exit (1);
  78.     }
  79.     if (argc > 2) {
  80.        if ((outfile = fopen(argv[2],"w")) == NULL ) { 
  81.           outfile = fopen(stdout,"w");    /* to the console instead */
  82.           printf("\007Bad output file, output will goto console\n");
  83.        }
  84.        else notcon=true;
  85.     }
  86.      else outfile = fopen(stdout,"w");
  87.     if (notcon) printf("BAKPRINT Ver: %04f By Craig Derouen\n\n",vers);
  88.     while (!(feof(infile))) {
  89.        getline();
  90.        if (notcon) printf("\tNow printing line %d\r",linecount);
  91.        fputs(outline,outfile); /* print out the line minus backspaces */
  92.        backline();    /* Print any chars that were paired with backspaces */
  93.        putc (lf,outfile);
  94.     }
  95.      fclose(infile);
  96.     fclose(outfile);
  97.     if (notcon) printf("\n");
  98.     exit (0);
  99. }
  100.  
  101. getline () {
  102.  
  103. int tabcount=0;   
  104.  
  105. /* Read in a line of text and put it into the buffer. Strip
  106.     all the backspaces and their respective characters */
  107.  
  108.     charpos=0;    /* Starting fresh */
  109.     outcount=0; /* Number of chars in the line */
  110.     backcount=0;
  111.      backflag=false;
  112.      line=false;
  113.        while (((testchar=getc(infile)) != EOF ) && (!line)) {
  114.         switch(testchar) {
  115.             case cr : {     /* We must be at the end of the line */
  116.                ++linecount;
  117.                outline[outcount++]=testchar;
  118.                line=true;
  119.                break;
  120.              }
  121.  
  122.              case lf : {
  123.                line=true;
  124.                break;        /* Just swallow the linefeeds */
  125.             }
  126.  
  127.             case bsp : {    /* This is the one we are looking for */
  128.                backflag=true;
  129.                testchar=getc(infile); /* Get the next character */
  130.                if (testchar !=cr) {
  131.                        bkspline[backcount++] = charpos; /* Get the current position */
  132.                        bkspline[backcount++] = testchar; /* Put in the character */
  133.                }
  134.                else ungetc(testchar,infile); /* Put it back */
  135.                break;
  136.             }
  137.              
  138.              case tab : {    /* Expand any tabs */
  139.                tabcount=tabpos-(charpos & tabpos);
  140.                while (tabcount) {
  141.                   outline[outcount++]=blank; /* Put in a blank */
  142.                   charpos++;
  143.                   tabcount--;
  144.                 }
  145.             }
  146.  
  147.             default : {  /* the rest of the story */
  148.                outline[outcount++]=testchar; /* save it */
  149.                charpos++;
  150.                break;
  151.             }
  152.         }
  153.     }
  154.      outline[outcount++]=NULL;
  155. }
  156.  
  157. backline() {    
  158.  
  159. /* This function will print any characters that were paired with the 
  160. backspace key in the original line. They will be placed in their proper
  161. position in the line */
  162.  
  163.     int count=0,skip;
  164.     if (backflag) { 
  165.  
  166.        /* Create a string of blanks */
  167.        while (count < maxlen)
  168.           testline[count++]=blank;
  169.        count=0;
  170.        while (backcount) {
  171.           skip=bkspline[count++];    /* Get the char position */
  172.           testline[skip-1]=bkspline[count++];
  173.           backcount --;
  174.           backcount --;
  175.        }
  176.         testline[outcount++]=cr;
  177.        testline[outcount]=NULL;
  178.        fputs(testline,outfile); 
  179.     }
  180. }
  181.       b