home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
-
- #include "hippo.h"
- #include "hippoutil.h"
-
- #define LINE_SIZE 513
-
- #define INDEX(rows, columns, totcols) ((rows) * (totcols) + (columns))
-
- 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[LINE_SIZE]; /* input strings */
- 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;
- }
-
- 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,LINE_SIZE, ifile) != NULL)
- {
- /*
- * break lines into tokens by ';'
- */
- i = 0;
- tokA[i] = strtok(line,";");
- while (tokA[i] != NULL)
- {
- /* check that there is really something there. */
- for (j=0; j<strlen(tokA[i]) && !isalnum(tokA[i][j]); j++);
- if (isalnum(tokA[i][j])) 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);
-
- 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[INDEX(i,j,nt->ndim)] );
- fprintf(outfile,"\n");
- }
-
- return 0;
- }
-