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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #include "hippo.h"
  7. #include "hippoutil.h"
  8.  
  9. #define LINE_SIZE 513
  10.  
  11. #define INDEX(rows, columns, totcols) ((rows) * (totcols) + (columns))
  12.  
  13. static char *parsestr( char **token );
  14.  
  15. ntuple h_fileParse(FILE* ifile, ntuple oldnt, int verbose)
  16. {
  17.      int i,j;
  18.      int n_tok;
  19.      int rc;
  20.      int nt_dim;
  21.      int titleset = 0;        /* flags and placemarkers */
  22.      int labelsset = 0;
  23.      int append;
  24.  
  25.      ntuple thisnt = oldnt;    /* to return */
  26.  
  27.      char line[LINE_SIZE];    /* input strings */
  28.      char *token;
  29.      char **tokA;
  30.      int n_tokA = 10;
  31.  
  32.      char *title = NULL;
  33.  
  34.      char **label;
  35.      float *nt;
  36.      int n_col = 10;
  37.  
  38.      if (thisnt != NULL) 
  39.      {
  40.       nt_dim = h_getNtDim(thisnt);
  41.       append = 1;
  42.      }
  43.      else
  44.      {
  45.       append = nt_dim = 0;
  46.      }
  47.      
  48.      tokA = (char **) malloc( n_tokA * sizeof(char *) );
  49.      label = (char **) malloc( n_col * sizeof(char *) );
  50.      nt = (float *) malloc( n_col * sizeof(float) );
  51.      
  52.      for (i=0; i<n_col; i++) label[i] = NULL;
  53.      
  54.      while (fgets(line,LINE_SIZE, ifile) != NULL)
  55.      {
  56.       /*
  57.        * break lines into tokens by ';'
  58.        */
  59.       i = 0;
  60.       tokA[i] = strtok(line,";");
  61.       while (tokA[i] != NULL)
  62.       {
  63.            /* check that there is really something there. */
  64.            for (j=0; j<strlen(tokA[i]) && !isalnum(tokA[i][j]); j++);
  65.            if (isalnum(tokA[i][j])) i++;
  66.  
  67.            if (i>=n_tokA)
  68.            {
  69.             tokA = (char **)realloc( tokA, (n_tokA+5)*sizeof(char *));
  70.             n_tokA += 5;
  71.            }
  72.            tokA[i] = strtok(NULL,";");
  73.  
  74.       }
  75.       n_tok = i;
  76.  
  77.       
  78.       for (i=0; i<n_tok; i++)
  79.       {
  80.            if (sscanf(tokA[i],"%f",&nt[0]) == 1)
  81.            {
  82.             /*
  83.              * this is string of numbers.
  84.              * read in as many columns as possible.
  85.              */
  86.             rc = 1;
  87.             token = strtok(tokA[i]," ,\t");
  88.             j = 0;
  89.             while (token != NULL && rc == 1)
  90.             {        
  91.              if (j>=n_col)
  92.              {
  93.                   n_col += 5;
  94.                   label = (char **)realloc(label, 
  95.                                n_col*sizeof(char *));
  96.                   nt = (float *)realloc(nt,
  97.                             n_col*sizeof(float));
  98.              }
  99.              if ((rc = sscanf(token,"%f",&nt[j]))
  100.                  == 1) j++;
  101.              token = strtok(NULL," ,\t");
  102.             }
  103.             if (thisnt == NULL) 
  104.             {
  105.              /*
  106.               * create the ntuple.
  107.               */
  108.              nt_dim = j;
  109.              thisnt = h_new(nt_dim);
  110.              if (verbose) fprintf(stderr,
  111.                           "ntuple dimension = %d\n",
  112.                           nt_dim);
  113.             }
  114.  
  115.             /*
  116.              * fill the ntuple.
  117.              */
  118.             if (j >= nt_dim)
  119.             {
  120.              h_arrayFill(thisnt, nt );
  121.              if (verbose)
  122.              {
  123.                   for (j=0;j<nt_dim;j++)
  124.                    fprintf(stderr,"%f ",nt[j]);
  125.                   fprintf(stderr,"\n");
  126.              }
  127.             }
  128.            }
  129.            else
  130.            {
  131.             /* 
  132.              * character string
  133.              */
  134.             if (! titleset)
  135.             {
  136.              title = parsestr(&tokA[i]);
  137.              if (title != NULL) titleset = 1;
  138.              if (verbose && title != NULL) 
  139.                   fprintf(stderr,"title = %s\n",title);
  140.             }
  141.             else if (! labelsset)
  142.             {
  143.              j = 0;
  144.              while (j == 0 || label[j-1] != NULL)
  145.              {        
  146.                   if (j>=n_col)
  147.                   {
  148.                    n_col += 5;
  149.                    label = (char **)
  150.                     realloc(label, 
  151.                         n_col*sizeof(char *));
  152.                    nt = (float *)
  153.                     realloc(nt,
  154.                         n_col*sizeof(float));
  155.                   }
  156.                   label[j] = parsestr(&tokA[i]);
  157.                   if (verbose && label[j] != NULL)
  158.                    fprintf(stderr,"label %d = %s\n",
  159.                        j,label[j]);
  160.                   j++;
  161.              }
  162.              if (j > 0) labelsset = 1;
  163.             }
  164.            }
  165.       }
  166.      }
  167.  
  168.      if (thisnt == NULL)
  169.      {
  170.       h_error("h_fileParse: file does contain any tuples");
  171.       return NULL;
  172.      }
  173.      
  174.      /*
  175.       * set the title and labels (must do it late, since ntuple
  176.       *   may not be defined when title/labels are read in).
  177.       */
  178.      if (!append)
  179.      {
  180.       if (title != NULL) h_setNtTitle( thisnt, title );
  181.       for (i=0; i<nt_dim && label[i]!=NULL; i++ )
  182.            h_setNtLabel( thisnt, i, label[i]);
  183.      }
  184.      
  185.      
  186.      return thisnt;
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193. static char *parsestr( char **token )
  194. {
  195.      char *s, *string;
  196.      char quote;
  197.      
  198.      while (**token != '\"' && **token != '\'' && **token != '\0') (*token)++;
  199.      
  200.      if (**token == '\0') return NULL;
  201.      
  202.      quote = **token;
  203.      (*token)++;
  204.      string = *token;
  205.      
  206.      while (**token != quote && **token != '\0') (*token)++;
  207.      **token = '\0';
  208.      (*token)++;
  209.      
  210.      s = (char *) malloc( (strlen(string)+1) * sizeof(char) );
  211.      strcpy(s,string);
  212.      
  213.      return s;
  214. }
  215.  
  216.      
  217.  
  218. int h_nt2text( FILE *outfile, ntuple nt )
  219. {
  220.      int i,j;
  221.      
  222.      if (nt == NULL || outfile == NULL) return -1;
  223.      
  224.      if (nt->title != NULL) 
  225.       fprintf(outfile,"\"%s\"\n", nt->title);
  226.      
  227.      for (i=0; i<nt->ndim; i++)
  228.       fprintf(outfile,"\"%s\" ",nt->label[i]);
  229.      fprintf(outfile,"\n");
  230.      
  231.      for (i=0; i<nt->ndata; i++)
  232.      {
  233.       for (j=0; j<nt->ndim; j++)
  234.            fprintf(outfile,"%g ",nt->data[INDEX(i,j,nt->ndim)] );
  235.       fprintf(outfile,"\n");
  236.      }
  237.      
  238.      return 0;
  239. }
  240.