project_files/frontlib/model/mapcfg.c
changeset 7275 15f722e0b96f
child 7314 6171f0bad318
equal deleted inserted replaced
7273:8eed495fd8da 7275:15f722e0b96f
       
     1 #include "mapcfg.h"
       
     2 
       
     3 #include "../util/util.h"
       
     4 #include "../util/logging.h"
       
     5 
       
     6 #include <stdlib.h>
       
     7 #include <stdio.h>
       
     8 #include <string.h>
       
     9 #include <errno.h>
       
    10 
       
    11 void removeNewline(char *str) {
       
    12 	for(;*str;str++) {
       
    13 		if(*str=='\n' || *str=='\r') {
       
    14 			*str = 0;
       
    15 			return;
       
    16 		}
       
    17 	}
       
    18 }
       
    19 
       
    20 int flib_mapcfg_read(const char *dataDirPath, const char *mapname, flib_mapcfg *out) {
       
    21 	int result = -1;
       
    22 	if(!log_badparams_if(!dataDirPath || !mapname || !out)
       
    23 			&& !log_e_if(flib_contains_dir_separator(mapname), "Illegal character in mapname %s", mapname)) {
       
    24 		char *path = flib_asprintf("%sMaps/%s/map.cfg", dataDirPath, mapname);
       
    25 		if(path) {
       
    26 			FILE *file = fopen(path, "rb");
       
    27 			if(!log_e_if(!file, "Unable to open map config file %s", path)) {
       
    28 				if(!log_e_if(!fgets(out->theme, sizeof(out->theme), file), "Error reading theme from %s", path)) {
       
    29 					removeNewline(out->theme);
       
    30 					char buf[64];
       
    31 					if(!log_e_if(!fgets(buf, sizeof(buf), file), "Error reading hoglimit from %s", path)) {
       
    32 						removeNewline(buf);
       
    33 						errno = 0;
       
    34 						out->hogLimit = strtol(buf, NULL, 10);
       
    35 						result = !log_e_if(errno, "Invalid hoglimit in %s: %i", path, buf);
       
    36 					}
       
    37 				}
       
    38 				fclose(file);
       
    39 			}
       
    40 		}
       
    41 		free(path);
       
    42 	}
       
    43 	return result;
       
    44 }