GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
token.c File Reference

GIS Library - Tokenize strings. More...

#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/glocale.h>
Include dependency graph for token.c:

Go to the source code of this file.

Functions

char ** G_tokenize (const char *buf, const char *delim)
 Tokenize string. More...
 
char ** G_tokenize2 (const char *buf, const char *delim, const char *valchar)
 Tokenize string. More...
 
int G_number_of_tokens (char **tokens)
 Return number of tokens. More...
 
void G_free_tokens (char **tokens)
 Free memory allocated to tokens. More...
 

Detailed Description

GIS Library - Tokenize strings.

(C) 2001-2008, 2011-2013 by the GRASS Development Team

This program is free software under the GNU General Public License (>=v2). Read the file COPYING that comes with GRASS for details.

Author
USA CERL and others

Definition in file token.c.

Function Documentation

void G_free_tokens ( char **  tokens)

Free memory allocated to tokens.

Note: G_free_tokens() must be called when finished with tokens to release memory.

Parameters
[out]tokens

Definition at line 204 of file token.c.

References G_free(), and NULL.

Referenced by G_get_window(), and G_parser().

int G_number_of_tokens ( char **  tokens)

Return number of tokens.

Parameters
tokens
Returns
number of tokens

Definition at line 185 of file token.c.

References NULL.

Referenced by G_set_keywords().

char** G_tokenize ( const char *  buf,
const char *  delim 
)

Tokenize string.

Given a string, buf, turn delimiter, delim, into '\0' (NULL) and place pointers to tokens in tokens. buf must not contain a new line (
). delim may consist of more than one character. G_free_tokens() must be called when finished with tokens to release memory.

Example:

1 char **tokens;
2 int ntok, i;
3 tokens = G_tokenize(buf, " |:,");
4 ntok = G_number_of_tokens(tokens);
5 for (i=0; i < ntok; i++) {
6  G_debug(1, "%d=[%s]", i, tokens[i]);
7 }
8 G_free_tokens(tokens);
Parameters
bufinput string
delimstring delimiter
Returns
pointer to string token

Definition at line 48 of file token.c.

References NULL.

Referenced by G_get_window(), G_parser(), and G_set_keywords().

char** G_tokenize2 ( const char *  buf,
const char *  delim,
const char *  valchar 
)

Tokenize string.

This fuction behaves similarly to G_tokenize().

It introduces valchar which defines borders of token. Within token delim is ignored.

Example:

1 char *str = "a,'b,c',d";
2 
3 char **tokens1, **tokens2;
4 int ntok1, ntok2;
5 
6 tokens1 = G_tokenize(str, ",");
7 ntok1 = G_number_of_tokens(tokens1);
8 
9 tokens1 = G_tokenize2(str, ",", "'");
10 ntok2 = G_number_of_tokens(tokens2);

In this example ntok1 will be 4, ntok2 only 3, i.e. { "a", "'b, c'", "d"}

Parameters
bufinput string
delimstring delimiter
valcharcharacter defining border of token
Returns
pointer to string token

Definition at line 84 of file token.c.