ab41e76ce43b4a243507e319520600119327626d
[gnulib.git] / lib / argz.in.h
1 /* Routines for dealing with '\0' separated arg vectors.
2    Copyright (C) 1995,96,97,98,99,2000,2004,2007 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _ARGZ_H
19 #define _ARGZ_H 1
20
21
22 #define __need_error_t
23 #include <errno.h>
24 #include <string.h>             /* Need size_t, and strchr is called below.  */
25
26 #ifndef const
27 # define const const
28 #endif
29
30 #ifndef __error_t_defined
31 typedef int error_t;
32 #endif
33
34
35
36 /* Make a '\0' separated arg vector from a unix argv vector, returning it in
37    ARGZ, and the total length in LEN.  If a memory allocation error occurs,
38    ENOMEM is returned, otherwise 0.  The result can be destroyed using free. */
39
40 extern error_t argz_create (char *const __argv[], char **restrict __argz,
41                             size_t *restrict __len);
42
43 /* Make a '\0' separated arg vector from a SEP separated list in
44    STRING, returning it in ARGZ, and the total length in LEN.  If a
45    memory allocation error occurs, ENOMEM is returned, otherwise 0.
46    The result can be destroyed using free.  */
47
48 extern error_t argz_create_sep (const char *restrict string,
49                                 int __sep, char **restrict __argz,
50                                 size_t *restrict __len);
51
52 /* Returns the number of strings in ARGZ.  */
53
54 extern size_t argz_count (const char *__argz, size_t __len)
55 ;
56
57 /* Puts pointers to each string in ARGZ into ARGV, which must be large enough
58    to hold them all.  */
59
60 extern void argz_extract (const char *restrict __argz, size_t __len,
61                           char **restrict __argv);
62
63 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
64    except the last into the character SEP.  */
65
66 extern void argz_stringify (char *__argz, size_t __len, int __sep);
67
68 /* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN.  */
69
70 extern error_t argz_append (char **restrict __argz,
71                             size_t *restrict __argz_len,
72                             const char *restrict __buf, size_t __buf_len)
73 ;
74
75 /* Append STR to the argz vector in ARGZ & ARGZ_LEN.  */
76
77 extern error_t argz_add (char **restrict __argz,
78                          size_t *restrict __argz_len,
79                          const char *restrict str);
80
81 /* Append SEP separated list in STRING to the argz vector in ARGZ &
82    ARGZ_LEN.  */
83
84 extern error_t argz_add_sep (char **restrict __argz,
85                              size_t *restrict __argz_len,
86                              const char *restrict string, int __delim)
87 ;
88
89 /* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there.  */
90
91 extern void argz_delete (char **restrict __argz,
92                          size_t *restrict __argz_len,
93                          char *restrict __entry);
94
95 /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
96    existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
97    Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
98    ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ.  If BEFORE is not
99    in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
100    ARGZ, ENOMEM is returned, else 0.  */
101
102 extern error_t argz_insert (char **restrict __argz,
103                             size_t *restrict __argz_len,
104                             char *restrict __before,
105                             const char *restrict __entry);
106
107 /* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
108    ARGZ as necessary.  If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
109    incremented by number of replacements performed.  */
110
111 extern error_t argz_replace (char **restrict __argz,
112                              size_t *restrict __argz_len,
113                              const char *restrict str,
114                              const char *restrict __with,
115                              unsigned int *restrict __replace_count);
116 \f
117 /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
118    are no more.  If entry is NULL, then the first entry is returned.  This
119    behavior allows two convenient iteration styles:
120
121     char *entry = 0;
122     while ((entry = argz_next (argz, argz_len, entry)))
123       ...;
124
125    or
126
127     char *entry;
128     for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
129       ...;
130 */
131
132 extern char *argz_next (const char *restrict __argz, size_t __argz_len,
133                         const char *restrict __entry);
134
135 #ifdef __USE_EXTERN_INLINES
136 __extern_inline char *
137 __NTH (argz_next (const char *__argz, size_t __argz_len,
138                     const char *__entry))
139 {
140   if (__entry)
141     {
142       if (__entry < __argz + __argz_len)
143         __entry = strchr (__entry, '\0') + 1;
144
145       return __entry >= __argz + __argz_len ? (char *) NULL : (char *) __entry;
146     }
147   else
148     return __argz_len > 0 ? (char *) __argz : 0;
149 }
150 __extern_inline char *
151 __NTH (argz_next (const char *__argz, size_t __argz_len,
152                   const char *__entry))
153 {
154   return argz_next (__argz, __argz_len, __entry);
155 }
156 #endif /* Use extern inlines.  */
157
158
159 #endif /* argz.h */