#if defined (MSDOS)
#define SHORTY 1
#else
#if defined (__MSDOS__)
#define SHORTY 1
#endif
#endif
#if defined (vms)
#define SYS_OK 1
#endif
#if defined (unix)
#define SYS_OK  0
#endif
#include <stdio.h>
#define LONG long 
#define  INT32                int
#define UINT32 unsigned       int
#define  INT16          short int
#define UINT16 unsigned short int
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <cdftools.h>
#include <cdf.h>
/* define the program name, and version number and date (see makecdf.c) */
struct {
   char version[44];
} IDENT;
/* define the runtime parameters data structure */
struct {
   char infname[80];        /* input file parameter */
   char ffdfname[80];       /* flatfile description parameter */
   char incdfname[80];      /* input CDF/skeleton filename parameter */
   char outcdfname[80];	    /* output CDF filename parameter */
   LONG progress;           /* send progress output */
   LONG debug;              /* send debug output */
   LONG sendtolog;          /* send output to log file 0=false,1=true */
   LONG sendtoterm;         /* send output to terminal */
   LONG haltateof;          /* halt only when input file EOF reached */
   LONG numrecs;            /* #input recs to process iff haltateof=0 */
   LONG autocase;           /* case to use when auto-naming 0=lower,1=upper */
   LONG autostyle;          /* autonaming style: 0=ISTP, 1=ISIS */
} RPARMS;

/* define the structure to hold list of input file names and paths */
LONG FLISTne;
struct FLISTelem {
   char Pathname[80];       /* name of input path(s) */
   char Filename[80];       /* name of input file(s) */
} FLISTz;
struct FLISTelem *FLIST;

/* define the structure to hold information about the output CDF file */
struct {
   CDFid outCDFid;          /* id of output cdf */
   LONG  encoding;          /* encoding of output cdf */
   LONG  CDFrec;            /* CDF record number */
} OUTCDF;

/* define the structure to hold CDF variable information */
LONG CLISTne;
struct CLISTelem {
   LONG flagone;           /* flag indicating if first min/max values  */
   LONG ccount;            /* max number of characters in valid value  */
   void *vmin;             /* pointer to autogenerated validmin values */
   void *vmax;             /* pointer to autogenerated validmax values */
   char form[12];          /* autogenerated format value */
} CLISTz;
struct CLISTelem *CLIST;


/* define structure to hold the input file variable descriptions */
LONG VLISTne;
struct VLISTelem {
   char vname[40];      /* variable name in input file */
   char cdfvname[40];   /* variable name in cdf file, if NULL then = vname */
   char format[12];     /* C language format */
   LONG cdfvarnum;      /* cdf variable number, -1=not a CDF var */
   LONG cdfvartype;     /* cdf variable type */
   LONG clistnum;       /* index of var in list of cdf variable list */
   LONG ccount;         /* num of chars in largest valid conversion  */
   LONG ndims;          /* number of dims, 0=scalar,1=vector,2=image */
   LONG dims[3];        /* dimension sizes */
   LONG nelems;         /* total number of elements */
   LONG eperr;          /* elements per record */
   LONG Elem[2];        /* element#'s, special case: non-contiguous data */
   LONG length;         /* #bytes in flatfile by var iff formatted */
   LONG constant;       /* var is constant, 0=false,1=true */
   LONG header;         /* var is in header, 0=false,1=true */
   LONG eol;            /* var is followed by endofline,0=false,1=true */
   LONG majority;       /* order of elements if 2D,0=row,1=column */
   LONG bsubr;          /* begin subrecord#, 0=not start of subrec */
   LONG esubr;          /* end subrecord#, 0=not end of subrec */
   double fillval;      /* data fillval for replace with ISTP fill */
   short int autovalids;/* autogenerate validmin/max 0=f,1=t */
   short int autoscales;/* autogenerate scalemin/max 0=f,1=t */
   short int autoformat;/* autogenerate format values 0=f,1=t */
   short int autofill;	/* autoreplace fillval with ISTP value */
   void *buffer;        /* pointer to input buffer */
   void *dataptr;       /* pointer to data buffer */
} VLISTz;
struct VLISTelem *VLIST;


/* define structure to hold the ordered list for file reading */
LONG OLISTne;
struct OLISTelem{
   short int vnum;		/* variable number */
   short int velem;		/* variable element number */
   short int rflag;		/* read flag */
   short int pflag;		/* process flag */
} OLISTz;
struct OLISTelem *OLIST;


/* define the structure to hold any subrecord information */
LONG SRECSne;
struct SRECSelem {
   LONG   countr;	/* subrecord counter */
   LONG   nreps;        /* number of repetitions */
   double delta;        /* delta-T (ms) between reps */
} SRECSz;
struct SRECSelem *SRECS;


/* define the structure to hold non-variable info about input file */
struct {
   LONG format;         /* 0=freeform,1=formatted */
   LONG dtype;          /* 0=text,1=binary    freeform must be textual */
   LONG ttype;          /* 0=ASCII,1=EBCDIC */
   LONG btype;          /* vax,sun,ibmpc etc. used only if formatted binary */
   LONG buflen;         /* length of file input buffer */
   LONG recnum;         /* current input file record number */
   /*char delimiter;       parsing delimiter, used only if freeform text */
   FILE *fptr;          /* pointer to input stream */
   char *buffer;        /* pointer to input buffer */
   char *bptr;          /* pointer that floats in input buffer */
   char delimiter,dumie[3];      /* parsing delimiter, used only if freeform text */
} INFILE;


/* define structure to hold info about log file */
struct {
   LONG nrecs;          /* logfile record counter */
   LONG maxrecs;        /* max log file number of records */
   char fname[80];      /* name of log file */
   char message[512];    /* buffer for messages to be output */
   FILE *fptr;          /* pointer to logfile stream */
  LONG appendlog; /* Flag for one log file holding all CDFs in this run.*/
  char appendname[80]; /* Name of log file from previous CDF made in this run.*/
  LONG big;            /* 1 if max was reached and logfile was closed. */
} LOGFILE;

/* define structure to hold info about Epoch generation */
struct {
    LONG algorithm;     /* generation algorithm */
    LONG operand[10];   /* generation operands */
    LONG EpGenerator;   /* input variable which triggers epoch calculations */
    LONG Epvarnum;      /* cdf variable# of Epoch */
    double FirstEp;     /* value of first epoch computed */
    double BaseEp;      /* value of base Epoch of algorithm=4 */
} EpALG;


/* Declare function prototypes for makeCDF */
extern void StripName PROTOARGs((char *oldname, char *newname));
extern void MSG_control PROTOARGs((LONG mtype, char *instring, CDFstatus status));
extern LONG Validate_BOOLEAN PROTOARGs((char *instring));
extern LONG Validate_POSINT PROTOARGs((char *instring));
extern double Validate_FILLVAL PROTOARGs((char *instring, LONG cdfvartype));
extern LONG Validate_FORMAT PROTOARGs((char *ins, char *out,
                                       LONG *ilength, LONG *cdfvartype));
extern LONG Separate_Varnames PROTOARGs((char *instring, char *vname1,
                              char *vname2, LONG *velem1, LONG *velem2));
extern LONG Preview_RPARMS PROTOARGs((FILE *file_ptr));
extern LONG Import_RPARMS PROTOARGs((FILE *file_ptr));
extern LONG Import_INFILE PROTOARGs((FILE *file_ptr));
extern LONG Import_EpALG PROTOARGs((FILE *file_ptr));
extern LONG Import_Variables PROTOARGs((FILE *file_ptr));
extern LONG Preview_FFD PROTOARGs((void));
extern LONG Import_FFD PROTOARGs((void));
extern LONG SREC_Bounds PROTOARGs((LONG subrecno,LONG *firstvar,LONG *lastvar));
extern LONG Build_OLIST PROTOARGs((void));
extern LONG inCDFform PROTOARGs((char *filename));
extern LONG Generate_CDFskeleton PROTOARGs((void));
extern LONG Match_Variables PROTOARGs((void));
extern LONG Fix_Format PROTOARGs((char *oldformat, char *newformat));
extern LONG Allocate_Buffers PROTOARGs((void));
extern LONG Determine_Bufsize PROTOARGs((void));
/*extern void Read_Infile PROTOARGs((void));*/
extern int Read_Infile PROTOARGs((void));
extern LONG Open_Infile PROTOARGs((LONG ifile));
extern LONG Parser1 PROTOARGs((LONG vnum, LONG velem));
extern LONG Parser2 PROTOARGs((LONG vnum, LONG velem));
extern LONG Parser3 PROTOARGs((LONG vnum, LONG velem));
extern LONG AutoMinMax PROTOARGs((LONG vnum));
extern LONG AutoFiller PROTOARGs((LONG vnum));
extern LONG AutoFormat PROTOARGs((LONG vnum, char *form));
extern LONG Open_OutCDF PROTOARGs((void));
extern LONG WriteEpochToCDF PROTOARGs((double depoch, LONG CDFrec));
extern LONG WriteToCDF PROTOARGs((LONG vnum, LONG CDFrec));
extern LONG Generate_LFILEID PROTOARGs((char *lfileid));
extern LONG Set_LFILEID PROTOARGs((char *lfileid));
extern LONG Set_AutoVattrs PROTOARGs((void));
extern LONG AbbrSCname PROTOARGs((char *source, char *attr));
extern LONG AutoNamer PROTOARGs((char *oldname, char *newname));
extern LONG ShortName PROTOARGs((char *lfileid));
extern LONG Cleanup_makeCDF PROTOARGs((LONG ifile));
extern LONG Build_FLIST PROTOARGs((void));
extern LONG Generate_Epoch PROTOARGs((double *EpVAL));
extern LONG Determine_EpGenerator PROTOARGs((void));
extern LONG epochParse2 PROTOARGs((char *instring, double *Etime));
extern LONG epochParse3 PROTOARGs((char *instring, double *Etime));
extern LONG epochParse4 PROTOARGs((char *instring, double *Etime));
extern LONG julian_date PROTOARGs((LONG y, LONG m, LONG d));
extern LONG Get_Year PROTOARGs((LONG vnum, LONG *lyear));
extern LONG Get_Month PROTOARGs((LONG vnum, LONG *lmonth));
extern LONG Get_Day PROTOARGs((LONG vnum, LONG *lday));
extern void Convert_DOY PROTOARGs((LONG lyear, LONG *lmonth, LONG *lday));
extern void Compute_DOY PROTOARGs((LONG lyear, LONG *lmonth, LONG *lday));
extern LONG Get_Hour PROTOARGs((LONG vnum, double *dval));
extern LONG Get_Minute PROTOARGs((LONG vnum, double *dval));
extern LONG Get_Second PROTOARGs((LONG vnum, double *dval));
extern LONG Get_MSecs PROTOARGs((LONG vnum, double *dval));
extern LONG Get_Epoch PROTOARGs((LONG vnum, double *dval));
extern LONG Get_BaseEpoch PROTOARGs((double *dval));
extern LONG Compute_Epoch PROTOARGs((double *dval));
extern LONG Compute_Epoch8 PROTOARGs((double *dval));
extern LONG Compute_Epoch9 PROTOARGs((double *dval));
extern LONG Parse_Filename PROTOARGs((char *fname));
extern LONG ConvertBuffer PROTOARGs((LONG srcEncoding, LONG dstEncoding,
                                     LONG negToPosFp0mode, LONG dataType,
                                     LONG numElems,
                                     void *srcbuffer, void *dstBuffer));
