ConfigData: use simple linked list instead of GSList
This commit is contained in:
parent
e44e0fab9f
commit
378ebad1c8
@ -32,10 +32,11 @@ extern "C" {
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
config_param::config_param(const char *_value, int _line)
|
config_param::config_param(const char *_value, int _line)
|
||||||
:value(g_strdup(_value)), line(_line) {}
|
:next(nullptr), value(g_strdup(_value)), line(_line) {}
|
||||||
|
|
||||||
config_param::~config_param()
|
config_param::~config_param()
|
||||||
{
|
{
|
||||||
|
delete next;
|
||||||
g_free(value);
|
g_free(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
#include "gcc.h"
|
#include "gcc.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#include <glib.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -54,6 +53,12 @@ struct block_param {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct config_param {
|
struct config_param {
|
||||||
|
/**
|
||||||
|
* The next config_param with the same name. The destructor
|
||||||
|
* deletes the whole chain.
|
||||||
|
*/
|
||||||
|
struct config_param *next;
|
||||||
|
|
||||||
char *value;
|
char *value;
|
||||||
unsigned int line;
|
unsigned int line;
|
||||||
|
|
||||||
@ -67,7 +72,7 @@ struct config_param {
|
|||||||
bool used;
|
bool used;
|
||||||
|
|
||||||
config_param(int _line=-1)
|
config_param(int _line=-1)
|
||||||
:value(nullptr), line(_line), used(false) {}
|
:next(nullptr), value(nullptr), line(_line), used(false) {}
|
||||||
|
|
||||||
gcc_nonnull_all
|
gcc_nonnull_all
|
||||||
config_param(const char *_value, int _line=-1);
|
config_param(const char *_value, int _line=-1);
|
||||||
@ -92,7 +97,7 @@ struct config_param {
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
struct ConfigData {
|
struct ConfigData {
|
||||||
std::array<GSList *, std::size_t(CONF_MAX)> params;
|
std::array<config_param *, std::size_t(CONF_MAX)> params;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -137,6 +137,19 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gcc_nonnull_all
|
||||||
|
static void
|
||||||
|
Append(config_param *&head, config_param *p)
|
||||||
|
{
|
||||||
|
assert(p->next == nullptr);
|
||||||
|
|
||||||
|
config_param **i = &head;
|
||||||
|
while (*i != nullptr)
|
||||||
|
i = &(*i)->next;
|
||||||
|
|
||||||
|
*i = p;
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
|
ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
|
||||||
{
|
{
|
||||||
@ -179,12 +192,12 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned i = ParseConfigOptionName(name);
|
const unsigned i = unsigned(o);
|
||||||
const ConfigTemplate &option = config_templates[i];
|
const ConfigTemplate &option = config_templates[i];
|
||||||
GSList *¶ms = config_data.params[i];
|
config_param *&head = config_data.params[i];
|
||||||
|
|
||||||
if (params != NULL && !option.repeatable) {
|
if (head != nullptr && !option.repeatable) {
|
||||||
param = (struct config_param *)params->data;
|
param = head;
|
||||||
g_set_error(error_r, config_quark(), 0,
|
g_set_error(error_r, config_quark(), 0,
|
||||||
"config parameter \"%s\" is first defined "
|
"config parameter \"%s\" is first defined "
|
||||||
"on line %i and redefined on line %i\n",
|
"on line %i and redefined on line %i\n",
|
||||||
@ -244,7 +257,7 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, GError **error_r)
|
|||||||
param = new config_param(value, count);
|
param = new config_param(value, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
params = g_slist_append(params, param);
|
Append(head, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -39,20 +39,10 @@ extern "C" {
|
|||||||
|
|
||||||
static ConfigData config_data;
|
static ConfigData config_data;
|
||||||
|
|
||||||
static void
|
|
||||||
config_param_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
|
||||||
{
|
|
||||||
struct config_param *param = (struct config_param *)data;
|
|
||||||
|
|
||||||
delete param;
|
|
||||||
}
|
|
||||||
|
|
||||||
void config_global_finish(void)
|
void config_global_finish(void)
|
||||||
{
|
{
|
||||||
for (auto i : config_data.params) {
|
for (auto i : config_data.params)
|
||||||
g_slist_foreach(i, config_param_free_callback, NULL);
|
delete i;
|
||||||
g_slist_free(i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void config_global_init(void)
|
void config_global_init(void)
|
||||||
@ -66,10 +56,8 @@ ReadConfigFile(const Path &path, GError **error_r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
config_param_check(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
Check(const config_param *param)
|
||||||
{
|
{
|
||||||
struct config_param *param = (struct config_param *)data;
|
|
||||||
|
|
||||||
if (!param->used)
|
if (!param->used)
|
||||||
/* this whole config_param was not queried at all -
|
/* this whole config_param was not queried at all -
|
||||||
the feature might be disabled at compile time?
|
the feature might be disabled at compile time?
|
||||||
@ -86,26 +74,17 @@ config_param_check(gpointer data, G_GNUC_UNUSED gpointer user_data)
|
|||||||
void config_global_check(void)
|
void config_global_check(void)
|
||||||
{
|
{
|
||||||
for (auto i : config_data.params)
|
for (auto i : config_data.params)
|
||||||
g_slist_foreach(i, config_param_check, NULL);
|
for (const config_param *p = i; p != nullptr; p = p->next)
|
||||||
|
Check(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct config_param *
|
const struct config_param *
|
||||||
config_get_next_param(ConfigOption option, const struct config_param * last)
|
config_get_next_param(ConfigOption option, const struct config_param * last)
|
||||||
{
|
{
|
||||||
GSList *node = config_data.params[unsigned(option)];
|
config_param *param = last != nullptr
|
||||||
|
? last->next
|
||||||
if (last) {
|
: config_data.params[unsigned(option)];
|
||||||
node = g_slist_find(node, last);
|
if (param != nullptr)
|
||||||
if (node == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
node = g_slist_next(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node == NULL)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
struct config_param *param = (struct config_param *)node->data;
|
|
||||||
param->used = true;
|
param->used = true;
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user