Bug #1267: Integrate METIS graph partitioning library
[charm.git] / src / libs / ck-libs / metis / GKlib / tokenizer.c
blob5efd262db91307067bbbeee849d57232d55335ed
1 /*!
2 \file tokenizer.c
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
7 split function.
9 \date Started 11/23/04
10 \author George
11 \version\verbatim $Id: tokenizer.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim
15 #include <GKlib.h>
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)
24 int i, ntoks, slen;
26 tokens->strbuf = gk_strdup(str);
28 slen = strlen(str);
29 str = tokens->strbuf;
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]))
35 i++;
37 if (i == slen)
38 break;
40 ntoks++;
42 /* Consume all the consecutive characters from the token */
43 while (i<slen && !strchr(delim, str[i]))
44 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]))
56 str[i++] = '\0';
58 if (i == slen)
59 break;
61 tokens->list[ntoks++] = str+i;
63 /* Consume all the consecutive characters from the token */
64 while (i<slen && !strchr(delim, str[i]))
65 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);