1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/***************************************************************
LanceMan's quickplot --- a fast interactive 2D plotter
Copyright (C) 1998, 1999 Lance Arsenault
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Or look at http://www.gnu.org/copyleft/gpl.html .
**********************************************************************/
/* $Id: read_ascii_file.c,v 1.8 1999/02/27 04:21:59 lance Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "data.h"
#define DEBUG 0
#define DELTA_STR_LENGTH 20 /* must be > 1 */
#define is_it_seperater_char(c) ((c) == ' ' || (c) == '\t' || (c) == ',')
extern void verbose_read_report(Plot *plot, unsigned long num_points,
int num_fields, const char *file);
extern void realloc_plot_data_and_max_min(Plot *plot, int num_fields);
extern int check_data_size(Plot *plot,int num_fields,
unsigned long num_points);
extern void add_data_size(Plot *plot, struct Data **currentdata,
int num_fields,unsigned long *num_points);
static int file_cleanup_return(int return_val, char *linein_str,
struct Data **d, double *readin)
{
free(linein_str);
free(d);
free(readin);
return return_val;
}
/* make the string linein_str bigger if it needs it */
static void check_linein_str_length(int current_line_length,char **linein_str)
{
static int linein_str_length;
if(*linein_str == NULL)
linein_str_length = 0;
if(linein_str_length <= current_line_length)
assert(NULL != (*linein_str = (char *) realloc((void *) *linein_str,
sizeof(char)*(
linein_str_length = current_line_length + DELTA_STR_LENGTH))));
}
static int get_next_line(FILE *fileptr, char **linein_str)
{
int line_length,i;
for(line_length=0;(i = getc(fileptr))!=EOF && i!='\n';line_length++)
{
check_linein_str_length(line_length,linein_str);
(*linein_str)[line_length] = i;
}
check_linein_str_length(line_length,linein_str);
(*linein_str)[line_length] = '\0';
return line_length;
}
static void skip_lines(FILE *fileptr, int num, char *file)
{
int i,j;
for(i=0;i<num;i++)
while( (j =getc(fileptr)) != '\n' && j != EOF);
}
static int count_fields(const char *str)
{
int i,num_fields,j;
num_fields = 0;
for(i = str[j=0]; i != '\0';)
{
/* skip spacer */
while(i != '\0' && is_it_seperater_char(i))
i =str[++j];
if(i != '\0' && !is_it_seperater_char(i))
num_fields++; /* got one */
/* go to next spacer */
while(i != '\0' && !is_it_seperater_char(i))
i =str[++j];
}
return num_fields;
}
/* put in ' ' in place of the first num strings
* that are seperated by seperater_char
* i.e. is_it_seperater_char()=TRUE
*/
static void strip_strings(char *str, int num)
{
int i,j;
for(j=0;is_it_seperater_char(str[j])
&& str[j] != '\0';j++)
str[j] = ' ';
for(i=0;i<num && str[j] != '\0';i++)
{
while(!is_it_seperater_char(str[j])
&& str[j] != '\0')
str[j++] = ' ';
while(is_it_seperater_char(str[j])
&& str[j] != '\0')
str[j++] = ' ';
}
}
/* returns number of doubles read in = num_fields on success
* or -1 no more data in file
* or -2 for bad data line error and set linein_str
*
* x is x[num_fields] is filled with the data in the current line
* from the file input in the stream of fileptr
*/
static int readin_data_line(FILE *fileptr, char *data_line_in,
double *x,int num_fields)
{
int i,j=1;
if(count_fields(data_line_in) == 0 && feof(fileptr))
return -1; /* no more data in file */
for(i=0;i<num_fields && 1 == (j= sscanf(data_line_in,"%lf",&x[i]));i++)
strip_strings(data_line_in,1);
if(j != 1)
return -2; /* bad data line error in string data_line_in */
return num_fields; /* success */
}
int read_ascii_file(FILE *fileptr, char *file, Plot *plot)
{
int i,j,k,num_fields,n;
unsigned long num_points /* number of points read in so far */
,num_Data; /* number of elements in the Data array plot->data[] */
double *readin;
struct Data **d;
char *line_in_str;
readin = NULL;
d = NULL;
line_in_str = NULL;
/* skip lines if option is set */
/*******************************/
skip_lines(fileptr,plot->skip_lines,file);
if((plot->flag & VERBOSE) && plot->skip_lines > 0)
fprintf(stderr,
"quickplot Info: skipped the first %d line(s) in reading in file: %s\n",
plot->skip_lines,file);
if(plot->flag & READ_LABELS)
{
/* fprintf(stderr,"file: %s\n",file); */
if(!(plot->flag & LABELS))/* no labels on the command line */
{
int i;
get_next_line(fileptr,&line_in_str);
if(plot->labels == NULL)
{
assert(NULL != (plot->labels = (char *) malloc((strlen(line_in_str) +1)
*sizeof(char))));
plot->labels[0] = '\0';
}
else
{
int j;
assert(NULL != (plot->labels = (char *)
realloc((void *) plot->labels ,
((j=strlen(plot->labels))+strlen(line_in_str)+2)
*sizeof(char))));
plot->labels[j++] = plot->label_seperator;
plot->labels[j] = '\0';
}
strcpy(&(plot->labels[i = strlen(plot->labels)]),line_in_str);
if(plot->flag & VERBOSE)
fprintf(stderr,
"quickplot Info: read in labels <%s> from file: %s\n",
line_in_str,file);
}
else
get_next_line(fileptr,&line_in_str);/* skip the label line */
}
/* count the number of data fields in the file */
/***********************************************/
get_next_line(fileptr,&line_in_str);
if((num_fields = count_fields(line_in_str)) < 1)
{
fprintf(stderr,
"quickplot ERROR: can't read any data from line number %d (shown below) from file: %s\nIs it binary data?\n",
plot->skip_lines+1,file);
return file_cleanup_return(1, line_in_str,d,readin);
}
#if(DEBUG)
printf("the first line read in %s in file %s has %d fields\n",
line_in_str,file,num_fields);
#endif
plot->dim += num_fields;
realloc_plot_data_and_max_min(plot,num_fields);
/* need array of Data pointers to move through data with */
assert((d = (struct Data **)malloc(sizeof(struct Data *)*num_fields)));
/* malloc dummy readin for reading in data */
assert(NULL !=(readin = (double *) malloc(sizeof(double)*num_fields)));
/* read in data and allocate data struct as we go */
num_Data = 0;/* size of Data array */
add_data_size(plot,d,num_fields,&num_Data);
/* get plot->min and plot->max initialed to the first value */
/************************************************************/
if((i = readin_data_line(fileptr,line_in_str,readin,num_fields)) == num_fields)
for(j=plot->dim-num_fields;j<plot->dim;j++)
plot->min[j] = plot->max[j] = readin[j + num_fields - plot->dim];/* ERROR HERE */
else
{
fprintf(stderr,
"quickplot ERROR: can't read any data from line number %d from file: %s\nIs it binary data?\n",
plot->skip_lines+1,file);
return file_cleanup_return(1, line_in_str,d,readin);
}
/** read in data **/
n = plot->dim - num_fields;
num_points = 0;
while(i == num_fields)
{
if(d[0] == NULL)
add_data_size(plot,d,num_fields,&num_Data);
for(j=0;j<num_fields;j++)
{
k = j+n;
d[j]->val = readin[j];
if(readin[j] > plot->max[k])
plot->max[k] = readin[j];
if(readin[j] < plot->min[k])
plot->min[k] = readin[j];
d[j] = d[j]->next;
}
num_points++;
get_next_line(fileptr,&line_in_str);
i = readin_data_line(fileptr,line_in_str,readin,num_fields);
}
if(i == -2)/* error in readin_data_line */
{
fprintf(stderr,
"quickplot ERROR: Error reading data in line number %ld in file: %s\n",
num_points+plot->skip_lines+1,file);
return file_cleanup_return(1, line_in_str,d,readin);
}
if(check_data_size(plot,num_fields,num_points))
return 1; /* error */
assert((plot->num_points = ((unsigned long *) realloc(
(void *) plot->num_points,
sizeof(unsigned long)*(plot->dim)))) != NULL);
for(i=plot->dim-num_fields;i<plot->dim;i++)
plot->num_points[i] = num_points;
verbose_read_report(plot,num_points,num_fields,file);
#if(0)
for(i=0;i<num_fields;i++)
d[i] = plot->data[plot->dim-num_fields +i];
while(d[0]!=NULL)
{
for(i=0;i<num_fields;i++)
{
printf("%g @%d ",d[i]->val,d[i]);
d[i] = d[i]->next;
printf("next %d ",d[i]);
}
printf("\n");
}
#endif
/* free memory and return */
return file_cleanup_return(0, line_in_str,d,readin);
}
|