From 0803746830363deae152d867c6cacfd599c91023 Mon Sep 17 00:00:00 2001 From: krizej <60076189+krizej@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:20:42 +0100 Subject: [PATCH 1/3] sep module --- modules/sep.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 modules/sep.c diff --git a/modules/sep.c b/modules/sep.c new file mode 100644 index 0000000..29df414 --- /dev/null +++ b/modules/sep.c @@ -0,0 +1,66 @@ +/* SEP - separator module for modfetch + * + * options: name (default value) + * sep (8) specifies how long the separator should be + * char (-) specifies the character (or string) to use for the separator + * + * author: krizej + * 09 Feb 2024 + */ + +#include "../mod.h" +#include +#include + +uint8_t version_major(void) { return 1; } +uint8_t version_minor(void) { return 0; } +uint8_t version_patch(void) { return 0; } +const char *module_name(void) { return "sep"; } + +static size_t length = 8; +static char *sep_char = "-"; +static size_t sep_char_length = 1; + +void init(char **config) { + for(char *opt = *config; opt != NULL; opt = *++config) { + if(!strncmp(opt, "sep", 3)) { + char *valstr = opt + 4; // skip 'sep=' + long value = strtol(valstr, NULL, 10); + + if(value >= 0) { + length = (size_t) value; + } + } else if(!strncmp(opt, "char", 4)) { + char *value = opt + 5; // skip 'char=' + + if(*value != 0) { // todo: see if there are any more characters that should be rejected + sep_char_length = strlen(value); + sep_char = malloc(sep_char_length + 1); + strncpy(sep_char, value, sep_char_length); + sep_char[sep_char_length] = '\0'; + } else { + sep_char = NULL; + } + } + } +} + +const char *get(void) { + char *ret; + + if(sep_char == NULL) { + ret = malloc(1); + *ret = 0; + return ret; + } + + ret = malloc(length * sep_char_length + 1); + memset(ret, 0, length * sep_char_length + 1); + + for(size_t i = 0; i < length; i++) + strncat(ret, sep_char, sep_char_length); + + ret[length * sep_char_length] = '\0'; // just to be sure :-D + + return ret; +} -- 2.46.0 From 6104ed8199ae5d06cfaf0a9a68f6628be4cd386e Mon Sep 17 00:00:00 2001 From: krizej <60076189+krizej@users.noreply.github.com> Date: Fri, 9 Feb 2024 22:17:20 +0100 Subject: [PATCH 2/3] code style --- modules/sep.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/sep.c b/modules/sep.c index 29df414..b4284d7 100644 --- a/modules/sep.c +++ b/modules/sep.c @@ -18,24 +18,24 @@ uint8_t version_patch(void) { return 0; } const char *module_name(void) { return "sep"; } static size_t length = 8; -static char *sep_char = "-"; +static char *sep_char = (char[]){'-', '\0'}; static size_t sep_char_length = 1; void init(char **config) { - for(char *opt = *config; opt != NULL; opt = *++config) { - if(!strncmp(opt, "sep", 3)) { + for (char *opt = *config; opt != NULL; opt = *++config) { + if (strncmp(opt, "sep", 3) == 0) { char *valstr = opt + 4; // skip 'sep=' long value = strtol(valstr, NULL, 10); - if(value >= 0) { + if (value >= 0) { length = (size_t) value; } - } else if(!strncmp(opt, "char", 4)) { + } else if (strncmp(opt, "char", 4) == 0) { char *value = opt + 5; // skip 'char=' - if(*value != 0) { // todo: see if there are any more characters that should be rejected + if (*value != 0) { // todo: see if there are any more characters that should be rejected sep_char_length = strlen(value); - sep_char = malloc(sep_char_length + 1); + sep_char = malloc((sep_char_length + 1) * sizeof(*sep_char)); strncpy(sep_char, value, sep_char_length); sep_char[sep_char_length] = '\0'; } else { @@ -48,16 +48,16 @@ void init(char **config) { const char *get(void) { char *ret; - if(sep_char == NULL) { - ret = malloc(1); + if (sep_char == NULL) { + ret = malloc(1 * sizeof(*ret)); *ret = 0; return ret; } - ret = malloc(length * sep_char_length + 1); + ret = malloc((length * sep_char_length + 1) * sizeof(*ret)); memset(ret, 0, length * sep_char_length + 1); - for(size_t i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) strncat(ret, sep_char, sep_char_length); ret[length * sep_char_length] = '\0'; // just to be sure :-D -- 2.46.0 From f1a28fb6b494fb1d1ac9c81212d93ed36b9227a0 Mon Sep 17 00:00:00 2001 From: krizej <60076189+krizej@users.noreply.github.com> Date: Fri, 9 Feb 2024 22:43:44 +0100 Subject: [PATCH 3/3] =?UTF-8?q?9RUTJM039a\\IBTDQB5bQC7Lc8;R;RZF]T@ED0FFBQ?= =?UTF-8?q?=3F\\G;9[=3FL0S<^B7@45YH8UU91]QNG?= =?UTF-8?q?=C2=B4}\x94\x9fOo6\x9b=C2=B7\x9a\\w\x81tW]=C2=B381`\x92VMW>K?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/sep.c | 64 +++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/modules/sep.c b/modules/sep.c index b4284d7..e3e9916 100644 --- a/modules/sep.c +++ b/modules/sep.c @@ -18,49 +18,49 @@ uint8_t version_patch(void) { return 0; } const char *module_name(void) { return "sep"; } static size_t length = 8; -static char *sep_char = (char[]){'-', '\0'}; +static char *sep_char = "-"; static size_t sep_char_length = 1; void init(char **config) { - for (char *opt = *config; opt != NULL; opt = *++config) { - if (strncmp(opt, "sep", 3) == 0) { - char *valstr = opt + 4; // skip 'sep=' - long value = strtol(valstr, NULL, 10); + for (char *opt = *config; opt != NULL; opt = *++config) { + if (strncmp(opt, "sep", 3) == 0) { + char *valstr = opt + 4; // skip 'sep=' + long value = strtol(valstr, NULL, 10); - if (value >= 0) { - length = (size_t) value; - } - } else if (strncmp(opt, "char", 4) == 0) { - char *value = opt + 5; // skip 'char=' + if (value >= 0) { + length = (size_t) value; + } + } else if (strncmp(opt, "char", 4) == 0) { + char *value = opt + 5; // skip 'char=' - if (*value != 0) { // todo: see if there are any more characters that should be rejected - sep_char_length = strlen(value); - sep_char = malloc((sep_char_length + 1) * sizeof(*sep_char)); - strncpy(sep_char, value, sep_char_length); - sep_char[sep_char_length] = '\0'; - } else { - sep_char = NULL; - } - } - } + if (*value != 0) { // todo: see if there are any more characters that should be rejected + sep_char_length = strlen(value); + sep_char = malloc((sep_char_length + 1) * sizeof(char)); + strncpy(sep_char, value, sep_char_length); + sep_char[sep_char_length] = '\0'; + } else { + sep_char = NULL; + } + } + } } const char *get(void) { - char *ret; + char *ret; - if (sep_char == NULL) { - ret = malloc(1 * sizeof(*ret)); - *ret = 0; - return ret; - } + if (sep_char == NULL) { + ret = malloc(1 * sizeof(char)); + *ret = 0; + return ret; + } - ret = malloc((length * sep_char_length + 1) * sizeof(*ret)); - memset(ret, 0, length * sep_char_length + 1); + ret = malloc((length * sep_char_length + 1) * sizeof(char)); + memset(ret, 0, length * sep_char_length + 1); - for (size_t i = 0; i < length; i++) - strncat(ret, sep_char, sep_char_length); + for (size_t i = 0; i < length; i++) + strncat(ret, sep_char, sep_char_length); - ret[length * sep_char_length] = '\0'; // just to be sure :-D + ret[length * sep_char_length] = '\0'; // just to be sure :-D - return ret; + return ret; } -- 2.46.0