modfetch/modules/sep.c
jacekpoz a6a1d0c65e
ignore discarded qualifiers warning upon assigning string literal
ccls still shows me the warning and another one for the `diagnostic
ignored` saying `unknown warning group`, not sure if it's fixable in
ccls, might be time to move to clang/d? although I've heard it has
issues too, not sure
2024-02-12 00:44:13 +01:00

80 lines
2.1 KiB
C

/* SEP - separator module for modfetch
*
* options: <type> name (default value)
* <number> sep (8) specifies how long the separator should be
* <string> char (-) specifies the character (or string) to use for the separator
*
* author: krizej
* 09 Feb 2024
*/
#include <mod.h>
#include <string.h>
#include <stdlib.h>
static const semver _version = {
.major = 1,
.minor = 0,
.patch = 0,
};
semver version(void) { return _version; }
const char *name(void) { return "sep"; }
static size_t length = 8;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
static char *sep_char = "-";
#pragma GCC diagnostic pop
static size_t sep_char_length = 1;
uint8_t init(semver api_ver, char **config) {
if (!sveq(api_ver, API_VERSION)) {
return MFERR_APIVER;
}
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) { // 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;
}
}
}
return 0;
}
const char *get(void) {
char *ret;
if (sep_char == NULL) {
ret = malloc(1 * sizeof(char));
*ret = 0;
return ret;
}
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);
ret[length * sep_char_length] = '\0'; // just to be sure :-D
return ret;
}