3 \brief String tokenization routines
5 This file contains various routines for splitting an input string into
6 tokens and returning them in form of a list. The goal is to mimic perl's
11 \version\verbatim $Id: tokenizer.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
18 /************************************************************************
19 * This function tokenizes a string based on the user-supplied delimiters
20 * list. The resulting tokens are returned into an array of strings.
21 *************************************************************************/
22 void gk_strtokenize(char *str
, char *delim
, gk_Tokens_t
*tokens
)
26 tokens
->strbuf
= gk_strdup(str
);
31 /* Scan once to determine the number of tokens */
32 for (ntoks
=0, i
=0; i
<slen
;) {
33 /* Consume all the consecutive characters from the delimiters list */
34 while (i
<slen
&& strchr(delim
, str
[i
]))
42 /* Consume all the consecutive characters from the token */
43 while (i
<slen
&& !strchr(delim
, str
[i
]))
48 tokens
->ntoks
= ntoks
;
49 tokens
->list
= (char **)gk_malloc(ntoks
*sizeof(char *), "strtokenize: tokens->list");
52 /* Scan a second time to mark and link the tokens */
53 for (ntoks
=0, i
=0; i
<slen
;) {
54 /* Consume all the consecutive characters from the delimiters list */
55 while (i
<slen
&& strchr(delim
, str
[i
]))
61 tokens
->list
[ntoks
++] = str
+i
;
63 /* Consume all the consecutive characters from the token */
64 while (i
<slen
&& !strchr(delim
, str
[i
]))
70 /************************************************************************
71 * This function frees the memory associated with a gk_Tokens_t
72 *************************************************************************/
73 void gk_freetokenslist(gk_Tokens_t
*tokens
)
75 gk_free((void *)&tokens
->list
, &tokens
->strbuf
, LTERM
);