NeoMutt  2024-12-12-14-g7b49f7
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, struct ExpandoNode);
42}
43
48void node_free(struct ExpandoNode **ptr)
49{
50 if (!ptr || !*ptr)
51 return;
52
53 struct ExpandoNode *node = *ptr;
54
55 struct ExpandoNode **enp = NULL;
56 ARRAY_FOREACH(enp, &node->children)
57 {
58 node_free(enp);
59 }
60 ARRAY_FREE(&node->children);
61
62 if (node->ndata_free)
63 node->ndata_free(&node->ndata);
64
65 FREE(&node->format);
66 FREE(&node->text);
67
68 FREE(ptr);
69}
70
76void node_add_child(struct ExpandoNode *node, struct ExpandoNode *child)
77{
78 if (!node)
79 return;
80
81 ARRAY_ADD(&node->children, child);
82}
83
91struct ExpandoNode *node_get_child(const struct ExpandoNode *node, int index)
92{
93 if (!node)
94 return NULL;
95
96 struct ExpandoNode **ptr = ARRAY_GET(&node->children, index);
97 if (!ptr)
98 return NULL;
99
100 return *ptr;
101}
102
108struct ExpandoNode *node_last(struct ExpandoNode *node)
109{
110 if (!node)
111 return NULL;
112
113 struct ExpandoNode **np = NULL;
114 while ((np = ARRAY_LAST(&node->children)) && *np)
115 {
116 node = *np;
117 }
118
119 return node;
120}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition: array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_LAST(head)
Convenience method to get the last element.
Definition: array.h:144
#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
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Convenience wrapper for the library headers.
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
struct ExpandoNode * node_get_child(const struct ExpandoNode *node, int index)
Get a child of an ExpandoNode.
Definition: node.c:91
void node_add_child(struct ExpandoNode *node, struct ExpandoNode *child)
Add a child to an ExpandoNode.
Definition: node.c:76
struct ExpandoNode * node_last(struct ExpandoNode *node)
Find the last Node in a tree.
Definition: node.c:108
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:77
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
const char * text
Node-specific text.
Definition: node.h:73
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition: node.h:78
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75