GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/token.c
Go to the documentation of this file.
1 
17 #include <stdlib.h>
18 #include <grass/gis.h>
19 
20 
33 char **G_tokenize(const char *buf, const char *delim)
34 {
35  int i;
36  char **tokens;
37  char *p;
38 
39  i = 0;
40  while (!G_index(delim, *buf) && (*buf == ' ' || *buf == '\t')) /* needed for G_free () */
41  buf++;
42 
43  p = G_store(buf);
44 
45  tokens = (char **)G_malloc(sizeof(char *));
46 
47  while (1) {
48  while (!G_index(delim, *p) && (*p == ' ' || *p == '\t'))
49  p++;
50  if (*p == 0)
51  break;
52  tokens[i++] = p;
53  tokens = (char **)G_realloc((char *)tokens, (i + 1) * sizeof(char *));
54 
55  while (*p && (G_index(delim, *p) == NULL))
56  p++;
57  if (*p == 0)
58  break;
59  *p++ = 0;
60  }
61  tokens[i] = NULL;
62 
63  return (tokens);
64 }
65 
66 
76 int G_number_of_tokens(char **tokens)
77 {
78  int n;
79 
80  for (n = 0; tokens[n] != NULL; n++) {
81  /* nothing */
82  }
83 
84  return n;
85 }
86 
87 
98 int G_free_tokens(char **tokens)
99 {
100  if (tokens[0] != NULL)
101  G_free(tokens[0]);
102  G_free(tokens);
103 
104  return (0);
105 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
int G_free_tokens(char **tokens)
Free memory allocated to tokens.
Definition: gis/token.c:98
char * G_index(const char *str, int delim)
delimiter
Definition: gis/index.c:16
char ** G_tokenize(const char *buf, const char *delim)
Tokenize string.
Definition: gis/token.c:33
int G_number_of_tokens(char **tokens)
Return number of tokens.
Definition: gis/token.c:76
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
int n
Definition: dataquad.c:291