From f5077b5180c58dec734d1c1a8d19c8632496a864 Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Thu, 26 Jul 2012 15:52:04 +0200 Subject: lib/strutils: add string_add_to_idarray() - parse and add to id list Signed-off-by: Milan Broz Signed-off-by: Karel Zak --- lib/strutils.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib/strutils.c') diff --git a/lib/strutils.c b/lib/strutils.c index 036ae0639..5dda13805 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -478,6 +478,33 @@ int string_to_idarray(const char *list, int ary[], size_t arysz, return n; } +/* + * Parses the array like string_to_idarray but if format is "+aaa,bbb" + * it adds fields to array instead of replacing them. + */ +int string_add_to_idarray(const char *list, int ary[], size_t arysz, + int *ary_pos, int (name2id)(const char *, size_t)) +{ + const char *list_add; + int r; + + if (!list || !*list || !ary_pos || + *ary_pos < 0 || (size_t) *ary_pos > arysz) + return -1; + + if (list[0] == '+') + list_add = &list[1]; + else { + list_add = list; + *ary_pos = 0; + } + + r = string_to_idarray(list_add, &ary[*ary_pos], arysz - *ary_pos, name2id); + if (r > 0) + *ary_pos += r; + return r; +} + /* * LIST ::= [, ] * -- cgit v1.2.3-55-g7522