]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Add some cleanup functions
authorMattia Basaglia <mattia.basaglia@gmail.com>
Fri, 28 Nov 2014 17:00:36 +0000 (18:00 +0100)
committerMattia Basaglia <mattia.basaglia@gmail.com>
Fri, 28 Nov 2014 17:00:36 +0000 (18:00 +0100)
xml.c

diff --git a/xml.c b/xml.c
index 8201906d27aa795182596ff1969781b090d3b7a3..f739674e8abd79e53de1d0c58081beeb185653b5 100644 (file)
--- a/xml.c
+++ b/xml.c
@@ -332,6 +332,18 @@ struct Tiled_PropertyNode {
 
 typedef Tiled_PropertyNode* Tiled_Properties;
 
+// clear the properties structure
+static void tmx_properties_clean(Tiled_Properties props)
+{
+       if ( !props )
+               return;
+       if ( props->next )
+               tmx_properties_clean(props->next);
+       free(props->property.name);
+       free(props->property.value);
+       free(props);
+}
+
 // Image/Tile data
 typedef struct {
        // Name of the image file
@@ -344,6 +356,12 @@ typedef struct {
        unsigned id;
 } Tiled_Image;
 
+// Clean a Tiled_Image(Doesn't free the given pointer)
+static void tmx_image_clean(Tiled_Image *img)
+{
+       free(img->source);
+}
+
 // Tileset
 typedef struct {
        // Images of acollection or single image split in a grid
@@ -381,14 +399,6 @@ struct Tiled_Layer
        Tiled_Layer* next_layer;
 };
 
-// Load common data for <layer> <objectgroup> and <imagelayer>
-static Tiled_Layer tmx_layer_common(xmlNodePtr xml_node)
-{
-       Tiled_Layer l;
-       // TODO
-       return l;
-}
-
 // Tile layer
 typedef struct {
        Tiled_Layer layer;
@@ -396,13 +406,6 @@ typedef struct {
        unsigned data_size;
 } Tiled_TileLayer;
 
-// Load <layer>
-static Tiled_TileLayer* tmx_layer(xmlNodePtr xml_node)
-{
-       // TODO
-       return NULL;
-}
-
 // Object
 typedef struct {
        char* name;
@@ -426,19 +429,27 @@ typedef struct {
        unsigned objects_size;
 } Tiled_ObjectLayer;
 
-// load <objectgroup>
-static Tiled_ObjectLayer* tmx_objectgroup(xmlNodePtr xml_node)
-{
-       // TODO
-       return NULL;
-}
-
 // Image layer
 typedef struct {
        Tiled_Layer layer;
        Tiled_Image image;
 } Tiled_ImageLayer;
 
+// Load common data for <layer> <objectgroup> and <imagelayer>
+static Tiled_Layer tmx_layer_common(xmlNodePtr xml_node)
+{
+       Tiled_Layer l;
+       // TODO
+       return l;
+}
+
+// Load <layer>
+static Tiled_TileLayer* tmx_layer(xmlNodePtr xml_node)
+{
+       // TODO
+       return NULL;
+}
+
 // load <imagelayer>
 static Tiled_ImageLayer* tmx_imagelayer(xmlNodePtr xml_node)
 {
@@ -446,6 +457,31 @@ static Tiled_ImageLayer* tmx_imagelayer(xmlNodePtr xml_node)
        return NULL;
 }
 
+// load <objectgroup>
+static Tiled_ObjectLayer* tmx_objectgroup(xmlNodePtr xml_node)
+{
+       // TODO
+       return NULL;
+}
+
+// clean up <layer> and friends
+static void tmx_layer_clean(Tiled_Layer* layer)
+{
+       if ( !layer )
+               return;
+       if ( layer->next_layer )
+               tmx_layer_clean(layer->next_layer);
+       free(layer->name);
+       tmx_properties_clean(layer->properties);
+       if ( layer->type == TILED_TILELAYER )
+               free( ((Tiled_TileLayer*)layer)->data );
+       else if ( layer->type == TILED_OBJECTLAYER )
+               ; // TODO clean objects
+       else if ( layer->type == TILED_IMAGELAYER )
+               tmx_image_clean(&((Tiled_ImageLayer*)layer)->image);
+       
+}
+
 // Map
 typedef struct {
        // File format version
@@ -558,3 +594,12 @@ static Tiled_Map* tmx_map_load(const char* filename)
        
        return map;
 }
+
+// Clean up the map struct
+static void tmx_map_clean(Tiled_Map* map)
+{
+       tmx_properties_clean(map->properties);
+       tmx_layer_clean(map->first_layer);
+       free(map);
+}
+