diff options
author | Neal H. Walfield <[email protected]> | 2015-03-12 12:03:50 +0000 |
---|---|---|
committer | Neal H. Walfield <[email protected]> | 2015-03-12 12:54:31 +0000 |
commit | e40636fefa7f9ded8216e3e55606ffc5c34cbce6 (patch) | |
tree | 24fa595ebea5ca130113baad1e96179bcf64bf71 /common/stringhelp.c | |
parent | agent: Improve error reporting from Pinentry. (diff) | |
download | gnupg-e40636fefa7f9ded8216e3e55606ffc5c34cbce6.tar.gz gnupg-e40636fefa7f9ded8216e3e55606ffc5c34cbce6.zip |
common: Add new helper function, strsplit.
* common/stringhelp.h (strsplit): New declaration.
* common/stringhelp.c (strsplit): New function.
* common/t-stringhelp.c (test_strsplit): New function.
(main): Call it here.
--
Diffstat (limited to 'common/stringhelp.c')
-rw-r--r-- | common/stringhelp.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/common/stringhelp.c b/common/stringhelp.c index 42e1bcbbb..61386cc38 100644 --- a/common/stringhelp.c +++ b/common/stringhelp.c @@ -2,6 +2,7 @@ * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, * 2008, 2009, 2010 Free Software Foundation, Inc. * Copyright (C) 2014 Werner Koch + * Copyright (C) 2015 g10 Code GmbH * * This file is part of JNLIB, which is a subsystem of GnuPG. * @@ -48,6 +49,7 @@ # include <windows.h> #endif +#include "util.h" #include "libjnlib-config.h" #include "utf8conv.h" #include "sysutils.h" @@ -1196,3 +1198,39 @@ xstrconcat (const char *s1, ...) } return result; } + +/* Split a string into fields at DELIM. REPLACEMENT is the character + to replace the delimiter with (normally: '\0' so that each field is + NUL terminated). The caller is responsible for freeing the result. + Note: this function modifies STRING! If you need the original + value, then you should pass a copy to this function. + + If malloc fails, this function returns NULL. */ +char ** +strsplit (char *string, char delim, char replacement, int *count) +{ + int fields = 1; + char *t; + char **result; + + /* First, count the number of fields. */ + for (t = strchr (string, delim); t; t = strchr (t + 1, delim)) + fields ++; + + result = xtrycalloc (sizeof (*result), (fields + 1)); + if (! result) + return NULL; + + result[0] = string; + fields = 1; + for (t = strchr (string, delim); t; t = strchr (t + 1, delim)) + { + result[fields ++] = t + 1; + *t = replacement; + } + + if (count) + *count = fields; + + return result; +} |