forked from vieter-v/libvieter
refactor: slightly change api
parent
30e086ad6b
commit
11709cc611
6
Makefile
6
Makefile
|
@ -1,3 +1,6 @@
|
||||||
|
# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great
|
||||||
|
# base for this Makefile
|
||||||
|
|
||||||
LIB_FILENAME ?= libvieter.a
|
LIB_FILENAME ?= libvieter.a
|
||||||
|
|
||||||
BUILD_DIR ?= build
|
BUILD_DIR ?= build
|
||||||
|
@ -22,7 +25,8 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
||||||
# object file is also recompiled if only a header is changed.
|
# object file is also recompiled if only a header is changed.
|
||||||
# -MP: generate a dummy target for every header file (according to the docs it
|
# -MP: generate a dummy target for every header file (according to the docs it
|
||||||
# prevents some errors when removing header files)
|
# prevents some errors when removing header files)
|
||||||
CFLAGS ?= $(INC_FLAGS) -MMD -MP -Wall -Werror -Wextra
|
CFLAGS ?= -MMD -MP -Wall -Werror -Wextra
|
||||||
|
CFLAGS += $(INC_FLAGS)
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: vieter
|
all: vieter
|
||||||
|
|
|
@ -37,17 +37,16 @@ typedef struct vieter_cron_simple_time {
|
||||||
|
|
||||||
vieter_cron_expression *ce_init();
|
vieter_cron_expression *ce_init();
|
||||||
|
|
||||||
void vieter_cron_ce_free(vieter_cron_expression *ce);
|
void vieter_cron_expr_free(vieter_cron_expression *ce);
|
||||||
|
|
||||||
void vieter_cron_ce_next(vieter_cron_simple_time *out,
|
void vieter_cron_expr_next(vieter_cron_simple_time *out,
|
||||||
vieter_cron_expression *ce,
|
vieter_cron_expression *ce,
|
||||||
vieter_cron_simple_time *ref);
|
vieter_cron_simple_time *ref);
|
||||||
|
|
||||||
void vieter_cron_ce_next_from_now(vieter_cron_simple_time *out,
|
void vieter_cron_expr_next_from_now(vieter_cron_simple_time *out,
|
||||||
vieter_cron_expression *ce);
|
vieter_cron_expression *ce);
|
||||||
|
|
||||||
enum vieter_cron_parse_error
|
enum vieter_cron_parse_error vieter_cron_expr_parse(vieter_cron_expression *out,
|
||||||
vieter_cron_parse_expression(vieter_cron_expression *out,
|
|
||||||
const char *expression);
|
const char *expression);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
|
|
||||||
const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
const uint8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||||
|
|
||||||
vieter_cron_expression *vieter_cron_expression_init() {
|
vieter_cron_expression *vieter_cron_expr_init() {
|
||||||
return malloc(sizeof(vieter_cron_expression));
|
return malloc(sizeof(vieter_cron_expression));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ce_free(vieter_cron_expression *ce) {
|
void vieter_cron_expr_free(vieter_cron_expression *ce) {
|
||||||
free(ce->months);
|
free(ce->months);
|
||||||
free(ce->days);
|
free(ce->days);
|
||||||
free(ce->hours);
|
free(ce->hours);
|
||||||
|
@ -15,7 +15,8 @@ void ce_free(vieter_cron_expression *ce) {
|
||||||
free(ce);
|
free(ce);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vieter_cron_next(vieter_cron_simple_time *out, vieter_cron_expression *ce,
|
void vieter_cron_expr_next(vieter_cron_simple_time *out,
|
||||||
|
vieter_cron_expression *ce,
|
||||||
vieter_cron_simple_time *ref) {
|
vieter_cron_simple_time *ref) {
|
||||||
// For all of these values, the rule is the following: if their value is
|
// 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
|
// the length of their respective array in the CronExpression object, that
|
||||||
|
@ -100,7 +101,7 @@ void vieter_cron_next(vieter_cron_simple_time *out, vieter_cron_expression *ce,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void vieter_cron_next_from_now(vieter_cron_simple_time *out,
|
void vieter_cron_expr_next_from_now(vieter_cron_simple_time *out,
|
||||||
vieter_cron_expression *ce) {
|
vieter_cron_expression *ce) {
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
struct tm gm;
|
struct tm gm;
|
||||||
|
@ -114,5 +115,5 @@ void vieter_cron_next_from_now(vieter_cron_simple_time *out,
|
||||||
.hour = gm.tm_hour,
|
.hour = gm.tm_hour,
|
||||||
.minute = gm.tm_min};
|
.minute = gm.tm_min};
|
||||||
|
|
||||||
vieter_cron_next(out, ce, &ref);
|
vieter_cron_expr_next(out, ce, &ref);
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,8 @@ const uint8_t max_parts = 4;
|
||||||
* - a/c
|
* - a/c
|
||||||
* - a-b/c
|
* - a-b/c
|
||||||
*/
|
*/
|
||||||
vieter_cron_parse_error vieter_cron_expression_parse_range(uint64_t *out,
|
vieter_cron_parse_error vieter_cron_expr_parse_range(uint64_t *out, char *s,
|
||||||
char *s, uint8_t min,
|
uint8_t min, uint8_t max) {
|
||||||
uint8_t max) {
|
|
||||||
size_t slash_index = 0, dash_index = 0;
|
size_t slash_index = 0, dash_index = 0;
|
||||||
size_t s_index = 0;
|
size_t s_index = 0;
|
||||||
char cur_char;
|
char cur_char;
|
||||||
|
@ -121,8 +120,8 @@ vieter_cron_parse_error vieter_cron_expression_parse_range(uint64_t *out,
|
||||||
* min-max range the part represents. A part consists of one or more range
|
* min-max range the part represents. A part consists of one or more range
|
||||||
* expressions, separated by commas.
|
* expressions, separated by commas.
|
||||||
*/
|
*/
|
||||||
vieter_cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
|
vieter_cron_parse_error vieter_cron_expr_parse_part(uint64_t *out, char *s,
|
||||||
uint8_t max) {
|
uint8_t min, uint8_t max) {
|
||||||
*out = 0;
|
*out = 0;
|
||||||
|
|
||||||
char *next;
|
char *next;
|
||||||
|
@ -131,7 +130,7 @@ vieter_cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
|
||||||
while ((next = strchr(s, ',')) != NULL) {
|
while ((next = strchr(s, ',')) != NULL) {
|
||||||
next[0] = '\0';
|
next[0] = '\0';
|
||||||
|
|
||||||
res = vieter_cron_expression_parse_range(out, s, min, max);
|
res = vieter_cron_expr_parse_range(out, s, min, max);
|
||||||
|
|
||||||
if (res != vieter_cron_parse_ok) {
|
if (res != vieter_cron_parse_ok) {
|
||||||
return res;
|
return res;
|
||||||
|
@ -141,7 +140,7 @@ vieter_cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure to parse the final range as well
|
// Make sure to parse the final range as well
|
||||||
return vieter_cron_expression_parse_range(out, s, min, max);
|
return vieter_cron_expr_parse_range(out, s, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -197,8 +196,7 @@ uint8_t bf_to_nums(uint8_t **out, uint64_t bf, uint8_t min, uint8_t max) {
|
||||||
/*
|
/*
|
||||||
* Parse a cron expression string into a cron_expression struct.
|
* Parse a cron expression string into a cron_expression struct.
|
||||||
*/
|
*/
|
||||||
vieter_cron_parse_error
|
vieter_cron_parse_error vieter_cron_expr_parse(vieter_cron_expression *out,
|
||||||
vieter_cron_parse_expression(vieter_cron_expression *out,
|
|
||||||
const char *expression) {
|
const char *expression) {
|
||||||
// The parsing functions modify the input string in-place
|
// The parsing functions modify the input string in-place
|
||||||
char *s = strdup(expression);
|
char *s = strdup(expression);
|
||||||
|
@ -259,7 +257,7 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
|
||||||
|
|
||||||
// Months
|
// Months
|
||||||
if (part_count >= 4) {
|
if (part_count >= 4) {
|
||||||
res = ce_parse_part(&bit_field, parts[3], min[3], max[3]);
|
res = vieter_cron_expr_parse_part(&bit_field, parts[3], min[3], max[3]);
|
||||||
|
|
||||||
if (res != vieter_cron_parse_ok) {
|
if (res != vieter_cron_parse_ok) {
|
||||||
goto end;
|
goto end;
|
||||||
|
@ -283,7 +281,8 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
|
||||||
if (part_count >= 3) {
|
if (part_count >= 3) {
|
||||||
bit_field = 0;
|
bit_field = 0;
|
||||||
|
|
||||||
res = ce_parse_part(&bit_field, parts[2], min[2], max_day_value);
|
res = vieter_cron_expr_parse_part(&bit_field, parts[2], min[2],
|
||||||
|
max_day_value);
|
||||||
|
|
||||||
if (res != vieter_cron_parse_ok) {
|
if (res != vieter_cron_parse_ok) {
|
||||||
free(out->months);
|
free(out->months);
|
||||||
|
@ -301,7 +300,7 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
|
||||||
// Hours
|
// Hours
|
||||||
bit_field = 0;
|
bit_field = 0;
|
||||||
|
|
||||||
res = ce_parse_part(&bit_field, parts[1], min[1], max[1]);
|
res = vieter_cron_expr_parse_part(&bit_field, parts[1], min[1], max[1]);
|
||||||
|
|
||||||
if (res != vieter_cron_parse_ok) {
|
if (res != vieter_cron_parse_ok) {
|
||||||
free(out->months);
|
free(out->months);
|
||||||
|
@ -315,7 +314,7 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
|
||||||
// Minutes
|
// Minutes
|
||||||
bit_field = 0;
|
bit_field = 0;
|
||||||
|
|
||||||
res = ce_parse_part(&bit_field, parts[0], min[0], max[0]);
|
res = vieter_cron_expr_parse_part(&bit_field, parts[0], min[0], max[0]);
|
||||||
|
|
||||||
if (res != vieter_cron_parse_ok) {
|
if (res != vieter_cron_parse_ok) {
|
||||||
free(out->months);
|
free(out->months);
|
||||||
|
|
|
@ -26,7 +26,7 @@ void test_not_allowed() {
|
||||||
|
|
||||||
while (expressions[i] != NULL) {
|
while (expressions[i] != NULL) {
|
||||||
vieter_cron_expression out;
|
vieter_cron_expression out;
|
||||||
TEST_CHECK_(vieter_cron_parse_expression(&out, expressions[i]) != vieter_cron_parse_ok, "%s should error", expressions[i]);
|
TEST_CHECK_(vieter_cron_expr_parse(&out, expressions[i]) != vieter_cron_parse_ok, "%s should error", expressions[i]);
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue