[atomix/wip/kill-libxml] Implemented parsing for new level format



commit a908dbd7a151dcb0342d0b7d219d09fb2dab06ac
Author: Robert Roth <robert roth off gmail com>
Date:   Mon Mar 30 07:20:35 2015 +0300

    Implemented parsing for new level format

 src/level-manager.c |    6 +-
 src/level.c         |   14 +++--
 src/playfield.c     |  184 ++++++++++++++++++++++++++++++++++-----------------
 src/tile.c          |   23 ++-----
 4 files changed, 141 insertions(+), 86 deletions(-)
---
diff --git a/src/level-manager.c b/src/level-manager.c
index 5433594..8805e7c 100644
--- a/src/level-manager.c
+++ b/src/level-manager.c
@@ -436,18 +436,18 @@ static Level *load_level (gchar *filename)
          if (!g_ascii_strcasecmp (node->name, "environment"))
            {
              level->priv->environment =
-               playfield_new_from_xml (node->xmlChildrenNode);
+               playfield_new_from_xml (node);
            }
 
          else if (!g_ascii_strcasecmp (node->name, "goal"))
            {
              level->priv->goal =
-               playfield_new_from_xml (node->xmlChildrenNode);
+               playfield_new_from_xml (node);
            }
          else if (!g_ascii_strcasecmp (node->name, "scenario"))
            {
              level->priv->scenario =
-               playfield_new_from_xml (node->xmlChildrenNode);
+               playfield_new_from_xml (node);
            }
          else if (!g_ascii_strcasecmp (node->name, "text"))
            {
diff --git a/src/level.c b/src/level.c
index 530da98..46a8471 100644
--- a/src/level.c
+++ b/src/level.c
@@ -190,12 +190,12 @@ level_parser_start_element (GMarkupParseContext  *context,
   const gchar *prop_value;
   Level *level = LEVEL (user_data);
   PlayField *playfield = NULL;
+  guint rows = 0;
+  guint cols = 0;
 
-  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);
@@ -205,7 +205,13 @@ level_parser_start_element (GMarkupParseContext  *context,
              !g_strcmp0 (element_name, "scenario"))
   {
     playfield = playfield_new ();
-    printf("pushind subparser\n");
+    prop_value = get_attribute_value ("n_rows", attribute_names, attribute_values);
+    rows = (guint) atoi (prop_value);
+    prop_value = get_attribute_value ("n_columns", attribute_names, attribute_values);
+    cols = (guint) atoi (prop_value);
+
+    playfield_set_matrix_size (playfield, rows, cols);
+
     g_markup_parse_context_push (context, &playfield_parser, playfield);
   }
   
@@ -219,13 +225,11 @@ level_parser_end_element (GMarkupParseContext  *context,
 {
   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"))
     {
diff --git a/src/playfield.c b/src/playfield.c
index 80f99f3..859b057 100644
--- a/src/playfield.c
+++ b/src/playfield.c
@@ -30,6 +30,18 @@ static GObjectClass *parent_class = NULL;
 static void playfield_class_init (GObjectClass *class);
 static void playfield_init (PlayField *pf);
 static void playfield_finalize (GObject *object);
+static void
+position_parser_start_element (GMarkupParseContext  *context,
+                               const gchar          *element_name,
+                               const gchar         **attribute_names,
+                               const gchar         **attribute_values,
+                               gpointer              user_data,
+                               GError              **error);
+static void
+position_parser_end_element (GMarkupParseContext  *context,
+                             const gchar          *element_name,
+                             gpointer              user_data,
+                             GError              **error);
 
 struct _PlayFieldPrivate
 {
@@ -38,6 +50,13 @@ struct _PlayFieldPrivate
   Tile **matrix;
 };
 
+typedef struct
+{
+  guint row;
+  guint col;
+  Tile *tile;
+} TileData;
+
 GType playfield_get_type (void)
 {
   static GType object_type = 0;
@@ -131,6 +150,8 @@ void set_tile (PlayField *pf, gint row, gint col, Tile *new_tile)
 
   g_return_if_fail (IS_PLAYFIELD (pf));
 
+  g_return_if_fail (row < pf->priv->n_rows || col < pf->priv->n_cols);
+
   priv = pf->priv;
 
   tile = get_tile (pf, row, col);
@@ -219,21 +240,21 @@ void playfield_set_matrix_size (PlayField *pf, guint n_rows, guint n_cols)
     {
       // free the left over tiles;
       for (row = 0; row < old_n_rows; row++)
-       {
-         for (col = 0; col < old_n_cols; col++)
-           {
-             Tile *tile = get_tile (pf, row, col);
-             if (row >= n_rows && tile)
-               g_object_unref (tile);
-             else if (col >= n_cols && tile)
-               g_object_unref (tile);
-           }
-       }
+      {
+        for (col = 0; col < old_n_cols; col++)
+        {
+          Tile *tile = get_tile (pf, row, col);
+          if (row >= n_rows && tile)
+            g_object_unref (tile);
+          else if (col >= n_cols && tile)
+            g_object_unref (tile);
+        }
+      }
     }
 
   new_matrix = g_malloc0 (n_rows * n_cols * sizeof (Tile *));
   memcpy (new_matrix, pf->priv->matrix,
-         old_n_rows * old_n_cols * sizeof (Tile *));
+      old_n_rows * old_n_cols * sizeof (Tile *));
 
   g_free (pf->priv->matrix);
   pf->priv->matrix = new_matrix;
@@ -435,54 +456,30 @@ PlayField *playfield_new_from_xml (xmlNodePtr node)
   PlayField *pf;
   gint row, col;
   gint n_rows, n_cols;
-  gchar *prop_value;
-  gchar *content;
 
   g_return_val_if_fail (node != NULL, NULL);
 
   pf = playfield_new ();
   row = 0;
   col = 0;
-  n_rows = 0;
-  n_cols = 0;
+
+  n_rows = atoi (xmlGetProp (node, "n_rows"));
+  n_cols = atoi (xmlGetProp (node, "n_columns"));
+
+  playfield_set_matrix_size (pf, n_rows, n_cols);
 
   /* reading non empty tiles */
-  for (; node != NULL; node = node->next)
+  for (child_node = node->xmlChildrenNode;
+       child_node != NULL; child_node = child_node->next)
+  {
+    if (!g_ascii_strcasecmp (child_node->name, "position"))
     {
-      if (!g_ascii_strcasecmp (node->name, "n_rows"))
-       {
-         content = xmlNodeGetContent (node);
-         n_rows = atoi (content);
-       }
-      else if (!g_ascii_strcasecmp (node->name, "n_columns"))
-       {
-         content = xmlNodeGetContent (node);
-         n_cols = atoi (content);
-         playfield_set_matrix_size (pf, n_rows, n_cols);
-       }
-      else if (!g_ascii_strcasecmp (node->name, "row"))
-       {
-         prop_value = xmlGetProp (node, "no");
-         row = atoi (prop_value);
-         for (child_node = node->xmlChildrenNode;
-              child_node != NULL; child_node = child_node->next)
-           {
-             if (!g_ascii_strcasecmp (child_node->name, "col"))
-               {
-                 prop_value = xmlGetProp (child_node, "no");
-                 col = atoi (prop_value);
-                 read_tile (pf, row, col, child_node->xmlChildrenNode);
-               }
-           }
-       }
-      else if (!g_ascii_strcasecmp (node->name, "text"))
-       {
-       }
-      else
-       {
-         g_warning ("Skipping unexpected Tag %s.", node->name);
-       }
+      row = atoi (xmlGetProp (child_node, "row"));
+      col = atoi (xmlGetProp (child_node, "col"));
+      read_tile (pf, row, col, child_node->xmlChildrenNode);
     }
+  }
+
   return pf;
 }
 
@@ -829,6 +826,53 @@ static GMarkupParser tile_parser =
   xml_parser_log_error
 };
 
+static GMarkupParser position_parser =
+{
+  position_parser_start_element,
+  position_parser_end_element,
+  NULL,
+  NULL,
+  xml_parser_log_error
+};
+
+static void
+position_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;
+
+  if (!g_strcmp0 (element_name, "tile"))
+  {
+    tile = tile_new (TILE_TYPE_UNKNOWN);
+    g_markup_parse_context_push (context, &tile_parser, tile);
+  } else
+  {
+    printf ("position: starting %s\n", element_name);
+  }
+}
+
+static void
+position_parser_end_element (GMarkupParseContext  *context,
+                             const gchar          *element_name,
+                             gpointer              user_data,
+                             GError              **error)
+{
+  TileData *tile_data = user_data;
+
+
+  if (!g_strcmp0 (element_name, "tile"))
+  {
+    tile_data->tile = g_markup_parse_context_pop (context);
+  } else
+  {
+    printf ("position: ending %s\n", element_name);
+  }
+}
+
 void
 playfield_parser_start_element (GMarkupParseContext  *context,
                                 const gchar          *element_name,
@@ -837,25 +881,38 @@ playfield_parser_start_element (GMarkupParseContext  *context,
                                 gpointer              user_data,
                                 GError              **error)
 {
-  Tile *tile = NULL;
+  TileData *tile_data = NULL;
+  const gchar *current = NULL;
+  gint number = 0;
 
-  printf ("playfield: starting %s\n", element_name);
-  if (!g_strcmp0 (element_name, "tile"))
+  if (!g_strcmp0 (element_name, "position"))
   {
-    tile = tile_new (TILE_TYPE_UNKNOWN);
-    printf("pushing subparser for tile\n");
-    g_markup_parse_context_push (context, &tile_parser, tile);
+    tile_data = g_slice_new (TileData);
+
+    current = get_attribute_value ("row", attribute_names, attribute_values);
+    number = atoi (current);
+    tile_data->row = (guint) number;
+
+    current = get_attribute_value ("col", attribute_names, attribute_values);
+    number = atoi (current);
+    
+    tile_data->col = (guint) number;
+    tile_data->tile = NULL;
+    g_markup_parse_context_push (context, &position_parser, tile_data);
+  } else
+  {
+    printf ("playfield: starting %s\n", element_name);
   }
 }
 
 void
 playfield_parser_text (GMarkupParseContext  *context,
                        const gchar          *text,
-                       gsize                text_len,
+                       gsize                 text_len,
                        gpointer              user_data,
                        GError              **error)
 {
-  printf ("playfield: text %s\n", text);
+  //printf ("playfield: text %s\n", text);
 }
 
 void
@@ -864,11 +921,16 @@ playfield_parser_end_element (GMarkupParseContext  *context,
                               gpointer              user_data,
                               GError              **error)
 {
-  Tile *tile = NULL;
+  PlayField *playfield = user_data;
+  TileData *tile_data = NULL;
 
-  printf ("playfield: ending %s\n", element_name);
-  if (!g_strcmp0 (element_name, "tile"))
+  if (!g_strcmp0 (element_name, "position"))
+  {
+    tile_data = g_markup_parse_context_pop (context);
+    playfield_set_tile (playfield, tile_data->row, tile_data->col, tile_data->tile);
+    g_slice_free (TileData, tile_data);
+  } else
   {
-    tile = g_markup_parse_context_pop (context);
+    printf ("playfield: ending %s\n", element_name);
   }
 }
diff --git a/src/tile.c b/src/tile.c
index da60971..770ae91 100644
--- a/src/tile.c
+++ b/src/tile.c
@@ -322,25 +322,14 @@ Tile *tile_new_from_xml (xmlNodePtr node)
   g_return_val_if_fail (node != NULL, NULL);
   g_return_val_if_fail (!g_ascii_strcasecmp (node->name, "tile"), NULL);
 
-  tile = NULL;
+  type  = string_to_tile_type (xmlGetProp (node, "type"));
+  tile = tile_new (type);
+  base_id = g_quark_from_string (xmlGetProp (node, "base"));
+  tile_set_base_id (tile, base_id);
 
   for (child = node->xmlChildrenNode; child != NULL; child = child->next)
     {
-      if (!g_ascii_strcasecmp (child->name, "type"))
-       {
-         g_assert (tile == NULL);
-         content = xmlNodeGetContent (child);
-         type = string_to_tile_type (content);
-         tile = tile_new (type);
-       }
-      else if (!g_ascii_strcasecmp (child->name, "base"))
-       {
-         g_assert (tile != NULL);
-         content = xmlNodeGetContent (child);
-         base_id = g_quark_from_string (content);
-         tile_set_base_id (tile, base_id);
-       }
-      else if (!g_ascii_strcasecmp (child->name, "underlay"))
+      if (!g_ascii_strcasecmp (child->name, "underlay"))
        {
          g_assert (tile != NULL);
          content = xmlNodeGetContent (child);
@@ -380,7 +369,7 @@ tile_parser_start_element (GMarkupParseContext  *context,
 void
 tile_parser_text (GMarkupParseContext  *context,
                   const gchar          *text,
-                  gsize                text_len,
+                  gsize                 text_len,
                   gpointer              user_data,
                   GError              **error)
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]