project_files/frontlib/model/mapcfg.c
author Medo <smaxein@googlemail.com>
Wed, 27 Jun 2012 18:02:45 +0200
changeset 7275 15f722e0b96f
child 7314 6171f0bad318
permissions -rw-r--r--
frontlib: Getting there :) Added commandline client for testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7275
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "mapcfg.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include "../util/util.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include "../util/logging.h"
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include <stdlib.h>
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <stdio.h>
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <string.h>
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <errno.h>
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
void removeNewline(char *str) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
	for(;*str;str++) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
		if(*str=='\n' || *str=='\r') {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
			*str = 0;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
			return;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
int flib_mapcfg_read(const char *dataDirPath, const char *mapname, flib_mapcfg *out) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
	int result = -1;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	if(!log_badparams_if(!dataDirPath || !mapname || !out)
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
			&& !log_e_if(flib_contains_dir_separator(mapname), "Illegal character in mapname %s", mapname)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
		char *path = flib_asprintf("%sMaps/%s/map.cfg", dataDirPath, mapname);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
		if(path) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
			FILE *file = fopen(path, "rb");
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
			if(!log_e_if(!file, "Unable to open map config file %s", path)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
				if(!log_e_if(!fgets(out->theme, sizeof(out->theme), file), "Error reading theme from %s", path)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
					removeNewline(out->theme);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
					char buf[64];
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
					if(!log_e_if(!fgets(buf, sizeof(buf), file), "Error reading hoglimit from %s", path)) {
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
						removeNewline(buf);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
						errno = 0;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
						out->hogLimit = strtol(buf, NULL, 10);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
						result = !log_e_if(errno, "Invalid hoglimit in %s: %i", path, buf);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
					}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
				}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
				fclose(file);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
			}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
		}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
		free(path);
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	}
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
	return result;
15f722e0b96f frontlib: Getting there :) Added commandline client for testing
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
}