.
[gnulib.git] / lib / readtokens.c
1 /* readtokens.c  -- Functions for reading tokens from an input stream.
2    Copyright (C) 1990-1991 Jim Meyering.
3
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)
7    any later version.
8
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.
13
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.
17
18    Written by Jim Meyering. */
19
20 /* This almost supercedes xreadline stuff -- using delim="\n"
21    gives the same functionality, except that these functions
22    would never return empty lines.
23
24    To Do:
25      - To allow '\0' as a delimiter, I will have to change
26        interfaces to permit specification of delimiter-string
27        length.
28    */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <stdio.h>
35
36 #ifdef STDC_HEADERS
37 #include <stdlib.h>
38 #endif
39
40 #if defined (STDC_HEADERS) || defined(HAVE_STRING_H)
41 #include <string.h>
42 /* An ANSI string.h and pre-ANSI memory.h might conflict.  */
43 #if !defined (STDC_HEADERS) && defined (HAVE_MEMORY_H)
44 #include <memory.h>
45 #endif /* not STDC_HEADERS and HAVE_MEMORY_H */
46 #else /* not STDC_HEADERS and not HAVE_STRING_H */
47 #include <strings.h>
48 /* memory.h and strings.h conflict on some systems.  */
49 #endif /* not STDC_HEADERS and not HAVE_STRING_H */
50
51 #include "readtokens.h"
52 #include "xalloc.h"
53
54 #define STREQ(a,b) ((a) == (b) || ((a) && (b) && *(a) == *(b) \
55                                    && strcmp(a, b) == 0))
56
57 /* Initialize a tokenbuffer. */
58
59 void
60 init_tokenbuffer (token_buffer *tokenbuffer)
61 {
62   tokenbuffer->size = INITIAL_TOKEN_LENGTH;
63   tokenbuffer->buffer = ((char *) xmalloc (INITIAL_TOKEN_LENGTH));
64 }
65
66 /* Read a token from `stream' into `tokenbuffer'.
67    Upon return, the token is in tokenbuffer->buffer and
68    has a trailing '\0' instead of the original delimiter.
69    The function value is the length of the token not including
70    the final '\0'.  When EOF is reached (i.e. on the call
71    after the last token is read), -1 is returned and tokenbuffer
72    isn't modified.
73
74    This function will work properly on lines containing NUL bytes
75    and on files that aren't newline-terminated.  */
76
77 long
78 readtoken (FILE *stream, const char *delim, int n_delim,
79            token_buffer *tokenbuffer)
80 {
81   char *p;
82   int c, i, n;
83   static const char *saved_delim = NULL;
84   static char isdelim[256];
85   int same_delimiters;
86
87   if (delim == NULL && saved_delim == NULL)
88     abort ();
89
90   same_delimiters = 0;
91   if (delim != saved_delim && saved_delim != NULL)
92     {
93       same_delimiters = 1;
94       for (i = 0; i < n_delim; i++)
95         {
96           if (delim[i] != saved_delim[i])
97             {
98               same_delimiters = 0;
99               break;
100             }
101         }
102     }
103
104   if (!same_delimiters)
105     {
106       const char *t;
107       saved_delim = delim;
108       for (i = 0; i < sizeof (isdelim); i++)
109         isdelim[i] = 0;
110       for (t = delim; *t; t++)
111         isdelim[(unsigned int) *t] = 1;
112     }
113
114   p = tokenbuffer->buffer;
115   n = tokenbuffer->size;
116   i = 0;
117
118   /* FIXME: don't fool with this caching BS.  Use strchr instead.  */
119   /* skip over any leading delimiters */
120   for (c = getc (stream); c >= 0 && isdelim[c]; c = getc (stream))
121     {
122       /* empty */
123     }
124
125   for (;;)
126     {
127       if (i >= n)
128         {
129           n = 3 * (n / 2 + 1);
130           p = xrealloc (p, (unsigned int) n);
131         }
132       if (c < 0)
133         {
134           if (i == 0)
135             return (-1);
136           p[i] = 0;
137           break;
138         }
139       if (isdelim[c])
140         {
141           p[i] = 0;
142           break;
143         }
144       p[i++] = c;
145       c = getc (stream);
146     }
147
148   tokenbuffer->buffer = p;
149   tokenbuffer->size = n;
150   return (i);
151 }
152
153 /* Return a NULL-terminated array of pointers to tokens
154    read from `stream.'  The number of tokens is returned
155    as the value of the function.
156    All storage is obtained through calls to malloc();
157
158    %%% Question: is it worth it to do a single
159    %%% realloc() of `tokens' just before returning? */
160
161 int
162 readtokens (FILE *stream, int projected_n_tokens,
163             const char *delim, int n_delim,
164             char ***tokens_out, long **token_lengths)
165 {
166   token_buffer tb, *token = &tb;
167   int token_length;
168   char **tokens;
169   long *lengths;
170   int sz;
171   int n_tokens;
172
173   n_tokens = 0;
174   if (projected_n_tokens > 0)
175     projected_n_tokens++;       /* add one for trailing NULL pointer */
176   else
177     projected_n_tokens = 64;
178   sz = projected_n_tokens;
179   tokens = (char **) xmalloc (sz * sizeof (char *));
180   lengths = (long *) xmalloc (sz * sizeof (long));
181
182   init_tokenbuffer (token);
183   for (;;)
184     {
185       char *tmp;
186       token_length = readtoken (stream, delim, n_delim, token);
187       if (n_tokens >= sz)
188         {
189           sz *= 2;
190           tokens = (char **) xrealloc (tokens, sz * sizeof (char *));
191           lengths = (long *) xrealloc (lengths, sz * sizeof (long));
192         }
193
194       if (token_length < 0)
195         {
196           /* don't increment n_tokens for NULL entry */
197           tokens[n_tokens] = NULL;
198           lengths[n_tokens] = -1;
199           break;
200         }
201       tmp = (char *) xmalloc ((token_length + 1) * sizeof (char));
202       lengths[n_tokens] = token_length;
203       tokens[n_tokens] = strncpy (tmp, token->buffer,
204                                   (unsigned) (token_length + 1));
205       n_tokens++;
206     }
207
208   free (token->buffer);
209   *tokens_out = tokens;
210   if (token_lengths != NULL)
211     *token_lengths = lengths;
212   return n_tokens;
213 }