GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
gis/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 gis/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 gis/token.c.

Function Documentation

◆ G_free_tokens()

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 gis/token.c.

References G_free(), and NULL.

Referenced by check_create_export_opts(), check_create_import_opts(), GPJ_grass_to_osr2(), V1_close_ogr(), and Vect__open_cursor_next_line_pg().

◆ G_number_of_tokens()

int G_number_of_tokens ( char **  tokens)

Return number of tokens.

Parameters
tokens
Returns
number of tokens

Definition at line 185 of file gis/token.c.

References NULL.

Referenced by G_set_keywords(), GPJ_grass_to_osr2(), and Vect__open_cursor_next_line_pg().

◆ G_tokenize()

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:

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

Definition at line 48 of file gis/token.c.

Referenced by check_create_export_opts(), check_create_import_opts(), check_mapset_in_layer_name(), G_set_keywords(), GPJ_grass_to_osr2(), and Vect__open_cursor_next_line_pg().

◆ G_tokenize2()

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

Tokenize string.

This function behaves similarly to G_tokenize().

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

Example:

char *str = "a,'b,c',d";
char **tokens1, **tokens2;
int ntok1, ntok2;
tokens1 = G_tokenize(str, ",");
ntok1 = G_number_of_tokens(tokens1);
tokens1 = G_tokenize2(str, ",", "'");
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 gis/token.c.