// Copyright (c) 2011 Bryan Schumaker. #include #include #include #include #include #include using namespace std; static map preferences; static void save(ofstream &stream, void *data) { map::iterator it; println("Saving preferences!"); for (it = preferences.begin(); it != preferences.end(); it++) { stream << it->first << endl; stream << it->second << endl; } } static void load(ifstream &stream) { string key, value; int val; println("Loading preferences!"); while (stream.good()) { getline(stream, key); if (!stream.good()) break; getline(stream, value); if (!stream.good()) break; val = atoi(value.c_str()); println(key + " = %d", val); preferences[key] = val; } } static void set_preference(const string &key, int p) { preferences[key] = p; libsaria::app::save("prefs", save, NULL); } static bool key_exists(const string &key) { map::iterator it; it = preferences.find(key); return it != preferences.end(); } namespace libsaria { void prefs::init() { libsaria::app::read_now("prefs", load); } int prefs::get(const string &key) { map::iterator it; it = preferences.find(key); if (it == preferences.end()) return 0; return it->second; } void prefs::set(const string &key, int value) { if (get(key) != value) set_preference(key, value); } int prefs::init(const string &key, int value) { if (!key_exists(key)) { set_preference(key, value); return value; } return get(key); } }; /* Namespace: libsaria */