[comp.sys.atari.st] Source code for program to convert file to double column

cw@vaxwaller.UUCP (Carl Weidling) (07/13/87)

I posted the object code for a program to convert a file to double column.
Here is the source:
/*  a program make double columns */
#include <stdio.h>
#include <osbind.h>
#define ROW 80
#define HALFROW ROW/2
#define COLUMN 52
#define DOUBLE COLUMN * 2
char buff[DOUBLE][ROW];
char printbuf[80];
FILE *ifp,*ofp;
char * copyrite[] =
{
"This program was written by Carl Weidling",
/* Naturally, the next lines referring to Mark Williams Company should be
 * deleted or modified if this program is compiled differently; it refers to
 * runtime library modules that get linked in, NOT to this source code.
 * If you are reading this, then presumably you have the source code, feel
 * free to make changes, fix bugs, but I hope after adding your own touches
 * you will maintain the spirit of freeware in which this was originally
 * written.
*/
"This was compiled using Mark Williams C and contains runtime library",
"routines making this a composite program and I am required to state:",
"Portions of this program, copyright 1984, Mark Williams Company.",
"Mark Williams Company license requires this copyright notice on exterior",
"labels of diskettes with this program, and on ROMs with this program.",
"The only other requirement is that the program not be distributed for",
"profit.",
"For an explanation of how to use it, invoke it with no arguments.",
0
};

char * info[] =
{"This program requires 2 arguments, the 1st is the input file name,",
  "the second is an output file name.  The program reads data from the",
  "input file and double columns it and outputs it to the 2nd file.  If",
  "the line width of a record of the 1st file is greater than half a page",
  "the extra will be overwritten and disappear.",
0
};

main(argc,argv)
int argc;
char *argv[];
{
  if (argc <= 1)
  {
	xplain(info);
	getack("   Happy computing, cw\r\n");
	return;
  } else xplain(copyrite);
  if (argc > 2)
  {
	if ((ifp = fopen(argv[1],"r")) == NULL)
	{
		sprintf(printbuf,"cannot open %s for input\r\n",argv[1]);
		getack(printbuf);
		return;
	}
	if ((ofp = fopen(argv[2],"w")) == NULL)
	{
		sprintf(printbuf,"cannot open %s for output\r\n",argv[2]);
		getack(printbuf);
		fclose(ifp);
		return;
	}
  } else
	{
		getack("Not enough arguments\r\n");
		return;
	}
  if (argc > 3) Cconws("I only use the 1st two arguments\r\n");
  doit();
  fclose(ifp);
  fclose(ofp);
  return;
}

doit()
{
  char pbuff[ROW];
  short i,j,k;
  do
  {
  	for (i = 0; i < DOUBLE ; ++i)
  	{
		for (j = 0; j < ROW; ++j) buff[i][j] = ' ';
		if (fgets(&buff[i][0],ROW,ifp) == NULL) /* Read line by line to EOF */
		{
			break;
		}
		for (j = 0; j < ROW; ++j)
			if (buff[i][j] < ' ') buff[i][j] = ' ';
  	}
  	if (i == DOUBLE)
  	{
		for (j = 0; j < COLUMN; ++j)
		{
			for (k = 0; k < HALFROW; ++k)
			{
				pbuff[k] = buff[j][k];
			}
			for ( ; k < ROW; ++k)
			{
				pbuff[k] = buff[j+COLUMN][k-HALFROW];
			}
			for (k = 0; k < ROW ; ++k)
				if (pbuff[k] < ' ') pbuff[k] = ' ';
			pbuff[ROW-1] = '\n';
			pbuff[ROW] = 0;
			fprintf(ofp,pbuff);
		}
		fputc('\014',ofp);
		fputc('\n',ofp);
  	} else	/* End of the line */
	  {
		if (i == 0)
		{
			exit(1);
		}
		if (i > COLUMN)
  		{
			for (j = 0; j < i - COLUMN; ++j)
			{
				for (k = 0; k < HALFROW; ++k)
				{
					pbuff[k] = buff[j][k];
				}
				for ( ; k < ROW; ++k)
				{
					pbuff[k] = buff[j+COLUMN][k-HALFROW];
				}
				for (k = 0; k < ROW ; ++k)
					if (pbuff[k] < ' ') pbuff[k] = ' ';
				pbuff[ROW-1] = '\n';
				pbuff[ROW] = 0;
				fprintf(ofp,pbuff);
			}
			for ( ; j < COLUMN; ++j)
			{
				for (k = 0; k < ROW; ++k)
				{
					pbuff[k] = buff[j][k];
					if (pbuff[k] < ' ') pbuff[k] = ' ';
				}
				pbuff[ROW-1] = '\n';
				pbuff[ROW] = 0;
				fprintf(ofp,pbuff);
			}
			fputc('\014',ofp);
			fputc('\n',ofp);
			exit(1);
		} else	/* i less than column */
		  {
			for (j = 0; j < i; ++j)
			{
				for (k = 0; k < ROW; ++k)
				{
					pbuff[k] = buff[j][k];
					if (pbuff[k] < ' ') pbuff[k] = ' ';
				}
				pbuff[ROW-1] = '\n';
				pbuff[ROW] = 0;
				fprintf(ofp,pbuff);
			}
			fputc('\014',ofp);
			fputc('\n',ofp);
			exit(1);
		  }
	}
  } while (i == DOUBLE);
}
xplain(msg)
char *msg[];
{ register char **index;
  for (index = msg; *index != 0 ; ++index)
  {
	Cconws(*index);
	Cconws("\r\n");
  }
}
  
getack(ptr)	/* Gets acknowledgement from user */
char *ptr;
{
	Cconws(ptr);
	Cconws("Hit any key to continue\r\n");
	Cconin();
}