26 #include <stdio.h> |
26 #include <stdio.h> |
27 #include <string.h> |
27 #include <string.h> |
28 #include <errno.h> |
28 #include <errno.h> |
29 |
29 |
30 void removeNewline(char *str) { |
30 void removeNewline(char *str) { |
31 for(;*str;str++) { |
31 for(;*str;str++) { |
32 if(*str=='\n' || *str=='\r') { |
32 if(*str=='\n' || *str=='\r') { |
33 *str = 0; |
33 *str = 0; |
34 return; |
34 return; |
35 } |
35 } |
36 } |
36 } |
37 } |
37 } |
38 |
38 |
39 int flib_mapcfg_read(const char *dataDirPath, const char *mapname, flib_mapcfg *out) { |
39 int flib_mapcfg_read(const char *dataDirPath, const char *mapname, flib_mapcfg *out) { |
40 int result = -1; |
40 int result = -1; |
41 if(!log_badargs_if4(dataDirPath==NULL, mapname==NULL, out==NULL, flib_contains_dir_separator(mapname))) { |
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); |
42 char *path = flib_asprintf("%sMaps/%s/map.cfg", dataDirPath, mapname); |
43 if(path) { |
43 if(path) { |
44 FILE *file = fopen(path, "rb"); |
44 FILE *file = fopen(path, "rb"); |
45 if(!log_e_if(!file, "Unable to open map config file %s", path)) { |
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)) { |
46 if(!log_e_if(!fgets(out->theme, sizeof(out->theme), file), "Error reading theme from %s", path)) { |
47 removeNewline(out->theme); |
47 removeNewline(out->theme); |
48 char buf[64]; |
48 char buf[64]; |
49 if(fgets(buf, sizeof(buf), file)) { |
49 if(fgets(buf, sizeof(buf), file)) { |
50 removeNewline(buf); |
50 removeNewline(buf); |
51 errno = 0; |
51 errno = 0; |
52 out->hogLimit = strtol(buf, NULL, 10); |
52 out->hogLimit = strtol(buf, NULL, 10); |
53 result = !log_e_if(errno, "Invalid hoglimit in %s: %i", path, buf); |
53 result = !log_e_if(errno, "Invalid hoglimit in %s: %i", path, buf); |
54 } else { |
54 } else { |
55 result = 0; |
55 result = 0; |
56 } |
56 } |
57 } |
57 } |
58 fclose(file); |
58 fclose(file); |
59 } |
59 } |
60 } |
60 } |
61 free(path); |
61 free(path); |
62 } |
62 } |
63 return result; |
63 return result; |
64 } |
64 } |