(Temporarily) set "animate" to "none" by default (broken feature).
[gf1.git] / configfile.c
blob6b2c92d397d5880587ea227f136abddfa8fe258f
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 "configfile.h"
26 #include <stdio.h>
27 #include <string.h>
29 typedef struct {
30 char name[40];
31 int value;
32 } configinfo;
34 int compconfig (void * data1, void * data2);
35 void delconfig (void * data);
37 int writeconfigfile (const char * fname, listheader * configlist)
39 FILE * cfg;
40 configinfo * configitem;
41 int i;
43 cfg = fopen (fname, "w");
44 if (cfg == NULL)
46 return (-1);
49 i = 1;
50 while ((configitem = (configinfo *) llitembynr (configlist, i)) != NULL)
52 fprintf (cfg, "%s = %d\n", configitem->name, configitem->value);
53 i++;
56 fclose (cfg);
58 return (0);
61 listheader * readconfigfile (const char * fname)
63 FILE * cfg;
64 listheader * configlist;
65 char buffer[200],
66 tempstr[100];
67 int result,
68 value;
69 configinfo * configitem;
71 configlist = (listheader *) malloc (sizeof (listheader));
72 newlist (configlist);
74 /* read configuration file */
75 cfg = fopen (fname, "r");
76 if (cfg == NULL)
78 return (configlist);
81 while (fgets (buffer, 200, cfg) != NULL)
83 result = sscanf (buffer, " %s = %d", tempstr, &value);
84 if (result == 2)
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 */
92 free (configitem);
97 fclose (cfg);
99 return (configlist);
103 void clearconfiglist (listheader * configlist)
105 emptyll (configlist, delconfig);
106 free (configlist);
108 return;
112 int compconfig (void * data1, void * data2)
114 configinfo * item1 = data1,
115 * item2 = data2;
117 return (strcmp (item1->name, item2->name));
120 void delconfig (void * data)
122 configinfo * item = data;
124 free (item);
126 return;
130 ** parameters:
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
136 ** returns:
137 ** value
139 int findconfigvalue (listheader * clist, const char * name, char colour, int defval)
141 configinfo search_c,
142 * config_p,
143 * configitem;
145 if (clist == NULL)
147 return (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 */
173 free (configitem);
176 return (defval);
179 void changeconfigvalue (listheader * clist, const char * name, int newval)
181 configinfo search_c,
182 * config_p,
183 * configitem;
185 if (clist == NULL)
187 return;
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;
196 return;
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 */
206 free (configitem);
209 return;