void destroyServiceNode()

in bundles/http_admin/http_admin/src/service_tree.c [192:250]


void destroyServiceNode(service_tree_t *svc_tree, service_tree_node_t *node, int *tree_item_count, int *tree_svc_count) {
    if(node != NULL && tree_item_count != NULL && tree_svc_count != NULL) {
        bool has_underlaying_services = false;

        //If this service node has children then check if we need to destroy them first
        if(node->children != NULL) {
            service_tree_node_t *node_cpy = node;
            while(node_cpy->children != NULL && has_underlaying_services == false) {
                service_tree_node_t *node_cpy_horizontal = node_cpy->children;
                has_underlaying_services |= (node_cpy_horizontal->svc_data->service != NULL);
                while(node_cpy_horizontal->next != NULL && has_underlaying_services == false) {
                    has_underlaying_services |= (node_cpy_horizontal->svc_data->service != NULL);
                    node_cpy_horizontal = node_cpy_horizontal->next;
                }
                node_cpy = node_cpy->children;
            }

            //No underlaying services means we can delete all children
            if(!has_underlaying_services) {
                destroyChildrenFromServiceNode(node, tree_item_count, tree_svc_count);
            }
        }

        //Decrement service count if a service was present
        if(node->svc_data->service != NULL) {
            node->svc_data->service = NULL;
            (*tree_svc_count)--;
        }

        //When no underlaying services found, we have to take care of some pointers and memory...
        if(!has_underlaying_services){
            //Set new children pointer for parent when this is the first child in the tree.
            //When no next is present the children pointer should become NULL.
            if(node->parent != NULL && node->prev == NULL){
                node->parent->children = node->next;
            }

            //If current node to delete is the root node, set new root node pointer
            if(svc_tree->root_node == node) {
                svc_tree->root_node = node->next;
            }

            //Set new previous pointer for the next node if present.
            if(node->next != NULL) {
                node->next->prev = node->prev;
            }

            //Set new next pointer if a previous is present
            if(node->prev != NULL) {
                node->prev->next = node->next;
            }

            free(node->svc_data->sub_uri);
            free(node->svc_data);
            free(node);
            (*tree_item_count)--;
        }
    }
}