/* $Id$
*/

*/   txt2neut - read in points filed and write out Patran Neutral file    */

*/  07 May 99  DSC  Created  */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

/* declare File type for input and output files
   input_file and output_file are pointer variables */
FILE * input_file;
FILE * output_file;
char line[1000];

int getline();
int wrt_values(void);
int wrt_title_card(void);
int wrt_summary(void);
int wrt_footer(void);

/*--------------------------------------------------------------------*/

int getline()
{
   int c, i;
   extern char line[];

   for (i = 0; i < 999 &&
               ((c = getc(input_file)) != EOF) &&
               (c != '\n') ; ++i)
   {
      line[i] = c;
   }

   if (c == EOF)
   {
      i = -1;
   }

   if (c == '\n')
   {
      line[i] = c;
      ++i;
   }

   line[i] = '\0';
   return i;

}

/*--------------------------------------------------------------------*/
int wrt_values(void)
{
   int ipkt=31,id,iv=0,kc=1;
   int n1,n2,n3,n4,n5;
   int len_line;
   float x,y,z;

   n1=n2=n3=n4=n5=0;

/* getline() returns a value of -1 at EOF */

   id = 0;
   len_line = 0;

   while (len_line >= 0)
   {
      id++;

      len_line = getline();

/* getline() returns a value of -1 at EOF */
      if (len_line > 0)
      {
         sscanf(line,"%f %f %f", &x, &y, &z);
         fprintf(output_file,"%2d%8d%8d%8d%8d%8d%8d%8d%8d\n",
                 ipkt,id,iv,kc,n1,n2,n3,n4,n5);
         fprintf(output_file,"%16.9f%16.9f%16.9f\n",x,y,z);
      }
   }

   return 0;
}

/*--------------------------------------------------------------------*/

int wrt_title_card(void)
{
   int ipkt=25,id=0,iv=0,kc=1;
   int n1,n2,n3,n4,n5;

   n1=n2=n3=n4=n5=0;

   fprintf(output_file,"%2d%8d%8d%8d%8d%8d%8d%8d%8d \n",
             ipkt,id,iv,kc,n1,n2,n3,n4,n5);
   fprintf(output_file,"CFX-Build 5 Neutral File\n");

   return 0;
}

/*--------------------------------------------------------------------*/
int wrt_summary(void)
{
   int ipkt=26,id=0,iv=0,kc=1;
   int n1,n2,n3,n4,n5;

   char month[12][4] = { "Jan\0","Feb\0","Mar\0","Apr\0",
                         "May\0","Jun\0","Jul\0","Aug\0",
                         "Sep\0","Oct\0","Nov\0","Dec\0"};


   time_t current_time;
   struct tm *lt;


   n1=n2=n3=n4=n5=0;

   fprintf(output_file,"%2d%8d%8d%8d%8d%8d%8d%8d%8d \n",
             ipkt,id,iv,kc,n1,n2,n3,n4,n5);

   current_time = time(0);

   lt = localtime(&current_time);

   fprintf(output_file,"%d-%s-%d 3.0\n",
         lt->tm_mday,month[lt->tm_mon],lt->tm_year);

   return 0;
}

/*--------------------------------------------------------------------*/
int wrt_footer(void)
{
   int ipkt=99,id=0,iv=0,kc=1;
   int n1,n2,n3,n4,n5;

   n1=n2=n3=n4=n5=0;

   fprintf(output_file,"%2d%8d%8d%8d%8d%8d%8d%8d%8d\n",
             ipkt,id,iv,kc,n1,n2,n3,n4,n5);

   return 0;
}
/*--------------------------------------------------------------------*/

main(int argc, char * argv[])
{
   extern char line[];
   int iret = 0;

   int ctr,n;
   char *arg1=NULL;
   char *arg2=NULL;
   char *arg3=NULL;
   char *arg4=NULL;
   char file_in[81];
   char file_out[81];

   char *usgmsg[] = {
        "Usage: txt2neut -input file_in -output file_out",
        "where:",
        "    file_in  = points filename ",
        "    file_out = output filename ",
        " eg. txt2neut -input points.txt -output points.out ",
        NULL
        };

/* Look at the arguments given on the command line */

/*   for (ctr = 0; ctr < argc; ctr++)
   {
      printf("argument #%d is -> |%s|\n", ctr, argv[ctr]);

   }  */

   if (argc>0)
      arg1 = argv[1];
   if (argc>1)
      arg2 = argv[2];
   if (argc>2)
      arg3 = argv[3];
   if (argc>3)
      arg4 = argv[4];

   if (argc != 5)
   {
      for (n = 0; usgmsg[n] != NULL; n++)
            printf ("%s\n", usgmsg[n]);
        exit (0);
   }


   if (arg1 && (strcmp(arg1,"-input") == 0))
   {
      if (arg2)
      {
         strcpy(file_in,arg2);
      }
   }
   else
   {
      for (n = 0; usgmsg[n] != NULL; n++)
         printf ("%s\n", usgmsg[n]);
      exit (0);
   }


   if (arg3 && (strcmp(arg3,"-output") == 0))
   {
      if (arg4)
      {
         strcpy(file_out,arg4);
      }
   }
   else
   {
      for (n = 0; usgmsg[n] != NULL; n++)
         printf ("%s\n", usgmsg[n]);
      exit (0);
   }



/* Open input and output files */
   input_file = fopen(file_in,"r");

/* check if input file exists */
   if (input_file == NULL)
   {
      printf("Could not open input file\n");
      exit(0);
   }

/* check if output file exists */
   output_file = fopen(file_out,"w");

   if (output_file == NULL)
   {
      printf("Could not open output file\n");
      exit(0);
   }


      iret = wrt_title_card();
      if (iret != 0 )
      {
         printf("Error writing title card");
         exit(0);
      }

      iret = wrt_summary();
      if (iret != 0 )
      {
         printf("Error writing summary card");
         exit(0);
      }

/* read a line from the input file
   and write to the output file */
      iret = wrt_values();
      if (iret != 0 )
      {
         printf("Error reading and writing x,y,z values");
         exit(0);
      }

      iret = wrt_footer();
      if (iret != 0 )
      {
         printf("Error writing footer");
         exit(0);
      }

/* close the input and output files */
fclose(input_file);
fclose(output_file);

}

/*---------------------------------------------------------------*/

