Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121// App-wide OPTIONS persistence KAT (io/options_ini.hpp) — the flat-file equivalent of the original's// original's registry OPTIONS section (SOUND/MUSIC/SPEED, int-encoded 1=on/0=off,// SPEED = wSimulationSpeed−1). The point of the store: rewriting one key must NOT drop the others (the// earlier single-key SPEED writer clobbered the whole file), and hand-added keys (e.g. AUTOSAVE) survive.#include "sc2k/io/options_ini.hpp"#include <cstdio>#include <fstream>#include <string>
#include "test_harness.hpp"
static std::string slurp(const std::string& p) { std::ifstream f(p); return std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());}
int main() { using namespace sc2k::io; const std::string path = "/tmp/sc2k_options_ini_test.ini"; std::remove(path.c_str());
// 1) Missing file → every get() returns its default (no throw, no crash). { OptionsIni o; o.load(path); CHECK(o.get("SOUND", 1) == 1); CHECK(o.get("MUSIC", 1) == 1); CHECK(o.get("SPEED", 0) == 0); CHECK(o.get("NOPE", 42) == 42); }
// 2) set + save + reload round-trips each value. { OptionsIni o; o.load(path); o.set("SOUND", 0); o.set("MUSIC", 1); o.set("SPEED", 3); CHECK(o.save()); OptionsIni r; r.load(path); CHECK(r.get("SOUND", 1) == 0); CHECK(r.get("MUSIC", 0) == 1); CHECK(r.get("SPEED", 0) == 3); }
// 3) NO-CLOBBER: persisting one key (the toggle path) preserves the others already on disk. { OptionsIni o; o.load(path); // has SOUND=0 MUSIC=1 SPEED=3 from step 2 CHECK(o.set_and_save("SOUND", 1)); // player re-enables sound OptionsIni r; r.load(path); CHECK(r.get("SOUND", 9) == 1); // changed CHECK(r.get("MUSIC", 9) == 1); // untouched CHECK(r.get("SPEED", 9) == 3); // untouched }
// 4) An unknown key hand-added to the file survives a save of a different key (AUTOSAVE has no UI yet). { { std::ofstream f(path, std::ios::trunc); f << "[OPTIONS]\nSPEED=2\nAUTOSAVE=5\nSOUND=0\n"; } OptionsIni o; o.load(path); CHECK(o.get("AUTOSAVE", -1) == 5); o.set_and_save("SOUND", 1); // flips SOUND, must keep AUTOSAVE OptionsIni r; r.load(path); CHECK(r.get("AUTOSAVE", -1) == 5); CHECK(r.get("SOUND", -1) == 1); CHECK(r.get("SPEED", -1) == 2); }
// 5) Robust parsing: section header case-insensitive, key case-folded, comments/blank lines ignored, // surrounding whitespace trimmed, non-integer values skipped (fall back to default). { { std::ofstream f(path, std::ios::trunc); f << "; sc2k options\n\n[options]\n Sound = 1 \n# a comment\nMUSIC=notanumber\nSPEED=4\n"; } OptionsIni o; o.load(path); CHECK(o.get("SOUND", -1) == 1); // "Sound" folds to SOUND, spaces trimmed CHECK(o.get("MUSIC", 7) == 7); // garbage value ignored → default CHECK(o.get("SPEED", -1) == 4); }
// 6) The written file uses the exact [OPTIONS] section the original registry section is named after. { OptionsIni o; o.load(path); o.set("SOUND", 1); CHECK(o.save()); CHECK(slurp(path).rfind("[OPTIONS]", 0) == 0); // begins with the section header }
// 7) Autosave cadence (AUTOSAVE = interval in years; fires every N played years, off when <=0). { CHECK(!autosave_due(0, 1)); // year 0 (just started) never saves CHECK(autosave_due(1, 1)); // N=1 → every year CHECK(autosave_due(5, 1)); CHECK(!autosave_due(1, 3)); // N=3 → years 3,6,9… CHECK(!autosave_due(2, 3)); CHECK(autosave_due(3, 3)); CHECK(autosave_due(6, 3)); CHECK(!autosave_due(4, 3)); CHECK(!autosave_due(5, 0)); // 0 = off, never fires CHECK(!autosave_due(5, -2)); // negative = off }
std::remove(path.c_str()); if (failures == 0) { std::printf("options_ini: [OPTIONS] persistence + autosave cadence OK\n"); return 0; } std::printf("options_ini: %d failure(s)\n", failures); return 1;}