NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stddef.h>
32#include "mutt/lib.h"
33#include "node.h"
34
39struct ExpandoNode *node_new(void)
40{
41 return mutt_mem_calloc(1, sizeof(struct ExpandoNode));
42}
43
48void node_free(struct ExpandoNode **ptr)
49{
50 if (!ptr || !*ptr)
51 return;
52
53 struct ExpandoNode *node = *ptr;
54 if (node->ndata_free)
55 {
56 node->ndata_free(&node->ndata);
57 }
58
59 struct ExpandoNode **enp = NULL;
60 ARRAY_FOREACH(enp, &node->children)
61 {
62 node_tree_free(enp);
63 }
64
65 FREE(&node->format);
66
67 ARRAY_FREE(&node->children);
68
69 FREE(ptr);
70}
71
76void node_tree_free(struct ExpandoNode **ptr)
77{
78 if (!ptr || !*ptr)
79 return;
80
81 struct ExpandoNode *node = *ptr;
82 while (node)
83 {
84 struct ExpandoNode *n = node;
85 node = node->next;
86 node_free(&n);
87 }
88
89 *ptr = NULL;
90}
91
99struct ExpandoNode *node_get_child(const struct ExpandoNode *node, int index)
100{
101 if (!node)
102 return NULL;
103
104 struct ExpandoNode **ptr = ARRAY_GET(&node->children, index);
105 if (!ptr)
106 return NULL;
107
108 return *ptr;
109}
110
117void node_set_child(struct ExpandoNode *node, int index, struct ExpandoNode *child)
118{
119 if (!node)
120 return;
121
122 struct ExpandoNode *old = node_get_child(node, index);
123 node_tree_free(&old);
124
125 ARRAY_SET(&node->children, index, child);
126}
127
133void node_append(struct ExpandoNode **root, struct ExpandoNode *new_node)
134{
135 if (!*root)
136 {
137 *root = new_node;
138 return;
139 }
140
141 struct ExpandoNode *node = *root;
142 while (node->next)
143 {
144 node = node->next;
145 }
146
147 node->next = new_node;
148}
#define ARRAY_SET(head, idx, elem)
Set an element in the array.
Definition: array.h:123
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:109
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
void node_set_child(struct ExpandoNode *node, int index, struct ExpandoNode *child)
Set the child of an ExpandoNode.
Definition: node.c:117
void node_tree_free(struct ExpandoNode **ptr)
Free a tree of ExpandoNodes.
Definition: node.c:76
struct ExpandoNode * node_get_child(const struct ExpandoNode *node, int index)
Get a child of an ExpandoNode.
Definition: node.c:99
void node_append(struct ExpandoNode **root, struct ExpandoNode *new_node)
Append an ExpandoNode to an existing node.
Definition: node.c:133
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition: node.c:48
Basic Expando Node.
Basic Expando Node.
Definition: node.h:67
void * ndata
Private node data.
Definition: node.h:80
struct ExpandoFormat * format
Formatting info.
Definition: node.h:73
struct ExpandoNode * next
Linked list.
Definition: node.h:69
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition: node.h:81
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75