summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorshennetsind <ind@henn.et>2014-01-18 18:34:21 -0200
committershennetsind <ind@henn.et>2014-01-18 18:34:21 -0200
commita9cc02e1dea69e14a1b1e3d1cf27aaa271ac0b4b (patch)
tree1f8951ab4a7b8d8adc715e00edeb24ab0c492c8e /src/common
parent59662a1656aa1db6f7cd0f92ce10a3ff66a979a4 (diff)
downloadhercules-a9cc02e1dea69e14a1b1e3d1cf27aaa271ac0b4b.tar.gz
hercules-a9cc02e1dea69e14a1b1e3d1cf27aaa271ac0b4b.tar.bz2
hercules-a9cc02e1dea69e14a1b1e3d1cf27aaa271ac0b4b.tar.xz
hercules-a9cc02e1dea69e14a1b1e3d1cf27aaa271ac0b4b.zip
Libconfig Interfaced
Ready for plugin use. Thanks to Haruna Signed-off-by: shennetsind <ind@henn.et>
Diffstat (limited to 'src/common')
-rw-r--r--src/common/HPM.c23
-rw-r--r--src/common/conf.c158
-rw-r--r--src/common/conf.h92
-rw-r--r--src/common/core.c2
4 files changed, 216 insertions, 59 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c
index 426fac94a..e31437b72 100644
--- a/src/common/HPM.c
+++ b/src/common/HPM.c
@@ -261,21 +261,21 @@ void hplugins_config_read(void) {
fclose(fp);
}
- if (conf_read_file(&plugins_conf, config_filename))
+ if (libconfig->read_file(&plugins_conf, config_filename))
return;
if( HPM->symbol_defaults_sub )
HPM->symbol_defaults_sub();
- plist = config_lookup(&plugins_conf, "plugins_list");
+ plist = libconfig->lookup(&plugins_conf, "plugins_list");
if (plist != NULL) {
- int length = config_setting_length(plist), i;
+ int length = libconfig->setting_length(plist), i;
char filename[60];
for(i = 0; i < length; i++) {
- if( !strcmpi(config_setting_get_string_elem(plist,i),"HPMHooking") ) {//must load it first
+ if( !strcmpi(libconfig->setting_get_string_elem(plist,i),"HPMHooking") ) {//must load it first
struct hplugin *plugin;
- snprintf(filename, 60, "plugins/%s%s", config_setting_get_string_elem(plist,i), DLL_EXT);
+ snprintf(filename, 60, "plugins/%s%s", libconfig->setting_get_string_elem(plist,i), DLL_EXT);
if( ( plugin = HPM->load(filename) ) ) {
bool (*func)(bool *fr);
bool (*addhook_sub) (enum HPluginHookType type, const char *target, void *hook, unsigned int pID);
@@ -289,12 +289,12 @@ void hplugins_config_read(void) {
}
}
for(i = 0; i < length; i++) {
- if( strcmpi(config_setting_get_string_elem(plist,i),"HPMHooking") ) {//now all others
- snprintf(filename, 60, "plugins/%s%s", config_setting_get_string_elem(plist,i), DLL_EXT);
+ if( strcmpi(libconfig->setting_get_string_elem(plist,i),"HPMHooking") ) {//now all others
+ snprintf(filename, 60, "plugins/%s%s", libconfig->setting_get_string_elem(plist,i), DLL_EXT);
HPM->load(filename);
}
}
- config_destroy(&plugins_conf);
+ libconfig->destroy(&plugins_conf);
}
if( HPM->plugin_count )
@@ -688,11 +688,8 @@ void hplugins_share_defaults(void) {
HPM->share(SQL,"SQL");
/* timer */
HPM->share(timer,"timer");
- /* libconfig (temp) */
- HPM->share(config_setting_lookup_string,"config_setting_lookup_string");
- HPM->share(config_setting_lookup_int,"config_setting_lookup_int");
- HPM->share(config_setting_get_member,"config_setting_get_member");
- HPM->share(config_setting_length,"config_setting_length");
+ /* libconfig */
+ HPM->share(libconfig,"libconfig");
}
void hpm_init(void) {
diff --git a/src/common/conf.c b/src/common/conf.c
index 813069ac4..b816b2f7f 100644
--- a/src/common/conf.c
+++ b/src/common/conf.c
@@ -7,13 +7,16 @@
#include "../common/showmsg.h" // ShowError
-int conf_read_file(config_t *config, const char *config_filename)
-{
- config_init(config);
- if (!config_read_file(config, config_filename)) {
+/* interface source */
+struct libconfig_interface libconfig_s;
+
+
+int conf_read_file(config_t *config, const char *config_filename) {
+ libconfig->init(config);
+ if (!libconfig->read_file_src(config, config_filename)) {
ShowError("%s:%d - %s\n", config_error_file(config),
config_error_line(config), config_error_text(config));
- config_destroy(config);
+ libconfig->destroy(config);
return 1;
}
return 0;
@@ -22,71 +25,63 @@ int conf_read_file(config_t *config, const char *config_filename)
//
// Functions to copy settings from libconfig/contrib
//
-static void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src);
-static void config_setting_copy_elem(config_setting_t *parent, const config_setting_t *src);
-static void config_setting_copy_aggregate(config_setting_t *parent, const config_setting_t *src);
-int config_setting_copy(config_setting_t *parent, const config_setting_t *src);
-
-void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src)
-{
+void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src) {
if (config_setting_is_aggregate(src)) {
- config_setting_copy_aggregate(parent, src);
+ libconfig->setting_copy_aggregate(parent, src);
}
else {
config_setting_t *set;
- if( config_setting_get_member(parent, config_setting_name(src)) != NULL )
+ if( libconfig->setting_get_member(parent, config_setting_name(src)) != NULL )
return;
- if ((set = config_setting_add(parent, config_setting_name(src), config_setting_type(src))) == NULL)
+ if ((set = libconfig->setting_add(parent, config_setting_name(src), config_setting_type(src))) == NULL)
return;
if (CONFIG_TYPE_INT == config_setting_type(src)) {
- config_setting_set_int(set, config_setting_get_int(src));
- config_setting_set_format(set, src->format);
+ libconfig->setting_set_int(set, libconfig->setting_get_int(src));
+ libconfig->setting_set_format(set, src->format);
} else if (CONFIG_TYPE_INT64 == config_setting_type(src)) {
- config_setting_set_int64(set, config_setting_get_int64(src));
- config_setting_set_format(set, src->format);
+ libconfig->setting_set_int64(set, libconfig->setting_get_int64(src));
+ libconfig->setting_set_format(set, src->format);
} else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
- config_setting_set_float(set, config_setting_get_float(src));
+ libconfig->setting_set_float(set, libconfig->setting_get_float(src));
} else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
- config_setting_set_string(set, config_setting_get_string(src));
+ libconfig->setting_set_string(set, libconfig->setting_get_string(src));
} else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
- config_setting_set_bool(set, config_setting_get_bool(src));
+ libconfig->setting_set_bool(set, libconfig->setting_get_bool(src));
}
}
}
-void config_setting_copy_elem(config_setting_t *parent, const config_setting_t *src)
-{
+void config_setting_copy_elem(config_setting_t *parent, const config_setting_t *src) {
config_setting_t *set = NULL;
if (config_setting_is_aggregate(src))
- config_setting_copy_aggregate(parent, src);
+ libconfig->setting_copy_aggregate(parent, src);
else if (CONFIG_TYPE_INT == config_setting_type(src)) {
- set = config_setting_set_int_elem(parent, -1, config_setting_get_int(src));
- config_setting_set_format(set, src->format);
+ set = libconfig->setting_set_int_elem(parent, -1, libconfig->setting_get_int(src));
+ libconfig->setting_set_format(set, src->format);
} else if (CONFIG_TYPE_INT64 == config_setting_type(src)) {
- set = config_setting_set_int64_elem(parent, -1, config_setting_get_int64(src));
- config_setting_set_format(set, src->format);
+ set = libconfig->setting_set_int64_elem(parent, -1, libconfig->setting_get_int64(src));
+ libconfig->setting_set_format(set, src->format);
} else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
- config_setting_set_float_elem(parent, -1, config_setting_get_float(src));
+ libconfig->setting_set_float_elem(parent, -1, libconfig->setting_get_float(src));
} else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
- config_setting_set_string_elem(parent, -1, config_setting_get_string(src));
+ libconfig->setting_set_string_elem(parent, -1, libconfig->setting_get_string(src));
} else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
- config_setting_set_bool_elem(parent, -1, config_setting_get_bool(src));
+ libconfig->setting_set_bool_elem(parent, -1, libconfig->setting_get_bool(src));
}
}
-void config_setting_copy_aggregate(config_setting_t *parent, const config_setting_t *src)
-{
+void config_setting_copy_aggregate(config_setting_t *parent, const config_setting_t *src) {
config_setting_t *newAgg;
int i, n;
- if( config_setting_get_member(parent, config_setting_name(src)) != NULL )
+ if( libconfig->setting_get_member(parent, config_setting_name(src)) != NULL )
return;
- newAgg = config_setting_add(parent, config_setting_name(src), config_setting_type(src));
+ newAgg = libconfig->setting_add(parent, config_setting_name(src), config_setting_type(src));
if (newAgg == NULL)
return;
@@ -95,22 +90,101 @@ void config_setting_copy_aggregate(config_setting_t *parent, const config_settin
for (i = 0; i < n; i++) {
if (config_setting_is_group(src)) {
- config_setting_copy_simple(newAgg, config_setting_get_elem(src, i));
+ libconfig->setting_copy_simple(newAgg, libconfig->setting_get_elem(src, i));
} else {
- config_setting_copy_elem(newAgg, config_setting_get_elem(src, i));
+ libconfig->setting_copy_elem(newAgg, libconfig->setting_get_elem(src, i));
}
}
}
-int config_setting_copy(config_setting_t *parent, const config_setting_t *src)
-{
+int config_setting_copy(config_setting_t *parent, const config_setting_t *src) {
+
if (!config_setting_is_group(parent) && !config_setting_is_list(parent))
return CONFIG_FALSE;
if (config_setting_is_aggregate(src)) {
- config_setting_copy_aggregate(parent, src);
+ libconfig->setting_copy_aggregate(parent, src);
} else {
- config_setting_copy_simple(parent, src);
+ libconfig->setting_copy_simple(parent, src);
}
return CONFIG_TRUE;
}
+
+void libconfig_defaults(void) {
+ libconfig = &libconfig_s;
+
+ libconfig->read = config_read;
+ libconfig->write = config_write;
+ /* */
+ libconfig->set_auto_convert = config_set_auto_convert;
+ libconfig->get_auto_convert = config_get_auto_convert;
+ /* */
+ libconfig->read_string = config_read_string;
+ libconfig->read_file_src = config_read_file;
+ libconfig->write_file = config_write_file;
+ /* */
+ libconfig->set_destructor = config_set_destructor;
+ libconfig->set_include_dir = config_set_include_dir;
+ /* */
+ libconfig->init = config_init;
+ libconfig->destroy = config_destroy;
+ /* */
+ libconfig->setting_get_int = config_setting_get_int;
+ libconfig->setting_get_int64 = config_setting_get_int64;
+ libconfig->setting_get_float = config_setting_get_float;
+ libconfig->setting_get_bool = config_setting_get_bool;
+ libconfig->setting_get_string = config_setting_get_string;
+ /* */
+ libconfig->setting_lookup_int = config_setting_lookup_int;
+ libconfig->setting_lookup_int64 = config_setting_lookup_int64;
+ libconfig->setting_lookup_float = config_setting_lookup_float;
+ libconfig->setting_lookup_bool = config_setting_lookup_bool;
+ libconfig->setting_lookup_string = config_setting_lookup_string;
+ /* */
+ libconfig->setting_set_int = config_setting_set_int;
+ libconfig->setting_set_int64 = config_setting_set_int64;
+ libconfig->setting_set_bool = config_setting_set_bool;
+ libconfig->setting_set_string = config_setting_set_string;
+ /* */
+ libconfig->setting_set_format = config_setting_set_format;
+ libconfig->setting_get_format = config_setting_get_format;
+ /* */
+ libconfig->setting_get_int_elem = config_setting_get_int_elem;
+ libconfig->setting_get_int64_elem = config_setting_get_int64_elem;
+ libconfig->setting_get_float_elem = config_setting_get_float_elem;
+ libconfig->setting_get_bool_elem = config_setting_get_bool_elem;
+ libconfig->setting_get_string_elem = config_setting_get_string_elem;
+ /* */
+ libconfig->setting_set_int_elem = config_setting_set_int_elem;
+ libconfig->setting_set_int64_elem = config_setting_set_int64_elem;
+ libconfig->setting_set_float_elem = config_setting_set_float_elem;
+ libconfig->setting_set_bool_elem = config_setting_set_bool_elem;
+ libconfig->setting_set_string_elem = config_setting_set_string_elem;
+ /* */
+ libconfig->setting_index = config_setting_index;
+ libconfig->setting_length = config_setting_length;
+ /* */
+ libconfig->setting_get_elem = config_setting_get_elem;
+ libconfig->setting_get_member = config_setting_get_member;
+ /* */
+ libconfig->setting_add = config_setting_add;
+ libconfig->setting_remove = config_setting_remove;
+ libconfig->setting_remove_elem = config_setting_remove_elem;
+ /* */
+ libconfig->setting_set_hook = config_setting_set_hook;
+ /* */
+ libconfig->lookup = config_lookup;
+ libconfig->lookup_from = config_lookup_from;
+ /* */
+ libconfig->lookup_int = config_lookup_int;
+ libconfig->lookup_int64 = config_lookup_int64;
+ libconfig->lookup_float = config_lookup_float;
+ libconfig->lookup_bool = config_lookup_bool;
+ libconfig->lookup_string = config_lookup_string;
+ /* those are custom and are from src/common/conf.c */
+ libconfig->read_file = conf_read_file;
+ libconfig->setting_copy_simple = config_setting_copy_simple;
+ libconfig->setting_copy_elem = config_setting_copy_elem;
+ libconfig->setting_copy_aggregate = config_setting_copy_aggregate;
+ libconfig->setting_copy = config_setting_copy;
+}
diff --git a/src/common/conf.h b/src/common/conf.h
index d223505db..05e2b1ada 100644
--- a/src/common/conf.h
+++ b/src/common/conf.h
@@ -1,5 +1,6 @@
-// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
-// For more information, see LICENCE in the main folder
+// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena Dev Teams
#ifndef _CONF_H_
#define _CONF_H_
@@ -7,7 +8,90 @@
#include "../common/cbasetypes.h"
#include "../../3rdparty/libconfig/libconfig.h"
-int conf_read_file(config_t *config, const char *config_filename);
-int config_setting_copy(config_setting_t *parent, const config_setting_t *src);
+/**
+ * The libconfig interface -- specially for plugins, but we enforce it throughought the core to be consistent
+ **/
+struct libconfig_interface {
+ int (*read) (config_t *config, FILE *stream);
+ void (*write) (const config_t *config, FILE *stream);
+ /* */
+ void (*set_auto_convert) (config_t *config, int flag);
+ int (*get_auto_convert) (const config_t *config);
+ /* */
+ int (*read_string) (config_t *config, const char *str);
+ int (*read_file_src) (config_t *config, const char *filename);
+ int (*write_file) (config_t *config, const char *filename);
+
+ void (*set_destructor) (config_t *config, void (*destructor)(void *));
+ void (*set_include_dir) (config_t *config, const char *include_dir);
+
+ void (*init) (config_t *config);
+ void (*destroy) (config_t *config);
+
+ int (*setting_get_int) (const config_setting_t *setting);
+ long long (*setting_get_int64) (const config_setting_t *setting);
+ double (*setting_get_float) (const config_setting_t *setting);
+
+ int (*setting_get_bool) (const config_setting_t *setting);
+
+ const char * (*setting_get_string) (const config_setting_t *setting);
+
+ int (*setting_lookup_int) (const config_setting_t *setting, const char *name, int *value);
+ int (*setting_lookup_int64) (const config_setting_t *setting, const char *name, long long *value);
+ int (*setting_lookup_float) (const config_setting_t *setting, const char *name, double *value);
+ int (*setting_lookup_bool) (const config_setting_t *setting, const char *name, int *value);
+ int (*setting_lookup_string) (const config_setting_t *setting, const char *name, const char **value);
+ int (*setting_set_int) (config_setting_t *setting ,int value);
+ int (*setting_set_int64) (config_setting_t *setting, long long value);
+ int (*setting_set_float) (config_setting_t *setting, double value);
+ int (*setting_set_bool) (config_setting_t *setting, int value);
+ int (*setting_set_string) (config_setting_t *setting, const char *value);
+
+ int (*setting_set_format) (config_setting_t *setting, short format);
+ short (*setting_get_format) (const config_setting_t *setting);
+
+ int (*setting_get_int_elem) (const config_setting_t *setting, int idx);
+ long long (*setting_get_int64_elem) (const config_setting_t *setting, int idx);
+ double (*setting_get_float_elem) (const config_setting_t *setting, int idx);
+ int (*setting_get_bool_elem) (const config_setting_t *setting, int idx);
+ const char * (*setting_get_string_elem) (const config_setting_t *setting, int idx);
+ config_setting_t * (*setting_set_int_elem) (config_setting_t *setting, int idx, int value);
+ config_setting_t * (*setting_set_int64_elem) (config_setting_t *setting, int idx, long long value);
+ config_setting_t * (*setting_set_float_elem) (config_setting_t *setting, int idx, double value);
+ config_setting_t * (*setting_set_bool_elem) (config_setting_t *setting, int idx, int value);
+ config_setting_t * (*setting_set_string_elem) (config_setting_t *setting, int idx, const char *value);
+
+ int (*setting_index) (const config_setting_t *setting);
+ int (*setting_length) (const config_setting_t *setting);
+
+ config_setting_t * (*setting_get_elem) (const config_setting_t *setting, unsigned int idx);
+ config_setting_t * (*setting_get_member) (const config_setting_t *setting, const char *name);
+
+ config_setting_t * (*setting_add) (config_setting_t *parent, const char *name, int type);
+ int (*setting_remove) (config_setting_t *parent, const char *name);
+
+ int (*setting_remove_elem) (config_setting_t *parent, unsigned int idx);
+ void (*setting_set_hook) (config_setting_t *setting, void *hook);
+
+ config_setting_t * (*lookup) (const config_t *config, const char *path);
+ config_setting_t * (*lookup_from) (config_setting_t *setting, const char *path);
+ int (*lookup_int) (const config_t *config, const char *path, int *value);
+ int (*lookup_int64) (const config_t *config, const char *path, long long *value);
+ int (*lookup_float) (const config_t *config, const char *path, double *value);
+ int (*lookup_bool) (const config_t *config, const char *path, int *value);
+ int (*lookup_string) (const config_t *config, const char *path, const char **value);
+
+ /* those are custom and are from src/common/conf.c */
+ /* Functions to copy settings from libconfig/contrib */
+ int (*read_file) (config_t *config, const char *config_filename);
+ void (*setting_copy_simple) (config_setting_t *parent, const config_setting_t *src);
+ void (*setting_copy_elem) (config_setting_t *parent, const config_setting_t *src);
+ void (*setting_copy_aggregate) (config_setting_t *parent, const config_setting_t *src);
+ int (*setting_copy) (config_setting_t *parent, const config_setting_t *src);
+};
+
+struct libconfig_interface *libconfig;
+
+void libconfig_defaults(void);
#endif // _CONF_H_
diff --git a/src/common/core.c b/src/common/core.c
index 8178a48a5..5c1e58801 100644
--- a/src/common/core.c
+++ b/src/common/core.c
@@ -19,6 +19,7 @@
#include "../config/core.h"
#include "../common/HPM.h"
#include "../common/utils.h"
+ #include "../common/conf.h"
#endif
#include <stdio.h>
@@ -282,6 +283,7 @@ void core_defaults(void) {
strlib_defaults();
malloc_defaults();
#ifndef MINICORE
+ libconfig_defaults();
sql_defaults();
timer_defaults();
db_defaults();