File: read.c

package info (click to toggle)
xgraph 12.1-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 816 kB
  • ctags: 996
  • sloc: ansic: 7,994; sh: 2,971; makefile: 90
file content (361 lines) | stat: -rw-r--r-- 8,166 bytes parent folder | download | duplicates (5)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* $Header: /usr/src/mash/repository/vint/xgraph/read.c,v 1.2 1999/12/03 23:17:45 heideman Exp $ */
/*
 * read.c: Dataset read code
 *
 * Routines:
 *	int ReadData();
 *
 * $Log: read.c,v $
 * Revision 1.2  1999/12/03 23:17:45  heideman
 * apply xgraph_no_animation.patch
 *
 * Revision 1.1.1.1  1999/12/03 23:15:53  heideman
 * xgraph-12.0
 *
 */
#ifndef lint
static char rcsid[] = "$Id: read.c,v 1.2 1999/12/03 23:17:45 heideman Exp $";
#endif

#include "copyright.h"
#include <stdio.h>
#include <math.h>
#include <pwd.h>
#include <ctype.h>
#include "xgraph.h"
#include "xtb.h"
#include "hard_devices.h"
#include "params.h"
/*
 * New dataset reading code
 */

static int setNumber = 0;
static PointList **curSpot = (PointList **) 0;
static PointList *curList = (PointList *) 0;
static int newGroup = 0;
static int redundant_set = 0;

#ifdef DO_DER
extern void Der1();
#endif

static int
rdSet(fn)
char   *fn;			/* Reading from file `fn' */

/*
 * Set up new dataset.  Will return zero if there are too many data sets.
 */
{
    char    setname[100];

    if (!redundant_set) {
	if (setNumber < MAXSETS) {
	    (void) sprintf(setname, "Set %d", setNumber);
	    if ((strcmp(PlotData[setNumber].setName, setname) == 0) && fn) {
		PlotData[setNumber].setName = fn;
	    }
	    curSpot = &(PlotData[setNumber].list);
	    PlotData[setNumber].list = (PointList *) 0;
	    newGroup = 1;
	    setNumber++;
	    redundant_set = 1;
	    return 1;
	}
	else {
	    return 0;
	}
    }
    else {
	return 1;
    }
}

static void
rdSetName(name)
char   *name;			/* New set name */

/*
 * Sets the name of a data set.  Automatically makes a copy.
 */
{
    PlotData[setNumber - 1].setName = STRDUP(name);
}

static void
rdGroup()
/*
 * Set up for reading new group of points within a dataset.
 */
{
    newGroup = 1;
}

static void
rdPoint(xval, yval)
double  xval,
        yval;			/* New point         */

/*
 * Adds a new point to the current group of the current
 * data set.
 */
{
    if (newGroup) {
	*curSpot = (PointList *) Malloc(sizeof(PointList));
	curList = *curSpot;
	curSpot = &(curList->next);
	curList->numPoints = 0;
	curList->allocSize = INITSIZE;
	curList->xvec = (double *) Malloc((unsigned)
					  (INITSIZE * sizeof(double)));
	curList->yvec = (double *) Malloc((unsigned)
					  (INITSIZE * sizeof(double)));
	curList->next = (PointList *) 0;
	newGroup = 0;
    }
    if (curList->numPoints >= curList->allocSize) {
	curList->allocSize *= 2;
	curList->xvec = (double *) Realloc((char *) curList->xvec,
					   (unsigned) (curList->allocSize *
						       sizeof(double)));
	curList->yvec = (double *) Realloc((char *) curList->yvec,
					   (unsigned) (curList->allocSize *
						       sizeof(double)));
    }

    curList->xvec[curList->numPoints] = xval;
    curList->yvec[curList->numPoints] = yval;

    (curList->numPoints)++;
    redundant_set = 0;
}

static int
rdFindMax()
/*
 * Returns the maximum number of items in any one group of any
 * data set.
 */
{
    int     i;
    PointList *list;
    int     max = -1;

    for (i = 0; i < setNumber; i++) {
	for (list = PlotData[i].list; list; list = list->next) {
	    if (list->numPoints > max)
		max = list->numPoints;
	}
    }
    return max;
}


typedef enum line_type {
    EMPTY, COMMENT, SETNAME, DRAWPNT, MOVEPNT, SETPARAM, ERROR
}       LineType;

typedef struct point_defn {
    double  xval,
            yval;
}       Point;

typedef struct parmval_defn {
    char   *name,
           *value;
}       ParmVals;

typedef struct line_info {
    LineType type;
    union val_defn {
	char   *str;		/* SETNAME, ERROR   */
	Point   pnt;		/* DRAWPNT, MOVEPNT */
	ParmVals parm;		/* SETPARAM         */
    }       val;
}       LineInfo;

static  LineType
parse_line(line, result)
char   *line;			/* Line to parse   */
LineInfo *result;		/* Returned result */

/*
 * Parses `line' into one of the types given in the definition
 * of LineInfo.  The appropriate values are filled into `result'.
 * Below are the current formats for each type:
 *   EMPTY:	All white space
 *   COMMENT:	Starts with "#"
 *   SETNAME:	A name enclosed in double quotes
 *   DRAWPNT:	Two numbers optionally preceded by keyword "draw"
 *   MOVEPNT:	Two numbers preceded by keyword "move"
 *   SETPARAM:  Two non-null strings separated by ":"
 *   ERROR:	Not any of the above (an error message is returned)
 * Note that often the values are pointers into the line itself
 * and should be copied if they are to be used over a long period.
 */
{
    char   *first;

    /* Find first non-space character */
    while (*line && isspace(*line))
	line++;
    if (*line) {
	if (*line == '#') {
	    /* comment */
	    result->type = COMMENT;
	}
	else if (*line == '"') {
	    /* setname */
	    result->type = SETNAME;
	    line++;
	    result->val.str = line;
	    while (*line && (*line != '\n') && (*line != '"'))
		line++;
	    if (*line)
		*line = '\0';
	}
	else {
	    first = line;
	    while (*line && !isspace(*line))
		line++;
	    if (*line) {
		*line = '\0';
		if (stricmp(first, "move") == 0) {
		    /* MOVEPNT */
		    if (sscanf(line + 1, "%lf %lf",
			       &result->val.pnt.xval,
			       &result->val.pnt.yval) == 2) {
			result->type = MOVEPNT;
		    }
		    else {
			result->type = ERROR;
			result->val.str = "Cannot read move coordinates";
		    }
		}
		else if (stricmp(first, "draw") == 0) {
		    /* DRAWPNT */
		    if (sscanf(line + 1, "%lf %lf",
			       &result->val.pnt.xval,
			       &result->val.pnt.yval) == 2) {
			result->type = DRAWPNT;
		    }
		    else {
			result->type = ERROR;
			result->val.str = "Cannot read draw coordinates";
		    }
		}
		else if (first[strlen(first) - 1] == ':') {
		    /* SETPARAM */
		    first[strlen(first) - 1] = '\0';
		    result->val.parm.name = first;
		    line++;
		    while (*line && isspace(*line))
			line++;
		    /* may be a \n at end of it */
		    if (line[strlen(line) - 1] == '\n') {
			line[strlen(line) - 1] = '\0';
		    }
		    result->val.parm.value = line;
		    result->type = SETPARAM;
		}
		else if (sscanf(first, "%lf", &result->val.pnt.xval) == 1) {
		    /* DRAWPNT */
		    if (sscanf(line + 1, "%lf", &result->val.pnt.yval) == 1) {
			result->type = DRAWPNT;
		    }
		    else {
			result->type = ERROR;
			result->val.str = "Cannot read second coordinate";
		    }
		}
		else {
		    /* ERROR */
		    result->type = ERROR;
		    result->val.str = "Unknown line type";
		}
	    }
	    else {
		/* ERROR */
		result->type = ERROR;
		result->val.str = "Premature end of line";
	    }
	}
    }
    else {
	/* empty */
	result->type = EMPTY;
    }
    return result->type;
}


int
ReadData(stream, filename)
FILE   *stream;
char   *filename;

/*
 * Reads in the data sets from the supplied stream.  If the format
 * is correct,  it returns the current maximum number of points across
 * all data sets.  If there is an error,  it returns -1.
 */
{
    char    buffer[MAXBUFSIZE];
    LineInfo info;
    int     line_count = 0;
    int     errors = 0;

    if (!rdSet(filename)) {
	(void) fprintf(stderr, "Error in file `%s' at line %d:\n  %s\n",
		       filename, line_count,
		       "Too many data sets - extra data ignored");
	return -1;
    }
    while (fgets(buffer, MAXBUFSIZE, stream)) {
	line_count++;
	switch (parse_line(buffer, &info)) {
	case EMPTY:
	    if (!rdSet(filename)) {
		(void) fprintf(stderr, "Error in file `%s' at line %d:\n  %s\n",
			       filename, line_count,
			       "Too many data sets - extra data ignored");
		return -1;
	    }
	    break;
	case COMMENT:
	    /* nothing */
	    break;
	case SETNAME:
	    rdSetName(info.val.str);
	    break;
	case DRAWPNT:
	    rdPoint(info.val.pnt.xval, info.val.pnt.yval);
	    break;
	case MOVEPNT:
	    rdGroup();
	    rdPoint(info.val.pnt.xval, info.val.pnt.yval);
	    break;
	case SETPARAM:
	    param_reset(info.val.parm.name, info.val.parm.value);
	    break;
	default:
	    if (filename) {
		(void) fprintf(stderr, "Error in file `%s' at line %d:\n  %s\n",
			       filename, line_count, info.val.str);
		errors++;
	    }
	    break;
	}
    }
#ifdef DO_DER
    Der1();
#endif
    if (errors)
	return -1;
    else
	return rdFindMax();
}