home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / HippoDraw / hippo / nt2text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-28  |  3.5 KB  |  170 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_ntout=0;
  30.      
  31.      int verbose = 0;
  32.      
  33.      char filenm[80];        /* files and filenames */
  34.      FILE *ifile = stdin;
  35.      FILE *ofile = stdout;
  36. #ifndef __STDC__
  37.      static
  38. #endif
  39.      char outfilenm[80] = "";
  40.  
  41.      ntuple *nt_list = NULL;        /* hippo objects */
  42.      display *d_list = NULL;
  43.  
  44. #ifndef vms
  45.      while ((c = getopt( argc, argv, "vn:o:f:i:")) != -1)
  46.      {
  47.       
  48.       switch (c)
  49.       {
  50.       case 'n':
  51.            sscanf(optarg,"%d",&i_ntout);
  52.            break;
  53.            
  54.       case 'i':
  55.       case 'f':
  56.            sscanf(optarg,"%s",filenm);
  57.            if (verbose) fprintf(stderr,"Input text file = %s\n",filenm);
  58.            if ( (ifile = fopen(filenm,"r")) == NULL )
  59.            {
  60.             fprintf(stderr,"Error opening text input file %s\n",
  61.                 filenm);
  62.             exit(EXIT_FAILURE);
  63.            }
  64.            break;
  65.            
  66.       case 'o':
  67.            sscanf(optarg,"%s",outfilenm);
  68.            if (verbose) fprintf(stderr,"outfile = %s\n",outfilenm);
  69.            if ( (ofile = fopen(outfilenm,"w")) == NULL )
  70.            {
  71.             fprintf(stderr,"Error opening hippo output file %s\n",
  72.                 outfilenm);
  73.             exit(EXIT_FAILURE);
  74.            }
  75.            break;
  76.  
  77.       case 'v':
  78.            verbose = 1;
  79.            break;
  80.            
  81.       default:
  82.       case '?':
  83.            fprintf(stderr,"Usage: [-v] [-a <appendfile>] [-f <inputfile>]\n");
  84.            fprintf(stderr,"[-o <outputfile>] [<inputfile> | stdin] [<outputfile> | stdout\n");
  85.            exit(EXIT_FAILURE);
  86.       }
  87.      }
  88. #endif                /* ifndef vms */
  89.  
  90.      /*
  91.       * input file not in option form.
  92.       */
  93.      if (optind < argc)
  94.      {
  95.       sscanf(argv[optind],"%s",filenm);
  96.       if (verbose) fprintf(stderr,"Input text file = %s\n",filenm);
  97.       if ( (ifile = fopen(filenm,"r")) == NULL )
  98.       {
  99.            fprintf(stderr,"Error opening text input file %s\n",
  100.                filenm);
  101.            exit(EXIT_FAILURE);
  102.       }
  103.       optind++;
  104.      }
  105.      
  106.      /*
  107.       * output file not in option form.
  108.       */
  109.      if (optind < argc)
  110.      {
  111.       sscanf(argv[optind],"%s",outfilenm);
  112.       if (verbose) fprintf(stderr,"outfile = %s\n",outfilenm);
  113.       if ( (ofile = fopen(outfilenm,"w")) == NULL )
  114.       {
  115.            fprintf(stderr,"Error opening hippo output file %s\n",
  116.                outfilenm);
  117.            exit(EXIT_FAILURE);
  118.       }
  119.       optind++;
  120.      }
  121.  
  122.      /*
  123.       * handle appending ntuple to existing file.
  124.       */
  125.      if (h_readStream(ifile,&d_list,&nt_list) != 0)
  126.      {
  127.       fprintf(stderr,"Could not read file\n");
  128.       cleanup(nt_list, d_list);
  129.       exit(EXIT_FAILURE);
  130.      }
  131.      
  132.      i = 0;
  133.      while (nt_list[i] != NULL) i++;
  134.      if (i_ntout >= i) 
  135.      {
  136.       fprintf(stderr,"There are only %d ntuple in the input file\n",i);
  137.       fprintf(stderr,"Remember: ntuples are numbered from 0\n");
  138.       cleanup(nt_list, d_list);
  139.       exit(EXIT_FAILURE);
  140.      }
  141.      
  142.      /*
  143.       * all set. Do it!
  144.       */
  145.      h_nt2text( ofile, nt_list[i_ntout] );
  146.  
  147.      cleanup(nt_list, d_list);
  148.      exit(EXIT_SUCCESS);
  149. }
  150.  
  151. static void cleanup(ntuple *nt_list, display *d_list)
  152. {
  153.      int i;
  154.       
  155.      if (nt_list != NULL)
  156.      {
  157.       i = 0;
  158.       while (nt_list[i] != NULL) h_freeNt(nt_list[i++]);
  159.       free(nt_list);
  160.      }
  161.  
  162.      if (d_list != NULL)
  163.      {
  164.       i = 0;
  165.       while (d_list[i] != NULL) h_freeDisp(d_list[i++]);
  166.       free(d_list);
  167.      }
  168. }
  169.  
  170.