2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * emp_config.c: Allows config file to control server config. from a file
30 * Known contributors to this file:
32 * Steve McClure, 1998-2000
38 * 1. Change other constants - such as Num Countries etc.
39 * Just requires variables to be assigned, then dynamic allocation in
40 * a few places. Some checks needed in the server to check the world
41 * hasn't changed size etc.
42 * 2. Could look at loading in planes, units etc. Should be easy enough.
51 #include <stdlib.h> /* atoi free atol */
57 #include "gen.h" /* parse */
60 static int emp_config_dummy;
62 /* things that can be changed */
63 struct keymatch configkeys[] = {
64 #define EMP_CONFIG_C_OUTPUT
65 #include "econfig-spec.h"
66 #undef EMP_CONFIG_C_OUTPUT
69 static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
72 * read in empire configuration
75 emp_config(char *file)
89 if ((fp = fopen(file, "r")) == NULL) {
90 if (file == dflt_econfig && errno == ENOENT)
92 fprintf(stderr, "Can't open %s for reading (%s)\n",
93 file, strerror(errno));
97 while (fgets(buf, sizeof buf, fp) != NULL) {
99 for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
100 if (!buf[i] || buf[i] == '#')
102 if (parse(buf, av, NULL, scanspace, NULL) < 0) {
103 fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
107 if ((kp = keylookup(av[0], configkeys)) == NULL) {
108 fprintf(stderr, "%s:%d: Unknown config key %s\n",
114 fprintf(stderr, "%s:%d: Config key %s needs a value\n",
120 switch (kp->km_type) {
122 *(int *)kp->km_data = atoi(av[1]);
125 *(float *)kp->km_data = atof(av[1]);
128 *(double *)kp->km_data = atof(av[1]);
131 *(long *)kp->km_data = atol(av[1]);
134 if (kp->km_flags & KM_ALLOC)
135 free(*(char **)kp->km_data);
136 *(char **)kp->km_data = strdup(av[1]);
137 kp->km_flags |= KM_ALLOC;
143 fprintf(stderr, "%s:%d: Junk after value of config key %s\n",
149 WORLD_X &= ~1; /* make even */
154 /* find the key in the table */
155 static struct keymatch *
156 keylookup(register s_char *command, struct keymatch *tbl)
158 register struct keymatch *kp;
160 if (command == 0 || *command == 0)
162 for (kp = tbl; kp->km_key != 0; kp++) {
163 if (strcmp(kp->km_key, command) == 0)
170 print_config(FILE *fp)
174 fprintf(fp, "# Empire Configuration File:\n");
175 for (kp = configkeys; kp->km_key; kp++) {
176 if (kp->km_comment) {
177 if (kp->km_comment[0] != '\n')
179 fprintf(fp, "%s\n", kp->km_comment);
183 switch (kp->km_type) {
185 fprintf(fp, "%s \"%s\"\n", kp->km_key, *(char **)kp->km_data);
188 fprintf(fp, "%s %d\n", kp->km_key, *(int *)kp->km_data);
191 fprintf(fp, "%s %g\n", kp->km_key, *(float *)kp->km_data);
194 fprintf(fp, "%s %g\n", kp->km_key, *(double *)kp->km_data);
197 fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);