Commit e943b9bf authored by Max Kellermann's avatar Max Kellermann

conf: use GLib's GSList library

Use GLib's singly-linked list library instead of our custom one.
parent 11832367
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "conf.h" #include "conf.h"
#include "utils.h" #include "utils.h"
#include "buffer2array.h" #include "buffer2array.h"
#include "list.h"
#include "path.h" #include "path.h"
#include <glib.h> #include <glib.h>
...@@ -37,11 +36,13 @@ ...@@ -37,11 +36,13 @@
#define CONF_LINE_TOKEN_MAX 3 #define CONF_LINE_TOKEN_MAX 3
typedef struct _configEntry { typedef struct _configEntry {
const char *name;
unsigned char mask; unsigned char mask;
List *configParamList;
GSList *params;
} ConfigEntry; } ConfigEntry;
static List *configEntriesList; static GSList *config_entries;
static int get_bool(const char *value) static int get_bool(const char *value)
{ {
...@@ -77,8 +78,10 @@ ConfigParam *newConfigParam(const char *value, int line) ...@@ -77,8 +78,10 @@ ConfigParam *newConfigParam(const char *value, int line)
return ret; return ret;
} }
static void freeConfigParam(ConfigParam * param) static void
config_param_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
{ {
ConfigParam *param = data;
int i; int i;
if (param->value) if (param->value)
...@@ -99,13 +102,14 @@ static void freeConfigParam(ConfigParam * param) ...@@ -99,13 +102,14 @@ static void freeConfigParam(ConfigParam * param)
free(param); free(param);
} }
static ConfigEntry *newConfigEntry(int repeatable, int block) static ConfigEntry *
newConfigEntry(const char *name, int repeatable, int block)
{ {
ConfigEntry *ret = g_new(ConfigEntry, 1); ConfigEntry *ret = g_new(ConfigEntry, 1);
ret->name = name;
ret->mask = 0; ret->mask = 0;
ret->configParamList = ret->params = NULL;
makeList((ListFreeDataFunc *) freeConfigParam, 1);
if (repeatable) if (repeatable)
ret->mask |= CONF_REPEATABLE_MASK; ret->mask |= CONF_REPEATABLE_MASK;
...@@ -115,32 +119,53 @@ static ConfigEntry *newConfigEntry(int repeatable, int block) ...@@ -115,32 +119,53 @@ static ConfigEntry *newConfigEntry(int repeatable, int block)
return ret; return ret;
} }
static void freeConfigEntry(ConfigEntry * entry) static void
config_entry_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
{ {
freeList(entry->configParamList); ConfigEntry *entry = data;
g_slist_foreach(entry->params, config_param_free, NULL);
g_slist_free(entry->params);
free(entry); free(entry);
} }
static ConfigEntry *
config_entry_get(const char *name)
{
GSList *list;
for (list = config_entries; list != NULL;
list = g_slist_next(list)) {
ConfigEntry *entry = list->data;
if (strcmp(entry->name, name) == 0)
return entry;
}
return NULL;
}
static void registerConfigParam(const char *name, int repeatable, int block) static void registerConfigParam(const char *name, int repeatable, int block)
{ {
ConfigEntry *entry; ConfigEntry *entry;
if (findInList(configEntriesList, name, NULL)) entry = config_entry_get(name);
if (entry != NULL)
g_error("config parameter \"%s\" already registered\n", name); g_error("config parameter \"%s\" already registered\n", name);
entry = newConfigEntry(repeatable, block); entry = newConfigEntry(name, repeatable, block);
config_entries = g_slist_prepend(config_entries, entry);
insertInList(configEntriesList, name, entry);
} }
void finishConf(void) void finishConf(void)
{ {
freeList(configEntriesList); g_slist_foreach(config_entries, config_entry_free, NULL);
g_slist_free(config_entries);
} }
void initConf(void) void initConf(void)
{ {
configEntriesList = makeList((ListFreeDataFunc *) freeConfigEntry, 1); config_entries = NULL;
/* registerConfigParam(name, repeatable, block); */ /* registerConfigParam(name, repeatable, block); */
registerConfigParam(CONF_MUSIC_DIR, 0, 0); registerConfigParam(CONF_MUSIC_DIR, 0, 0);
...@@ -262,7 +287,6 @@ void readConf(const char *file) ...@@ -262,7 +287,6 @@ void readConf(const char *file)
int argsMinusComment; int argsMinusComment;
int count = 0; int count = 0;
ConfigEntry *entry; ConfigEntry *entry;
void *voidPtr;
ConfigParam *param; ConfigParam *param;
if (!(fp = fopen(file, "r"))) { if (!(fp = fopen(file, "r"))) {
...@@ -293,16 +317,14 @@ void readConf(const char *file) ...@@ -293,16 +317,14 @@ void readConf(const char *file)
" %s\n", count, string); " %s\n", count, string);
} }
if (!findInList(configEntriesList, array[0], &voidPtr)) { entry = config_entry_get(array[0]);
if (entry == NULL)
g_error("unrecognized parameter in config file at " g_error("unrecognized parameter in config file at "
"line %i: %s\n", count, string); "line %i: %s\n", count, string);
}
entry = (ConfigEntry *) voidPtr;
if (!(entry->mask & CONF_REPEATABLE_MASK) && if (!(entry->mask & CONF_REPEATABLE_MASK) &&
entry->configParamList->numberOfNodes) { entry->params != NULL) {
param = entry->configParamList->firstNode->data; param = entry->params->data;
g_error("config parameter \"%s\" is first defined on " g_error("config parameter \"%s\" is first defined on "
"line %i and redefined on line %i\n", "line %i and redefined on line %i\n",
array[0], param->line, count); array[0], param->line, count);
...@@ -317,32 +339,29 @@ void readConf(const char *file) ...@@ -317,32 +339,29 @@ void readConf(const char *file)
} else } else
param = newConfigParam(array[1], count); param = newConfigParam(array[1], count);
insertInListWithoutKey(entry->configParamList, param); entry->params = g_slist_append(entry->params, param);
} }
fclose(fp); fclose(fp);
} }
ConfigParam *getNextConfigParam(const char *name, ConfigParam * last) ConfigParam *getNextConfigParam(const char *name, ConfigParam * last)
{ {
void *voidPtr;
ConfigEntry *entry; ConfigEntry *entry;
ListNode *node; GSList *node;
ConfigParam *param; ConfigParam *param;
if (!findInList(configEntriesList, name, &voidPtr)) entry = config_entry_get(name);
if (entry == NULL)
return NULL; return NULL;
entry = voidPtr; node = entry->params;
node = entry->configParamList->firstNode;
if (last) { if (last) {
while (node != NULL) { node = g_slist_find(node, last);
param = node->data; if (node == NULL)
node = node->nextNode; return NULL;
if (param == last)
break; node = g_slist_next(node);
}
} }
if (node == NULL) if (node == NULL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment