1 /* readtokens.c -- Functions for reading tokens from an input stream.
2 Copyright (C) 1990-1991, 1999, 2001, 2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 Written by Jim Meyering. */
20 /* This almost supercedes xreadline stuff -- using delim="\n"
21 gives the same functionality, except that these functions
22 would never return empty lines.
25 - To allow '\0' as a delimiter, I will have to change
26 interfaces to permit specification of delimiter-string
34 #include "readtokens.h"
40 #include "unlocked-io.h"
43 #define STREQ(a,b) ((a) == (b) || ((a) && (b) && *(a) == *(b) \
44 && strcmp(a, b) == 0))
46 /* Initialize a tokenbuffer. */
49 init_tokenbuffer (token_buffer *tokenbuffer)
51 tokenbuffer->size = INITIAL_TOKEN_LENGTH;
52 tokenbuffer->buffer = xmalloc (INITIAL_TOKEN_LENGTH);
55 /* Read a token from `stream' into `tokenbuffer'.
56 Upon return, the token is in tokenbuffer->buffer and
57 has a trailing '\0' instead of the original delimiter.
58 The function value is the length of the token not including
59 the final '\0'. When EOF is reached (i.e. on the call
60 after the last token is read), -1 is returned and tokenbuffer
63 This function will work properly on lines containing NUL bytes
64 and on files that aren't newline-terminated. */
67 readtoken (FILE *stream,
70 token_buffer *tokenbuffer)
74 static const char *saved_delim = NULL;
75 static char isdelim[256];
78 if (delim == NULL && saved_delim == NULL)
82 if (delim != saved_delim && saved_delim != NULL)
85 for (i = 0; i < n_delim; i++)
87 if (delim[i] != saved_delim[i])
100 for (j = 0; j < sizeof (isdelim); j++)
102 for (t = delim; *t; t++)
103 isdelim[(unsigned char) *t] = 1;
106 p = tokenbuffer->buffer;
107 n = tokenbuffer->size;
110 /* FIXME: don't fool with this caching BS. Use strchr instead. */
111 /* skip over any leading delimiters */
112 for (c = getc (stream); c >= 0 && isdelim[c]; c = getc (stream))
122 p = xrealloc (p, (unsigned int) n);
140 tokenbuffer->buffer = p;
141 tokenbuffer->size = n;
145 /* Return a NULL-terminated array of pointers to tokens
146 read from `stream.' The number of tokens is returned
147 as the value of the function.
148 All storage is obtained through calls to malloc();
150 %%% Question: is it worth it to do a single
151 %%% realloc() of `tokens' just before returning? */
154 readtokens (FILE *stream,
155 int projected_n_tokens,
159 long **token_lengths)
161 token_buffer tb, *token = &tb;
169 if (projected_n_tokens > 0)
170 projected_n_tokens++; /* add one for trailing NULL pointer */
172 projected_n_tokens = 64;
173 sz = projected_n_tokens;
174 tokens = xmalloc (sz * sizeof (char *));
175 lengths = xmalloc (sz * sizeof (long));
177 init_tokenbuffer (token);
181 token_length = readtoken (stream, delim, n_delim, token);
185 tokens = xrealloc (tokens, sz * sizeof (char *));
186 lengths = xrealloc (lengths, sz * sizeof (long));
189 if (token_length < 0)
191 /* don't increment n_tokens for NULL entry */
192 tokens[n_tokens] = NULL;
193 lengths[n_tokens] = -1;
196 tmp = xmalloc ((token_length + 1) * sizeof (char));
197 lengths[n_tokens] = token_length;
198 tokens[n_tokens] = strncpy (tmp, token->buffer,
199 (unsigned) (token_length + 1));
203 free (token->buffer);
204 *tokens_out = tokens;
205 if (token_lengths != NULL)
206 *token_lengths = lengths;