token.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include "gis.h"
00003 /* break buf into tokens. delimiters are replaced by NULLs
00004    and tokens array will point to varous locations in buf
00005    buf must not have a new line
00006 
00007    tokens = G_tokenize (string, delimstr);
00008    ntokens = G_number_of_tokens (tokens);
00009    G_free_tokens (tokens);
00010 
00011 
00012    NOTE G_free_tokens() must be called when you are finished with tokens to
00013    release the memory
00014 */
00015 
00016 /*   Given buf,  turn delimiters in '\0'  and place pointers to tokens
00017  *      in  tokens.
00018  */
00019 
00020 char **G_tokenize ( char *buf, char *delim)
00021 {
00022         int i;
00023         char **tokens;
00024 
00025         i = 0;
00026         while (*buf == ' ' || *buf == '\t')  /* needed for free () */
00027                 buf++;
00028 
00029         buf = G_store (buf);
00030 
00031         tokens = (char **) G_malloc (sizeof (char *));
00032 
00033         while (1)
00034         {
00035                 while (*buf == ' ' || *buf == '\t')
00036                         buf++;
00037                 if (*buf == 0)
00038                         break;
00039                 tokens[i++] = buf;
00040                 tokens = (char **) G_realloc ((char *) tokens, (i+1) * sizeof (char *));
00041 
00042                 while (*buf && (G_index(delim,*buf) == NULL))
00043                         buf++;
00044                 if (*buf == 0)
00045                         break;
00046                 *buf++ = 0;
00047         }
00048         tokens[i] = NULL;
00049 
00050         return (tokens);
00051 }
00052 
00053 int G_number_of_tokens(char **tokens)
00054 {
00055         int n;
00056 
00057         for (n = 0; tokens[n] != NULL ; n++)
00058         {
00059          /* nothing */
00060         }
00061         return n;
00062 }
00063 
00064 int G_free_tokens (char **tokens)
00065 {
00066     if (tokens[0] != NULL)
00067         free (tokens[0]);
00068     free (tokens);
00069     return (0);
00070 }

Generated on Wed Aug 23 17:49:23 2006 for GRASS by  doxygen 1.4.7