4 ** functions for reading the gipf-configfile
7 ** Copyright (C) 1998 Kurt Van den Branden
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "configfile.h"
32 int compconfig (void * data1
, void * data2
);
33 void delconfig (void * data
);
35 int writeconfigfile (char * fname
, listheader
* configlist
)
38 configinfo
* configitem
;
41 cfg
= fopen (fname
, "w");
48 while ((configitem
= (configinfo
*) llitembynr (configlist
, i
)) != NULL
)
50 fprintf (cfg
, "%s = %d\n", configitem
->name
, configitem
->value
);
59 listheader
* readconfigfile (char * fname
)
62 listheader
* configlist
;
67 configinfo
* configitem
;
69 configlist
= (listheader
*) malloc (sizeof (listheader
));
72 /* read configuration file */
73 cfg
= fopen (fname
, "r");
79 while (fgets (buffer
, 200, cfg
) != NULL
)
81 result
= sscanf (buffer
, " %s = %d", tempstr
, &value
);
84 configitem
= (configinfo
*) malloc (sizeof (configinfo
));
85 strcpy (configitem
->name
, tempstr
);
86 configitem
->value
= value
;
88 if (insertll (configlist
, (void *) configitem
, compconfig
) != 0)
89 { /* item already in the list */
101 void clearconfiglist (listheader
* configlist
)
103 emptyll (configlist
, delconfig
);
110 int compconfig (void * data1
, void * data2
)
112 configinfo
* item1
= data1
,
115 return (strcmp (item1
->name
, item2
->name
));
118 void delconfig (void * data
)
120 configinfo
* item
= data
;
129 ** clist: list with config-info from config-file
130 ** name: string to look for
131 ** colour: colour to add to string
132 ** defval: default value if nothing found in the config-file
137 int findconfigvalue (listheader
* clist
, char * name
, char colour
, int defval
)
148 /* search for a specific configuration value for this colour player */
149 sprintf (search_c
.name
, "%s_%c", name
, colour
);
150 if ((config_p
= (configinfo
*)
151 searchll (clist
, (void *) &search_c
, compconfig
)) != NULL
)
153 return (config_p
->value
);
156 /* search for a general configuration value for the parameter */
157 strcpy (search_c
.name
, name
);
158 if ((config_p
= (configinfo
*)
159 searchll (clist
, (void *) &search_c
, compconfig
)) != NULL
)
161 return (config_p
->value
);
164 /* if parameter not found, add it to the list with the default value */
165 configitem
= (configinfo
*) malloc (sizeof (configinfo
));
166 strcpy (configitem
->name
, name
);
167 configitem
->value
= defval
;
169 if (insertll (clist
, (void *) configitem
, compconfig
) != 0)
170 { /* item already in the list */
177 void changeconfigvalue (listheader
* clist
, char * name
, int newval
)
188 /* search for a general configuration value for the parameter */
189 strcpy (search_c
.name
, name
);
190 if ((config_p
= (configinfo
*)
191 searchll (clist
, (void *) &search_c
, compconfig
)) != NULL
)
193 config_p
->value
= newval
;
197 /* if parameter not found, add it to the list with the new value */
198 configitem
= (configinfo
*) malloc (sizeof (configinfo
));
199 strcpy (configitem
->name
, name
);
200 configitem
->value
= newval
;
202 if (insertll (clist
, (void *) configitem
, compconfig
) != 0)
203 { /* item already in the list */