/*
 * $Id: paddb.c,v 1.1 89/11/11 17:29:43 tynor Exp $
 *----------------------------------------------------------------------------
 *	FPLAN - Flight Planner
 *	Steve Tynor
 *	tynor@prism.gatech.edu
 *
 *	This program is in the public domain. Permission to copy,
 * distribute, modify this program is hearby given as long as this header
 * remains. If you redistribute this program after modifying it, please
 * document your changes so that I do not take the blame (or credit) for
 * those changes.  If you fix bugs or add features, please send me a
 * patch so that I can keep the 'official' version up-to-date.
 *
 *	Bug reports are welcome and I'll make an attempt to fix those
 * that are reported.
 *
 *	USE AT YOUR OWN RISK! I assume no responsibility for any
 * errors in this program, its database or documentation. I will make an
 * effort to fix bugs, but if you crash and burn because, for example,
 * fuel estimates in this program were inaccurate, it's your own fault
 * for trusting somebody else's code! Remember, as PIC, it's _your_
 * responsibility to do complete preflight planning. Use this program as
 * a flight planning aid, but verify its results before using them.
 *----------------------------------------------------------------------------
 */

static char rcsid[] = "$Id: paddb.c,v 1.1 89/11/11 17:29:43 tynor Exp $";

#include <stdio.h>
#include "version.h"

#define EXIT_GOOD 0
#define EXIT_BAD  1

#define MAX(a,b) ((a)>=(b)?(a):(b))

/*----------------------------------------------------------------------------*/
usage (progname)
     char *progname;
{
   fprintf (stderr, "FPLAN %s\n", VERSION);
   fprintf (stderr, "usage: %s text-file db-file\n", progname);
   exit (EXIT_BAD);
}

/*----------------------------------------------------------------------------*/
int main (argc, argv)
     int argc;
     char *argv[];
{
#define BUFSIZE 200
   FILE *in, *out;
   int recl = 0;
   int len, i;
   char buffer[BUFSIZE];

#ifdef MSDOS
#define CREATE_MODE "wb"
#define LINE_TERMINATOR_LEN 2	/* CR-LF */
#else
#define CREATE_MODE "w"
#define LINE_TERMINATOR_LEN 1	/* LF */
#endif

   if (argc != 3)
      usage (argv[0]);

   if (! (in = fopen (argv[1], "r"))) {
      fprintf (stderr, "ERROR: opening input file: %s\n", argv[1]);
      usage (argv[0]);
   }

   if (! (out = fopen (argv[2], CREATE_MODE))) {
      fprintf (stderr, "ERROR: opening output file: %s\n", argv[2]);
      usage (argv[0]);
   }

   /*
    * first pass: find the max record length
    */
   while (fgets (buffer, BUFSIZE, in))
      recl = MAX (recl, strlen (buffer) - LINE_TERMINATOR_LEN);

   fclose (in);

   /*
    * second pass: copy each record to the output; pad each record
    */

   if (! (in = fopen (argv[1], "r"))) {
      fprintf (stderr, "ERROR: opening input file: %s\n", argv[1]);
      usage (argv[0]);
   }
   while (fgets (buffer, BUFSIZE, in)) {
      len = strlen (buffer) - LINE_TERMINATOR_LEN;
      buffer[len] = '\0';
      fprintf (out, "%s", buffer);
      for (i = len; i < recl; i++)
	 putc ('\0', out);
      putc ('\n', out);
   }
   fclose (in);
   fclose (out);
}

