1 /* readtokens.c -- Functions for reading tokens from an input stream.
2 Copyright (C) 1990-1991 Jim Meyering.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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
40 #if defined (STDC_HEADERS) || defined(HAVE_STRING_H)
42 /* An ANSI string.h and pre-ANSI memory.h might conflict. */
43 #if !defined (STDC_HEADERS) && defined (HAVE_MEMORY_H)
45 #endif /* not STDC_HEADERS and HAVE_MEMORY_H */
46 #else /* not STDC_HEADERS and not HAVE_STRING_H */
48 /* memory.h and strings.h conflict on some systems. */
49 #endif /* not STDC_HEADERS and not HAVE_STRING_H */
51 #include "readtokens.h"
55 #define STREQ(a,b) ((a) == (b) || ((a) && (b) && *(a) == *(b) \
56 && strcmp(a, b) == 0))
58 /* Initialize a tokenbuffer. */
61 init_tokenbuffer (token_buffer *tokenbuffer)
63 tokenbuffer->size = INITIAL_TOKEN_LENGTH;
64 tokenbuffer->buffer = ((char *) xmalloc (INITIAL_TOKEN_LENGTH));
67 /* Read a token from `stream' into `tokenbuffer'.
68 Upon return, the token is in tokenbuffer->buffer and
69 has a trailing '\0' instead of the original delimiter.
70 The function value is the length of the token not including
71 the final '\0'. When EOF is reached (i.e. on the call
72 after the last token is read), -1 is returned and tokenbuffer
75 This function will work properly on lines containing NUL bytes
76 and on files that aren't newline-terminated. */
79 readtoken (FILE *stream, const char *delim, int n_delim,
80 token_buffer *tokenbuffer)
84 static const char *saved_delim = NULL;
85 static char isdelim[256];
88 if (delim == NULL && saved_delim == NULL)
92 if (delim != saved_delim && saved_delim != NULL)
95 for (i = 0; i < n_delim; i++)
97 if (delim[i] != saved_delim[i])
105 if (!same_delimiters)
109 for (i = 0; i < sizeof (isdelim); i++)
111 for (t = delim; *t; t++)
112 isdelim[(unsigned int) *t] = 1;
115 p = tokenbuffer->buffer;
116 n = tokenbuffer->size;
119 /* FIXME: don't fool with this caching BS. Use strchr instead. */
120 /* skip over any leading delimiters */
121 for (c = getc (stream); c >= 0 && isdelim[c]; c = getc (stream))
131 p = xrealloc (p, (unsigned int) n);
149 tokenbuffer->buffer = p;
150 tokenbuffer->size = n;
154 /* Return a NULL-terminated array of pointers to tokens
155 read from `stream.' The number of tokens is returned
156 as the value of the function.
157 All storage is obtained through calls to malloc();
159 %%% Question: is it worth it to do a single
160 %%% realloc() of `tokens' just before returning? */
163 readtokens (FILE *stream, int projected_n_tokens,
164 const char *delim, int n_delim,
165 char ***tokens_out, long **token_lengths)
167 token_buffer tb, *token = &tb;
175 if (projected_n_tokens > 0)
176 projected_n_tokens++; /* add one for trailing NULL pointer */
178 projected_n_tokens = 64;
179 sz = projected_n_tokens;
180 tokens = (char **) xmalloc (sz * sizeof (char *));
181 lengths = (long *) xmalloc (sz * sizeof (long));
183 init_tokenbuffer (token);
187 token_length = readtoken (stream, delim, n_delim, token);
191 tokens = (char **) xrealloc (tokens, sz * sizeof (char *));
192 lengths = (long *) xrealloc (lengths, sz * sizeof (long));
195 if (token_length < 0)
197 /* don't increment n_tokens for NULL entry */
198 tokens[n_tokens] = NULL;
199 lengths[n_tokens] = -1;
202 tmp = (char *) xmalloc ((token_length + 1) * sizeof (char));
203 lengths[n_tokens] = token_length;
204 tokens[n_tokens] = strncpy (tmp, token->buffer,
205 (unsigned) (token_length + 1));
209 free (token->buffer);
210 *tokens_out = tokens;
211 if (token_lengths != NULL)
212 *token_lengths = lengths;