tokenizer.c
Go to the documentation of this file.
1 
15 #include <GKlib.h>
16 
17 
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)
23 {
24  int i, ntoks, slen;
25 
26  tokens->strbuf = gk_strdup(str);
27 
28  slen = strlen(str);
29  str = tokens->strbuf;
30 
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++;
36 
37  if (i == slen)
38  break;
39 
40  ntoks++;
41 
42  /* Consume all the consecutive characters from the token */
43  while (i<slen && !strchr(delim, str[i]))
44  i++;
45  }
46 
47 
48  tokens->ntoks = ntoks;
49  tokens->list = (char **)gk_malloc(ntoks*sizeof(char *), "strtokenize: tokens->list");
50 
51 
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';
57 
58  if (i == slen)
59  break;
60 
61  tokens->list[ntoks++] = str+i;
62 
63  /* Consume all the consecutive characters from the token */
64  while (i<slen && !strchr(delim, str[i]))
65  i++;
66  }
67 }
68 
69 
70 /************************************************************************
71 * This function frees the memory associated with a gk_Tokens_t
72 *************************************************************************/
74 {
75  gk_free((void *)&tokens->list, &tokens->strbuf, LTERM);
76 }
77 
gk_strdup
char * gk_strdup(char *orgstr)
Duplicates a string.
Definition: string.c:372
gk_free
void gk_free(void **ptr1,...)
Definition: memory.c:202
gk_Tokens_t::strbuf
char * strbuf
Definition: gk_struct.h:141
gk_Tokens_t
Definition: gk_struct.h:139
gk_Tokens_t::list
char ** list
Definition: gk_struct.h:142
gk_strtokenize
void gk_strtokenize(char *str, char *delim, gk_Tokens_t *tokens)
Definition: tokenizer.c:22
gk_Tokens_t::ntoks
int ntoks
Definition: gk_struct.h:140
LTERM
#define LTERM
Definition: gk_defs.h:14
str
Definition: pytypes.h:1558
GKlib.h
gk_freetokenslist
void gk_freetokenslist(gk_Tokens_t *tokens)
Definition: tokenizer.c:73
gk_malloc
void * gk_malloc(size_t nbytes, char *msg)
Definition: memory.c:140
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:14