/*	TextFormat by Russell Wallace 1992. This program is in the public domain
	and may be freely used and distributed.

	Usage: TextFormat <sourcefile> <destfile> [linelength]
	TextFormat will take an ASCII text file produced with a word processor
	such as Scribble or WordPerfect and convert it to a formatted text file
	which can be loaded into a text editor such as Ed by putting a linefeed
	(ASCII 10) character at appropriate intervals to mark lines. It will also
	filter out any non-ASCII characters from the source file. Linelength is
	optional; if a value is not given, 76 will be used (to allow a margin for
	window borders etc.)

	This version (1.0) was compiled with Aztec C 3.4. If you are compiling
	with Lattice, use the short integer option.

	If you give copies of this program to anyone, please distribute source
	code with the executable program. */

#include <libraries/dos.h>
#include <exec/types.h>

#define MAXBUFFER 512L		/* length of I/O buffers */
#define MAXWORD 128L		/* length of word buffer */
#define EOF 257				/* non-ASCII to indicate end of file */

char wordbuffer[MAXWORD];	/* buffer to store word as read in */
char inbuffer[MAXBUFFER],outbuffer[MAXBUFFER];
struct FileInfoBlock *output;
LONG inpoint,maxin,outpoint;

int charin (fp)				/* input a character */
struct FileInfoBlock *fp;
{
	LONG Read ();
	if (++inpoint>=maxin)
	{
		maxin=Read (fp,inbuffer,MAXBUFFER);
		inpoint=0;
	}
	if (maxin==0)
		return (EOF);
	return (inbuffer[inpoint]);
}

charout (ch,fp)				/* output a character */
int ch;
struct FileInfoBlock *fp;
{
	LONG Write ();
	if ((++outpoint>=MAXBUFFER)||(ch==EOF))
	{
		Write (fp,outbuffer,outpoint);
		outpoint=0;
	}
	outbuffer[outpoint]=(char)ch;
}

main (argc,argv)
int argc;		/* number of arguments input by user */
char *argv[];	/* pointers to each argument */
{
	int i,letter,screenwidth;
	int column=0;
	struct FileInfoBlock *fp,*outfp;
	int ch,oldch,charin ();
	inpoint=maxin=MAXBUFFER-1;
	outpoint=-1;
	screenwidth=76;		/* Default value of line length */
	output=Output ();
	if ((argc<3)||(argc>4)||(*argv[1]=='?'))
	{
Write (output,"Usage: TextFormat <sourcefile> <destfile> [linelength]\n",55L);
		exit (9);
	}
	if ((fp=Open (argv[1],MODE_OLDFILE))==0)	/* Can't open file */
	{
		fileerrmess (argv[1]);
		exit (10);
	}
	if (!(outfp=Open (argv[2],MODE_NEWFILE)))
	{
		fileerrmess (argv[2]);
		Close (fp);
		exit (11);
	}
	if (argc==4)
		screenwidth=atoi (argv[3]);
	if (screenwidth<5)
	{
		Write (output,"Please give a reasonable linelength value\n",42L);
		Close (fp);
		Close (outfp);
		exit (12);
	}
Loop:				/* come back to here as long as not EOF */
	letter=0;		/* Start at first letter of word */
	for (;;)
	{
		if ((ch=charin (fp))==EOF)
			break;
		wordbuffer[letter++]=(char)ch;
		if (ch=='\n' || ch==' ' || ch==9 || letter==MAXWORD)
			break;
	}
	if (letter)		/* if there's any word to output */
	{
		if (letter+column>=screenwidth)
		{
			charout ('\n',outfp);	/* insert LF where necessary */
			column=0;
		}
		for (i=0;i<letter;i++)
		{
			charout ((int)wordbuffer[i],outfp);
			column++;
			if (wordbuffer[i]=='\n')
				column=0;	/* if output LF,back to column zero */
		}
	}
	if (ch!=EOF)
		goto Loop;
	charout (EOF,outfp);	/* Force write of buffered output */
	Close (fp);
	Close (outfp);
}

fileerrmess (s)
char *s;
{
	Write (output,"ERROR: Can't open file ",23L);
	Write (output,s,(long)strlen (s));
	Write (output,"\n",1L);
}
