From 849467870ee1c10e0a7b1e89cfc9e8214e4963fe Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Mon, 17 Jul 2017 13:00:44 +0200 Subject: common: New function split_fields_colon. * common/stringhelp.c (split_fields_colon): New. * common/t-stringhelp.c (test_split_fields_colon): New test. (main): Call that test. Signed-off-by: Werner Koch --- common/stringhelp.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'common/stringhelp.c') diff --git a/common/stringhelp.c b/common/stringhelp.c index 3b481e862..0abac8ae5 100644 --- a/common/stringhelp.c +++ b/common/stringhelp.c @@ -1339,6 +1339,42 @@ split_fields (char *string, char **array, int arraysize) } +/* Split a string into colon delimited fields A pointer to each field + * is stored in ARRAY. Stop splitting at ARRAYSIZE fields. The + * function modifies STRING. The number of parsed fields is returned. + * Note that leading and trailing spaces are not removed from the fields. + * Example: + * + * char *fields[2]; + * if (split_fields (string, fields, DIM (fields)) < 2) + * return // Not enough args. + * foo (fields[0]); + * foo (fields[1]); + */ +int +split_fields_colon (char *string, char **array, int arraysize) +{ + int n = 0; + char *p, *pend; + + p = string; + do + { + if (n == arraysize) + break; + array[n++] = p; + pend = strchr (p, ':'); + if (!pend) + break; + *pend++ = 0; + p = pend; + } + while (*p); + + return n; +} + + /* Version number parsing. */ -- cgit v1.2.3