src/lib/common/conftab.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  conftab.c: Load game configuration files
00029  * 
00030  *  Known contributors to this file:
00031  *     Markus Armbruster, 2006
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include <errno.h>
00037 #include <stdio.h>
00038 #include "file.h"
00039 #include "optlist.h"
00040 #include "product.h"
00041 #include "prototypes.h"
00042 
00043 static int read_custom_table_file(char *);
00044 
00045 int
00046 read_builtin_tables(void)
00047 {
00048     struct empfile *ep;
00049     FILE *fp;
00050     int lineno, res;
00051 
00052     /*
00053      * Need to read config files for tables referenced through
00054      * ep->cadef[].ca_table before those for ep.  empfile[] is ordered
00055      * that way.
00056      */
00057     for (ep = empfile; ep->name; ep++) {
00058         if (!EF_IS_GAME_STATE(ep->uid) && ep->file) {
00059             if ((fp = fopen(ep->file, "r")) == NULL) {
00060                 fprintf(stderr, "Can't open %s for reading (%s)\n",
00061                         ep->file, strerror(errno));
00062                 return -1;
00063             }
00064             lineno = 1;
00065             res = xundump(fp, ep->file, &lineno, ep->uid);
00066             if (res >= 0 && getc(fp) != EOF) {
00067                 fprintf(stderr, "%s: Junk after the table\n",
00068                         ep->file);
00069                 res = EF_BAD;
00070             }
00071             fclose(fp);
00072             if (res < 0)
00073                 return -1;
00074         }
00075     }
00076 
00077     return 0;
00078 }
00079 
00080 /*
00081  * Read user configuration tables.
00082  * Return 0 on success, -1 on failure.
00083  */
00084 int
00085 read_custom_tables(void)
00086 {
00087     char *tmp = strdup(custom_tables);
00088     char *fname;
00089     int res = 0;
00090 
00091     for (fname = strtok(tmp, " \t"); fname; fname = strtok(NULL, " \t")) {
00092         if (read_custom_table_file(fname) < 0)
00093             res = -1;
00094     }
00095 
00096     free(tmp);
00097     return res;
00098 }
00099 
00100 /*
00101  * Read configuration table file FNAME.
00102  * Return 0 on success, -1 on error.
00103  */
00104 static int
00105 read_custom_table_file(char *fname)
00106 {
00107     int lineno, res, n;
00108     FILE *fp;
00109 
00110     if (!(fp = fopen(fname, "r"))) {
00111         fprintf(stderr, "Can't open config table %s for reading (%s)\n",
00112                 fname, strerror(errno));
00113         return -1;
00114     }
00115 
00116     lineno = 1;
00117     for (n = 0; (res = xundump(fp, fname, &lineno, EF_BAD)) >= 0; n++)
00118         empfile[res].flags |= EFF_CUSTOM;
00119     if (res != EF_BAD && n == 0)
00120         fprintf(stderr, "Warning: configuration file %s is empty\n", fname);
00121 
00122     fclose(fp);
00123     return -(res == EF_BAD);
00124 }

Generated on Fri Mar 28 11:01:14 2008 for empserver by  doxygen 1.5.2