|
1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU General Public License |
|
7 * as published by the Free Software Foundation; either version 2 |
|
8 * of the License, or (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 */ |
|
19 |
|
20 #include "mapcfg.h" |
|
21 |
|
22 #include "../util/util.h" |
|
23 #include "../util/logging.h" |
|
24 |
|
25 #include <stdlib.h> |
|
26 #include <stdio.h> |
|
27 #include <string.h> |
|
28 #include <errno.h> |
|
29 |
|
30 void removeNewline(char *str) { |
|
31 for(;*str;str++) { |
|
32 if(*str=='\n' || *str=='\r') { |
|
33 *str = 0; |
|
34 return; |
|
35 } |
|
36 } |
|
37 } |
|
38 |
|
39 int flib_mapcfg_read(const char *dataDirPath, const char *mapname, flib_mapcfg *out) { |
|
40 int result = -1; |
|
41 if(!log_badargs_if4(dataDirPath==NULL, mapname==NULL, out==NULL, flib_contains_dir_separator(mapname))) { |
|
42 char *path = flib_asprintf("%sMaps/%s/map.cfg", dataDirPath, mapname); |
|
43 if(path) { |
|
44 FILE *file = fopen(path, "rb"); |
|
45 if(!log_e_if(!file, "Unable to open map config file %s", path)) { |
|
46 if(!log_e_if(!fgets(out->theme, sizeof(out->theme), file), "Error reading theme from %s", path)) { |
|
47 removeNewline(out->theme); |
|
48 char buf[64]; |
|
49 if(fgets(buf, sizeof(buf), file)) { |
|
50 removeNewline(buf); |
|
51 errno = 0; |
|
52 out->hogLimit = strtol(buf, NULL, 10); |
|
53 result = !log_e_if(errno, "Invalid hoglimit in %s: %i", path, buf); |
|
54 } else { |
|
55 result = 0; |
|
56 } |
|
57 } |
|
58 fclose(file); |
|
59 } |
|
60 } |
|
61 free(path); |
|
62 } |
|
63 return result; |
|
64 } |