HpFile.c 12.4 KB
Newer Older
1
#include "Main.h"
2
3
4
5
6
7
8
9
10
11
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Defines.h"
#include "Error.h"
#include "HpFile.h"
#include "Utilities.h"

#ifndef atof
12
double atof PROTO((const char *));
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
#endif

/* own stuff already included */

#define N_MARKS 50		/* start size of the mark table */
#define N_SAMPLES 500		/* start size of the sample table */

char *theident;
char *thestring;
int theinteger;
floatish thefloatish;
int ch;						/* last character read  */
token thetok; 					/* last token           */
int linenum;					/* current line number  */
int endfile;					/* true at end of file  */

static boolish gotjob = 0;			/* "JOB" read	        */
static boolish gotdate = 0;			/* "DATE" read          */
static boolish gotvalueunit = 0;		/* "VALUE_UNIT" read    */
static boolish gotsampleunit = 0;		/* "SAMPLE_UNIT" read   */
static boolish insample = 0;			/* true when in sample  */

static floatish lastsample;			/* the last sample time */

static void GetHpLine PROTO((FILE *));		/* forward */
static void GetHpTok  PROTO((FILE *));		/* forward */

static struct entry *GetEntry PROTO((char *));	/* forward */

static void MakeIdentTable PROTO((void));	/* forward */

char *jobstring;
char *datestring;

char *sampleunitstring;
char *valueunitstring;

floatish *samplemap;		/* sample intervals	*/
floatish *markmap;		/* sample marks		*/

/*
 *	An extremely simple parser. The input is organised into lines of 
 *	the form
 *
 *      JOB s              -- job identifier string
 *	DATE s		   -- date string 
 *	SAMPLE_UNIT s	   -- sample unit eg "seconds" 
 *	VALUE_UNIT s	   -- value unit eg "bytes" 
 *	MARK i	   	   -- sample mark 
 *	BEGIN_SAMPLE i 	   -- start of ith sample 
 *	identifier i	   -- there are i identifiers in this sample 
 *	END_SAMPLE i   	   -- end of ith sample 
 *
 */

void
69
GetHpFile(FILE *infp)
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
{
    nsamples = 0;
    nmarks   = 0;
    nidents  = 0;

    ch = ' ';
    endfile = 0;
    linenum = 1;
    lastsample = 0.0;

    GetHpTok(infp);

    while (endfile == 0) {
	GetHpLine(infp);
    }

    if (!gotjob) {
	Error("%s: JOB missing", hpfile);
    }

    if (!gotdate) {
	Error("%s: DATE missing", hpfile);
    }

    if (!gotvalueunit) {
	Error("%s: VALUE_UNIT missing", hpfile);
    }

    if (!gotsampleunit) {
	Error("%s: SAMPLE_UNIT missing", hpfile);
    }

    if (nsamples == 0) {
	Error("%s: contains no samples", hpfile);
    }


    MakeIdentTable();

    fclose(hpfp);
}


/*
 *      Read the next line from the input, check the syntax, and perform
 *	the appropriate action.
 */

static void
119
GetHpLine(FILE *infp)
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
{
    static intish nmarkmax = 0, nsamplemax = 0;

    switch (thetok) {
    case JOB_TOK:
	GetHpTok(infp);
	if (thetok != STRING_TOK) {
	    Error("%s, line %d: string must follow JOB", hpfile, linenum);
        }
	jobstring = thestring;
	gotjob = 1;
        GetHpTok(infp);
	break;

    case DATE_TOK:
	GetHpTok(infp);
	if (thetok != STRING_TOK) {
	    Error("%s, line %d: string must follow DATE", hpfile, linenum);
        }
	datestring = thestring;
	gotdate = 1;
        GetHpTok(infp);
	break;

    case SAMPLE_UNIT_TOK:
	GetHpTok(infp);
	if (thetok != STRING_TOK) {
	    Error("%s, line %d: string must follow SAMPLE_UNIT", hpfile, 
	          linenum);
        }
	sampleunitstring = thestring;
	gotsampleunit = 1;
        GetHpTok(infp);
	break;

    case VALUE_UNIT_TOK:
        GetHpTok(infp);
	if (thetok != STRING_TOK) {
	    Error("%s, line %d: string must follow VALUE_UNIT", hpfile, 
	          linenum);
        }
	valueunitstring = thestring;
	gotvalueunit = 1;
        GetHpTok(infp);
	break;

    case MARK_TOK:
	GetHpTok(infp);
        if (thetok != FLOAT_TOK) {
            Error("%s, line %d, floating point number must follow MARK",
	          hpfile, linenum);
        }
	if (insample) {
	    Error("%s, line %d, MARK occurs within sample", hpfile, linenum);
	}
	if (nmarks >= nmarkmax) {
	    if (!markmap) {
		nmarkmax = N_MARKS;
		markmap = (floatish*) xmalloc(nmarkmax * sizeof(floatish));
	    } else {
		nmarkmax *= 2;
		markmap = (floatish*) xrealloc(markmap, nmarkmax * sizeof(floatish));
	    }
	}
	markmap[ nmarks++ ] = thefloatish; 
        GetHpTok(infp);
        break;

    case BEGIN_SAMPLE_TOK: 
	insample = 1;
	GetHpTok(infp); 
	if (thetok != FLOAT_TOK) {
	    Error("%s, line %d, floating point number must follow BEGIN_SAMPLE",	          hpfile, linenum);
	}
	if (thefloatish < lastsample) {
	    Error("%s, line %d, samples out of sequence", hpfile, linenum);
	} else {
	    lastsample = thefloatish;
        }
	if (nsamples >= nsamplemax) {
	    if (!samplemap) {
		nsamplemax = N_SAMPLES;
		samplemap = (floatish*) xmalloc(nsamplemax * sizeof(floatish));
	    } else {
		nsamplemax *= 2;
		samplemap = (floatish*) xrealloc(samplemap, 
	                                      nsamplemax * sizeof(floatish));
	    }
	}
	samplemap[ nsamples ] = thefloatish;
	GetHpTok(infp);
	break;

    case END_SAMPLE_TOK: 
	insample = 0;
	GetHpTok(infp); 
	if (thetok != FLOAT_TOK) {
	    Error("%s, line %d: floating point number must follow END_SAMPLE", 
                  hpfile, linenum);
	}
        nsamples++;
	GetHpTok(infp);
	break;

    case IDENTIFIER_TOK:
	GetHpTok(infp);
	if (thetok != INTEGER_TOK) {
	    Error("%s, line %d: integer must follow identifier", hpfile, 
                  linenum);
	}
        StoreSample(GetEntry(theident), nsamples, (floatish) theinteger);
	GetHpTok(infp); 
        break;

    case EOF_TOK:
        endfile = 1;
	break;

    default:
	Error("%s, line %d: %s unexpected", hpfile, linenum,
	      TokenToString(thetok));
	break;
    }
}


char *
247
TokenToString(token t)
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
{
   switch (t) {
	case EOF_TOK:		return "EOF";
	case INTEGER_TOK:	return "integer";
	case FLOAT_TOK:		return "floating point number";
	case IDENTIFIER_TOK:	return "identifier";
	case STRING_TOK:	return "string";
	case BEGIN_SAMPLE_TOK:  return "BEGIN_SAMPLE";
	case END_SAMPLE_TOK:    return "END_SAMPLE";
	case JOB_TOK:		return "JOB";
	case DATE_TOK:		return "DATE";
	case SAMPLE_UNIT_TOK:   return "SAMPLE_UNIT";
	case VALUE_UNIT_TOK:    return "VALUE_UNIT";
	case MARK_TOK:		return "MARK";

	case X_RANGE_TOK:	return "X_RANGE";
	case Y_RANGE_TOK:	return "Y_RANGE";
	case ORDER_TOK:		return "ORDER";
	case SHADE_TOK:		return "SHADE";
        default:		return "(strange token)";
    }
}

/*
 *	Read the next token from the input and assign its value
 *	to the global variable "thetok". In the case of numbers,
 *	the corresponding value is also assigned to "theinteger"
 *	or "thefloatish" as appropriate; in the case of identifiers 
 *	it is assigned to "theident".
 */

static void
280
GetHpTok(FILE *infp)
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
{

    while (isspace(ch)) {		/* skip whitespace */
	if (ch == '\n') linenum++;
	ch = getc(infp);
    } 

    if (ch == EOF) {
	thetok = EOF_TOK;
	return;
    }

    if (isdigit(ch)) {
	thetok = GetNumber(infp);
	return;
    } else if (ch == '\"') {
	GetString(infp);
	thetok = STRING_TOK;
	return;
    } else if (IsIdChar(ch)) {
	ASSERT(! (isdigit(ch)));	/* ch can't be a digit here */
	GetIdent(infp);
303
	if (!isupper((int)theident[0])) {
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
	    thetok = IDENTIFIER_TOK;
	} else if (strcmp(theident, "BEGIN_SAMPLE") == 0) {
            thetok = BEGIN_SAMPLE_TOK;
	} else if (strcmp(theident, "END_SAMPLE") == 0) {
            thetok = END_SAMPLE_TOK;
	} else if (strcmp(theident, "JOB") == 0) {
	    thetok = JOB_TOK;
	} else if (strcmp(theident, "DATE") == 0) {
	    thetok = DATE_TOK;
	} else if (strcmp(theident, "SAMPLE_UNIT") == 0) {
	    thetok = SAMPLE_UNIT_TOK;
	} else if (strcmp(theident, "VALUE_UNIT") == 0) {
	    thetok = VALUE_UNIT_TOK;
	} else if (strcmp(theident, "MARK") == 0) {
	    thetok = MARK_TOK;
	} else {
            thetok = IDENTIFIER_TOK;
	}
	return;
    } else {
	Error("%s, line %d: strange character (%c)", hpfile, linenum, ch);
    }
}


/*
 *	Read a sequence of digits and convert the result to an integer
 *	or floating point value (assigned to the "theinteger" or 
 *	"thefloatish").
 */

335
static char numberstring[ NUMBER_LENGTH + 1 ];
336
337

token
338
GetNumber(FILE *infp)
339
340
341
342
343
344
345
346
347
348
349
350
351
352
{
    int i;
    int containsdot;
 
    ASSERT(isdigit(ch)); /* we must have a digit to start with */

    containsdot = 0;

    for (i = 0; i < NUMBER_LENGTH && (isdigit(ch) || ch == '.'); i++) {
        numberstring[ i ] = ch;
        containsdot |= (ch == '.'); 
        ch = getc(infp);
    }   
 
353
    ASSERT(i <= NUMBER_LENGTH); /* did not overflow */
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

    numberstring[ i ] = '\0';
 
    if (containsdot) {
        thefloatish = (floatish) atof(numberstring);
	return FLOAT_TOK;
    } else {
	theinteger = atoi(numberstring);
	return INTEGER_TOK;
    }
}

/*
 *	Read a sequence of identifier characters and assign the result 
 *	to the string "theident".
 */

void
372
GetIdent(FILE *infp)
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
{
    unsigned int i;
    char idbuffer[5000];

    for (i = 0; i < (sizeof idbuffer)-1 && IsIdChar(ch); i++) {
	idbuffer[ i ] = ch;
	ch = getc(infp);
    }
    
    idbuffer[ i ] = '\0';

    if (theident)
	free(theident);

    theident = copystring(idbuffer);
}


/*
 *	Read a sequence of characters that make up a string and 
 *	assign the result to "thestring".
 */

void
397
GetString(FILE *infp)
398
399
{
    unsigned int i;
400
401
    char *stringbuffer;
    size_t stringbuffersize;
402
403
404

    ASSERT(ch == '\"');

405
406
407
    stringbuffersize = 5000;
    stringbuffer = xmalloc(stringbuffersize);

408
409
    ch = getc(infp);	/* skip the '\"' that begins the string */

410
411
    i = 0;
    while (ch != '\"') {
412
413
414
415
        if (ch == EOF) {
	        Error("%s, line %d: EOF when expecting \"", hpfile, linenum, ch);
        }
        else if (i == stringbuffersize - 1) {
416
417
418
419
420
            stringbuffersize = 2 * stringbuffersize;
            stringbuffer = xrealloc(stringbuffer, stringbuffersize);
        }
        stringbuffer[ i++ ] = ch;
        ch = getc(infp);
421
422
423
424
425
    }

    stringbuffer[i] = '\0'; 
    thestring = copystring(stringbuffer);

Nicolas Trangez's avatar
Nicolas Trangez committed
426
427
    free(stringbuffer);

428
429
430
431
432
433
    ASSERT(ch == '\"');

    ch = getc(infp);      /* skip the '\"' that terminates the string */
}

boolish
434
IsIdChar(int ch)
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
{
    return (!isspace(ch));
}


/*
 *      The information associated with each identifier is stored
 *	in a linked list of chunks. The table below allows the list
 *	of chunks to be retrieved given an identifier name.
 */

#define N_HASH       	513 

static struct entry* hashtable[ N_HASH ];

static intish
451
Hash(char *s)
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
{
    int r;
 
    for (r = 0; *s; s++) {
        r = r + r + r + *s;
    }

    if (r < 0) r = -r;

    return r % N_HASH;
}

/*
 *      Get space for a new chunk. Initialise it, and return a pointer 
 *	to the new chunk.
 */
 
static struct chunk*
470
MakeChunk(void)
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
{
    struct chunk* ch;
    struct datapoint* d;

    ch = (struct chunk*) xmalloc( sizeof(struct chunk) );
 
    d = (struct datapoint*) xmalloc (sizeof(struct datapoint) * N_CHUNK);

    ch->nd = 0; 
    ch->d = d;
    ch->next = 0;
    return ch;
}


/*
 *      Get space for a new entry. Initialise it, and return a pointer 
 *	to the new entry.
 */
 
struct entry *
492
MakeEntry(char *name)
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
{
    struct entry* e;

    e = (struct entry *) xmalloc(sizeof(struct entry));
    e->chk = MakeChunk();
    e->name = copystring(name); 
    return e;
}

/*
 *	Get the entry associated with "name", creating a new entry if 
 *	necessary.
 */

static struct entry *
508
GetEntry(char *name)
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
{
    intish h;
    struct entry* e;
 
    h = Hash(name);
 
    for (e = hashtable[ h ]; e; e = e->next) {
        if (strcmp(e->name, name) == 0) {
            break;
        }
    }
 
    if (e) {
	return (e); 
    } else {
        nidents++;
        e = MakeEntry(name);
        e->next = hashtable[ h ];
        hashtable[ h ] = e;
        return (e);
    }
}


/*
 *      Store information from a sample. 
 */
 
void
538
StoreSample(struct entry *en, intish bucket, floatish value)
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
{
    struct chunk* chk; 

    for (chk = en->chk; chk->next != 0; chk = chk->next)
	; 

    if (chk->nd < N_CHUNK) {
	chk->d[ chk->nd ].bucket = bucket;
	chk->d[ chk->nd ].value  = value;
	chk->nd += 1;
    } else {
	struct chunk* t;
	t = chk->next = MakeChunk(); 
	t->d[ 0 ].bucket = bucket;
	t->d[ 0 ].value  = value;
	t->nd += 1;
    }
}


struct entry** identtable;

/*
 *	The hash table is useful while reading the input, but it
 *	becomes a liability thereafter. The code below converts 
 *	it to a more easily processed table.
 */

static void
568
MakeIdentTable(void)
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
{
    intish i;
    intish j;
    struct entry* e;

    nidents = 0;
    for (i = 0; i < N_HASH; i++) {
        for (e = hashtable[ i ]; e; e = e->next) {
	    nidents++;
        }
    }

    identtable = (struct entry**) xmalloc(nidents * sizeof(struct entry*));
    j = 0;

    for (i = 0; i < N_HASH; i++) {
        for (e = hashtable[ i ]; e; e = e->next, j++) {
	    identtable[ j ] = e; 
        }
    }
}