util: add a_strsplit()
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
5a5a1b32c3
commit
903e694611
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue