summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Makefile.in30
-rw-r--r--src/common/conf.c109
-rw-r--r--src/common/conf.h13
-rw-r--r--src/common/showmsg.c16
-rw-r--r--src/common/showmsg.h3
5 files changed, 162 insertions, 9 deletions
diff --git a/src/common/Makefile.in b/src/common/Makefile.in
index 8dab1d816..7897b6cfb 100644
--- a/src/common/Makefile.in
+++ b/src/common/Makefile.in
@@ -2,11 +2,13 @@
COMMON_OBJ = obj_all/core.o obj_all/socket.o obj_all/timer.o obj_all/db.o obj_all/plugins.o obj_all/lock.o \
obj_all/nullpo.o obj_all/malloc.o obj_all/showmsg.o obj_all/strlib.o obj_all/utils.o \
obj_all/grfio.o obj_all/mapindex.o obj_all/ers.o obj_all/md5calc.o \
- obj_all/minicore.o obj_all/minisocket.o obj_all/minimalloc.o obj_all/random.o obj_all/des.o
+ obj_all/minicore.o obj_all/minisocket.o obj_all/minimalloc.o obj_all/random.o obj_all/des.o \
+ obj_all/conf.o
COMMON_H = mmo.h plugin.h version.h \
core.h socket.h timer.h db.h plugins.h lock.h \
nullpo.h malloc.h showmsg.h strlib.h utils.h \
- grfio.h mapindex.h ers.h md5calc.h random.h des.h
+ grfio.h mapindex.h ers.h md5calc.h random.h des.h \
+ conf.h
COMMON_SQL_OBJ = obj_sql/sql.o
COMMON_SQL_H = sql.h
@@ -15,6 +17,13 @@ MT19937AR_OBJ = ../../3rdparty/mt19937ar/mt19937ar.o
MT19937AR_H = ../../3rdparty/mt19937ar/mt19937ar.h
MT19937AR_INCLUDE = -I../../3rdparty/mt19937ar
+LIBCONFIG_OBJ = ../../3rdparty/libconfig/libconfig.o ../../3rdparty/libconfig/grammar.o \
+ ../../3rdparty/libconfig/scanctx.o ../../3rdparty/libconfig/scanner.o ../../3rdparty/libconfig/strbuf.o
+LIBCONFIG_H = ../../3rdparty/libconfig/libconfig.h ../../3rdparty/libconfig/grammar.h \
+ ../../3rdparty/libconfig/parsectx.h ../../3rdparty/libconfig/scanctx.h ../../3rdparty/libconfig/scanner.h \
+ ../../3rdparty/libconfig/strbuf.h ../../3rdparty/libconfig/wincompat.h
+LIBCONFIG_INCLUDE = -I../../3rdparty/libconfig
+
HAVE_MYSQL=@HAVE_MYSQL@
ifeq ($(HAVE_MYSQL),yes)
ALL_DEPENDS=txt sql
@@ -59,21 +68,24 @@ obj_all:
obj_sql:
-mkdir obj_sql
-common: obj_all $(COMMON_OBJ) $(MT19937AR_OBJ)
+common: obj_all $(COMMON_OBJ) $(MT19937AR_OBJ) $(LIBCONFIG_OBJ)
common_sql: obj_sql $(COMMON_SQL_OBJ)
-obj_all/%.o: %.c $(COMMON_H) $(MT19937AR_H)
- @CC@ @CFLAGS@ $(MT19937AR_INCLUDE) @LDFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
+obj_all/%.o: %.c $(COMMON_H) $(MT19937AR_H) $(LIBCONFIG_H)
+ @CC@ @CFLAGS@ $(MT19937AR_INCLUDE) $(LIBCONFIG_INCLUDE) @LDFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
-obj_all/mini%.o: %.c $(COMMON_H) $(MT19937AR_H)
- @CC@ @CFLAGS@ $(MT19937AR_INCLUDE) -DMINICORE @LDFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
+obj_all/mini%.o: %.c $(COMMON_H) $(MT19937AR_H) $(LIBCONFIG_H)
+ @CC@ @CFLAGS@ $(MT19937AR_INCLUDE) $(LIBCONFIG_INCLUDE) -DMINICORE @LDFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
-obj_sql/%.o: %.c $(COMMON_H) $(COMMON_SQL_H)
- @CC@ @CFLAGS@ @MYSQL_CFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
+obj_sql/%.o: %.c $(COMMON_H) $(COMMON_SQL_H) $(LIBCONFIG_H)
+ @CC@ @CFLAGS@ $(LIBCONFIG_INCLUDE) @MYSQL_CFLAGS@ @CPPFLAGS@ -c $(OUTPUT_OPTION) $<
# missing object files
MT19937AR_OBJ:
@$(MAKE) -C ../../3rdparty/mt19937ar
+
+LIBCONFIG_OBJ:
+ @$(MAKE) -C ../../3rdparty/libconfig
diff --git a/src/common/conf.c b/src/common/conf.c
new file mode 100644
index 000000000..eed39d409
--- /dev/null
+++ b/src/common/conf.c
@@ -0,0 +1,109 @@
+// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
+// For more information, see LICENCE in the main folder
+
+#include "conf.h"
+#include <libconfig.h>
+
+#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)) {
+ ShowError("%s:%d - %s\n", config_error_file(config),
+ config_error_line(config), config_error_text(config));
+ config_destroy(config);
+ return 1;
+ }
+ return 0;
+}
+
+//
+// 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)
+{
+ if (config_setting_is_aggregate(src)) {
+ config_setting_copy_aggregate(parent, src);
+ }
+ else {
+ config_setting_t *set = config_setting_add(parent, config_setting_name(src), config_setting_type(src));
+
+ if (set == 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);
+ } 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);
+ } else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
+ config_setting_set_float(set, config_setting_get_float(src));
+ } else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
+ config_setting_set_string(set, config_setting_get_string(src));
+ } else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
+ config_setting_set_bool(set, config_setting_get_bool(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);
+ 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);
+ } 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);
+ } else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) {
+ set = config_setting_set_float_elem(parent, -1, config_setting_get_float(src));
+ } else if (CONFIG_TYPE_STRING == config_setting_type(src)) {
+ set = config_setting_set_string_elem(parent, -1, config_setting_get_string(src));
+ } else if (CONFIG_TYPE_BOOL == config_setting_type(src)) {
+ set = config_setting_set_bool_elem(parent, -1, config_setting_get_bool(src));
+ }
+}
+
+void config_setting_copy_aggregate(config_setting_t *parent, const config_setting_t *src)
+{
+ config_setting_t *newAgg;
+ int i, n;
+
+ newAgg = config_setting_add(parent, config_setting_name(src), config_setting_type(src));
+
+ if (newAgg == NULL)
+ return;
+
+ n = config_setting_length(src);
+
+ for (i = 0; i < n; i++) {
+ if (config_setting_is_group(src)) {
+ config_setting_copy_simple(newAgg, config_setting_get_elem(src, i));
+ } else {
+ config_setting_copy_elem(newAgg, config_setting_get_elem(src, i));
+ }
+ }
+}
+
+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);
+ } else {
+ config_setting_copy_simple(parent, src);
+ }
+ return CONFIG_TRUE;
+}
diff --git a/src/common/conf.h b/src/common/conf.h
new file mode 100644
index 000000000..0b70a0c79
--- /dev/null
+++ b/src/common/conf.h
@@ -0,0 +1,13 @@
+// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
+// For more information, see LICENCE in the main folder
+
+#ifndef _CONF_H_
+#define _CONF_H_
+
+#include "../common/cbasetypes.h"
+#include <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);
+
+#endif // _CONF_H_ \ No newline at end of file
diff --git a/src/common/showmsg.c b/src/common/showmsg.c
index 7a6f632e7..b77bf3949 100644
--- a/src/common/showmsg.c
+++ b/src/common/showmsg.c
@@ -13,6 +13,8 @@
#include <time.h>
#include <stdlib.h> // atexit
+#include <libconfig.h>
+
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
@@ -872,6 +874,20 @@ int ShowWarning(const char *string, ...) {
va_end(ap);
return ret;
}
+int ShowConfigWarning(config_setting_t *config, const char *string, ...)
+{
+ StringBuf buf;
+ int ret;
+ va_list ap;
+ StringBuf_Init(&buf);
+ StringBuf_AppendStr(&buf, string);
+ StringBuf_Printf(&buf, " (%s:%d)\n", config_setting_source_file(config), config_setting_source_line(config));
+ va_start(ap, string);
+ ret = _vShowMessage(MSG_WARNING, StringBuf_Value(&buf), ap);
+ va_end(ap);
+ StringBuf_Destroy(&buf);
+ return ret;
+}
int ShowDebug(const char *string, ...) {
int ret;
va_list ap;
diff --git a/src/common/showmsg.h b/src/common/showmsg.h
index 5f80a4312..839f1dc7e 100644
--- a/src/common/showmsg.h
+++ b/src/common/showmsg.h
@@ -4,6 +4,8 @@
#ifndef _SHOWMSG_H_
#define _SHOWMSG_H_
+#include <libconfig.h>
+
// for help with the console colors look here:
// http://www.edoceo.com/liberum/?doc=printf-with-color
// some code explanation (used here):
@@ -92,5 +94,6 @@ extern int ShowWarning(const char *, ...);
extern int ShowDebug(const char *, ...);
extern int ShowError(const char *, ...);
extern int ShowFatalError(const char *, ...);
+extern int ShowConfigWarning(config_setting_t *config, const char *string, ...);
#endif /* _SHOWMSG_H_ */