-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadconfig.c
More file actions
619 lines (579 loc) · 14.3 KB
/
readconfig.c
File metadata and controls
619 lines (579 loc) · 14.3 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
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
538
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* readconfig.c -- Functions to read configuration information from a file
Copyright (C) 2003 Robin Hogan <[email protected]> */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "readconfig.h"
#include "random.h"
extern FILE * yyin;
extern FILE * yyerr;
extern int yyparse(void);
extern rc_data * parsed_data;
/* Find param in data, returning an element of the rc_data list, or
NULL if not present. Note that the search is case insensitive. */
rc_data *
rc_find(rc_data *data, char *param)
{
if (data->param) {
if (strcasecmp(param, data->param) == 0) {
return data;
}
else if (data->next) {
return rc_find(data->next, param);
}
}
return NULL;
}
/* Add a param-value pair to an existing rc_data structure,
overwriting an existing param with the same name (case
insensitive). Note that param and value are the actual pointers
added to the structure so they should not be freed after calling
this function. Return 1 on success, 0 if memory allocation of the
next element in the list failed. */
static
int
__rc_register(rc_data *data, char *param, char *value)
{
while (data->param) {
if (strcasecmp(param, data->param) == 0) {
if (data->value) {
free(data->value);
}
data->value = value;
return 1;
}
data = data->next;
}
data->param = param;
data->value = value;
data->next = malloc(sizeof(rc_data));
if (!data->next) {
return 0;
}
else {
data->next->param = data->next->value = NULL;
data->next->next = NULL;
return 1;
}
}
/* Read configuration information from file called file_name and
return a pointer to the rc_data structure, or NULL if an error
occurred. If file_name is NULL then an empty rc_data structure is
returned. If err_file is not NULL, errors messages will be written
to err_file. */
rc_data *
rc_read(char *file_name, FILE *err_file)
{
FILE *file;
rc_data *data;
char *memory_error = "Error allocating memory for configuration information\n";
if (!file_name) {
/* Assign an empty data structure */
data = (rc_data *) malloc(sizeof(rc_data));
if (!data) {
if (err_file) {
fprintf(err_file, "%s", memory_error);
}
return NULL;
}
data->param = NULL;
data->value = NULL;
data->next = NULL;
return data;
}
file = fopen(file_name, "r");
if (!file) {
if (err_file) {
fprintf(err_file, "Error opening %s\n", file_name);
}
return NULL;
}
yyin = file;
yyerr = err_file;
if (yyparse() != 0) {
fprintf(err_file, "Error parsing %s\n", file_name);
if (parsed_data != NULL) {
free(parsed_data);
return NULL;
}
}
fclose(file);
data = parsed_data;
return data;
}
/* Free an rc_data structure created with rc_read() - rc_data is a
singly-linked list and its contents are recursively detelted. */
void
rc_clear(rc_data *data)
{
if (data->param) {
free(data->param);
}
if (data->value) {
free(data->value);
}
if (data->next) {
rc_clear(data->next);
}
free(data);
}
/* Add a param-value pair to an existing rc_data structure,
overwriting an existing param with the same name (case
insensitive). Returns 1 on success and 0 on failure. */
int
rc_register(rc_data *data, char *param, char *value)
{
char *newparam = strdup(param);
if (value) {
char *newvalue = strdup(value);
return __rc_register(data, newparam, newvalue);
}
else {
return __rc_register(data, newparam, NULL);
}
}
/* Search the command-line arguments for param=value pairs and -param
arguments and add them to the rc_data structure. */
int
rc_register_args(rc_data *data, int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++) {
/* Find an "=" sign in argument i */
if (argv[i][0] == '-' && argv[i][1]) {
char *param = strdup(argv[i]+1);
if (!param) {
return 0;
}
if (!__rc_register(data, param, NULL)) {
return 0;
}
}
else {
char *c = argv[i];
while (*c != '\0') {
if (*c == '=') {
/* Found one */
int param_length = c-argv[i];
char *value = strdup(c+1);
char *param = malloc(param_length+1);
if (!value || !param) {
return 0;
}
strncpy(param, argv[i], param_length);
param[param_length] = '\0';
if (!__rc_register(data, param, value)) {
return 0;
}
}
c++;
}
}
}
return 1;
}
/* Find the first config file on the command line, returning an index
to the argv array, and 0 if none is found. Basically the first
argument that contains no "=" sign and doesn't start with "-" is
returned. However, after a "--" argument, a filename begining with
"-" could in principle be returned. */
int
rc_get_file(int argc, char **argv)
{
int i;
int ignore_hyphen = 0;
for (i = 1; i < argc; i++) {
/* Find an "=" sign in argument i */
char *c = argv[i];
if (!ignore_hyphen && c[0] == '-') {
if (strcmp(c, "--")) {
ignore_hyphen = 1;
}
continue;
}
while (*c != '\0') {
if (*c == '=') {
break;
}
c++;
}
if (*c != '=') {
return i;
}
}
return 0;
}
/* Print contents of rc_data structure to file. */
void
rc_print(rc_data *data, FILE *file)
{
while (data->param) {
fprintf(file, "%s=", data->param);
if (data->value) {
fprintf(file, "\"%s\"\n", data->value);
}
else {
fprintf(file, "(no value)\n");
}
if (!(data = data->next)) {
break;
}
}
}
/* Return the contents of an rc_data structure as a string. Returns
NULL on failure. Free with rc_free(). */
char *
rc_sprint(rc_data *data)
{
int length = 0;
char *out = NULL;
while (data->param) {
int param_length = strlen(data->param);
out = realloc(out, length+param_length+2);
if (!out) {
return NULL;
}
strcpy(out+length, data->param);
length += (param_length+1);
if (data->value) {
int value_length = strlen(data->value);
out[length-1] = ' ';
out = realloc(out, length+value_length+2);
if (!out) {
return NULL;
}
strcpy(out+length, data->value);
length += (value_length+1);
}
out[length-1] = '\n';
out[length] = '\0';
if (!(data = data->next)) {
break;
}
}
return out;
}
/* Return 1 if param exists in data, 0 otherwise. */
int
rc_exists(rc_data *data, char *param)
{
return (rc_find(data, param) != NULL);
}
/* Interpret the value associated with param as a boolean, returning 1
if true and 0 if false. Note that 0 will be returned if param is
not present, or if it exists and is "0", "no" or "false" (or any
case insensitive variants). Any other scenario will result in 1
being returned */
int
rc_get_boolean(rc_data *data, char *param)
{
data = rc_find(data, param);
if (!data) {
return 0;
}
else if (!data->value) {
return 1;
}
else if (strncasecmp(data->value, "false", 5) == 0
|| strncasecmp(data->value, "no", 2) == 0) {
return 0;
}
else {
char *endptr = data->value;
double val = strtod(data->value, &endptr);
if (data->value == endptr || val != 0.0) {
return 1;
}
else {
return 0;
}
}
}
/* Interpret the value associated with param as an integer and return
it. If successful *status is set to 1, but if either param is not
present or the associated value cannot be interpreted as an
integer, *status is set to 0. */
int
rc_get_int(rc_data *data, char *param, int *status)
{
data = rc_find(data, param);
if (!data || !data->value) {
*status = 0;
return 0;
}
else {
char *endptr;
long val = strtol(data->value, &endptr, 10);
if (data->value == endptr) {
*status = 0;
return 0;
}
else {
*status = 1;
return val;
}
}
}
/* If param exists in data, set *value to the value associated with
param and return 1, otherwise leave *value untouched and return
0. */
int
rc_assign_int(rc_data *data, char *param, int *value)
{
int status;
int val = rc_get_int(data, param, &status);
if (status) {
*value = val;
}
return status;
}
real
rc_get_real(rc_data *data, char *param, int *status)
{
data = rc_find(data, param);
if (!data || !data->value) {
*status = 0;
return 0;
}
else {
char *endptr;
double val = strtod(data->value, &endptr);
if (data->value == endptr) {
*status = 0;
return 0;
}
else {
*status = 1;
return val;
}
}
}
/* As rc_assign_int() but with a real */
int
rc_assign_real(rc_data *data, char *param, real *value)
{
int status;
real val = rc_get_real(data, param, &status);
if (status) {
*value = val;
}
return status;
}
/* If param exists in data and can be interpretted as 1 or more
reals, assign *value to an array of reals, returning the number
found. If fewer than min_length are present, the array will be
padded with the last valid real found up to min_length. In this
case the number returned is the number of valid reals found, not
min_length. If no reals are found or if there is an error
allocating memory, *value is untouched and 0 is returned. */
int
rc_assign_real_array(rc_data *data, char *param,
real **value, int min_length)
{
int length;
real *val = rc_get_real_array(data, param, &length);
if (!val) {
return 0;
}
else if (length < min_length) {
int i;
val = realloc(val, min_length*sizeof(real));
if (!val) {
return 0;
}
for (i = length; i < min_length; i++) {
val[i] = val[length-1];
}
}
*value = val;
return length;
}
/* As rc_assign_real_array() except that if no reals are found then
an array of length "min_length" is returned containing
default_value. This function always returns min_length, except if
there was an error allocating memory in which case 0 is
returned. */
int
rc_assign_real_array_default(rc_data *data, char *param,
real **value, int min_length, real default_value)
{
int length;
real *val = rc_get_real_array(data, param, &length);
if (!val) {
int i;
val = malloc(min_length*sizeof(real));
if (!val) {
return 0;
}
for (i = 0; i < min_length; i++) {
val[i] = default_value;
}
}
else if (length < min_length) {
int i;
val = realloc(val, min_length*sizeof(real));
if (!val) {
return 0;
}
for (i = length; i < min_length; i++) {
val[i] = val[length-1];
}
}
*value = val;
return min_length;
}
/* Return the value associated with param as a string. NULL is
returned on failure or memory allocation error. The string should
be deallocated with rc_free(). */
char *
rc_get_string(rc_data *data, char *param)
{
data = rc_find(data, param);
if (!data || !data->value) {
return NULL;
}
else {
char *value = strdup(data->value);
if (value) {
/* Remove trailing whitespace */
char *ch = value + strlen(value) - 1;
while (*ch <= ' ') {
*ch = '\0';
ch--;
}
}
return value;
}
}
/* Free dynamically allocated data returned by rc_get_string(),
rc_assign_string(), rc_assign_real_array(),
rc_assign_real_array_default(), rc_get_int_array() and
rc_get_real_array(). */
void
rc_free(void *string)
{
if (string)
free(string);
}
/* If param exists in data, set *value to a pointer to a copy of the
string associated with param and return 1, otherwise leave value
untouched and return 0. Memory allocation error also results in 0
being returned. The string should be freed with rc_free(). */
int
rc_assign_string(rc_data *data, char *param, char **value)
{
char *str = rc_get_string(data, param);
if (str) {
if (*str) {
/* A genuine non-empty string */
*value = str;
return 1;
}
else {
/* An empty string */
free(str);
}
}
return 0;
}
/* If param exists, interpret the associated value as integers and
return a pointer to an int array or NULL on failure. *length
contains the number of integers assigned, 0 if none or memory
allocation error. The array should be freed with rc_free(). */
int *
rc_get_int_array(rc_data *data, char *param, int *length)
{
int *out = NULL;
data = rc_find(data, param);
*length = 0;
if (!data || !data->value) {
return NULL;
}
else {
char *endptr;
char *c = data->value;
while (*c) {
long val = strtol(c, &endptr, 10);
if (endptr != c) {
out = realloc(out, (++*length)*sizeof(int));
if (!out) {
*length = 0;
return NULL;
}
out[*length-1] = val;
c = endptr;
}
else {
break;
}
}
return out;
}
}
/* As rc_get_real_array() but with reals. */
real *
rc_get_real_array(rc_data *data, char *param, int *length)
{
real *out = NULL;
data = rc_find(data, param);
*length = 0;
if (!data || !data->value) {
return NULL;
}
else {
char *endptr;
char *c = data->value;
while (*c) {
double val = strtod(c, &endptr);
if (endptr != c) {
out = realloc(out, (++*length)*sizeof(real));
if (!out) {
*length = 0;
return NULL;
}
out[*length-1] = val;
c = endptr;
}
else {
break;
}
}
return out;
}
}
/* Generate the base field */
cg_field *
rc_generate_base_field(rc_data * config) {
/* Determine if we are using an effective size parameter */
char is_size = 0;
is_size = rc_get_boolean(config, "size_variable_name");
/* Create the base field */
real x_domain_size = 200000;
real z_domain_size = 2000;
int x_pixels = 128;
int z_pixels = 32;
real x_offset = 0.0;
real y_offset = 0.0;
real z_offset = 0.0;
rc_assign_real(config, "x_domain_size", &x_domain_size);
rc_assign_real(config, "z_domain_size", &z_domain_size);
rc_assign_int(config, "x_pixels", &x_pixels);
rc_assign_int(config, "z_pixels", &z_pixels);
rc_assign_real(config, "x_offset", &x_offset);
rc_assign_real(config, "y_offset", &y_offset);
rc_assign_real(config, "z_offset", &z_offset);
/* Set the domain parameters - note that ny=nx and dy=dx. */
real dx = x_domain_size/x_pixels;
real dz = z_domain_size/z_pixels;
/* Create cloud field structure */
char verbose = 0;
verbose = rc_get_boolean(config, "verbose");
if (verbose != 0) {
fprintf(stderr, "Creating new field measureing %dx%dx%d pixels\n",
x_pixels, x_pixels, z_pixels);
}
cg_field * field = cg_new_multi_field(x_pixels, x_pixels, z_pixels,
dx, dx, dz, x_offset, y_offset,
z_offset, is_size + 1);
return field;
}