refactor: slightly change api

main
Jef Roosens 2023-01-18 14:12:02 +01:00
parent 30e086ad6b
commit 11709cc611
5 changed files with 35 additions and 32 deletions

View File

@ -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
BUILD_DIR ?= build
@ -22,7 +25,8 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
# 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
# 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
all: vieter

View File

@ -37,17 +37,16 @@ typedef struct vieter_cron_simple_time {
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,
vieter_cron_expression *ce,
vieter_cron_simple_time *ref);
void vieter_cron_expr_next(vieter_cron_simple_time *out,
vieter_cron_expression *ce,
vieter_cron_simple_time *ref);
void vieter_cron_ce_next_from_now(vieter_cron_simple_time *out,
vieter_cron_expression *ce);
void vieter_cron_expr_next_from_now(vieter_cron_simple_time *out,
vieter_cron_expression *ce);
enum vieter_cron_parse_error
vieter_cron_parse_expression(vieter_cron_expression *out,
const char *expression);
enum vieter_cron_parse_error vieter_cron_expr_parse(vieter_cron_expression *out,
const char *expression);
#endif

View File

@ -3,11 +3,11 @@
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));
}
void ce_free(vieter_cron_expression *ce) {
void vieter_cron_expr_free(vieter_cron_expression *ce) {
free(ce->months);
free(ce->days);
free(ce->hours);
@ -15,8 +15,9 @@ void ce_free(vieter_cron_expression *ce) {
free(ce);
}
void vieter_cron_next(vieter_cron_simple_time *out, vieter_cron_expression *ce,
vieter_cron_simple_time *ref) {
void vieter_cron_expr_next(vieter_cron_simple_time *out,
vieter_cron_expression *ce,
vieter_cron_simple_time *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
@ -100,8 +101,8 @@ void vieter_cron_next(vieter_cron_simple_time *out, vieter_cron_expression *ce,
}
}
void vieter_cron_next_from_now(vieter_cron_simple_time *out,
vieter_cron_expression *ce) {
void vieter_cron_expr_next_from_now(vieter_cron_simple_time *out,
vieter_cron_expression *ce) {
time_t t = time(NULL);
struct tm gm;
gmtime_r(&t, &gm);
@ -114,5 +115,5 @@ void vieter_cron_next_from_now(vieter_cron_simple_time *out,
.hour = gm.tm_hour,
.minute = gm.tm_min};
vieter_cron_next(out, ce, &ref);
vieter_cron_expr_next(out, ce, &ref);
}

View File

@ -41,9 +41,8 @@ const uint8_t max_parts = 4;
* - a/c
* - a-b/c
*/
vieter_cron_parse_error vieter_cron_expression_parse_range(uint64_t *out,
char *s, uint8_t min,
uint8_t max) {
vieter_cron_parse_error vieter_cron_expr_parse_range(uint64_t *out, char *s,
uint8_t min, uint8_t max) {
size_t slash_index = 0, dash_index = 0;
size_t s_index = 0;
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
* expressions, separated by commas.
*/
vieter_cron_parse_error ce_parse_part(uint64_t *out, char *s, uint8_t min,
uint8_t max) {
vieter_cron_parse_error vieter_cron_expr_parse_part(uint64_t *out, char *s,
uint8_t min, uint8_t max) {
*out = 0;
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) {
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) {
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
return vieter_cron_expression_parse_range(out, s, min, max);
return vieter_cron_expr_parse_range(out, s, min, max);
}
/*
@ -197,9 +196,8 @@ 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.
*/
vieter_cron_parse_error
vieter_cron_parse_expression(vieter_cron_expression *out,
const char *expression) {
vieter_cron_parse_error vieter_cron_expr_parse(vieter_cron_expression *out,
const char *expression) {
// The parsing functions modify the input string in-place
char *s = strdup(expression);
char *orig_s = s;
@ -259,7 +257,7 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
// Months
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) {
goto end;
@ -283,7 +281,8 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
if (part_count >= 3) {
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) {
free(out->months);
@ -301,7 +300,7 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
// Hours
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) {
free(out->months);
@ -315,7 +314,7 @@ vieter_cron_parse_expression(vieter_cron_expression *out,
// Minutes
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) {
free(out->months);

View File

@ -26,7 +26,7 @@ void test_not_allowed() {
while (expressions[i] != NULL) {
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++;
}