home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / HippoDraw / hippo / text2nt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-28  |  4.5 KB  |  212 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "hippo.h"
  6.  
  7. #ifdef sun
  8. #define EXIT_FAILURE 1
  9. #define EXIT_SUCCESS 0
  10. #endif
  11.  
  12. #ifndef vms
  13. extern int getopt(int, char **, char *);
  14. #endif
  15.  
  16. static void cleanup(ntuple *nt_list, display *d_list);
  17.  
  18. int main(int argc, char **argv)
  19. {
  20. #ifndef vms
  21.      extern char *optarg;
  22.      extern int optind;
  23. #else
  24.      int optind = 1;
  25. #endif
  26.  
  27.      int c;
  28.      int i;
  29.      int i_appnt=0, i_thisnt=0;
  30.      
  31.      int append = 0;
  32.      int verbose = 0;
  33.      
  34.      char filenm[80];        /* files and filenames */
  35.      FILE *ifile = stdin;
  36.      FILE *ofile = stdout;
  37. #ifndef __STDC__
  38.      static
  39. #endif
  40.      char outfilenm[80] = "",
  41.       appfilenm[80] = "";
  42.  
  43.      ntuple *nt_list = NULL;    /* hippo objects */
  44.      ntuple appnt=NULL;
  45.      display *d_list = NULL;
  46.  
  47. #ifndef vms
  48.      while ((c = getopt( argc, argv, "a:vn:o:f:i:")) != -1)
  49.      {
  50.       
  51.       switch (c)
  52.       {
  53.       case 'n':
  54.            sscanf(optarg,"%d",&i_appnt);
  55.            break;
  56.            
  57.       case 'a':
  58.            /* append ntuple to present output file */
  59.            append = 1;
  60.            sscanf(optarg,"%s",appfilenm);
  61.            if (verbose) fprintf(stderr,"Input hippo file = %s\n",
  62.                     appfilenm);
  63.            break;
  64.            
  65.       case 'i':
  66.       case 'f':
  67.            sscanf(optarg,"%s",filenm);
  68.            if (verbose) fprintf(stderr,"Input text file = %s\n",filenm);
  69.            if ( (ifile = fopen(filenm,"r")) == NULL )
  70.            {
  71.             fprintf(stderr,"Error opening text input file %s\n",
  72.                 filenm);
  73.             exit(EXIT_FAILURE);
  74.            }
  75.            break;
  76.            
  77.       case 'o':
  78.            sscanf(optarg,"%s",outfilenm);
  79.            if (verbose) fprintf(stderr,"outfile = %s\n",outfilenm);
  80.            if ( (ofile = fopen(outfilenm,"w")) == NULL )
  81.            {
  82.             fprintf(stderr,"Error opening hippo output file %s\n",
  83.                 outfilenm);
  84.             exit(EXIT_FAILURE);
  85.            }
  86.            break;
  87.  
  88.       case 'v':
  89.            verbose = 1;
  90.            break;
  91.            
  92.       default:
  93.       case '?':
  94.            fprintf(stderr,
  95.                "Usage: [-v] [-a <appendfile>] [-f <inputfile>]\n");
  96.            fprintf(stderr,
  97.                "[-o <outputfile>] [<inputfile> | stdin] [<outputfile> | stdout\n");
  98.            exit(EXIT_FAILURE);
  99.       }
  100.      }
  101. #endif                /* ifndef vms */
  102.  
  103.      /*
  104.       * input file not in option form.
  105.       */
  106.      if (optind < argc)
  107.      {
  108.       sscanf(argv[optind],"%s",filenm);
  109.       if (verbose) fprintf(stderr,"Input text file = %s\n",filenm);
  110.       if ( (ifile = fopen(filenm,"r")) == NULL )
  111.       {
  112.            fprintf(stderr,"Error opening text input file %s\n",
  113.                filenm);
  114.            exit(EXIT_FAILURE);
  115.       }
  116.       optind++;
  117.      }
  118.      
  119.      /*
  120.       * output file not in option form.
  121.       */
  122.      if (optind < argc)
  123.      {
  124.       sscanf(argv[optind],"%s",outfilenm);
  125.       if (verbose) fprintf(stderr,"outfile = %s\n",outfilenm);
  126.       if ( (ofile = fopen(outfilenm,"w")) == NULL )
  127.       {
  128.            fprintf(stderr,"Error opening hippo output file %s\n",
  129.                outfilenm);
  130.            exit(EXIT_FAILURE);
  131.       }
  132.       optind++;
  133.      }
  134.  
  135.      /*
  136.       * handle appending ntuple to existing file.
  137.       */
  138.      if (append && strcmp(appfilenm,"")>0)
  139.      {
  140.       if (verbose) fprintf(stderr,
  141.                    "Appending data to ntuple number %d\n",
  142.                    i_appnt);
  143.       h_read(appfilenm,&d_list,&nt_list);
  144.       i = 0;
  145.       while (nt_list[i] != NULL) i++;
  146.       if (i_appnt >= i) 
  147.       {
  148.            fprintf(stderr,"There are only %d ntuple in file %s\n",
  149.                i,appfilenm);
  150.            fprintf(stderr,"Remember: ntuples are numbered from 0\n");
  151.            cleanup(nt_list,d_list);
  152.            exit(EXIT_FAILURE);
  153.       }
  154.       else
  155.       {
  156.            appnt = nt_list[i_appnt];
  157.            i_thisnt = i_appnt;
  158.       }
  159.      }
  160.      else if (append)
  161.      {
  162.       fprintf(stderr,"Asked for append, but no input file\n");
  163.       exit(EXIT_FAILURE);
  164.      }
  165.      else
  166.      {
  167.       i_thisnt = 0;
  168.       nt_list = (ntuple *) malloc( 2*sizeof(ntuple) );
  169.       nt_list[1] = NULL;
  170.       d_list = (display *) malloc( sizeof(display) );
  171.       d_list[0] = NULL;
  172.       appnt = NULL;
  173.      }
  174.      
  175.      /*
  176.       * options are all set. Call h_fileParse to read file.
  177.       */
  178.      nt_list[i_thisnt] = h_fileParse(ifile, appnt, verbose );
  179.      
  180.      /* 
  181.       * write out the file.
  182.       */
  183.      if (h_writeStream(ofile, d_list, nt_list ) != 0)
  184.      {
  185.       fprintf(stderr,"Could not write file\n");
  186.       exit(EXIT_FAILURE);
  187.      }
  188.  
  189.      cleanup(nt_list,d_list);
  190.      exit(EXIT_SUCCESS);
  191. }
  192.  
  193. static void cleanup(ntuple *nt_list, display *d_list)
  194. {
  195.      int i;
  196.       
  197.      if (nt_list != NULL)
  198.      {
  199.       i = 0;
  200.       while (nt_list[i] != NULL) h_freeNt(nt_list[i++]);
  201.       free(nt_list);
  202.      }
  203.  
  204.      if (d_list != NULL)
  205.      {
  206.       i = 0;
  207.       while (d_list[i] != NULL) h_freeDisp(d_list[i++]);
  208.       free(d_list);
  209.      }
  210. }
  211.  
  212.