libsaria: Save preferences in idle task after they change

I made a WriteTask class to use for saving files.  This allows me to
create a file in the appdir and write out the new preferences if they've
changed.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-03-10 18:29:16 -05:00 committed by Bryan Schumaker
parent a0da654fc6
commit 06412afeed
11 changed files with 104 additions and 60 deletions

View File

@ -4,8 +4,7 @@
#include <string>
using namespace std;
enum FileState {
UNLOADED,
enum DataState {
DIRTY,
CLEAN,
};
@ -19,6 +18,8 @@ namespace libsaria
void init(string);
void mkdir();
void save(string, void (*)(ofstream &), DataState *);
} /* Namespace: app */
} /* Namespace: libsaria */

View File

@ -1,11 +1,17 @@
#ifndef LIBSARIA_IDLE_H
#define LIBSARIA_IDLE_H
#include <libsaria/task.h>
namespace libsaria
{
namespace idle
{
int run_task();
int queue_task(IdleTask *, bool);
void enable(void (*)());
};
};

View File

@ -8,16 +8,11 @@ namespace libsaria
{
namespace prefs
{
void init();
int get(string);
void set(string, int);
int init(string, int);
/*void load();
void set(string, bool);
void set(string, float);
bool get_bool(string);
float get_float(string, float);*/
}
}

View File

@ -1,6 +1,7 @@
#ifndef LIBSARIA_IDLE_TASK_H
#define LIBSARIA_IDLE_TASK_H
#include <libsaria/fs.h>
#include <cstddef>
class IdleTask
@ -14,6 +15,19 @@ class IdleTask
void queue_front();
};
class WriteTask : public IdleTask
{
private:
string filepath;
void (*func)(ofstream &);
DataState *state;
public:
WriteTask(string, void (*)(ofstream &), DataState *);
~WriteTask();
void run_task();
};
class IOTask : public IdleTask
{
private:

View File

@ -1,6 +1,7 @@
// Copyright (c) 2012 Bryan Schumaker.
#include <libsaria/fs.h>
#include <libsaria/task.h>
#include <libsaria/print.h>
#include <sys/stat.h>
@ -33,4 +34,9 @@ namespace libsaria
mkdir();
}
void app::save(string file, void (*func)(ofstream &), DataState *state)
{
new WriteTask(appdir + "/" + file, func, state);
}
} /* Namespace: libsaria */

View File

@ -1,10 +1,11 @@
// Copyright (c) 2011 Bryan Schumaker.
#include <libsaria/idle.h>
#include <queue>
using namespace std;
#include <libsaria/callback.h>
#include "idle.h"
static bool enabled = false;
static void (*cb_task_queued)() = NULL;;
static deque<IdleTask *> idle_queue;
namespace libsaria
@ -29,13 +30,25 @@ namespace libsaria
*/
int idle::queue_task(IdleTask *task, bool front)
{
if (enabled == false) {
task->run_task();
delete task;
return 0;
}
if (front == true)
idle_queue.push_front(task);
else
idle_queue.push_back(task);
if (idle_queue.size() == 1)
trigger_callback(IDLE_TASK_QUEUED);
cb_task_queued();
return idle_queue.size();
}
void idle::enable(void (*queued_cb)())
{
cb_task_queued = queued_cb;
enabled = true;
}
};

View File

@ -1,15 +0,0 @@
#ifndef LIBSARIA_IDLE_COMPONENT_H
#define LIBSARIA_IDLE_COMPONENT_H
#include <libsaria/idle.h>
#include <libsaria/task.h>
namespace libsaria
{
namespace idle
{
int queue_task(IdleTask *, bool);
};
};
#endif /* LIBSARIA_IDLE_COMPONENT_H */

View File

@ -1,6 +1,9 @@
// Copyright (c) 2012 Bryan Schumaker.
#include <libsaria/task.h>
#include <fstream>
using namespace std;
IOTask::IOTask(void (*fn)())
{
func = fn;
@ -26,3 +29,27 @@ void IOTask::run_task()
else
func_aux(aux);
}
WriteTask::WriteTask(string file, void (*fn)(ofstream &), DataState *s)
{
filepath = file;
func = fn;
state = s;
queue();
}
WriteTask::~WriteTask()
{
}
void WriteTask::run_task()
{
if (*state == CLEAN)
return;
ofstream stream;
stream.open(filepath.c_str());
func(stream);
stream.close();
}

View File

@ -1,6 +1,5 @@
// Copyright (c) 2011 Bryan Schumaker.
#include <libsaria/idle.h>
#include "idle.h"
/*
* The IdleTask should be constructed through `new` and not

View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <libsaria/libsaria.h>
#include <libsaria/prefs.h>
#include <libsaria/fs.h>
/*#include <libsaria/audio.h>
#include <libsaria/path.h>
@ -22,6 +23,7 @@ namespace libsaria
println("Initializing libsaria");
app::init(name);
prefs::init();
/*libsaria::prefs::load();
audio::init(argc, argv);

View File

@ -1,25 +1,14 @@
// Copyright (c) 2011 Bryan Schumaker.
#include <map>
using namespace std;
#include <libsaria/print.h>
#include <libsaria/prefs.h>
#include <libsaria/fs.h>
enum pref_t {
INT,
BOOL,
FLOAT,
};
#include <map>
#include <fstream>
using namespace std;
union Preference {
int integer;
bool boolean;
float floating;
};
static FileState state = UNLOADED;
static map<string, union Preference> preferences;
static DataState state = CLEAN;
static map<string, int> preferences;
/*static void load_pref(InFile &in)
{
@ -65,17 +54,22 @@ static void save()
save_pref(it->first, it->second, out);
}*/
static void save()
static void save(ofstream &stream)
{
println("Saving!");
map<string, int>::iterator it;
for (it = preferences.begin(); it != preferences.end(); it++) {
stream << it->first << endl;
stream << it->second << endl;
}
state = CLEAN;
}
static void set_preference(string key, Preference &p)
static void set_preference(string key, int p)
{
preferences[key] = p;
if (state != UNLOADED)
save();
state = DIRTY;
libsaria::app::save("prefs", save, &state);
}
/*
namespace libsaria
@ -136,32 +130,34 @@ namespace libsaria
namespace libsaria
{
void prefs::init()
{
state = CLEAN;
}
int prefs::get(string key)
{
map<string, union Preference>::iterator it;
map<string, int>::iterator it;
it = preferences.find(key);
if (it == preferences.end())
return 0;
return it->second.integer;
return it->second;
}
void prefs::set(string key, int value)
{
union Preference p;
p.integer = value;
set_preference(key, p);
if (get(key) != value)
set_preference(key, value);
}
int prefs::init(string key, int value)
{
map<string, union Preference>::iterator it;
it = preferences.find(key);
if (it == preferences.end()) {
set(key, value);
int cur = get(key);
if (cur == 0){
set_preference(key, value);
return value;
} else
return it->second.integer;
}
return cur;
}