From 11709cc611c02a4e9140409a0e81d639522c06f1 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 18 Jan 2023 14:12:02 +0100 Subject: [PATCH] refactor: slightly change api --- Makefile | 6 +++++- include/vieter_cron.h | 17 ++++++++--------- src/cron/expression.c | 15 ++++++++------- src/cron/parse.c | 27 +++++++++++++-------------- test/cron/test_parse.c | 2 +- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 313735c..238d4d3 100644 --- a/Makefile +++ b/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 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 diff --git a/include/vieter_cron.h b/include/vieter_cron.h index a6019a9..9088d4d 100644 --- a/include/vieter_cron.h +++ b/include/vieter_cron.h @@ -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 diff --git a/src/cron/expression.c b/src/cron/expression.c index f876bbc..42a8222 100644 --- a/src/cron/expression.c +++ b/src/cron/expression.c @@ -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); } diff --git a/src/cron/parse.c b/src/cron/parse.c index 247e052..5c8da06 100644 --- a/src/cron/parse.c +++ b/src/cron/parse.c @@ -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); diff --git a/test/cron/test_parse.c b/test/cron/test_parse.c index ee67027..030c720 100644 --- a/test/cron/test_parse.c +++ b/test/cron/test_parse.c @@ -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++; }