#include <dos.h>      /* for delay()   */
#include <conio.h>    /* for gotoxy()  */
#include <ctype.h>    /* for toupper() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "malloc.h"
#include <graphics.h>

typedef double REAL;

typedef struct Point
   {
      REAL Q[3];    /* Current Cartesian position     */
      REAL X[3];    /* Last point for plotting        */
      int  Color;   /* Circle color in graphic display*/
   } Cell;

typedef Cell * CellPtr;

CellPtr Point = NULL;    /* Size will be done by realloc() */
int MaxSize = 0;

void Prep(int);          /* Generate cells in Point */

#include "Gra-Disp.c"

main (int argc, char *argv[])
{
   FILE *fp = NULL;
   char  Line_In[80];
   Cell  In_Rec = { {0,0,0}, {0,0,0}, 0 };
   REAL  Seconds;
   int   Wait_Length,
         Mono,
         Size = 0,
         Idx,
         Jdx;

   if (argc > 1)
   {
      fp = fopen(argv[1], "r");
      if (fp == NULL)
         printf ("Unable to open %s for read.\n", argv[1]);
   }
   while (fp == NULL)
   {
      fputs ("File name:  ", stdout);
      gets  (Line_In);
      fp = fopen(Line_In, "r");
   }

   if (argc > 2)
      strcpy (Line_In, argv[2]);
   else
   {
      fputs ("Seconds between display of frames:  ", stdout);
      gets  (Line_In);
   }
   sscanf (Line_In, "%lf", &Seconds);
   Wait_Length = (int) (Seconds*1000 + 0.5);

   if (argc > 3)
      strcpy (Line_In, argv[3]);
   else
   {
      fputs ("Color display?  (Y/N)  ", stdout);
      gets  (Line_In);
   }
   Mono = (toupper(Line_In[0]) != 'Y');

   Size = 1;                 /* First frame will scale to size */
   Prep (Size-1);

   Graphic_Display (0, 1);   /* First call to initialize */

   while (! feof(fp))
   {
      if (fgets (Line_In, 80, fp) == NULL) /* in case feof() fails */
         break;
      while (Line_In[0] != '\n')
      {
         sscanf(Line_In, "%lf %lf %lf %d",
               &In_Rec.Q[0], &In_Rec.Q[1], &In_Rec.Q[2],
               &In_Rec.Color);
/*       As saved, the "Color" is actually Idx+1.  */
         Idx =In_Rec.Color - 1;
         if (Idx >= Size)   /* Need more Cells in Point */
            Prep ( (Size = Idx+1) - 1);
         for (Jdx = 0; Jdx < 3; Jdx++)
            Point[Idx].Q[Jdx] = In_Rec.Q[Jdx];
         if (Mono)
            Point[Idx].Color = -In_Rec.Color;
         else
            Point[Idx].Color = In_Rec.Color;
         fgets (Line_In, 80, fp);
      }
      Graphic_Display (Size, 0);
      delay (Wait_Length);
   }
   gotoxy(1,1);
   fputs ("Press <Enter> to exit ", stdout);
   do
      Idx = Wait_Length);
   }
   gotoxy(1,1);
   fputs ("Press <Enter> to exit ", stdout);
   do
      Idx = getchar();
   while (Idx != '\n' && Idx != EOF);
   closegraph();
   return 0;
}

void Prep (int Hi)
{
   int Idx, Jdx;

   if (MaxSize <= Hi)
   {
      Point = (CellPtr) realloc (Point, (Hi+1) * (sizeof *Point) );
      for (Idx = MaxSize; Idx <= Hi; Idx++)
         for (Jdx = 0; Jdx < 3; Jdx++)
            Point[Idx].X[Jdx] = 0;
      MaxSize = Hi + 1;
   }
}
