Start parsing options in the new way

This commit is contained in:
Gergely Polonkai 2016-07-04 17:18:58 +02:00
parent 3059bde5f0
commit 6eb1e09cc3
4 changed files with 175 additions and 52 deletions

View File

@ -32,7 +32,10 @@ wmud_SOURCES = \
enumtypes.h \
enumtypes.c \
wmudworld.c \
wmudworld.h
wmudworld.h \
wmud-configuration.c \
wmud-configuration.h \
$(NULL)
wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(GDA_LIBS) $(CURL_LIBS)

View File

@ -39,6 +39,8 @@
#include "menu.h"
#include "texts.h"
#include "wmud-configuration.h"
/**
* SECTION:utils
* @short_description: Utilities and uncategorized functions
@ -193,16 +195,32 @@ wmud_logger(const gchar *log_domain,
int
main(int argc, char **argv)
{
GError *err = NULL;
GThread *game_thread;
GMainContext *game_context;
GError *err = NULL;
GSList *game_menu = NULL;
WmudConfiguration *current_config = NULL;
gchar *filename = NULL;
g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, wmud_logger, NULL);
/* TODO: Command line parsing */
current_config = wmud_configuration_new();
// Process command line options
wmud_configuration_update_from_cmdline(
current_config,
&argc, &argv,
NULL);
/* TODO: Create signal handlers! */
if ((filename = wmud_configuration_get_filename(current_config)) == NULL) {
filename = WMUD_CONFDIR "/wmud.conf";
}
// Process configuration file
wmud_configuration_update_from_file(current_config, filename, NULL);
if (!wmud_config_init(&active_config, &err)) {
if (err) {
g_critical("Config file parsing error: %s", err->message);

View File

@ -2,19 +2,21 @@
typedef struct _WmudConfigurationPrivate {
gchar *file_name;
guint port;
gchar *database_dsn;
GKeyFile *key_file;
gchar *admin_email;
gchar *smtp_server;
gboolean smtp_tls;
gchar *smtp_username;
gchar *smtp_password;
gchar *smtp_sender;
gboolean *hide_single_race;
gboolean *hide_single_class;
guint *house_occupy_time;
guint *minimum_deities;
gboolean *clan_wars;
guint *maximum_group_size;
guint *trainable_abilities;
gboolean *reborn;
} WmudConfigurationPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(WmudConfiguration,
wmud_configuration,
G_TYPE_KEY_FILE);
G_TYPE_OBJECT);
static void
wmud_configuration_finalize(GObject *gobject)
@ -34,8 +36,97 @@ wmud_configuration_class_init(WmudConfigurationClass *klass)
static void
wmud_configuration_init(WmudConfiguration *configuration)
{
WmudConfigurationPrivate *priv = wmud_configuration_get_instance_private(
configuration);
priv->key_file = NULL;
priv->file_name = NULL;
priv->admin_email = NULL;
priv->hide_single_race = NULL;
priv->hide_single_class = NULL;
priv->house_occupy_time = NULL;
priv->minimum_deities = NULL;
priv->clan_wars = NULL;
priv->maximum_group_size = NULL;
priv->trainable_abilities = NULL;
priv->reborn = NULL;
}
void
wmud_configuration_update_from_cmdline(WmudConfiguration *configuration,
gint *argc,
gchar **argv[],
GError **error)
{
WmudConfigurationPrivate *priv = wmud_configuration_get_instance_private(
configuration);
GOptionEntry entries[] = {
{
"config-file", 'c',
0,
G_OPTION_ARG_FILENAME,
&(priv->file_name),
"The name of the configuration file to parse",
"FILE"
},
{NULL}
};
GError *err = NULL;
GOptionContext *context;
context = g_option_context_new("- Yet Another MUD Engine");
g_option_context_add_main_entries(context, entries, NULL);
if (!g_option_context_parse(context, argc, argv, &err)) {
g_print("Option parsing failed: %s\n", err->message);
g_object_unref(configuration);
// TODO: Update error!
return;
}
g_print("Config file: %s\n", priv->file_name);
}
void
wmud_configuration_update_from_file(WmudConfiguration *configuration,
gchar *filename,
GError **error)
{
WmudConfigurationPrivate *priv = wmud_configuration_get_instance_private(
configuration);
// Save the file name for possible later use
priv->file_name = g_strdup(filename);
priv->key_file = g_key_file_new();
if (!g_key_file_load_from_file(priv->key_file,
filename,
G_KEY_FILE_NONE,
error)) {
return;
}
}
WmudConfiguration *
wmud_configuration_new(gchar *filename)
{}
wmud_configuration_new(void)
{
WmudConfiguration *configuration = g_object_new(
WMUD_TYPE_CONFIGURATION, NULL);
// TODO: Update with built-in defaults
return configuration;
}
gchar *
wmud_configuration_get_filename(WmudConfiguration *configuration)
{
WmudConfigurationPrivate *priv = wmud_configuration_get_instance_private(
configuration);
return priv->file_name;
}

View File

@ -1,5 +1,5 @@
#ifndef __WMUD_CONFIGURATION_H__
#define __WMUD_CONFIGURATION_H__
#ifndef __WMUD_WMUD_CONFIGURATION_H__
#define __WMUD_WMUD_CONFIGURATION_H__
#include <glib-object.h>
@ -17,17 +17,28 @@ typedef struct _WmudConfigurationClass WmudConfigurationClass;
struct _WmudConfiguration {
/* Parent instance structure */
GKeyFile parent_instance;
GObject parent_instance;
/* Instance members */
};
struct _WmudConfigurationClass {
GKeyFileClass parent_class;
GObjectClass parent_class;
};
GType wmud_configuration_get_type(void) G_GNUC_CONST;
WmudConfiguration *wmud_configuration_new(void);
void wmud_configuration_update_from_cmdline(WmudConfiguration *configuration,
gint *argc,
gchar **argv[],
GError **error);
void wmud_configuration_update_from_file(WmudConfiguration *configuration,
gchar *filename,
GError **error);
gchar *wmud_configuration_get_filename(WmudConfiguration *configuration);
G_END_DECLS
#endif /* __WMUD_CONFIGURATION_H__ */
#endif /* __WMUD_WMUD_CONFIGURATION_H__ */