wmii/libcext/tokenize.c

34 lines
560 B
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <string.h>
#include "cext.h"
size_t cext_tokenize(char **result, size_t reslen, char *str, char delim)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
char *p, *n;
size_t i = 0;
2005-11-18 18:54:58 +03:00
if (!str)
return 0;
for (n = str; *n == ' '; n++);
p = n;
for (i = 0; *n != 0;) {
2005-11-18 18:54:58 +03:00
if (i == reslen)
return i;
if (*n == delim) {
*n = 0;
2005-11-18 18:54:58 +03:00
if (strlen(p))
result[i++] = p;
p = ++n;
} else
n++;
}
if (strlen(p))
result[i++] = p;
2005-12-05 01:45:59 +03:00
return i; /* number of tokens */
2005-11-18 18:54:58 +03:00
}