home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "hippo.h"
-
- #ifdef sun
- #define EXIT_FAILURE 1
- #define EXIT_SUCCESS 0
- #endif
-
- #ifndef vms
- extern int getopt(int, char **, char *);
- #endif
-
- static void cleanup(ntuple *nt_list, display *d_list);
-
- int main(int argc, char **argv)
- {
- #ifndef vms
- extern char *optarg;
- extern int optind;
- #else
- int optind = 1;
- #endif
-
- int c;
- int i;
- int i_appnt=0, i_thisnt=0;
-
- int append = 0;
- int verbose = 0;
-
- char filenm[80]; /* files and filenames */
- FILE *ifile = stdin;
- FILE *ofile = stdout;
- #ifndef __STDC__
- static
- #endif
- char outfilenm[80] = "",
- appfilenm[80] = "";
-
- ntuple *nt_list = NULL; /* hippo objects */
- ntuple appnt=NULL;
- display *d_list = NULL;
-
- #ifndef vms
- while ((c = getopt( argc, argv, "a:vn:o:f:i:")) != -1)
- {
-
- switch (c)
- {
- case 'n':
- sscanf(optarg,"%d",&i_appnt);
- break;
-
- case 'a':
- /* append ntuple to present output file */
- append = 1;
- sscanf(optarg,"%s",appfilenm);
- if (verbose) fprintf(stderr,"Input hippo file = %s\n",
- appfilenm);
- break;
-
- case 'i':
- case 'f':
- sscanf(optarg,"%s",filenm);
- if (verbose) fprintf(stderr,"Input text file = %s\n",filenm);
- if ( (ifile = fopen(filenm,"r")) == NULL )
- {
- fprintf(stderr,"Error opening text input file %s\n",
- filenm);
- exit(EXIT_FAILURE);
- }
- break;
-
- case 'o':
- sscanf(optarg,"%s",outfilenm);
- if (verbose) fprintf(stderr,"outfile = %s\n",outfilenm);
- if ( (ofile = fopen(outfilenm,"w")) == NULL )
- {
- fprintf(stderr,"Error opening hippo output file %s\n",
- outfilenm);
- exit(EXIT_FAILURE);
- }
- break;
-
- case 'v':
- verbose = 1;
- break;
-
- default:
- case '?':
- fprintf(stderr,
- "Usage: [-v] [-a <appendfile>] [-f <inputfile>]\n");
- fprintf(stderr,
- "[-o <outputfile>] [<inputfile> | stdin] [<outputfile> | stdout\n");
- exit(EXIT_FAILURE);
- }
- }
- #endif /* ifndef vms */
-
- /*
- * input file not in option form.
- */
- if (optind < argc)
- {
- sscanf(argv[optind],"%s",filenm);
- if (verbose) fprintf(stderr,"Input text file = %s\n",filenm);
- if ( (ifile = fopen(filenm,"r")) == NULL )
- {
- fprintf(stderr,"Error opening text input file %s\n",
- filenm);
- exit(EXIT_FAILURE);
- }
- optind++;
- }
-
- /*
- * output file not in option form.
- */
- if (optind < argc)
- {
- sscanf(argv[optind],"%s",outfilenm);
- if (verbose) fprintf(stderr,"outfile = %s\n",outfilenm);
- if ( (ofile = fopen(outfilenm,"w")) == NULL )
- {
- fprintf(stderr,"Error opening hippo output file %s\n",
- outfilenm);
- exit(EXIT_FAILURE);
- }
- optind++;
- }
-
- /*
- * handle appending ntuple to existing file.
- */
- if (append && strcmp(appfilenm,"")>0)
- {
- if (verbose) fprintf(stderr,
- "Appending data to ntuple number %d\n",
- i_appnt);
- h_read(appfilenm,&d_list,&nt_list);
- i = 0;
- while (nt_list[i] != NULL) i++;
- if (i_appnt >= i)
- {
- fprintf(stderr,"There are only %d ntuple in file %s\n",
- i,appfilenm);
- fprintf(stderr,"Remember: ntuples are numbered from 0\n");
- cleanup(nt_list,d_list);
- exit(EXIT_FAILURE);
- }
- else
- {
- appnt = nt_list[i_appnt];
- i_thisnt = i_appnt;
- }
- }
- else if (append)
- {
- fprintf(stderr,"Asked for append, but no input file\n");
- exit(EXIT_FAILURE);
- }
- else
- {
- i_thisnt = 0;
- nt_list = (ntuple *) malloc( 2*sizeof(ntuple) );
- nt_list[1] = NULL;
- d_list = (display *) malloc( sizeof(display) );
- d_list[0] = NULL;
- appnt = NULL;
- }
-
- /*
- * options are all set. Call h_fileParse to read file.
- */
- nt_list[i_thisnt] = h_fileParse(ifile, appnt, verbose );
-
- /*
- * write out the file.
- */
- if (h_writeStream(ofile, d_list, nt_list ) != 0)
- {
- fprintf(stderr,"Could not write file\n");
- exit(EXIT_FAILURE);
- }
-
- cleanup(nt_list,d_list);
- exit(EXIT_SUCCESS);
- }
-
- static void cleanup(ntuple *nt_list, display *d_list)
- {
- int i;
-
- if (nt_list != NULL)
- {
- i = 0;
- while (nt_list[i] != NULL) h_freeNt(nt_list[i++]);
- free(nt_list);
- }
-
- if (d_list != NULL)
- {
- i = 0;
- while (d_list[i] != NULL) h_freeDisp(d_list[i++]);
- free(d_list);
- }
- }
-
-