[atomix/wip/kill-libxml] Ported level sequence reading to GMarkup
- From: Robert Roth <robertroth src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [atomix/wip/kill-libxml] Ported level sequence reading to GMarkup
- Date: Sat, 17 Jan 2015 23:12:57 +0000 (UTC)
commit 63a17c6263bb84652870ef171ccdb1a96c685e24
Author: Robert Roth <robert roth off gmail com>
Date: Sun Jan 18 01:12:44 2015 +0200
Ported level sequence reading to GMarkup
src/Makefile.am | 1 +
src/level-manager.c | 83 ++++++++++++++++++++++++++++++++++++---------------
src/theme-manager.c | 25 +--------------
src/xml-util.c | 43 ++++++++++++++++++++++++++
src/xml-util.h | 34 +++++++++++++++++++++
5 files changed, 139 insertions(+), 47 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index f756f2c..79ddfac 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -34,6 +34,7 @@ noinst_LIBRARIES = libatomix.a
libatomix_a_SOURCES = \
tile.c tile.h \
+ xml-util.c xml-util.h \
theme.c theme.h theme-private.h \
theme-manager.c theme-manager.h \
level.c level.h level-private.h \
diff --git a/src/level-manager.c b/src/level-manager.c
index 8cac4df..56f872a 100644
--- a/src/level-manager.c
+++ b/src/level-manager.c
@@ -27,6 +27,7 @@
#include "level-manager.h"
#include "level-private.h"
+#include "xml-util.h"
static void search_level_in_dir (LevelManager *lm, gchar *dir_path);
static gchar *lookup_level_name (gchar *filename);
@@ -114,6 +115,44 @@ LevelManager *level_manager_new (void)
return lm;
}
+static void sequence_parser_error (GMarkupParseContext *context,
+ GError *error,
+ gpointer user_data)
+{
+ g_print ("Error while parsing level sequence: %s\n", error->message);
+}
+
+static void
+sequence_parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error)
+{
+ const gchar *prop_value;
+ LevelManager *lm = LEVEL_MANAGER (user_data);
+ LevelManagerPrivate *priv = lm->priv;
+
+ if (!g_strcmp0 (element_name, "level")) {
+ prop_value = get_attribute_value ("name", attribute_names, attribute_values);
+ lm->priv->level_seq = g_list_append (lm->priv->level_seq,
+ g_strdup (prop_value));
+ } else if (g_strcmp0 (element_name, "levelsequence") &&
+ g_strcmp0 (element_name, "text")) {
+ g_warning ("Ignoring sequence xml tag: %s", element_name);
+ }
+}
+
+static GMarkupParser sequence_parser =
+{
+ sequence_parser_start_element,
+ NULL,
+ NULL,
+ NULL,
+ xml_parser_log_error
+};
+
static void create_level_sequence (LevelManager *lm, gchar *file)
{
xmlDocPtr doc;
@@ -121,33 +160,29 @@ static void create_level_sequence (LevelManager *lm, gchar *file)
g_return_if_fail (IS_LEVEL_MANAGER (lm));
- if (!g_file_test (file, G_FILE_TEST_IS_REGULAR))
- return;
+ GFile *sequence_file;
+ gchar *sequence_contents;
+ gsize sequence_length;
+ GMarkupParseContext *parse_context;
- doc = xmlParseFile (file);
- if (doc == NULL)
+ if (!g_file_test (file, G_FILE_TEST_IS_REGULAR)) {
+ g_warning ("File not found: %s.", file);
return;
+ }
+
+ sequence_file = g_file_new_for_path (file);
+ if (g_file_load_contents (sequence_file, NULL, &sequence_contents, &sequence_length, NULL, NULL)) {
+ parse_context = g_markup_parse_context_new (&sequence_parser,
+ G_MARKUP_TREAT_CDATA_AS_TEXT,
+ lm,
+ NULL);
+ g_markup_parse_context_parse (parse_context, sequence_contents, sequence_length, NULL);
+ g_markup_parse_context_unref (parse_context);
+ g_free (sequence_contents);
+ }
+
+ g_object_unref (sequence_file);
- node = doc->xmlRootNode;
- if (!g_ascii_strcasecmp (node->name, "levelsequence"))
- {
- for (node = node->xmlChildrenNode; node != NULL; node = node->next)
- {
- if (!g_ascii_strcasecmp (node->name, "level"))
- {
- lm->priv->level_seq = g_list_append (lm->priv->level_seq,
- g_strdup (xmlGetProp
- (node, "name")));
- }
- else if (!g_ascii_strcasecmp (node->name, "text"))
- {
- }
- else
- {
- g_warning ("Ignoring unknown xml tag: %s", node->name);
- }
- }
- }
}
void level_manager_init_levels (LevelManager *lm)
diff --git a/src/theme-manager.c b/src/theme-manager.c
index 04700df..78bb497 100644
--- a/src/theme-manager.c
+++ b/src/theme-manager.c
@@ -23,6 +23,7 @@
#include "theme-manager.h"
#include "theme-private.h"
+#include "xml-util.h"
static GObjectClass *parent_class = NULL;
@@ -42,28 +43,6 @@ struct _ThemeManagerPrivate
GHashTable *themes;
};
-static const gchar* get_attribute_value (const gchar *attribute,
- const gchar **attribute_names,
- const gchar **attribute_values)
-{
- gint i = 0;
-
- while (attribute_names[i]) {
- if (!g_strcmp0 (attribute_names[i], attribute))
- return attribute_values[i];
- i++;
- }
-
- return NULL;
-}
-
-static void theme_parser_error (GMarkupParseContext *context,
- GError *error,
- gpointer user_data)
-{
- g_print ("Error while parsing theme\n");
-}
-
static void
theme_parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
@@ -154,7 +133,7 @@ static GMarkupParser theme_parser =
NULL,
NULL,
NULL,
- theme_parser_error
+ xml_parser_log_error
};
GType theme_manager_get_type (void)
diff --git a/src/xml-util.c b/src/xml-util.c
new file mode 100644
index 0000000..933f0dc
--- /dev/null
+++ b/src/xml-util.c
@@ -0,0 +1,43 @@
+/* Atomix -- a little puzzle game about atoms and molecules.
+ * Copyright (C) 2001 Jens Finke
+ * Copyright (C) 2005 Guilherme de S. Pastore
+ * Copyright (C) 2015 Robert Roth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "xml-util.h"
+
+const gchar* get_attribute_value (const gchar *attribute,
+ const gchar **attribute_names,
+ const gchar **attribute_values)
+{
+ gint i = 0;
+
+ while (attribute_names[i]) {
+ if (!g_strcmp0 (attribute_names[i], attribute))
+ return attribute_values[i];
+ i++;
+ }
+
+ return NULL;
+}
+
+void xml_parser_log_error (GMarkupParseContext *context,
+ GError *error,
+ gpointer user_data)
+{
+ g_print ("Error while parsing XML: %s with user data of type %s\n", error->message, G_OBJECT_TYPE_NAME
(user_data));
+}
diff --git a/src/xml-util.h b/src/xml-util.h
new file mode 100644
index 0000000..e634d0f
--- /dev/null
+++ b/src/xml-util.h
@@ -0,0 +1,34 @@
+/* Atomix -- a little puzzle game about atoms and molecules.
+ * Copyright (C) 2001 Jens Finke
+ * Copyright (C) 2005 Guilherme de S. Pastore
+ * Copyright (C) 2015 Robert Roth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _ATOMIX_XML_UTIL_H_
+#define _ATOMIX_XML_UTIL_H_
+
+#include <glib-object.h>
+
+const gchar* get_attribute_value (const gchar *attribute,
+ const gchar **attribute_names,
+ const gchar **attribute_values);
+
+void xml_parser_log_error (GMarkupParseContext *context,
+ GError *error,
+ gpointer user_data);
+
+#endif //_ATOMIX_XML_UTIL_H_
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]