1 /* readtokens.c -- Functions for reading tokens from an input stream.
3 Copyright (C) 1990-1991, 1999-2004, 2006, 2009-2010 Free Software
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Written by Jim Meyering. */
21 /* This almost supercedes xreadline stuff -- using delim="\n"
22 gives the same functionality, except that these functions
23 would never return empty lines. */
27 #include "readtokens.h"
35 # include "unlocked-io.h"
38 /* Initialize a tokenbuffer. */
41 init_tokenbuffer (token_buffer *tokenbuffer)
43 tokenbuffer->size = 0;
44 tokenbuffer->buffer = NULL;
47 /* Read a token from STREAM into TOKENBUFFER.
48 A token is delimited by any of the N_DELIM bytes in DELIM.
49 Upon return, the token is in tokenbuffer->buffer and
50 has a trailing '\0' instead of any original delimiter.
51 The function value is the length of the token not including
52 the final '\0'. Upon EOF (i.e. on the call after the last
53 token is read) or error, return -1 without modifying tokenbuffer.
54 The EOF and error conditions may be distinguished in the caller
55 by testing ferror (STREAM).
57 This function works properly on lines containing NUL bytes
58 and on files do not end with a delimiter. */
61 readtoken (FILE *stream,
64 token_buffer *tokenbuffer)
69 static const char *saved_delim = NULL;
70 static char isdelim[256];
73 if (delim == NULL && saved_delim == NULL)
76 same_delimiters = false;
77 if (delim != saved_delim && saved_delim != NULL)
79 same_delimiters = true;
80 for (i = 0; i < n_delim; i++)
82 if (delim[i] != saved_delim[i])
84 same_delimiters = false;
94 memset (isdelim, 0, sizeof isdelim);
95 for (j = 0; j < n_delim; j++)
97 unsigned char ch = delim[j];
102 /* FIXME: don't fool with this caching. Use strchr instead. */
103 /* skip over any leading delimiters */
104 for (c = getc (stream); c >= 0 && isdelim[c]; c = getc (stream))
109 p = tokenbuffer->buffer;
110 n = tokenbuffer->size;
118 p = x2nrealloc (p, &n, sizeof *p);
134 tokenbuffer->buffer = p;
135 tokenbuffer->size = n;
139 /* Build a NULL-terminated array of pointers to tokens
140 read from STREAM. Return the number of tokens read.
141 All storage is obtained through calls to xmalloc-like functions.
143 %%% Question: is it worth it to do a single
144 %%% realloc() of `tokens' just before returning? */
147 readtokens (FILE *stream,
148 size_t projected_n_tokens,
152 size_t **token_lengths)
154 token_buffer tb, *token = &tb;
160 if (projected_n_tokens == 0)
161 projected_n_tokens = 64;
163 projected_n_tokens++; /* add one for trailing NULL pointer */
165 sz = projected_n_tokens;
166 tokens = xnmalloc (sz, sizeof *tokens);
167 lengths = xnmalloc (sz, sizeof *lengths);
170 init_tokenbuffer (token);
174 size_t token_length = readtoken (stream, delim, n_delim, token);
177 tokens = x2nrealloc (tokens, &sz, sizeof *tokens);
178 lengths = xnrealloc (lengths, sz, sizeof *lengths);
181 if (token_length == (size_t) -1)
183 /* don't increment n_tokens for NULL entry */
184 tokens[n_tokens] = NULL;
185 lengths[n_tokens] = 0;
188 tmp = xnmalloc (token_length + 1, sizeof *tmp);
189 lengths[n_tokens] = token_length;
190 tokens[n_tokens] = memcpy (tmp, token->buffer, token_length + 1);
194 free (token->buffer);
195 *tokens_out = tokens;
196 if (token_lengths != NULL)
197 *token_lengths = lengths;