From 903e6946115d690cd080f4f7d7b28f7ea0759e99 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 8 Jul 2008 14:07:34 +0200 Subject: [PATCH] util: add a_strsplit() Signed-off-by: Julien Danjou --- common/util.c | 35 +++++++++++++++++++++++++++++++++++ common/util.h | 1 + 2 files changed, 36 insertions(+) diff --git a/common/util.c b/common/util.c index c8a0e3578..35fb26468 100644 --- a/common/util.c +++ b/common/util.c @@ -214,4 +214,39 @@ a_exec(const char *cmd) p_delete(&path); } +/** Split a string in substring. + * \param str The string to split. + * \param len The string length. + * \param delim The string delimiter. + * \return A list of string NULL terminted. + */ +char ** +a_strsplit(const char *str, ssize_t len, char delim) +{ + char **retval; + int comp, i, j = 0, n = 1; + + /* count components */ + for(i = 0; i < len; i++) + if(str[i] == delim) + n++; + + retval = p_new(char *, n); + + for(i = 0, comp = 0; comp < n; comp++) + { + if(str[i] == delim) + i++; + + for(j = i; str[j] != delim && j < len; j++); + + retval[comp] = p_dup(&str[i], j - i + 1); + retval[comp][j - i] = '\0'; + + i = j; + } + + return retval; +} + // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/common/util.h b/common/util.h index 6867ce58f..f52d43bb0 100644 --- a/common/util.h +++ b/common/util.h @@ -328,6 +328,7 @@ const char * position_tostr(position_t); void *name_func_lookup(const char *, const name_func_link_t *); const char * name_func_rlookup(void *, const name_func_link_t *); void a_exec(const char *); +char ** a_strsplit(const char *, ssize_t, char); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80