1 /* readtokens.c -- Functions for reading tokens from an input stream.
2 Copyright (C) 1990-1991, 1999 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 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
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 (tokenbuffer
)
62 token_buffer
*tokenbuffer
;
64 tokenbuffer
->size
= INITIAL_TOKEN_LENGTH
;
65 tokenbuffer
->buffer
= ((char *) xmalloc (INITIAL_TOKEN_LENGTH
));
68 /* Read a token from `stream' into `tokenbuffer'.
69 Upon return, the token is in tokenbuffer->buffer and
70 has a trailing '\0' instead of the original delimiter.
71 The function value is the length of the token not including
72 the final '\0'. When EOF is reached (i.e. on the call
73 after the last token is read), -1 is returned and tokenbuffer
76 This function will work properly on lines containing NUL bytes
77 and on files that aren't newline-terminated. */
80 readtoken (FILE *stream
,
83 token_buffer
*tokenbuffer
)
87 static const char *saved_delim
= NULL
;
88 static char isdelim
[256];
91 if (delim
== NULL
&& saved_delim
== NULL
)
95 if (delim
!= saved_delim
&& saved_delim
!= NULL
)
98 for (i
= 0; i
< n_delim
; i
++)
100 if (delim
[i
] != saved_delim
[i
])
108 if (!same_delimiters
)
112 for (i
= 0; i
< sizeof (isdelim
); i
++)
114 for (t
= delim
; *t
; t
++)
115 isdelim
[(unsigned int) *t
] = 1;
118 p
= tokenbuffer
->buffer
;
119 n
= tokenbuffer
->size
;
122 /* FIXME: don't fool with this caching BS. Use strchr instead. */
123 /* skip over any leading delimiters */
124 for (c
= getc (stream
); c
>= 0 && isdelim
[c
]; c
= getc (stream
))
134 p
= xrealloc (p
, (unsigned int) n
);
152 tokenbuffer
->buffer
= p
;
153 tokenbuffer
->size
= n
;
157 /* Return a NULL-terminated array of pointers to tokens
158 read from `stream.' The number of tokens is returned
159 as the value of the function.
160 All storage is obtained through calls to malloc();
162 %%% Question: is it worth it to do a single
163 %%% realloc() of `tokens' just before returning? */
166 readtokens (FILE *stream
,
167 int projected_n_tokens
,
171 long **token_lengths
)
173 token_buffer tb
, *token
= &tb
;
181 if (projected_n_tokens
> 0)
182 projected_n_tokens
++; /* add one for trailing NULL pointer */
184 projected_n_tokens
= 64;
185 sz
= projected_n_tokens
;
186 tokens
= (char **) xmalloc (sz
* sizeof (char *));
187 lengths
= (long *) xmalloc (sz
* sizeof (long));
189 init_tokenbuffer (token
);
193 token_length
= readtoken (stream
, delim
, n_delim
, token
);
197 tokens
= (char **) xrealloc (tokens
, sz
* sizeof (char *));
198 lengths
= (long *) xrealloc (lengths
, sz
* sizeof (long));
201 if (token_length
< 0)
203 /* don't increment n_tokens for NULL entry */
204 tokens
[n_tokens
] = NULL
;
205 lengths
[n_tokens
] = -1;
208 tmp
= (char *) xmalloc ((token_length
+ 1) * sizeof (char));
209 lengths
[n_tokens
] = token_length
;
210 tokens
[n_tokens
] = strncpy (tmp
, token
->buffer
,
211 (unsigned) (token_length
+ 1));
215 free (token
->buffer
);
216 *tokens_out
= tokens
;
217 if (token_lengths
!= NULL
)
218 *token_lengths
= lengths
;