1 /* readtokens.c -- Functions for reading tokens from an input stream.
3 Copyright (C) 1990-1991, 1999-2004, 2006 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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"
37 # include "unlocked-io.h"
40 #define STREQ(a,b) ((a) == (b) || ((a) && (b) && *(a) == *(b) \
41 && strcmp(a, b) == 0))
43 /* Initialize a tokenbuffer. */
46 init_tokenbuffer (token_buffer *tokenbuffer)
48 tokenbuffer->size = 0;
49 tokenbuffer->buffer = NULL;
52 /* Read a token from STREAM into TOKENBUFFER.
53 A token is delimited by any of the N_DELIM bytes in DELIM.
54 Upon return, the token is in tokenbuffer->buffer and
55 has a trailing '\0' instead of any original delimiter.
56 The function value is the length of the token not including
57 the final '\0'. Upon EOF (i.e. on the call after the last
58 token is read) or error, return -1 without modifying tokenbuffer.
59 The EOF and error conditions may be distinguished in the caller
60 by testing ferror (STREAM).
62 This function works properly on lines containing NUL bytes
63 and on files do not end with a delimiter. */
66 readtoken (FILE *stream,
69 token_buffer *tokenbuffer)
74 static const char *saved_delim = NULL;
75 static char isdelim[256];
78 if (delim == NULL && saved_delim == NULL)
81 same_delimiters = false;
82 if (delim != saved_delim && saved_delim != NULL)
84 same_delimiters = true;
85 for (i = 0; i < n_delim; i++)
87 if (delim[i] != saved_delim[i])
89 same_delimiters = false;
99 memset (isdelim, 0, sizeof isdelim);
100 for (j = 0; j < n_delim; j++)
102 unsigned char ch = delim[j];
107 /* FIXME: don't fool with this caching. Use strchr instead. */
108 /* skip over any leading delimiters */
109 for (c = getc (stream); c >= 0 && isdelim[c]; c = getc (stream))
114 p = tokenbuffer->buffer;
115 n = tokenbuffer->size;
123 p = x2nrealloc (p, &n, sizeof *p);
139 tokenbuffer->buffer = p;
140 tokenbuffer->size = n;
144 /* Build a NULL-terminated array of pointers to tokens
145 read from STREAM. Return the number of tokens read.
146 All storage is obtained through calls to xmalloc-like functions.
148 %%% Question: is it worth it to do a single
149 %%% realloc() of `tokens' just before returning? */
152 readtokens (FILE *stream,
153 size_t projected_n_tokens,
157 size_t **token_lengths)
159 token_buffer tb, *token = &tb;
165 if (projected_n_tokens == 0)
166 projected_n_tokens = 64;
168 projected_n_tokens++; /* add one for trailing NULL pointer */
170 sz = projected_n_tokens;
171 tokens = xnmalloc (sz, sizeof *tokens);
172 lengths = xnmalloc (sz, sizeof *lengths);
175 init_tokenbuffer (token);
179 size_t token_length = readtoken (stream, delim, n_delim, token);
182 tokens = x2nrealloc (tokens, &sz, sizeof *tokens);
183 lengths = xnrealloc (lengths, sz, sizeof *lengths);
186 if (token_length == (size_t) -1)
188 /* don't increment n_tokens for NULL entry */
189 tokens[n_tokens] = NULL;
190 lengths[n_tokens] = 0;
193 tmp = xnmalloc (token_length + 1, sizeof *tmp);
194 lengths[n_tokens] = token_length;
195 tokens[n_tokens] = memcpy (tmp, token->buffer, token_length + 1);
199 free (token->buffer);
200 *tokens_out = tokens;
201 if (token_lengths != NULL)
202 *token_lengths = lengths;