mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
common: New function split_fields.
* common/stringhelp.c (split_fields): New. * common/t-stringhelp.c: Include assert.h. (test_split_fields): New. (main): Call test. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
c41c46fa84
commit
5ba99d9302
3 changed files with 120 additions and 0 deletions
|
@ -1329,6 +1329,44 @@ strtokenize (const char *string, const char *delim)
|
|||
}
|
||||
|
||||
|
||||
/* Split a string into space delimited fields and remove leading and
|
||||
* trailing spaces from each field. 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.
|
||||
* 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 (char *string, char **array, int arraysize)
|
||||
{
|
||||
int n = 0;
|
||||
char *p, *pend;
|
||||
|
||||
for (p = string; *p == ' '; p++)
|
||||
;
|
||||
do
|
||||
{
|
||||
if (n == arraysize)
|
||||
break;
|
||||
array[n++] = p;
|
||||
pend = strchr (p, ' ');
|
||||
if (!pend)
|
||||
break;
|
||||
*pend++ = 0;
|
||||
for (p = pend; *p == ' '; p++)
|
||||
;
|
||||
}
|
||||
while (*p);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Version number parsing. */
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue