diff options
author | Werner Koch <[email protected]> | 2017-07-17 11:00:44 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2017-07-17 13:53:16 +0000 |
commit | 849467870ee1c10e0a7b1e89cfc9e8214e4963fe (patch) | |
tree | c0c02cb105b0b928236636e453767425d85dd8f8 /common/stringhelp.c | |
parent | tests: Improve 'shell.scm' script. (diff) | |
download | gnupg-849467870ee1c10e0a7b1e89cfc9e8214e4963fe.tar.gz gnupg-849467870ee1c10e0a7b1e89cfc9e8214e4963fe.zip |
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 <[email protected]>
Diffstat (limited to '')
-rw-r--r-- | common/stringhelp.c | 36 |
1 files changed, 36 insertions, 0 deletions
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. */ |