use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE
[gnulib.git] / lib / argv-iter.c
1 /* Iterate over arguments from argv or --files0-from=FILE
2    Copyright (C) 2008-2011 Free Software Foundation, Inc.
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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Jim Meyering.  */
18
19 #include <config.h>
20 #include "argv-iter.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 /* The attribute __pure__ was added in gcc 2.96.  */
26 #undef _GL_ATTRIBUTE_PURE
27 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
28 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
29 #else
30 # define _GL_ATTRIBUTE_PURE /* empty */
31 #endif
32
33 struct argv_iterator
34 {
35   /* Test FP to determine whether in read-mode or argv-mode. */
36   /* file-mode: fp records position */
37   FILE *fp;
38   size_t item_idx;
39   char *tok;
40   size_t buf_len;
41
42   /* argv-mode: record just argv and current pointer */
43   char **arg_list;
44   char **p;
45 };
46
47 struct argv_iterator *
48 argv_iter_init_argv (char **argv)
49 {
50   struct argv_iterator *ai = malloc (sizeof *ai);
51   if (!ai)
52     return NULL;
53   ai->fp = NULL;
54   ai->arg_list = argv;
55   ai->p = argv;
56   return ai;
57 }
58
59 /* Initialize to read from the stream, FP.
60    The input is expected to contain a list of NUL-delimited tokens.  */
61 struct argv_iterator *
62 argv_iter_init_stream (FILE *fp)
63 {
64   struct argv_iterator *ai = malloc (sizeof *ai);
65   if (!ai)
66     return NULL;
67   ai->fp = fp;
68   ai->tok = NULL;
69   ai->buf_len = 0;
70
71   ai->item_idx = 0;
72   ai->arg_list = NULL;
73   return ai;
74 }
75
76 char *
77 argv_iter (struct argv_iterator *ai, enum argv_iter_err *err)
78 {
79   if (ai->fp)
80     {
81       ssize_t len = getdelim (&ai->tok, &ai->buf_len, '\0', ai->fp);
82       if (len < 0)
83         {
84           *err = feof (ai->fp) ? AI_ERR_EOF : AI_ERR_READ;
85           return NULL;
86         }
87
88       *err = AI_ERR_OK;
89       ai->item_idx++;
90       return ai->tok;
91     }
92   else
93     {
94       if (*(ai->p) == NULL)
95         {
96           *err = AI_ERR_EOF;
97           return NULL;
98         }
99       else
100         {
101           *err = AI_ERR_OK;
102           return *(ai->p++);
103         }
104     }
105 }
106
107 size_t _GL_ATTRIBUTE_PURE
108 argv_iter_n_args (struct argv_iterator const *ai)
109 {
110   return ai->fp ? ai->item_idx : ai->p - ai->arg_list;
111 }
112
113 void
114 argv_iter_free (struct argv_iterator *ai)
115 {
116   if (ai->fp)
117     free (ai->tok);
118   free (ai);
119 }