separator module #1
1 changed files with 32 additions and 32 deletions
|
@ -18,49 +18,49 @@ uint8_t version_patch(void) { return 0; }
|
||||||
const char *module_name(void) { return "sep"; }
|
const char *module_name(void) { return "sep"; }
|
||||||
|
|
||||||
static size_t length = 8;
|
static size_t length = 8;
|
||||||
static char *sep_char = (char[]){'-', '\0'};
|
static char *sep_char = "-";
|
||||||
static size_t sep_char_length = 1;
|
static size_t sep_char_length = 1;
|
||||||
|
|
||||||
void init(char **config) {
|
void init(char **config) {
|
||||||
for (char *opt = *config; opt != NULL; opt = *++config) {
|
for (char *opt = *config; opt != NULL; opt = *++config) {
|
||||||
if (strncmp(opt, "sep", 3) == 0) {
|
if (strncmp(opt, "sep", 3) == 0) {
|
||||||
char *valstr = opt + 4; // skip 'sep='
|
char *valstr = opt + 4; // skip 'sep='
|
||||||
long value = strtol(valstr, NULL, 10);
|
long value = strtol(valstr, NULL, 10);
|
||||||
|
|
||||||
if (value >= 0) {
|
if (value >= 0) {
|
||||||
length = (size_t) value;
|
length = (size_t) value;
|
||||||
}
|
}
|
||||||
} else if (strncmp(opt, "char", 4) == 0) {
|
} else if (strncmp(opt, "char", 4) == 0) {
|
||||||
char *value = opt + 5; // skip 'char='
|
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_length = strlen(value);
|
||||||
sep_char = malloc((sep_char_length + 1) * sizeof(*sep_char));
|
sep_char = malloc((sep_char_length + 1) * sizeof(char));
|
||||||
strncpy(sep_char, value, sep_char_length);
|
strncpy(sep_char, value, sep_char_length);
|
||||||
sep_char[sep_char_length] = '\0';
|
sep_char[sep_char_length] = '\0';
|
||||||
} else {
|
} else {
|
||||||
sep_char = NULL;
|
sep_char = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *get(void) {
|
const char *get(void) {
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
if (sep_char == NULL) {
|
if (sep_char == NULL) {
|
||||||
ret = malloc(1 * sizeof(*ret));
|
ret = malloc(1 * sizeof(char));
|
||||||
*ret = 0;
|
*ret = 0;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = malloc((length * sep_char_length + 1) * sizeof(*ret));
|
ret = malloc((length * sep_char_length + 1) * sizeof(char));
|
||||||
memset(ret, 0, length * sep_char_length + 1);
|
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);
|
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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue