forked from mapio/gpsr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filecfg.c
74 lines (57 loc) · 2.13 KB
/
filecfg.c
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
/*
GPSR: Genetic Programming for Symbolic Regression
Copyright (C) 2012 Massimo Santini
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; either version 2 of the License, or
(at your option) any later version.
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
*/
#include "config.h"
#include <stdio.h>
#include <search.h>
#define PARAM_MAX 100
#define PARAM_LEN 40
#define PARAM_FMT "%40s = %40s\n"
#include "utils.h"
struct params {
char name[PARAM_LEN], value[PARAM_LEN];
};
static struct params param[PARAM_MAX];
static size_t paramcount;
static int paramcompar(const void *s1, const void *s2)
{
return strncmp( (char *)(((struct params *)s1)->name), (char *)(((struct params *)s2)->name), PARAM_LEN);
}
char *findparam( char *name )
{
struct params *found, t;
found = (struct params *)lfind( name, param, ¶mcount, sizeof(struct params), paramcompar );
/* found = (struct params *)qsearch( name, param, paramcount, sizeof(struct params), paramcompar ); */
if ( found ) {
memcpy( &t, found, sizeof(t) );
memcpy( found, &(param[--paramcount]), sizeof(t) );
memcpy( &(param[paramcount]), &t, sizeof(t) );
return param[paramcount].value;
} else return NULL;
}
void readparams( char *file )
{
FILE *in;
if ((in = fopen(file, "r")) == NULL)
yperror( "can't open config file '%s'", file );
paramcount = 0;
while ( !feof(in) ) {
if ( fscanf( in, PARAM_FMT, param[paramcount].name, param[paramcount].value ) != 2 )
yerror("file format error in config file '%s'\n", file );
paramcount++;
}
/* qsort( param, paramcount, sizeof(struct params), paramcompar ); */
fclose(in);
}