	/* PARSES MARK JOHNSON'S THRUST CURVES & PUTS OUT SHORT CURVES */

#include <stdio.h>
#include <math.h>
#include <string.h>

#define MAXLINE 150
#define ARRAY_LENGTH 50
#define CLS printf("\n \n \n \n \n \n")
#define TRUE 1
#define FALSE 0
/***************************************************************************/
void fix_ofname(ofname, manufact)
char *ofname, *manufact;
{
int i;
char strwork[MAXLINE];

/* removes slashes, prefixes "mj", suffixes ".eng", and truncates */
strcpy(strwork,"mj");
strwork[2]=*manufact;
strwork[3]='\0';
strcat(strwork,ofname);
/* if longer than 8, truncates */
strwork[8]='\0';
strcpy(ofname, strwork);

i=0;
while(ofname[i] != '\0')
{
if (ofname[i] == '/') ofname[i]='$';
/* CONVERT TO LOWER CASE */
if ((ofname[i] <= 'Z') && (ofname[i] >= 'A'))
   ofname[i] = ofname[i] + 'a' - 'A';
++i;
}
strcat(ofname, ".eng");
return;

}
/***************************************************************************/

void main()
{

char line[ARRAY_LENGTH][MAXLINE];
char ifilename[MAXLINE], ofilename[MAXLINE];
char strwork1[MAXLINE], strwork2[MAXLINE],  strwork3[MAXLINE];
char strwork4[MAXLINE],  strwork5[MAXLINE];

float impulse=0, mp=0, oldtime, oldf, time=0, f=0;

int line_point=0, go_on, go_on1, put_out, intervals, i;

FILE *ifp, *ofp, *fopen();

go_on = TRUE;

put_out = TRUE;

ifp = NULL;
while (ifp == NULL)
  {
  CLS;
  printf("Input File Name: ");
  gets(ifilename);
  ifp = fopen(ifilename, "r");
  }

fgets(line[line_point],MAXLINE, ifp);
if (feof(ifp))go_on=FALSE;
++line_point;

if (feof(ifp))put_out = FALSE;

line[1][0] = ';';
line[1][1]='\n';
while(go_on)
  {
  while ((line[1][0] == ';')&(go_on))
    {
    fgets(line[line_point],MAXLINE, ifp);
    if (feof(ifp))go_on=FALSE;
    if(go_on)
      {
      if(line[1][0] == ';')strcpy(line[0], line[1]);
      }
    else put_out = FALSE;
    }/* while line[line_point][0] == ';' */

  /* parse out the file name and propellant mass */
  sscanf(line[1], "%s %s %s %s %f %s %s", ofilename, strwork1, strwork2,
		   strwork3, &mp, strwork4, strwork5);

  fix_ofname(ofilename, strwork5);
  /* add extension to file name */

  mp=mp*1000.0;

  intervals = -1;
  impulse=oldtime=oldf=0.0;
  go_on1=TRUE;

  while (go_on1)
    {
    ++line_point;
    fgets(line[line_point],MAXLINE, ifp);
    if (feof(ifp))go_on=FALSE;
    if((go_on) && ((line[line_point][0] == ' ') ||
      (line[line_point][0] == '\t')))
      {
      sscanf(line[line_point], "%f %f", &time, &f);
      impulse += 0.5*(f+oldf)*(time-oldtime);
      ++intervals;
      oldf=f;
      oldtime=time;
      }/* if(go_on) */
     if((line[line_point][0] != ' ') &&
	(line[line_point][0] != '\t'))go_on1=FALSE;
     if (! go_on) go_on1=FALSE;
    }/*while (go_on1)*/

  if(put_out)
    {
    /* open the output file */
    ofp = fopen(ofilename, "w");
    if (ofp == NULL)
      {
      printf("Cannot allocate output file %s. Program terminates \n", ofilename);
      return;
      }

    /* write output file and close */
    fprintf(ofp,"%s",line[0]);
    ++intervals; /* allow for zero point to be added*/
    fprintf (ofp,"%f %f %f %i \n", impulse, mp, time, intervals);
    fprintf (ofp, "0.0  0.0\n");
    for(i=2; i<line_point; i++)
      {
      fprintf(ofp,"%s",line[i]);
      } /* for(i=0; i<line_point; i++) */
    fclose(ofp);

    /* move last line to zeroth line */
    strcpy(line[0], line[line_point]);
    strcpy(line[1], line[line_point]);
    line_point = 1;
  }/* if(put_out) */
  }/* while(go_on) */
  fclose(ifp);
  return;
} /* end of program */
