Support for g++-4.1.
[gf1.git] / configfile.c
blob8ad50799d2de7506dcad656303e59e6b18eee060
1 /*
2 ** $Id$
3 **
4 ** functions for reading the gipf-configfile
5 */
6 /*
7 ** Copyright (C) 1998 Kurt Van den Branden
8 **
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.
13 **
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.
18 **
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 <stdio.h>
25 #include "configfile.h"
27 typedef struct {
28 char name[40];
29 int value;
30 } configinfo;
32 int compconfig (void * data1, void * data2);
33 void delconfig (void * data);
35 int writeconfigfile (char * fname, listheader * configlist)
37 FILE * cfg;
38 configinfo * configitem;
39 int i;
41 cfg = fopen (fname, "w");
42 if (cfg == NULL)
44 return (-1);
47 i = 1;
48 while ((configitem = (configinfo *) llitembynr (configlist, i)) != NULL)
50 fprintf (cfg, "%s = %d\n", configitem->name, configitem->value);
51 i++;
54 fclose (cfg);
56 return (0);
59 listheader * readconfigfile (char * fname)
61 FILE * cfg;
62 listheader * configlist;
63 char buffer[200],
64 tempstr[100];
65 int result,
66 value;
67 configinfo * configitem;
69 configlist = (listheader *) malloc (sizeof (listheader));
70 newlist (configlist);
72 /* read configuration file */
73 cfg = fopen (fname, "r");
74 if (cfg == NULL)
76 return (configlist);
79 while (fgets (buffer, 200, cfg) != NULL)
81 result = sscanf (buffer, " %s = %d", tempstr, &value);
82 if (result == 2)
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 */
90 free (configitem);
95 fclose (cfg);
97 return (configlist);
101 void clearconfiglist (listheader * configlist)
103 emptyll (configlist, delconfig);
104 free (configlist);
106 return;
110 int compconfig (void * data1, void * data2)
112 configinfo * item1 = data1,
113 * item2 = data2;
115 return (strcmp (item1->name, item2->name));
118 void delconfig (void * data)
120 configinfo * item = data;
122 free (item);
124 return;
128 ** parameters:
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
134 ** returns:
135 ** value
137 int findconfigvalue (listheader * clist, char * name, char colour, int defval)
139 configinfo search_c,
140 * config_p,
141 * configitem;
143 if (clist == NULL)
145 return (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 */
171 free (configitem);
174 return (defval);
177 void changeconfigvalue (listheader * clist, char * name, int newval)
179 configinfo search_c,
180 * config_p,
181 * configitem;
183 if (clist == NULL)
185 return;
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;
194 return;
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 */
204 free (configitem);
207 return;