projects
/
gnulib.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ChangeLog: s/^ /\t/
[gnulib.git]
/
lib
/
readtokens.c
diff --git
a/lib/readtokens.c
b/lib/readtokens.c
index
438441c
..
672bc8f
100644
(file)
--- a/
lib/readtokens.c
+++ b/
lib/readtokens.c
@@
-1,10
+1,12
@@
/* readtokens.c -- Functions for reading tokens from an input stream.
/* readtokens.c -- Functions for reading tokens from an input stream.
- Copyright (C) 1990-1991, 1999-2004 Free Software Foundation, Inc.
- This program is free software; you can redistribute it and/or modify
+ Copyright (C) 1990-1991, 1999-2004, 2006, 2009-2011 Free Software
+ Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version
2, or (at your option)
- any later version.
+ the Free Software Foundation; either version
3 of the License, or
+
(at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@@
-12,8
+14,7
@@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Jim Meyering. */
Written by Jim Meyering. */
@@
-21,9
+22,7
@@
gives the same functionality, except that these functions
would never return empty lines. */
gives the same functionality, except that these functions
would never return empty lines. */
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
#include "readtokens.h"
#include "readtokens.h"
@@
-32,11
+31,11
@@
#include <string.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>
-#include "unlocked-io.h"
#include "xalloc.h"
#include "xalloc.h"
-#define STREQ(a,b) ((a) == (b) || ((a) && (b) && *(a) == *(b) \
- && strcmp(a, b) == 0))
+#if USE_UNLOCKED_IO
+# include "unlocked-io.h"
+#endif
/* Initialize a tokenbuffer. */
/* Initialize a tokenbuffer. */
@@
-62,9
+61,9
@@
init_tokenbuffer (token_buffer *tokenbuffer)
size_t
readtoken (FILE *stream,
size_t
readtoken (FILE *stream,
- const char *delim,
- size_t n_delim,
- token_buffer *tokenbuffer)
+
const char *delim,
+
size_t n_delim,
+
token_buffer *tokenbuffer)
{
char *p;
int c;
{
char *p;
int c;
@@
-81,13
+80,13
@@
readtoken (FILE *stream,
{
same_delimiters = true;
for (i = 0; i < n_delim; i++)
{
same_delimiters = true;
for (i = 0; i < n_delim; i++)
- {
- if (delim[i] != saved_delim[i])
- {
- same_delimiters = false;
- break;
- }
- }
+
{
+
if (delim[i] != saved_delim[i])
+
{
+
same_delimiters = false;
+
break;
+
}
+
}
}
if (!same_delimiters)
}
if (!same_delimiters)
@@
-96,7
+95,10
@@
readtoken (FILE *stream,
saved_delim = delim;
memset (isdelim, 0, sizeof isdelim);
for (j = 0; j < n_delim; j++)
saved_delim = delim;
memset (isdelim, 0, sizeof isdelim);
for (j = 0; j < n_delim; j++)
- isdelim[(unsigned char) delim[j]] = 1;
+ {
+ unsigned char ch = delim[j];
+ isdelim[ch] = 1;
+ }
}
/* FIXME: don't fool with this caching. Use strchr instead. */
}
/* FIXME: don't fool with this caching. Use strchr instead. */
@@
-112,21
+114,21
@@
readtoken (FILE *stream,
for (;;)
{
if (c < 0 && i == 0)
for (;;)
{
if (c < 0 && i == 0)
- return -1;
+
return -1;
if (i == n)
if (i == n)
- p = x2nrealloc (p, &n, sizeof *p);
+
p = x2nrealloc (p, &n, sizeof *p);
if (c < 0)
if (c < 0)
- {
- p[i] = 0;
- break;
- }
+
{
+
p[i] = 0;
+
break;
+
}
if (isdelim[c])
if (isdelim[c])
- {
- p[i] = 0;
- break;
- }
+
{
+
p[i] = 0;
+
break;
+
}
p[i++] = c;
c = getc (stream);
}
p[i++] = c;
c = getc (stream);
}
@@
-145,11
+147,11
@@
readtoken (FILE *stream,
size_t
readtokens (FILE *stream,
size_t
readtokens (FILE *stream,
- size_t projected_n_tokens,
- const char *delim,
- size_t n_delim,
- char ***tokens_out,
- size_t **token_lengths)
+
size_t projected_n_tokens,
+
const char *delim,
+
size_t n_delim,
+
char ***tokens_out,
+
size_t **token_lengths)
{
token_buffer tb, *token = &tb;
char **tokens;
{
token_buffer tb, *token = &tb;
char **tokens;
@@
-160,7
+162,7
@@
readtokens (FILE *stream,
if (projected_n_tokens == 0)
projected_n_tokens = 64;
else
if (projected_n_tokens == 0)
projected_n_tokens = 64;
else
- projected_n_tokens++; /* add one for trailing NULL pointer */
+ projected_n_tokens++;
/* add one for trailing NULL pointer */
sz = projected_n_tokens;
tokens = xnmalloc (sz, sizeof *tokens);
sz = projected_n_tokens;
tokens = xnmalloc (sz, sizeof *tokens);
@@
-173,18
+175,18
@@
readtokens (FILE *stream,
char *tmp;
size_t token_length = readtoken (stream, delim, n_delim, token);
if (n_tokens >= sz)
char *tmp;
size_t token_length = readtoken (stream, delim, n_delim, token);
if (n_tokens >= sz)
- {
- tokens = x2nrealloc (tokens, &sz, sizeof *tokens);
- lengths = xnrealloc (lengths, sz, sizeof *lengths);
- }
+
{
+
tokens = x2nrealloc (tokens, &sz, sizeof *tokens);
+
lengths = xnrealloc (lengths, sz, sizeof *lengths);
+
}
if (token_length == (size_t) -1)
if (token_length == (size_t) -1)
- {
- /* don't increment n_tokens for NULL entry */
- tokens[n_tokens] = NULL;
- lengths[n_tokens] = 0;
- break;
- }
+
{
+
/* don't increment n_tokens for NULL entry */
+
tokens[n_tokens] = NULL;
+
lengths[n_tokens] = 0;
+
break;
+
}
tmp = xnmalloc (token_length + 1, sizeof *tmp);
lengths[n_tokens] = token_length;
tokens[n_tokens] = memcpy (tmp, token->buffer, token_length + 1);
tmp = xnmalloc (token_length + 1, sizeof *tmp);
lengths[n_tokens] = token_length;
tokens[n_tokens] = memcpy (tmp, token->buffer, token_length + 1);
@@
-195,5
+197,7
@@
readtokens (FILE *stream,
*tokens_out = tokens;
if (token_lengths != NULL)
*token_lengths = lengths;
*tokens_out = tokens;
if (token_lengths != NULL)
*token_lengths = lengths;
+ else
+ free (lengths);
return n_tokens;
}
return n_tokens;
}