NeoMutt  2024-10-02-37-gfa9146
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 <stdbool.h>
32#include "mutt/lib.h"
33#include "node_condition.h"
34#include "node.h"
35#include "render.h"
36
40static int node_condition_render(const struct ExpandoNode *node,
41 const struct ExpandoRenderData *rdata, struct Buffer *buf,
42 int max_cols, void *data, MuttFormatFlags flags)
43{
44 ASSERT(node->type == ENT_CONDITION);
45
46 const struct ExpandoNode *node_cond = node_get_child(node, ENC_CONDITION);
47
48 // Discard any text returned, just use the return value as a bool
49 struct Buffer *buf_cond = buf_pool_get();
50 int rc = node_cond->render(node_cond, rdata, buf_cond, max_cols, data, flags);
51 buf_pool_release(&buf_cond);
52
53 if (rc == true)
54 {
55 const struct ExpandoNode *node_true = node_get_child(node, ENC_TRUE);
56 return node_render(node_true, rdata, buf, max_cols, data, flags);
57 }
58 else
59 {
60 const struct ExpandoNode *node_false = node_get_child(node, ENC_FALSE);
61 return node_render(node_false, rdata, buf, max_cols, data, flags);
62 }
63}
64
72struct ExpandoNode *node_condition_new(struct ExpandoNode *condition,
73 struct ExpandoNode *node_true,
74 struct ExpandoNode *node_false)
75{
76 ASSERT(condition);
77
78 struct ExpandoNode *node = node_new();
79
80 node->type = ENT_CONDITION;
82
83 ARRAY_SET(&node->children, ENC_CONDITION, condition);
84 ARRAY_SET(&node->children, ENC_TRUE, node_true);
85 ARRAY_SET(&node->children, ENC_FALSE, node_false);
86
87 return node;
88}
#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:91
Basic Expando Node.
@ ENT_CONDITION
True/False condition.
Definition: node.h:41
struct ExpandoNode * node_condition_new(struct ExpandoNode *condition, struct ExpandoNode *node_true, struct ExpandoNode *node_false)
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_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
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
Basic Expando Node.
Definition: node.h:67
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:91
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75