Skip to content

Commit

Permalink
slist: new slist 'flb_slist_split_string()' function
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Feb 6, 2020
1 parent 54378cc commit 3813788
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/fluent-bit/flb_slist.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ struct flb_slist_entry {

int flb_slist_create(struct mk_list *list);
int flb_slist_add(struct mk_list *head, const char *str);
int flb_slist_add_n(struct mk_list *head, const char *str, int len);
void flb_slist_destroy(struct mk_list *list);
int flb_slist_split_string(struct mk_list *list, const char *str,
int separator, int max_split);
void flb_slist_dump(struct mk_list *list);

#endif
127 changes: 124 additions & 3 deletions src/flb_slist.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_slist.h>

/* Initialize slist */
int flb_slist_create(struct mk_list *list)
{
mk_list_init(list);
return 0;
}

/* Append string as a new node into the list */
int flb_slist_add(struct mk_list *head, const char *str)
/* Append 'len' bytes of 'str' as a new string node into the list */
int flb_slist_add_n(struct mk_list *head, const char *str, int len)
{
struct flb_slist_entry *e;

Expand All @@ -41,7 +42,7 @@ int flb_slist_add(struct mk_list *head, const char *str)
return -1;
}

e->str = flb_sds_create(str);
e->str = flb_sds_create_len(str, len);
if (!e->str) {
flb_free(e);
return -1;
Expand All @@ -51,6 +52,126 @@ int flb_slist_add(struct mk_list *head, const char *str)
return 0;
}

/* Append NULL terminated string as a new node into the list */
int flb_slist_add(struct mk_list *head, const char *str)
{
int len;

if (!str) {
return -1;
}

len = strlen(str);
if (len <= 0) {
return -1;
}

return flb_slist_add_n(head, str, len);
}

/*
* Split a string using a separator, every splitted content is appended to the end of
* the slist list head.
*/
int flb_slist_split_string(struct mk_list *list, const char *str,
int separator, int max_split)
{
int i = 0;
int ret;
int count = 0;
int val_len;
int len;
int end;
char *p_init;
char *p_end;

if (!str) {
return -1;
}

len = strlen(str);
while (i < len) {
end = mk_string_char_search(str + i, separator, len - i);
if (end < 0) {
end = len - i;
}

p_init = (char *) str + i;
p_end = p_init + end - 1;

/* Remove empty spaces */
while (*p_init == ' ') {
p_init++;
}
while (*p_end == ' ' && p_end >= p_init) {
p_end--;
}

if (p_init > p_end) {
goto next;
}

if (p_init == p_end) {
if (*p_init == ' ') {
goto next;
}
val_len = 1;
}
else {
val_len = p_end - p_init + 1;
}

if (val_len == 0) {
goto next;
}

ret = flb_slist_add_n(list, p_init, val_len);
if (ret == -1) {
return -1;
}
count++;

/* Append remaining string as a new node ? */
if (count >= max_split && max_split > 0) {
p_end = p_init + end;
if (*p_end == separator) {
p_end++;
}
while (*p_end == ' ') {
p_end++;
}

if ((p_end - str) >= len) {
break;
}

ret = flb_slist_add(list, p_end);
if (ret == -1) {
return -1;
}
count++;
break;
}

next:
i += end + 1;
}

return count;
}

void flb_slist_dump(struct mk_list *list)
{
struct mk_list *head;
struct flb_slist_entry *e;

printf("[slist %p]\n", list);
mk_list_foreach(head, list) {
e = mk_list_entry(head, struct flb_slist_entry, _head);
printf(" - '%s'\n", e->str);
}
}

void flb_slist_destroy(struct mk_list *list)
{
struct mk_list *tmp;
Expand Down

0 comments on commit 3813788

Please sign in to comment.