map load_map(void) { char* dir = getenv("CUAUV_SOFTWARE"); if (dir == nullptr) throw s::runtime_error("cuauv::conf::load_map: CUAUV_SOFTWARE must be set to the root of the software repository with a trailing slash."); char* locale = getenv("CUAUV_LOCALE"); if (locale == nullptr) throw s::runtime_error("cuauv::conf::load_map: CUAUV_LOCALE must be defined corresponding to a config file in the conf directory."); s::string filename = s::string(dir) + "conf/" + s::string(locale) + ".conf"; s::ifstream ifs(filename); if (!ifs) throw s::runtime_error("cuauv::conf::load_map: Locale file does not exist or cannot be opened."); Json::Value root; Json::Reader reader; if (!reader.parse(ifs, root)) throw s::runtime_error("cuauv::conf::load_map: Locale file cannot be parsed."); PARSE_OBJECTS(objects); PARSE_INTEGER(num_particles); map m { objects, num_particles }; return m; };
static int load_highscores(void) { int i; FILE *in; if ((in = fopen(HIGHSCORE_FILE, "r")) == NULL) return 0; for (i = 0; i < NUM_HIGHSCORES; i++) { int score = 0, level = 0, wave = 0; char *name, *p, *end; static char line[256]; if (fgets(line, sizeof line, in) == NULL) panic("error reading highscore file"); name = strtok(line, " "); if (strlen(name) > MAX_NAME_LEN) panic("error reading highscore file"); #define PARSE_INTEGER(n) \ p = strtok(NULL, " "); \ if (p == NULL || ((n = strtol(p, &end, 10)), end == p)) \ panic("error reading highscore file"); PARSE_INTEGER(level) PARSE_INTEGER(wave) PARSE_INTEGER(score) strncpy(highscore_table[i].name, name, sizeof highscore_table[i].name - 1); highscore_table[i].level = level; highscore_table[i].wave = wave; highscore_table[i].score = score; } fclose(in); return 1; }