del -> unlink + const macro
[protos/libecoli.git] / lib / ecoli_parsed.c
index a70f066..4816222 100644 (file)
@@ -1,28 +1,5 @@
-/*
- * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in the
- *       documentation and/or other materials provided with the distribution.
- *     * Neither the name of the University of California, Berkeley nor the
- *       names of its contributors may be used to endorse or promote products
- *       derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
  */
 
 #include <stdio.h>
@@ -67,7 +44,7 @@ static int __ec_node_parse_child(const struct ec_node *node,
                if (child == NULL)
                        return -ENOMEM;
 
-               ec_parsed_add_child(state, child);
+               ec_parsed_link_child(state, child);
        } else {
                child = state;
        }
@@ -87,7 +64,7 @@ static int __ec_node_parse_child(const struct ec_node *node,
 
 free:
        if (!is_root) {
-               ec_parsed_del_child(state, child);
+               ec_parsed_unlink_child(state, child);
                ec_parsed_free(child);
        }
        return ret;
@@ -203,7 +180,7 @@ __ec_parsed_dup(const struct ec_parsed *root, const struct ec_parsed *ref,
                dup_child = __ec_parsed_dup(child, ref, new_ref);
                if (dup_child == NULL)
                        goto fail;
-               ec_parsed_add_child(dup, dup_child);
+               ec_parsed_link_child(dup, dup_child);
        }
 
        return dup;
@@ -213,9 +190,10 @@ fail:
        return NULL;
 }
 
-struct ec_parsed *ec_parsed_dup(struct ec_parsed *parsed) //XXX const
+struct ec_parsed *ec_parsed_dup(const struct ec_parsed *parsed)
 {
-       struct ec_parsed *root, *dup_root, *dup = NULL;
+       const struct ec_parsed *root;
+       struct ec_parsed *dup_root, *dup = NULL;
 
        root = ec_parsed_get_root(parsed);
        dup_root = __ec_parsed_dup(root, parsed, &dup);
@@ -236,6 +214,7 @@ void ec_parsed_free_children(struct ec_parsed *parsed)
        while (!TAILQ_EMPTY(&parsed->children)) {
                child = TAILQ_FIRST(&parsed->children);
                TAILQ_REMOVE(&parsed->children, child, next);
+               child->parent = NULL;
                ec_parsed_free(child);
        }
 }
@@ -245,9 +224,8 @@ void ec_parsed_free(struct ec_parsed *parsed)
        if (parsed == NULL)
                return;
 
-       // assert(parsed->parent == NULL); XXX
-       // or
-       // parsed = ec_parsed_get_root(parsed);
+       ec_assert_print(parsed->parent == NULL,
+                       "parent not NULL in ec_parsed_free()");
 
        ec_parsed_free_children(parsed);
        ec_strvec_free(parsed->strvec);
@@ -298,15 +276,14 @@ void ec_parsed_dump(FILE *out, const struct ec_parsed *parsed)
        __ec_parsed_dump(out, parsed, 0);
 }
 
-void ec_parsed_add_child(struct ec_parsed *parsed,
+void ec_parsed_link_child(struct ec_parsed *parsed,
        struct ec_parsed *child)
 {
        TAILQ_INSERT_TAIL(&parsed->children, child, next);
        child->parent = parsed;
 }
 
-// XXX we can remove the first arg ? parsed == child->parent ?
-void ec_parsed_del_child(struct ec_parsed *parsed, // XXX rename del in unlink?
+void ec_parsed_unlink_child(struct ec_parsed *parsed,
        struct ec_parsed *child)
 {
        TAILQ_REMOVE(&parsed->children, child, next);
@@ -340,16 +317,16 @@ const struct ec_node *ec_parsed_get_node(const struct ec_parsed *parsed)
        return parsed->node;
 }
 
-void ec_parsed_del_last_child(struct ec_parsed *parsed) // rename in free
+void ec_parsed_del_last_child(struct ec_parsed *parsed)
 {
        struct ec_parsed *child;
 
        child = ec_parsed_get_last_child(parsed);
-       ec_parsed_del_child(parsed, child);
+       ec_parsed_unlink_child(parsed, child);
        ec_parsed_free(child);
 }
 
-struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
+struct ec_parsed *__ec_parsed_get_root(struct ec_parsed *parsed)
 {
        if (parsed == NULL)
                return NULL;
@@ -360,7 +337,7 @@ struct ec_parsed *ec_parsed_get_root(struct ec_parsed *parsed)
        return parsed;
 }
 
-struct ec_parsed *ec_parsed_get_parent(struct ec_parsed *parsed)
+struct ec_parsed *ec_parsed_get_parent(const struct ec_parsed *parsed)
 {
        if (parsed == NULL)
                return NULL;