1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <stdlib.h>
#include "gis.h"
/* break buf into tokens. delimiters are replaced by NULLs
and tokens array will point to varous locations in buf
buf must not have a new line
tokens = G_tokenize (string, delimstr);
ntokens = G_number_of_tokens (tokens);
G_free_tokens (tokens);
NOTE G_free_tokens() must be called when you are finished with tokens to
release the memory
*/
/* Given buf, turn delimiters in '\0' and place pointers to tokens
* in tokens.
*/
char **G_tokenize ( char *buf, char *delim)
{
int i;
char **tokens;
i = 0;
while (*buf == ' ' || *buf == '\t') /* needed for free () */
buf++;
buf = G_store (buf);
tokens = (char **) G_malloc (sizeof (char *));
while (1)
{
while (*buf == ' ' || *buf == '\t')
buf++;
if (*buf == 0)
break;
tokens[i++] = buf;
tokens = (char **) G_realloc ((char *) tokens, (i+1) * sizeof (char *));
while (*buf && (G_index(delim,*buf) == NULL))
buf++;
if (*buf == 0)
break;
*buf++ = 0;
}
tokens[i] = NULL;
return (tokens);
}
int G_number_of_tokens(char **tokens)
{
int n;
for (n = 0; tokens[n] != NULL ; n++)
{
/* nothing */
}
return n;
}
int G_free_tokens (char **tokens)
{
if (tokens[0] != NULL)
free (tokens[0]);
free (tokens);
return (0);
}
|