NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_condbool.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdio.h>
32#include "mutt/lib.h"
33#include "node_condbool.h"
34#include "definition.h"
35#include "helpers.h"
36#include "node.h"
37#include "parse.h"
38#include "render.h"
39
48struct ExpandoNode *node_condbool_new(const char *start, const char *end, int did, int uid)
49{
50 struct ExpandoNode *node = node_new();
51
52 node->type = ENT_CONDBOOL;
53 node->did = did;
54 node->uid = uid;
56
57 return node;
58}
59
63struct ExpandoNode *node_condbool_parse(const char *str,
64 const struct ExpandoDefinition *defs,
65 ExpandoParserFlags flags, const char **parsed_until,
66 struct ExpandoParseError *err)
67{
68 const struct ExpandoDefinition *def = defs;
69
70 const char *format_end = skip_until_classic_expando(str);
71 const char *expando_end = skip_classic_expando(format_end, defs);
72 char expando[128] = { 0 };
73 const int expando_len = expando_end - format_end;
74 mutt_strn_copy(expando, format_end, expando_len, sizeof(expando));
75
76 while (def && def->short_name)
77 {
78 if (mutt_str_equal(def->short_name, expando))
79 {
80 if (def->parse)
81 {
82 return def->parse(str, def->did, def->uid, flags, parsed_until, err);
83 }
84 else
85 {
86 *parsed_until = expando_end;
87 return node_condbool_new(format_end, expando_end, def->did, def->uid);
88 }
89 }
90
91 def++;
92 }
93
94 err->position = format_end;
95 // L10N: e.g. "Unknown expando: %Q"
96 snprintf(err->message, sizeof(err->message), _("Unknown expando: %%%.*s"),
97 expando_len, format_end);
98 return NULL;
99}
100
104int node_condbool_render(const struct ExpandoNode *node,
105 const struct ExpandoRenderData *rdata, struct Buffer *buf,
106 int max_cols, void *data, MuttFormatFlags flags)
107{
108 ASSERT(node->type == ENT_CONDBOOL);
109
110 const struct ExpandoRenderData *rd_match = find_get_number(rdata, node->did, node->uid);
111 if (rd_match)
112 {
113 const long num = rd_match->get_number(node, data, flags);
114 return (num != 0); // bool-ify
115 }
116
117 rd_match = find_get_string(rdata, node->did, node->uid);
118 if (rd_match)
119 {
120 struct Buffer *buf_str = buf_pool_get();
121 rd_match->get_string(node, data, flags, buf_str);
122 const size_t len = buf_len(buf_str);
123 buf_pool_release(&buf_str);
124
125 return (len > 0); // bool-ify
126 }
127
128 return 0;
129}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
Define an Expando format string.
uint8_t ExpandoParserFlags
Flags for expando_parse(), e.g. EP_CONDITIONAL.
Definition: definition.h:41
const char * skip_classic_expando(const char *str, const struct ExpandoDefinition *defs)
Skip over the text of an Expando.
Definition: helpers.c:144
const char * skip_until_classic_expando(const char *start)
Search through string until we reach an Expando character.
Definition: helpers.c:128
const struct ExpandoRenderData * find_get_number(const struct ExpandoRenderData *rdata, int did, int uid)
Find a get_number() callback function.
Definition: helpers.c:47
const struct ExpandoRenderData * find_get_string(const struct ExpandoRenderData *rdata, int did, int uid)
Find a get_string() callback function.
Definition: helpers.c:71
Shared code.
Expando Parsing.
struct ExpandoNode * node_condbool_parse(const char *str, const struct ExpandoDefinition *defs, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Parse a CondBool format string - Implements ExpandoDefinition::parse() -.
Definition: node_condbool.c:63
int node_condbool_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Callback for every bool node - Implements ExpandoNode::render() -.
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
char * mutt_strn_copy(char *dest, const char *src, size_t len, size_t dsize)
Copy a sub-string into a buffer.
Definition: string.c:360
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
Basic Expando Node.
@ ENT_CONDBOOL
True/False boolean condition.
Definition: node.h:42
struct ExpandoNode * node_condbool_new(const char *start, const char *end, int did, int uid)
Create a new CondBool ExpandoNode.
Definition: node_condbool.c:48
Expando Node for a Conditional Boolean.
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
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
Definition of a format string.
Definition: definition.h:52
short uid
Unique ID in domain.
Definition: definition.h:56
struct ExpandoNode *(* parse)(const char *str, int did, int uid, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
Definition: definition.h:71
short did
Domain ID.
Definition: definition.h:55
const char * short_name
Short Expando name, e.g. "n".
Definition: definition.h:53
Basic Expando Node.
Definition: node.h:67
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition: node.h:70
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:91
int did
Domain ID, e.g. ED_EMAIL.
Definition: node.h:69
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
Buffer for parsing errors.
Definition: parse.h:35
const char * position
Position of error in original string.
Definition: parse.h:37
char message[256]
Error message.
Definition: parse.h:36
long(* get_number)(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Definition: render.h:78
void(* get_string)(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Definition: render.h:64