ftp.nice.ch/pub/next/science/mathematics/hippoplotamus.2.0.s.tar.gz#/hippo2.0/htextio.c

This is htextio.c in view mode; [Download] [Up]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "hippo.h"
#include "hutil.h"

GLOB_QUAL const char htextio_c_rcsid[] = 
     "$Id: htextio.c,v 5.0 1993/08/17 21:56:44 rensing Exp $";

static char *parsestr( char **token );

ntuple h_fileParse(FILE* ifile, ntuple oldnt, int verbose)
{
     int i,j;
     int n_tok;
     int rc;
     int nt_dim;
     int titleset = 0;		/* flags and placemarkers */
     int labelsset = 0;
     int append;

     ntuple thisnt = oldnt;	/* to return */

     char *line = NULL;	/* input strings */
     int lineLen = 256;
     char *token;
     char **tokA;
     int n_tokA = 10;

     char *title = NULL;

     char **label;
     float *nt;
     int n_col = 10;

     if (thisnt != NULL) 
     {
	  nt_dim = h_getNtDim(thisnt);
	  append = 1;
     }
     else
     {
	  append = nt_dim = 0;
     }
     
     line = (char *) malloc( lineLen * sizeof(char) );
     
     tokA = (char **) malloc( n_tokA * sizeof(char *) );
     label = (char **) malloc( n_col * sizeof(char *) );
     nt = (float *) malloc( n_col * sizeof(float) );
     
     for (i=0; i<n_col; i++) label[i] = NULL;
     
     while (fgets(line,lineLen, ifile) != NULL)
     {
	  while (line[strlen(line)-1] != '\n')
	  {
	       lineLen *= 2;
	       line = (char *) realloc( line, lineLen * sizeof(char) );
	       if (fgets(&line[strlen(line)],lineLen/2,ifile) == NULL) break;
	  }
	  
	  /*
	   * break lines into tokens by ';'
	   */
	  i = 0;
	  tokA[i] = strtok(line,";");
	  while (tokA[i] != NULL)
	  {
	       /* first, skip white space */
	       while (*tokA[i] != '\0' && isspace(*tokA[i])) tokA[i]++;
	       i++;
	       
	       if (i>=n_tokA)
	       {
		    tokA = (char **)realloc( tokA, (n_tokA+5)*sizeof(char *));
		    n_tokA += 5;
	       }
	       tokA[i] = strtok(NULL,";");

	  }
	  n_tok = i;

	  
	  for (i=0; i<n_tok; i++)
	  {
	       if (sscanf(tokA[i],"%f",&nt[0]) == 1)
	       {
		    /*
		     * this is string of numbers.
		     * read in as many columns as possible.
		     */
		    rc = 1;
		    token = strtok(tokA[i]," ,\t");
		    j = 0;
		    while (token != NULL && rc == 1)
		    {		
			 if (j>=n_col)
			 {
			      n_col += 5;
			      label = (char **)realloc(label, 
						       n_col*sizeof(char *));
			      nt = (float *)realloc(nt,
						    n_col*sizeof(float));
			 }
			 if ((rc = sscanf(token,"%f",&nt[j]))
			     == 1) j++;
			 token = strtok(NULL," ,\t");
		    }
		    if (thisnt == NULL) 
		    {
			 /*
			  * create the ntuple.
			  */
			 nt_dim = j;
			 thisnt = h_new(nt_dim);
			 if (verbose) fprintf(stderr,
					      "ntuple dimension = %d\n",
					      nt_dim);
		    }

		    /*
		     * fill the ntuple.
		     */
		    if (j >= nt_dim)
		    {
			 h_arrayFill(thisnt, nt );
			 if (verbose)
			 {
			      for (j=0;j<nt_dim;j++)
				   fprintf(stderr,"%f ",nt[j]);
			      fprintf(stderr,"\n");
			 }
		    }
	       }
	       else
	       {
		    /* 
		     * character string
		     */
		    if (! titleset)
		    {
			 title = parsestr(&tokA[i]);
			 if (title != NULL) titleset = 1;
			 if (verbose && title != NULL) 
			      fprintf(stderr,"title = %s\n",title);
		    }
		    else if (! labelsset)
		    {
			 j = 0;
			 while (j == 0 || label[j-1] != NULL)
			 {		
			      if (j>=n_col)
			      {
				   n_col += 5;
				   label = (char **)
					realloc(label, 
						n_col*sizeof(char *));
				   nt = (float *)
					realloc(nt,
						n_col*sizeof(float));
			      }
			      label[j] = parsestr(&tokA[i]);
			      if (verbose && label[j] != NULL)
				   fprintf(stderr,"label %d = %s\n",
					   j,label[j]);
			      j++;
			 }
			 if (j > 0) labelsset = 1;
		    }
	       }
	  }
     }

     if (thisnt == NULL)
     {
	  h_error("h_fileParse: file does contain any tuples");
	  return NULL;
     }
     
     /*
      * set the title and labels (must do it late, since ntuple
      *   may not be defined when title/labels are read in).
      */
     if (!append)
     {
	  if (title != NULL) h_setNtTitle( thisnt, title );
	  for (i=0; i<nt_dim && label[i]!=NULL; i++ )
	       h_setNtLabel( thisnt, i, label[i]);
     }
     
     
     return thisnt;
}





static char *parsestr( char **token )
{
     char *s, *string;
     char quote;
     
     while (**token != '\"' && **token != '\'' && **token != '\0') (*token)++;
     
     if (**token == '\0') return NULL;
     
     quote = **token;
     (*token)++;
     string = *token;
     
     while (**token != quote && **token != '\0') (*token)++;
     **token = '\0';
     (*token)++;
     
     s = (char *) malloc( (strlen(string)+1) * sizeof(char) );
     strcpy(s,string);
     
     return s;
}

     

int h_nt2text( FILE *outfile, ntuple nt )
{
     int i,j;
     
     if (nt == NULL || outfile == NULL) return -1;
     
     if (nt->title != NULL) 
	  fprintf(outfile,"\"%s\"\n", nt->title);
     else
	  fprintf(outfile,"\"\"\n");
     
     for (i=0; i<nt->ndim; i++)
	  fprintf(outfile,"\"%s\" ", nt->label[i]);
     fprintf(outfile,"\n");
     
     for (i=0; i<nt->ndata; i++)
     {
	  for (j=0; j<nt->ndim; j++)
	       fprintf(outfile,"%g ",nt->data[j][i] );
	  fprintf(outfile,"\n");
     }
     
     return 0;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.