separator module #1
1 changed files with 66 additions and 0 deletions
66
modules/sep.c
Normal file
66
modules/sep.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
/* 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>
|
||||
|
||||
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 = "-";
|
||||
krizej marked this conversation as resolved
Outdated
|
||||
static size_t sep_char_length = 1;
|
||||
|
||||
void init(char **config) {
|
||||
for (char *opt = *config; opt != NULL; opt = *++config) {
|
||||
krizej marked this conversation as resolved
Outdated
poz
commented
`for(...)` -> `for (...)`
same for if
krizej
commented
why not functions though boy why not functions though boy
poz
commented
functions are simply built different functions are simply built different
krizej
commented
that is why their braces should be on a new line ;-) that is why their braces should be on a new line ;-)
|
||||
if (strncmp(opt, "sep", 3) == 0) {
|
||||
krizej marked this conversation as resolved
Outdated
poz
commented
instead of instead of `!` use `== 0`, same in the if below
|
||||
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));
|
||||
krizej marked this conversation as resolved
Outdated
poz
commented
in all the mallocs do in all the mallocs do `sizeof(type)` even if the size is 1 byte
|
||||
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 * 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;
|
||||
}
|
Loading…
Reference in a new issue
implicitly cast the literal to
char*
because of a warningthat produces basically the same warning but don't worry i've found a way :-)
turn it back to "-"