diff --git a/include/libsaria/prefs.h b/include/libsaria/prefs.h new file mode 100644 index 00000000..0f124ef3 --- /dev/null +++ b/include/libsaria/prefs.h @@ -0,0 +1,19 @@ +#ifndef LIBSARIA_PREFS_H +#define LIBSARIA_PREFS_H + +#include +using namespace std; + +namespace libsaria +{ + namespace prefs + { + void load(); + + void set(string, bool); + + bool get_bool(string); + } +} + +#endif /* LIBSARIA_PREFS_H */ diff --git a/libsaria/libsaria.cpp b/libsaria/libsaria.cpp index 5c2233a6..3afea91b 100644 --- a/libsaria/libsaria.cpp +++ b/libsaria/libsaria.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -16,6 +17,7 @@ namespace libsaria println(get_saria_dir()); make_saria_dir(); libsaria::library::load(); + libsaria::prefs::load(); } void quit() diff --git a/libsaria/prefs.cpp b/libsaria/prefs.cpp new file mode 100644 index 00000000..fafe5f29 --- /dev/null +++ b/libsaria/prefs.cpp @@ -0,0 +1,94 @@ + +#include +using namespace std; + +#include +#include + +enum pref_t { + BOOL, +}; + +struct Preference { + pref_t type; + union value { + bool boolean; + } value_u; +}; + +static map preferences; + +static void load_pref(InFile &in) +{ + Preference p; + string key = in.read_str(); + p.type = (pref_t)in.read_ui(); + switch (p.type) { + case BOOL: + p.value_u.boolean = in.read_int(); + break; + default: + break; + } + preferences[key] = p; +} + +static void save_pref(string key, struct Preference &p, OutFile &out) +{ + out.write_str(key, false); + out.write_ui(p.type, false); + switch (p.type) { + case BOOL: + out.write_int(p.value_u.boolean, true); + break; + default: + out.write_str("", true); + } +} + +static void save() +{ + map::iterator it; + OutFile out("preferences"); + out.write_ui(preferences.size(), true); + for (it = preferences.begin(); it != preferences.end(); it++) + save_pref(it->first, it->second, out); +} + +static void set_preference(string key, struct Preference &p) +{ + preferences[key] = p; + save(); +} + +namespace libsaria +{ + + void prefs::load() + { + InFile in("preferences"); + unsigned int size = in.read_ui(); + for (unsigned int i = 0; i < size; i++) + load_pref(in); + } + + void prefs::set(string key, bool value) + { + struct Preference p; + p.type = BOOL; + p.value_u.boolean = value; + set_preference(key, p); + } + + bool prefs::get_bool(string key) + { + map::iterator it; + it = preferences.find(key); + if (it == preferences.end()) + return false; + if (it->second.type != BOOL) + return false; + return it->second.value_u.boolean; + } + +}