/*
	COPYRIGHT:

	DumpSpaces 36.1 (14.4.93) written by and Copyright (c) 1993 Ralph Seichter.
	Note that `DumpSpaces' isn't public domain, but it is freely distributable!

	PURPOSE:

	Strips text lines from unnecessary blanks.  All white spaces between the
	last printable character in a line and the actual \n are unnecessary for
	a human reader and waste disk space.

	USAGE:

	Text is read from standard input and written to standard output. Thus,
	you can easily redirect I/O in standard AmigaDOS fashion; e.g.:

				DumpSpaces >StrippedKeyboardInput
				DumpSpaces <foo >bar

	`DumpSpaces' requires dos.library V36 (OS 2.0) or better!

	DISCLAIMER:

	You agree to use `DumpSpaces' ENTIRELY AT YOUR OWN RISK. The author can't
	be held responsible for any kind of damage whatsoever resulting from using
	`DumpSpaces'.

	NOTES:

	This is a SAS/C V6.2 source and it should be compiled using the SCOPTIONS
	specified below:

		PARAMETERS=REGISTERS
		NOSTACKCHECK
		STRINGMERGE
		UNSIGNEDCHARS
		OPTIMIZE
		LINK
		SMALLCODE
		SMALLDATA
		STRIPDEBUG
		NOICONS
		OPTIMIZERTIME
		NOSTARTUP
*/

#define	MAXLINESIZE	2048

#include	<proto/dos.h>
#include	<proto/exec.h>
#include	<string.h>

STRPTR version = "\0$VER: DumpSpaces 36.1 (14.4.93) Copyright \xA9 1993 Ralph Seichter";


LONG dumpSpaces (VOID)
{
	struct DosLibrary *DOSBase;
	LONG rc = RETURN_ERROR;

	if (DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 36))
	{
		UBYTE line[MAXLINESIZE];
		BPTR in = Input ();
		BPTR out = Output ();
		BOOL skipLine = TRUE;
		LONG i;

		while (FGets (in, line, MAXLINESIZE - 1))
		{
			if (skipLine)
				/* The first line of text is a blank line which is magically */
				/* created by AmigaDOS. I have absolutely no idea why this is */
				/* done, but I decided to simply ignore this line. :-) */
				skipLine = FALSE;
			else
			{
				/* Check if there's anything printable in this line of text. */
				if ((i = strlen (line) - 1) > 0)
				{
					/* Now remove all trailing blank characters. */
					while (i >= 0 && (line[i] == ' ' || line[i] == '\t' || line[i] == '\n'))
						--i;
					i = (i < 0 ? 0 : i + 1);
					line[i] = '\n';
					line[i + 1] = '\0';
				}
				/* Write current (modified) text line. */
				if (0 != FPuts (out, line))
					break;
			}
		}
		if ((i = IoErr ()) != 0)
			PrintFault (i, NULL);
		CloseLibrary ((struct Library *)DOSBase);
	}
	return (rc);
}
