Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "crb/preferences.h"
static struct crb_preferences *prefs;
int crb_preferences_initialize(char *prefs_file)
{
if (prefs != NULL)
crb_preferences_cleanup();
if (prefs_file == NULL) {
printf("Must specify a valid preferences file.\n");
return 0;
}
prefs = malloc(sizeof(struct crb_preferences));
prefs->file = strdup(prefs_file);
INIT_LIST_HEAD(&prefs->list);
prefs->lua_state = lua_open();
luaL_openlibs(prefs->lua_state);
crb_preferences_load_prefs();
return 1;
}
int crb_preferences_cleanup(void)
{
free(prefs->file);
prefs->file = NULL;
lua_close(prefs->lua_state);
prefs->lua_state = NULL;
free(prefs);
prefs = NULL;
return 1;
}
static int crb_preferences_clear_prefs(void)
{
struct crb_preference *tmp;
struct list_head *pos, *q;
list_for_each_safe(pos, q, &prefs->list) {
tmp = list_entry(pos, struct crb_preference, preferences);
list_del(pos);
if (tmp->type == PREFERENCE_STRING)
free(tmp->svalue);
free(tmp);
}
return 1;
}
int crb_preferences_load_prefs(void)
{
struct crb_preference *pref;
if (prefs == NULL || prefs->lua_state == NULL) {
printf("Attempted to load preferences before performing initialization.\n");
return 0;
}
crb_preferences_clear_prefs();
/* Set defaults first */
pref = malloc(sizeof(struct crb_preference));
pref->name = PREFERENCE_SCREENHEIGHT;
pref->type = PREFERENCE_INTEGER;
pref->ivalue = PREFERENCE_SCREENHEIGHT_DEFAULT;
list_add(&(pref->preferences), &(prefs->list));
pref = malloc(sizeof(struct crb_preference));
pref->name = PREFERENCE_SCREENWIDTH;
pref->type = PREFERENCE_INTEGER;
pref->ivalue = PREFERENCE_SCREENWIDTH_DEFAULT;
list_add(&(pref->preferences), &(prefs->list));
/* Load preferences from Lua file */
if (luaL_loadfile(prefs->lua_state, prefs->file) || lua_pcall(prefs->lua_state, 0, 0, 0)) {
printf("Error loading preferences file:\n%s\n", lua_tostring(prefs->lua_state, -1));
return 0;
}
lua_getglobal(prefs->lua_state, PREFERENCE_SCREENHEIGHT);
/* todo */
return 1;
}
static struct crb_preference *crb_preferences_get_pref(const char *name)
{
struct list_head *pos;
struct crb_preference *pref;
if (prefs == NULL || name == NULL)
return NULL;
list_for_each(pos, &prefs->list) {
pref = list_entry(pos, struct crb_preference, preferences);
if (!strcmp(pref->name, name))
return pref;
}
return NULL;
}
int crb_preferences_pref_exists(const char *name)
{
return crb_preferences_get_pref(name) != NULL;
}
int crb_preferences_get_int(const char *name, int *value)
{
struct crb_preference *pref = crb_preferences_get_pref(name);
if (pref == NULL || pref->type != PREFERENCE_INTEGER)
return 0;
*value = pref->ivalue;
return 1;
}
int crb_preferences_get_string(const char *name, char **value)
{
struct crb_preference *pref = crb_preferences_get_pref(name);
if (pref == NULL || pref->type != PREFERENCE_STRING)
return 0;
*value = strdup(pref->svalue);
return 1;
}
int crb_preferences_put_int(const char *name, int value)
{
struct crb_preference *pref = crb_preferences_get_pref(name);
if (prefs == NULL)
return 0;
if (pref == NULL) {
pref = malloc(sizeof(struct crb_preference));
pref->name = name;
list_add(&(pref->preferences), &(prefs->list));
}
pref->type = PREFERENCE_INTEGER;
if (pref->svalue != NULL)
free(pref->svalue);
pref->svalue = NULL;
pref->ivalue = value;
return 1;
}
int crb_preferences_put_string(const char *name, char *value)
{
struct crb_preference *pref = crb_preferences_get_pref(name);
if (prefs == NULL)
return 0;
if (pref == NULL) {
pref = malloc(sizeof(struct crb_preference));
pref->name = name;
list_add(&(pref->preferences), &(prefs->list));
}
pref->type = PREFERENCE_STRING;
if (pref->svalue != NULL)
free(pref->svalue);
pref->ivalue = 0;
pref->svalue = strdup(value);
return 1;
}
int crb_preferences_save_prefs(void)
{
/* todo */
}
#ifndef PREFERENCES_H
#define PREFERENCES_H
#include "universal_include.h"
/* Preference constants */
#define PREFERENCE_SCREENWIDTH "ScreenWidth"
#define PREFERENCE_SCREENHEIGHT "ScreenHeight"
/* Preference defaults */
#define PREFERENCE_SCREENWIDTH_DEFAULT 854
#define PREFERENCE_SCREENHEIGHT_DEFAULT 480
enum preference_type {
PREFERENCE_INTEGER,
PREFERENCE_STRING
};
struct crb_preference {
char *name;
int ivalue;
char *svalue;
int type;
struct list_head preferences;
};
struct crb_preferences {
char *file;
struct list_head list;
lua_State *lua_state;
};
int crb_preferences_initialize(char *prefs_file);
int crb_preferences_cleanup(void);
int crb_preferences_load_prefs(void);
int crb_preferences_save_prefs(void);
int crb_preferences_pref_exists(const char *name);
int crb_preferences_get_int(const char *name, int *value);
int crb_preferences_get_string(const char *name, char **value);
int crb_preferences_put_int(const char *name, int value);
int crb_preferences_put_string(const char *name, char *value);
#endif /* PREFERENCES_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment