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.
24 #include "configfile.h"
34 int compconfig (void * data1
, void * data2
);
35 void delconfig (void * data
);
37 int writeconfigfile (const char * fname
, listheader
* configlist
)
40 configinfo
* configitem
;
43 cfg
= fopen (fname
, "w");
50 while ((configitem
= (configinfo
*) llitembynr (configlist
, i
)) != NULL
)
52 fprintf (cfg
, "%s = %d\n", configitem
->name
, configitem
->value
);
61 listheader
* readconfigfile (const char * fname
)
64 listheader
* configlist
;
69 configinfo
* configitem
;
71 configlist
= (listheader
*) malloc (sizeof (listheader
));
74 /* read configuration file */
75 cfg
= fopen (fname
, "r");
81 while (fgets (buffer
, 200, cfg
) != NULL
)
83 result
= sscanf (buffer
, " %s = %d", tempstr
, &value
);
86 configitem
= (configinfo
*) malloc (sizeof (configinfo
));
87 strcpy (configitem
->name
, tempstr
);
88 configitem
->value
= value
;
90 if (insertll (configlist
, (void *) configitem
, compconfig
) != 0)
91 { /* item already in the list */
103 void clearconfiglist (listheader
* configlist
)
105 emptyll (configlist
, delconfig
);
112 int compconfig (void * data1
, void * data2
)
114 configinfo
* item1
= data1
,
117 return (strcmp (item1
->name
, item2
->name
));
120 void delconfig (void * data
)
122 configinfo
* item
= data
;
131 ** clist: list with config-info from config-file
132 ** name: string to look for
133 ** colour: colour to add to string
134 ** defval: default value if nothing found in the config-file
139 int findconfigvalue (listheader
* clist
, const char * name
, char colour
, int defval
)
150 /* search for a specific configuration value for this colour player */
151 sprintf (search_c
.name
, "%s_%c", name
, colour
);
152 if ((config_p
= (configinfo
*)
153 searchll (clist
, (void *) &search_c
, compconfig
)) != NULL
)
155 return (config_p
->value
);
158 /* search for a general configuration value for the parameter */
159 strcpy (search_c
.name
, name
);
160 if ((config_p
= (configinfo
*)
161 searchll (clist
, (void *) &search_c
, compconfig
)) != NULL
)
163 return (config_p
->value
);
166 /* if parameter not found, add it to the list with the default value */
167 configitem
= (configinfo
*) malloc (sizeof (configinfo
));
168 strcpy (configitem
->name
, name
);
169 configitem
->value
= defval
;
171 if (insertll (clist
, (void *) configitem
, compconfig
) != 0)
172 { /* item already in the list */
179 void changeconfigvalue (listheader
* clist
, const char * name
, int newval
)
190 /* search for a general configuration value for the parameter */
191 strcpy (search_c
.name
, name
);
192 if ((config_p
= (configinfo
*)
193 searchll (clist
, (void *) &search_c
, compconfig
)) != NULL
)
195 config_p
->value
= newval
;
199 /* if parameter not found, add it to the list with the new value */
200 configitem
= (configinfo
*) malloc (sizeof (configinfo
));
201 strcpy (configitem
->name
, name
);
202 configitem
->value
= newval
;
204 if (insertll (clist
, (void *) configitem
, compconfig
) != 0)
205 { /* item already in the list */