forked from vieter-v/vieter
Compare commits
No commits in common. "85ea7166fbb887be54065548a7acc122e569053f" and "c3f7f11686f1fc2cc4d04bbf52c2262a8237d825" have entirely different histories.
85ea7166fb
...
c3f7f11686
|
|
@ -1,90 +0,0 @@
|
||||||
#include "expression.h"
|
|
||||||
|
|
||||||
const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
||||||
|
|
||||||
int ce_next(SimpleTime *out, CronExpression *ce, SimpleTime *ref) {
|
|
||||||
// For all of these values, the rule is the following: if their value is
|
|
||||||
// the length of their respective array in the CronExpression object, that
|
|
||||||
// means we've looped back around. This means that the "bigger" value has
|
|
||||||
// to be incremented by one. For example, if the minutes have looped
|
|
||||||
// around, that means that the hour has to be incremented as well.
|
|
||||||
uint8_t month_index = 0;
|
|
||||||
uint8_t day_index = 0;
|
|
||||||
uint8_t hour_index = 0;
|
|
||||||
uint8_t minute_index = 0;
|
|
||||||
|
|
||||||
// This chain is the same logic multiple times, namely that if a "bigger"
|
|
||||||
// value loops around, then the smaller value will always reset as well.
|
|
||||||
// For example, if we're going to a new day, the hour & minute will always
|
|
||||||
// be their smallest value again.
|
|
||||||
while (month_index < ce->month_count && ref->month > ce->months[month_index]) {
|
|
||||||
month_index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (month_index < ce->month_count && ref->month == ce->months[month_index]) {
|
|
||||||
while (day_index < ce->day_count && ref->day > ce->days[day_index]) {
|
|
||||||
day_index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (day_index < ce->days_count && ref->day == ce->days[day_index]) {
|
|
||||||
while (hour_index < ce->hour_count && ref->hour > ce->hours[hour_index]) {
|
|
||||||
hour_index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hour_index < ce->hours_count && ref->hour == ce->hours[hour_index]) {
|
|
||||||
// Minute is the only value where we explicitely make sure we
|
|
||||||
// can't match sref's value exactly. This is to ensure we only
|
|
||||||
// return values in the future.
|
|
||||||
while (minute_index < ce->minute_count && ref->minute > ce->minutes[minute_index]) {
|
|
||||||
minute_index++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Here, we increment the "bigger" values by one if the smaller ones loop
|
|
||||||
// around. The order is important, as it allows a sort-of waterfall effect
|
|
||||||
// to occur which updates all values if required.
|
|
||||||
if (minute_index == ce->minute_count && hour_index < ce->hour_count) {
|
|
||||||
hour_index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hour_index == ce->hour_count && day_index < ce->day_count) {
|
|
||||||
day_index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (day_index == ce->day_count && month_index < ce->month_count) {
|
|
||||||
month_index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->minute = ce->minutes[minute_index % ce->minute_count];
|
|
||||||
out->hour = ce->hours[hour_index % ce->hour_count];
|
|
||||||
out->day = ce->days[day_index % ce->day_count];
|
|
||||||
|
|
||||||
// Sometimes, we end up with a day that does not exist within the selected
|
|
||||||
// month, e.g. day 30 in February. When this occurs, we reset day back to
|
|
||||||
// the smallest value & loop over to the next month that does have this
|
|
||||||
// day.
|
|
||||||
if (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) {
|
|
||||||
out->day = ce->days[0];
|
|
||||||
month_index++;
|
|
||||||
|
|
||||||
while (out->day > month_days[ce->months[month_index % ce->month_count] - 1]) {
|
|
||||||
month_index++;
|
|
||||||
|
|
||||||
if (month_index == 2 * ce->month_count) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out->month = ce->months[month_index * ce->month_count];
|
|
||||||
|
|
||||||
if (month_index >= ce->month_count) {
|
|
||||||
out->year = ref->year + 1;
|
|
||||||
} else {
|
|
||||||
out->year = ref->year;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -3,13 +3,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef enum parse_error {
|
|
||||||
ParseOk = 0,
|
|
||||||
ParseInvalidExpression = 1,
|
|
||||||
ParseInvalidNumber = 2,
|
|
||||||
ParseOutOfRange = 3
|
|
||||||
} ParseError;
|
|
||||||
|
|
||||||
typedef struct cron_expression {
|
typedef struct cron_expression {
|
||||||
uint8_t *minutes;
|
uint8_t *minutes;
|
||||||
uint8_t *hours;
|
uint8_t *hours;
|
||||||
|
|
@ -21,17 +14,9 @@ typedef struct cron_expression {
|
||||||
uint8_t month_count;
|
uint8_t month_count;
|
||||||
} CronExpression;
|
} CronExpression;
|
||||||
|
|
||||||
typedef struct simple_time {
|
|
||||||
int year;
|
|
||||||
int month;
|
|
||||||
int day;
|
|
||||||
int hour;
|
|
||||||
int minute;
|
|
||||||
} SimpleTime;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a
|
* Given a
|
||||||
*/
|
*/
|
||||||
int ce_next(SimpleTime *out, CronExpression *ce, SimpleTime *ref);
|
int ce_next(struct tm *out, struct tm *ref);
|
||||||
|
|
||||||
ParseError ce_parse_expression(CronExpression *out, char *s);
|
int ce_parse(CronExpression *out, char *s);
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,13 @@
|
||||||
const uint8_t min[4] = {0, 0, 1, 1};
|
const uint8_t min[4] = {0, 0, 1, 1};
|
||||||
const uint8_t max[4] = {59, 23, 31, 12};
|
const uint8_t max[4] = {59, 23, 31, 12};
|
||||||
|
|
||||||
|
typedef enum parse_error {
|
||||||
|
ParseOk = 0,
|
||||||
|
ParseInvalidExpression = 1,
|
||||||
|
ParseInvalidNumber = 2,
|
||||||
|
ParseOutOfRange = 3
|
||||||
|
} ParseError;
|
||||||
|
|
||||||
#define SAFE_ATOI(v,s,min,max) \
|
#define SAFE_ATOI(v,s,min,max) \
|
||||||
int _##v = atoi(s); \
|
int _##v = atoi(s); \
|
||||||
if ((_##v) == 0 && strcmp((s), "0") != 0) { \
|
if ((_##v) == 0 && strcmp((s), "0") != 0) { \
|
||||||
|
|
@ -118,41 +125,11 @@ ParseError ce_parse_part(uint64_t *out, char *s, uint8_t min, uint8_t max) {
|
||||||
return ParseOk;
|
return ParseOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
|
ParseError ce_parse_expression(uint64_t *out, char *s) {
|
||||||
uint8_t capacity = 8;
|
|
||||||
uint8_t size = 0;
|
|
||||||
|
|
||||||
uint8_t *buf = malloc(capacity * sizeof(uint8_t));
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i <= max - min; i++) {
|
|
||||||
if ((1 << i) & bf) {
|
|
||||||
// Resize buffer if needed
|
|
||||||
if (size == capacity) {
|
|
||||||
capacity *= 2;
|
|
||||||
buf = realloc(buf, capacity * sizeof(uint8_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
buf[size] = min + i;
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resize buffer once more to remove any trailing unused bytes
|
|
||||||
if (size < capacity) {
|
|
||||||
buf = realloc(buf, size * sizeof(uint8_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
*out = buf;
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
ParseError ce_parse_expression(CronExpression *out, char *s) {
|
|
||||||
uint8_t part_count = 0;
|
uint8_t part_count = 0;
|
||||||
|
|
||||||
char *next;
|
char *next;
|
||||||
ParseError res;
|
ParseError res;
|
||||||
uint64_t bfs[4];
|
|
||||||
|
|
||||||
// Skip leading spaces
|
// Skip leading spaces
|
||||||
while (s[0] == ' ') {
|
while (s[0] == ' ') {
|
||||||
|
|
@ -161,7 +138,7 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
|
||||||
|
|
||||||
while (part_count < 4 && (next = strchr(s, ' ')) != NULL) {
|
while (part_count < 4 && (next = strchr(s, ' ')) != NULL) {
|
||||||
next[0] = '\0';
|
next[0] = '\0';
|
||||||
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
|
res = ce_parse_part(&out[part_count], s, min[part_count], max[part_count]);
|
||||||
|
|
||||||
if (res != ParseOk) {
|
if (res != ParseOk) {
|
||||||
return res;
|
return res;
|
||||||
|
|
@ -182,7 +159,7 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
|
||||||
// Parse final trailing part
|
// Parse final trailing part
|
||||||
if (part_count < 4 && s[0] != '\0') {
|
if (part_count < 4 && s[0] != '\0') {
|
||||||
// Make sure to parse the final range as well
|
// Make sure to parse the final range as well
|
||||||
res = ce_parse_part(&bfs[part_count], s, min[part_count], max[part_count]);
|
res = ce_parse_part(&out[part_count], s, min[part_count], max[part_count]);
|
||||||
|
|
||||||
if (res != ParseOk) {
|
if (res != ParseOk) {
|
||||||
return res;
|
return res;
|
||||||
|
|
@ -191,22 +168,12 @@ ParseError ce_parse_expression(CronExpression *out, char *s) {
|
||||||
part_count++;
|
part_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// At least two parts need to be provided
|
|
||||||
if (part_count < 2) {
|
|
||||||
return ParseInvalidExpression;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure there's always 4 parts, as expressions can have between 2 and 4 parts
|
// Ensure there's always 4 parts, as expressions can have between 2 and 4 parts
|
||||||
while (part_count < 4) {
|
while (part_count < 4) {
|
||||||
// Expression is augmented with '*' expressions
|
// Expression is augmented with '*' expressions
|
||||||
bfs[part_count] = ~0;
|
out[part_count] = ~0;
|
||||||
part_count++;
|
part_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->minute_count = bf_to_nums(&out->minutes, bfs[0], min[0], max[0]);
|
|
||||||
out->hour_count = bf_to_nums(&out->hours, bfs[1], min[1], max[1]);
|
|
||||||
out->day_count = bf_to_nums(&out->days, bfs[2], min[2], max[2]);
|
|
||||||
out->month_count = bf_to_nums(&out->months, bfs[3], min[3], max[3]);
|
|
||||||
|
|
||||||
return ParseOk;
|
return ParseOk;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
module expression
|
|
||||||
|
|
||||||
#flag -I @VMODROOT/c
|
|
||||||
#flag @VMODROOT/c/parse.o
|
|
||||||
#include "expression.h"
|
|
||||||
|
|
||||||
pub struct C.CronExpression {
|
|
||||||
minutes &u8
|
|
||||||
hours &u8
|
|
||||||
days &u8
|
|
||||||
months &u8
|
|
||||||
minute_count u8
|
|
||||||
hour_count u8
|
|
||||||
day_count u8
|
|
||||||
month_count u8
|
|
||||||
}
|
|
||||||
|
|
||||||
/* pub type CronExpression = C.CronExpression */
|
|
||||||
Loading…
Reference in New Issue