NeoMutt  2024-04-25-1-g3de005
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_condition.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <assert.h>
32#include <stdbool.h>
33#include "mutt/lib.h"
34#include "node_condition.h"
35#include "node.h"
36#include "render.h"
37
41static int node_condition_render(const struct ExpandoNode *node,
42 const struct ExpandoRenderData *rdata, struct Buffer *buf,
43 int max_cols, void *data, MuttFormatFlags flags)
44{
45 assert(node->type == ENT_CONDITION);
46
47 const struct ExpandoNode *node_cond = node_get_child(node, ENC_CONDITION);
48
49 // Discard any text returned, just use the return value as a bool
50 struct Buffer *buf_cond = buf_pool_get();
51 int rc = node_cond->render(node_cond, rdata, buf_cond, max_cols, data, flags);
52 buf_pool_release(&buf_cond);
53
54 if (rc == true)
55 {
56 const struct ExpandoNode *node_true = node_get_child(node, ENC_TRUE);
57 return node_tree_render(node_true, rdata, buf, max_cols, data, flags);
58 }
59 else
60 {
61 const struct ExpandoNode *node_false = node_get_child(node, ENC_FALSE);
62 return node_tree_render(node_false, rdata, buf, max_cols, data, flags);
63 }
64}
65
73struct ExpandoNode *node_condition_new(struct ExpandoNode *condition,
74 struct ExpandoNode *if_true_tree,
75 struct ExpandoNode *if_false_tree)
76{
77 assert(condition);
78 assert(if_true_tree);
79
80 struct ExpandoNode *node = node_new();
81
82 node->type = ENT_CONDITION;
84
85 ARRAY_SET(&node->children, ENC_CONDITION, condition);
86 ARRAY_SET(&node->children, ENC_TRUE, if_true_tree);
87 ARRAY_SET(&node->children, ENC_FALSE, if_false_tree);
88
89 return node;
90}
#define ARRAY_SET(head, idx, elem)
Set an element in the array.
Definition: array.h:123
static int node_condition_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a Conditional Node - Implements ExpandoNode::render() -.
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:99
Basic Expando Node.
@ ENT_CONDITION
True/False condition.
Definition: node.h:41
struct ExpandoNode * node_condition_new(struct ExpandoNode *condition, struct ExpandoNode *if_true_tree, struct ExpandoNode *if_false_tree)
Create a new Condition Expando Node.
Expando Node for a Condition.
@ ENC_CONDITION
Index of Condition Node.
@ ENC_FALSE
Index of False Node.
@ ENC_TRUE
Index of True Node.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
int node_tree_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a tree of ExpandoNodes into a string.
Definition: render.c:45
Render Expandos using Data.
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: render.h:32
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
Basic Expando Node.
Definition: node.h:69
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:96
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:70
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:77