libsaria: load preferences from disk during startup

This is one of the first things that happens when initializing libsaria.
This also allows me to remove the remaining commented out code from the
preferences file.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-03-10 19:05:23 -05:00 committed by Bryan Schumaker
parent 06412afeed
commit 84582ae9d8
4 changed files with 32 additions and 102 deletions

View File

@ -19,6 +19,7 @@ namespace libsaria
void mkdir();
void save(string, void (*)(ofstream &), DataState *);
void read_now(string, void (*)(ifstream &));
} /* Namespace: app */

View File

@ -25,7 +25,6 @@ class WriteTask : public IdleTask
WriteTask(string, void (*)(ofstream &), DataState *);
~WriteTask();
void run_task();
};
class IOTask : public IdleTask

View File

@ -6,6 +6,8 @@
#include <sys/stat.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
static string appdir;
static mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
@ -39,4 +41,12 @@ namespace libsaria
new WriteTask(appdir + "/" + file, func, state);
}
void app::read_now(string file, void (*func)(ifstream &))
{
ifstream stream;
stream.open((appdir + "/" + file).c_str());
func(stream);
stream.close();
}
} /* Namespace: libsaria */

View File

@ -10,54 +10,11 @@ using namespace std;
static DataState state = CLEAN;
static map<string, int> preferences;
/*static void load_pref(InFile &in)
{
Preference p;
string key;
in >> key >> (unsigned int &)p.type;
switch (p.type) {
case BOOL:
in >> p.value_u.boolean;
println("preferences[" + key + "] = %d", p.value_u.boolean);
break;
case FLOAT:
in >> p.value_u.floating;
println("preferences[" + key + "] = %f", p.value_u.floating);
break;
default:
break;
}
preferences[key] = p;
}
static void save_pref(string key, struct Preference &p, OutFile &out)
{
out << key << p.type;
switch (p.type) {
case BOOL:
out << p.value_u.boolean;
break;
case FLOAT:
out << p.value_u.floating;
break;
}
out << "\n";
}
static void save()
{
map<string, Preference>::iterator it;
OutFile out("preferences");
out << preferences.size() << "\n";
for (it = preferences.begin(); it != preferences.end(); it++)
save_pref(it->first, it->second, out);
}*/
static void save(ofstream &stream)
{
println("Saving!");
map<string, int>::iterator it;
println("Saving preferences!");
for (it = preferences.begin(); it != preferences.end(); it++) {
stream << it->first << endl;
stream << it->second << endl;
@ -65,73 +22,36 @@ static void save(ofstream &stream)
state = CLEAN;
}
static void load(ifstream &stream)
{
string key;
int value;
println("Loading preferences!");
while (stream.good()) {
stream >> key;
if (!stream.good())
break;
stream >> value;
if (!stream.good())
break;
println(key + " = %d", value);
preferences[key] = value;
}
}
static void set_preference(string key, int p)
{
preferences[key] = p;
state = DIRTY;
libsaria::app::save("prefs", save, &state);
}
/*
namespace libsaria
{
void prefs::load()
{
unsigned int size = 0;
InFile in("preferences");
if (!in.good())
return;
in >> size;
println("Preferences size: %d", size);
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);
}
void prefs::set(string key, float value)
{
struct Preference p;
p.type = FLOAT;
p.value_u.floating = value;
set_preference(key, p);
}
bool prefs::get_bool(string key)
{
map<string, Preference>::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;
}
float prefs::get_float(string key, float dfault)
{
map<string, Preference>::iterator it;
it = preferences.find(key);
if (it == preferences.end())
return dfault;
if (it->second.type != FLOAT)
return dfault;
return it->second.value_u.floating;
}
}*/
namespace libsaria
{
void prefs::init()
{
libsaria::app::read_now("prefs", load);
state = CLEAN;
}