__P-protect dcls.
[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 void *xmalloc ();
53 void *xrealloc ();
54
55 #define STREQ(a,b) ((a) == (b) || ((a) && (b) && *(a) == *(b) \
56                                    && strcmp(a, b) == 0))
57
58 /* Initialize a tokenbuffer. */
59
60 void
61 init_tokenbuffer (token_buffer *tokenbuffer)
62 {
63   tokenbuffer->size = INITIAL_TOKEN_LENGTH;
64   tokenbuffer->buffer = ((char *) xmalloc (INITIAL_TOKEN_LENGTH));
65 }
66
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
73    isn't modified.
74
75    This function will work properly on lines containing NUL bytes
76    and on files that aren't newline-terminated.  */
77
78 long
79 readtoken (FILE *stream, const char *delim, int n_delim,
80            token_buffer *tokenbuffer)
81 {
82   char *p;
83   int c, i, n;
84   static const char *saved_delim = NULL;
85   static char isdelim[256];
86   int same_delimiters;
87
88   if (delim == NULL && saved_delim == NULL)
89     abort ();
90
91   same_delimiters = 0;
92   if (delim != saved_delim && saved_delim != NULL)
93     {
94       same_delimiters = 1;
95       for (i = 0; i < n_delim; i++)
96         {
97           if (delim[i] != saved_delim[i])
98             {
99               same_delimiters = 0;
100               break;
101             }
102         }
103     }
104
105   if (!same_delimiters)
106     {
107       const char *t;
108       saved_delim = delim;
109       for (i = 0; i < sizeof (isdelim); i++)
110         isdelim[i] = 0;
111       for (t = delim; *t; t++)
112         isdelim[(unsigned int) *t] = 1;
113     }
114
115   p = tokenbuffer->buffer;
116   n = tokenbuffer->size;
117   i = 0;
118
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))
122     {
123       /* empty */
124     }
125
126   for (;;)
127     {
128       if (i >= n)
129         {
130           n = 3 * (n / 2 + 1);
131           p = xrealloc (p, (unsigned int) n);
132         }
133       if (c < 0)
134         {
135           if (i == 0)
136             return (-1);
137           p[i] = 0;
138           break;
139         }
140       if (isdelim[c])
141         {
142           p[i] = 0;
143           break;
144         }
145       p[i++] = c;
146       c = getc (stream);
147     }
148
149   tokenbuffer->buffer = p;
150   tokenbuffer->size = n;
151   return (i);
152 }
153
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();
158
159    %%% Question: is it worth it to do a single
160    %%% realloc() of `tokens' just before returning? */
161
162 int
163 readtokens (FILE *stream, int projected_n_tokens,
164             const char *delim, int n_delim,
165             char ***tokens_out, long **token_lengths)
166 {
167   token_buffer tb, *token = &tb;
168   int token_length;
169   char **tokens;
170   long *lengths;
171   int sz;
172   int n_tokens;
173
174   n_tokens = 0;
175   if (projected_n_tokens > 0)
176     projected_n_tokens++;       /* add one for trailing NULL pointer */
177   else
178     projected_n_tokens = 64;
179   sz = projected_n_tokens;
180   tokens = (char **) xmalloc (sz * sizeof (char *));
181   lengths = (long *) xmalloc (sz * sizeof (long));
182
183   init_tokenbuffer (token);
184   for (;;)
185     {
186       char *tmp;
187       token_length = readtoken (stream, delim, n_delim, token);
188       if (n_tokens >= sz)
189         {
190           sz *= 2;
191           tokens = (char **) xrealloc (tokens, sz * sizeof (char *));
192           lengths = (long *) xrealloc (lengths, sz * sizeof (long));
193         }
194
195       if (token_length < 0)
196         {
197           /* don't increment n_tokens for NULL entry */
198           tokens[n_tokens] = NULL;
199           lengths[n_tokens] = -1;
200           break;
201         }
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));
206       n_tokens++;
207     }
208
209   free (token->buffer);
210   *tokens_out = tokens;
211   if (token_lengths != NULL)
212     *token_lengths = lengths;
213   return n_tokens;
214 }