NeoMutt  2025-09-05-7-geaa2bd
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_conddate.c File Reference

Expando Node for a Conditional Date. More...

#include "config.h"
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "mutt/lib.h"
#include "node_conddate.h"
#include "helpers.h"
#include "node.h"
#include "parse.h"
#include "render.h"
+ Include dependency graph for node_conddate.c:

Go to the source code of this file.

Functions

struct NodeCondDatePrivatenode_conddate_private_new (int count, char period)
 Create new CondDate private data.
 
void node_conddate_private_free (void **ptr)
 Free CondDate private data - Implements ExpandoNode::ndata_free()
 
time_t cutoff_number (char period, int count)
 Calculate the cutoff time for n units.
 
time_t cutoff_this (char period)
 Calculate the cutoff time of this unit.
 
int node_conddate_render (const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render a CondDate Node - Implements ExpandoNode::render() -.
 
struct ExpandoNodenode_conddate_new (int count, char period, int did, int uid)
 Create a new CondDate ExpandoNode.
 
struct ExpandoNodenode_conddate_parse (const char *str, int did, int uid, const char **parsed_until, struct ExpandoParseError *err)
 Parse a CondDate format string.
 

Detailed Description

Expando Node for a Conditional Date.

Authors
  • Tóth János
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file node_conddate.c.

Function Documentation

◆ node_conddate_private_new()

struct NodeCondDatePrivate * node_conddate_private_new ( int  count,
char  period 
)

Create new CondDate private data.

Parameters
countNumber of 'units' to count
periodUnits, e.g. 'd' Day or 'm' Month
Return values
ptrNew CondDate private data

Definition at line 48 of file node_conddate.c.

49{
51
52 priv->count = count;
53 priv->period = period;
54
55 return priv;
56}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:47
Private data for a Conditional Date -.
Definition: node_conddate.h:33
int count
Number of 'units' to count.
Definition: node_conddate.h:34
char period
Units, e.g. 'd' Day or 'm' Month.
Definition: node_conddate.h:35
+ Here is the caller graph for this function:

◆ node_conddate_private_free()

void node_conddate_private_free ( void **  ptr)

Free CondDate private data - Implements ExpandoNode::ndata_free()

Parameters
ptrData to free

Definition at line 62 of file node_conddate.c.

63{
64 if (!ptr || !*ptr)
65 return;
66
67 FREE(ptr);
68}
#define FREE(x)
Definition: memory.h:62
+ Here is the caller graph for this function:

◆ cutoff_number()

time_t cutoff_number ( char  period,
int  count 
)

Calculate the cutoff time for n units.

Parameters
periodTime period, from [ymwdHM]
countNumber of time periods
Return values
numCutoff time

Calculate the cutoff time for, say, 3 months, or 2 hours.

Definition at line 78 of file node_conddate.c.

79{
80 time_t t = mutt_date_now();
81 struct tm tm = { 0 };
82 localtime_r(&t, &tm);
83
84 switch (period)
85 {
86 case 'y':
87 tm.tm_year -= count;
88 break;
89
90 case 'm':
91 tm.tm_mon -= count;
92 break;
93
94 case 'w':
95 tm.tm_mday -= (7 * count);
96 break;
97
98 case 'd':
99 tm.tm_mday -= count;
100 break;
101
102 case 'H':
103 tm.tm_hour -= count;
104 break;
105
106 case 'M':
107 tm.tm_min -= count;
108 break;
109 }
110
111 return mktime(&tm);
112}
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:456
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cutoff_this()

time_t cutoff_this ( char  period)

Calculate the cutoff time of this unit.

Parameters
periodTime period, from [ymwdHM]
Return values
numCutoff time

Calculate the cutoff time of, say, this day (today), this month.

Definition at line 121 of file node_conddate.c.

122{
123 time_t t = mutt_date_now();
124 struct tm tm = { 0 };
125 localtime_r(&t, &tm);
126
127 switch (period)
128 {
129 case 'y':
130 tm.tm_mon = 0; // January
132
133 case 'm':
134 tm.tm_mday = 1; // 1st of the month
136
137 case 'd':
138 tm.tm_hour = 0; // Beginning of day (Midnight)
140
141 case 'H':
142 tm.tm_min = 0; // Beginning of hour
144
145 case 'M':
146 tm.tm_sec = 0; // Beginning of minute
147 break;
148
149 case 'w':
150 tm.tm_hour = 0; // Beginning of day (Midnight)
151 tm.tm_min = 0; // Beginning of hour
152 tm.tm_sec = 0; // Beginning of minute
153 int bow = 0; // Beginning of week
154 if (tm.tm_wday == 0)
155 bow = 6; // LCOV_EXCL_LINE
156 else
157 bow = tm.tm_wday - 1; // LCOV_EXCL_LINE
158 tm.tm_mday -= bow;
159 break;
160 }
161
162 return mktime(&tm);
163}
#define FALLTHROUGH
Definition: lib.h:113
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_conddate_new()

struct ExpandoNode * node_conddate_new ( int  count,
char  period,
int  did,
int  uid 
)

Create a new CondDate ExpandoNode.

Parameters
countNumber of 'units' to count
periodUnits, e.g. 'd' Day or 'm' Month
didDomain ID
uidUnique ID
Return values
ptrNew CondDate ExpandoNode

Definition at line 198 of file node_conddate.c.

199{
200 struct ExpandoNode *node = node_new();
201 node->type = ENT_CONDDATE;
202 node->did = did;
203 node->uid = uid;
205
206 node->ndata = node_conddate_private_new(count, period);
208
209 return node;
210}
int node_conddate_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a CondDate Node - Implements ExpandoNode::render() -.
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
@ ENT_CONDDATE
True/False date condition.
Definition: node.h:43
struct NodeCondDatePrivate * node_conddate_private_new(int count, char period)
Create new CondDate private data.
Definition: node_conddate.c:48
void node_conddate_private_free(void **ptr)
Free CondDate private data - Implements ExpandoNode::ndata_free()
Definition: node_conddate.c:62
Basic Expando Node.
Definition: node.h:67
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition: node.h:70
void * ndata
Private node data.
Definition: node.h:77
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:92
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
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition: node.h:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_conddate_parse()

struct ExpandoNode * node_conddate_parse ( const char *  str,
int  did,
int  uid,
const char **  parsed_until,
struct ExpandoParseError err 
)

Parse a CondDate format string.

Parameters
[in]strString to parse
[in]didDomain ID
[in]uidUnique ID
[out]parsed_untilFirst character after parsed string
[out]errBuffer for errors

Definition at line 220 of file node_conddate.c.

223{
224 if (!str || (str[0] == '\0') || !parsed_until || !err)
225 return NULL;
226
227 int count = 0;
228 char period = '\0';
229
230 str++;
231
232 if (mutt_isdigit(*str))
233 {
234 unsigned short number = 0;
235 const char *end_ptr = mutt_str_atous(str, &number);
236
237 // NOTE(g0mb4): str is NOT NUL-terminated
238 if (!end_ptr || (number == USHRT_MAX))
239 {
240 err->position = str;
241 snprintf(err->message, sizeof(err->message), _("Invalid number: %s"), str);
242 return NULL;
243 }
244
245 count = number;
246 str = end_ptr;
247 };
248
249 // Allowed periods: year, month, week, day, hour, minute
250 if (!strchr("ymwdHM", *str))
251 {
252 err->position = str;
253 snprintf(err->message, sizeof(err->message),
254 // L10N: The 'ymwdHM' should not be translated
255 _("Invalid time period: '%c', must be one of 'ymwdHM'"), *str);
256 return NULL;
257 }
258
259 period = *str;
260 *parsed_until = str + 1;
261
262 return node_conddate_new(count, period, did, uid);
263}
const char * mutt_str_atous(const char *str, unsigned short *dst)
Convert ASCII string to an unsigned short.
Definition: atoi.c:270
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition: ctype.c:65
#define _(a)
Definition: message.h:28
struct ExpandoNode * node_conddate_new(int count, char period, int did, int uid)
Create a new CondDate ExpandoNode.
char message[1024]
Error message.
Definition: parse.h:38
const char * position
Position of error in original string.
Definition: parse.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function: