[atomix/wip/kill-libxml] Stub level-parser with sub-parsers
- From: Robert Roth <robertroth src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [atomix/wip/kill-libxml] Stub level-parser with sub-parsers
- Date: Mon, 30 Mar 2015 04:20:58 +0000 (UTC)
commit 42ff9b9f2afd585e774e722c8bbf8eff67f73524
Author: Robert Roth <robert roth off gmail com>
Date: Sun Mar 29 16:45:38 2015 +0300
Stub level-parser with sub-parsers
src/Makefile.am | 1 +
src/level-manager.c | 34 ++++++++++++++++++++++++
src/level.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++
src/level.h | 13 ++++++++-
src/playfield.c | 54 ++++++++++++++++++++++++++++++++++++++
src/playfield.h | 21 +++++++++++++++
src/tile.c | 29 +++++++++++++++++++++
src/tile.h | 21 +++++++++++++++
8 files changed, 243 insertions(+), 1 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 79ddfac..55bd8a3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -46,6 +46,7 @@ pf_test_SOURCES = \
playfield.h playfield.c \
tile.c tile.h \
theme.c theme.h \
+ xml-util.c xml-util.h \
pf-test.c
pf_test_LDADD = \
diff --git a/src/level-manager.c b/src/level-manager.c
index 10fe29c..5433594 100644
--- a/src/level-manager.c
+++ b/src/level-manager.c
@@ -115,6 +115,11 @@ LevelManager *level_manager_new (void)
return lm;
}
+/*=================================================================
+
+ Level_Manager level sequence parsing
+
+ ---------------------------------------------------------------*/
static void
sequence_parser_start_element (GMarkupParseContext *context,
const gchar *element_name,
@@ -146,6 +151,15 @@ static GMarkupParser sequence_parser =
xml_parser_log_error
};
+static GMarkupParser level_parser =
+{
+ level_parser_start_element,
+ level_parser_end_element,
+ NULL,
+ NULL,
+ xml_parser_log_error
+};
+
static void create_level_sequence (LevelManager *lm, gchar *file)
{
g_return_if_fail (IS_LEVEL_MANAGER (lm));
@@ -364,6 +378,12 @@ static Level *load_level (gchar *filename)
xmlNodePtr node;
Level *level = NULL;
gchar *prop_value;
+ // declarations for markup-based-loading
+ GFile *level_file;
+ gchar *level_contents;
+ gsize level_length;
+ GMarkupParseContext *parse_context;
+ Level *level2 = NULL;
g_return_val_if_fail (filename != NULL, NULL);
@@ -373,6 +393,20 @@ static Level *load_level (gchar *filename)
return NULL;
}
+ // markup-based loading here
+ level_file = g_file_new_for_path (filename);
+ if (g_file_load_contents (level_file, NULL, &level_contents, &level_length, NULL, NULL)) {
+ level2 = level_new ();
+ parse_context = g_markup_parse_context_new (&level_parser,
+ G_MARKUP_TREAT_CDATA_AS_TEXT,
+ level2,
+ NULL);
+ g_markup_parse_context_parse (parse_context, level_contents, level_length, NULL);
+ g_markup_parse_context_unref (parse_context);
+ g_free (level_contents);
+ }
+
+ // xml-based loading starts here
doc = xmlParseFile (filename);
if (doc == NULL)
diff --git a/src/level.c b/src/level.c
index 7981674..530da98 100644
--- a/src/level.c
+++ b/src/level.c
@@ -24,6 +24,7 @@
#include <dirent.h>
#include "level.h"
+#include "xml-util.h"
#include "level-private.h"
static void level_class_init (GObjectClass *class);
@@ -169,3 +170,73 @@ PlayField *level_get_scenario (Level *level)
return level->priv->scenario;
}
+static GMarkupParser playfield_parser =
+{
+ playfield_parser_start_element,
+ playfield_parser_end_element,
+ playfield_parser_text,
+ NULL,
+ xml_parser_log_error
+};
+
+void
+level_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;
+ Level *level = LEVEL (user_data);
+ PlayField *playfield = NULL;
+
+ printf ("starting %s\n", element_name);
+ if (!g_strcmp0 (element_name, "level"))
+ {
+ prop_value = get_attribute_value ("_name", attribute_names, attribute_values);
+ printf ("Level name is %s, level is %p\n", prop_value, level);
+ level->priv->name = g_strdup (prop_value);
+
+ prop_value = get_attribute_value ("formula", attribute_names, attribute_values);
+ level->priv->formula = g_strdup (prop_value);
+ } else if (!g_strcmp0 (element_name, "environment") ||
+ !g_strcmp0 (element_name, "goal") ||
+ !g_strcmp0 (element_name, "scenario"))
+ {
+ playfield = playfield_new ();
+ printf("pushind subparser\n");
+ g_markup_parse_context_push (context, &playfield_parser, playfield);
+ }
+
+}
+
+void
+level_parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error)
+{
+ PlayField* playfield = NULL;
+ Level *level = LEVEL (user_data);
+ LevelPrivate *priv = level->priv;
+
+ if (!g_strcmp0 (element_name, "environment") ||
+ !g_strcmp0 (element_name, "goal") ||
+ !g_strcmp0 (element_name, "scenario"))
+ {
+ printf ("ending %s\n", element_name);
+ playfield = g_markup_parse_context_pop (context);
+ if (!g_strcmp0 (element_name, "environment"))
+ {
+ level->priv->environment = playfield;
+ } else if (!g_strcmp0 (element_name, "scenario"))
+ {
+ level->priv->scenario = playfield;
+ } else if (!g_strcmp0 (element_name, "goal"))
+ {
+ level->priv->goal = playfield;
+ }
+ }
+}
+
diff --git a/src/level.h b/src/level.h
index 46e6041..2780795 100644
--- a/src/level.h
+++ b/src/level.h
@@ -53,5 +53,16 @@ PlayField* level_get_scenario (Level *level);
PlayField* level_get_goal (Level *level);
-
+void
+level_parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error);
+void
+level_parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error);
#endif /* _ATOMIX_LEVEL_H */
diff --git a/src/playfield.c b/src/playfield.c
index 99cab3c..80f99f3 100644
--- a/src/playfield.c
+++ b/src/playfield.c
@@ -17,6 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "playfield.h"
+#include "xml-util.h"
#include <string.h>
@@ -818,3 +819,56 @@ PlayField *playfield_generate_shadow (PlayField *pf)
return env_pf;
}
+
+static GMarkupParser tile_parser =
+{
+ tile_parser_start_element,
+ tile_parser_end_element,
+ tile_parser_text,
+ NULL,
+ xml_parser_log_error
+};
+
+void
+playfield_parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error)
+{
+ Tile *tile = NULL;
+
+ printf ("playfield: starting %s\n", element_name);
+ if (!g_strcmp0 (element_name, "tile"))
+ {
+ tile = tile_new (TILE_TYPE_UNKNOWN);
+ printf("pushing subparser for tile\n");
+ g_markup_parse_context_push (context, &tile_parser, tile);
+ }
+}
+
+void
+playfield_parser_text (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error)
+{
+ printf ("playfield: text %s\n", text);
+}
+
+void
+playfield_parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error)
+{
+ Tile *tile = NULL;
+
+ printf ("playfield: ending %s\n", element_name);
+ if (!g_strcmp0 (element_name, "tile"))
+ {
+ tile = g_markup_parse_context_pop (context);
+ }
+}
diff --git a/src/playfield.h b/src/playfield.h
index 9933a60..b25c76e 100644
--- a/src/playfield.h
+++ b/src/playfield.h
@@ -82,4 +82,25 @@ PlayField *playfield_generate_environment (PlayField * pf, Theme * theme);
PlayField *playfield_generate_shadow (PlayField * pf);
+void
+playfield_parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error);
+
+void
+playfield_parser_text (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error);
+
+void
+playfield_parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error);
+
#endif /* _ATOMIX_PLAY_FIELD_H */
diff --git a/src/tile.c b/src/tile.c
index d4545a2..da60971 100644
--- a/src/tile.c
+++ b/src/tile.c
@@ -366,3 +366,32 @@ Tile *tile_new_from_xml (xmlNodePtr node)
return tile;
}
+void
+tile_parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error)
+{
+// printf ("tile: text %s\n", element_name);
+}
+
+void
+tile_parser_text (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error)
+{
+// printf ("tile: text %s\n", text);
+}
+
+void
+tile_parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error)
+{
+// printf ("tile: ending %s\n", element_name);
+}
diff --git a/src/tile.h b/src/tile.h
index f4a3bb8..8166625 100644
--- a/src/tile.h
+++ b/src/tile.h
@@ -88,4 +88,25 @@ void tile_print (Tile * tile);
gboolean tile_is_equal (Tile * tile, Tile * comp);
+void
+tile_parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **attribute_names,
+ const gchar **attribute_values,
+ gpointer user_data,
+ GError **error);
+
+void
+tile_parser_text (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error);
+
+void
+tile_parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error);
+
#endif /* _TILE_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]