root / ImageMagick / trunk / ltdl / argz.c

Revision 11090, 6.3 kB (checked in by cristy, 3 months ago)
Line 
1/* argz.c -- argz implementation for non-glibc systems
2
3   Copyright (C) 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
4   Written by Gary V. Vaughan, 2004
5
6   NOTE: The canonical source of this file is maintained with the
7   GNU Libtool package.  Report bugs to bug-libtool@gnu.org.
8
9GNU Libltdl is free software; you can redistribute it and/or
10modify it under the terms of the GNU Lesser General Public
11License as published by the Free Software Foundation; either
12version 2 of the License, or (at your option) any later version.
13
14As a special exception to the GNU Lesser General Public License,
15if you distribute this file as part of a program or library that
16is built using GNU Libtool, you may include this file under the
17same distribution terms that you use for the rest of that program.
18
19GNU Libltdl is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22GNU Lesser General Public License for more details.
23
24You should have received a copy of the GNU Lesser General Public
25License along with GNU Libltdl; see the file COPYING.LIB.  If not, a
26copy can be downloaded from  http://www.gnu.org/licenses/lgpl.html,
27or obtained by writing to the Free Software Foundation, Inc.,
2851 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29*/
30
31#if defined(LTDL) && defined LT_CONFIG_H
32include LT_CONFIG_H
33#else
34include <config.h>
35#endif
36
37#include <argz.h>
38
39#include <assert.h>
40#include <stddef.h>
41#include <stdlib.h>
42#include <sys/types.h>
43#include <errno.h>
44#include <string.h>
45
46#define EOS_CHAR '\0'
47
48error_t
49argz_append (char **pargz, size_t *pargz_len, const char *buf, size_t buf_len)
50{
51  size_t argz_len;
52  char  *argz;
53
54  assert (pargz);
55  assert (pargz_len);
56  assert ((*pargz && *pargz_len) || (!*pargz && !*pargz_len));
57
58  /* If nothing needs to be appended, no more work is required.  */
59  if (buf_len == 0)
60    return 0;
61
62  /* Ensure there is enough room to append BUF_LEN.  */
63  argz_len = *pargz_len + buf_len;
64  argz = (char *) realloc (*pargz, argz_len);
65  if (!argz)
66    return ENOMEM;
67
68  /* Copy characters from BUF after terminating '\0' in ARGZ.  */
69  memcpy (argz + *pargz_len, buf, buf_len);
70
71  /* Assign new values.  */
72  *pargz = argz;
73  *pargz_len = argz_len;
74
75  return 0;
76}
77
78
79/* Add a string to the argz vector.  */
80error_t
81argz_add (char **pargz, size_t *pargz_len, const char *str)
82{
83  return argz_append (pargz, pargz_len, str, strlen (str) + 1);
84}
85
86
87error_t
88argz_create_sep (const char *str, int delim, char **pargz, size_t *pargz_len)
89{
90  size_t argz_len;
91  char *argz = 0;
92
93  assert (str);
94  assert (pargz);
95  assert (pargz_len);
96
97  /* Make a copy of STR, but replacing each occurrence of
98     DELIM with '\0'.  */
99  argz_len = 1+ strlen (str);
100  if (argz_len)
101    {
102      const char *p;
103      char *q;
104
105      argz = (char *) malloc (argz_len);
106      if (!argz)
107        return ENOMEM;
108
109      for (p = str, q = argz; *p != EOS_CHAR; ++p)
110        {
111          if (*p == delim)
112            {
113              /* Ignore leading delimiters, and fold consecutive
114                 delimiters in STR into a single '\0' in ARGZ.  */
115              if ((q > argz) && (q[-1] != EOS_CHAR))
116                *q++ = EOS_CHAR;
117              else
118                --argz_len;
119            }
120          else
121            *q++ = *p;
122        }
123      /* Copy terminating EOS_CHAR.  */
124      *q = *p;
125    }
126
127  /* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory.  */
128  if (!argz_len)
129    argz = (free (argz), (char *) 0);
130
131  /* Assign new values.  */
132  *pargz = argz;
133  *pargz_len = argz_len;
134
135  return 0;
136}
137
138
139error_t
140argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry)
141{
142  assert (pargz);
143  assert (pargz_len);
144  assert (entry && *entry);
145
146  /* No BEFORE address indicates ENTRY should be inserted after the
147     current last element.  */
148  if (!before)
149    return argz_append (pargz, pargz_len, entry, 1+ strlen (entry));
150
151  /* This probably indicates a programmer error, but to preserve
152     semantics, scan back to the start of an entry if BEFORE points
153     into the middle of it.  */
154  while ((before > *pargz) && (before[-1] != EOS_CHAR))
155    --before;
156
157  {
158    size_t entry_len    = 1+ strlen (entry);
159    size_t argz_len     = *pargz_len + entry_len;
160    size_t offset       = before - *pargz;
161    char   *argz        = (char *) realloc (*pargz, argz_len);
162
163    if (!argz)
164      return ENOMEM;
165
166    /* Make BEFORE point to the equivalent offset in ARGZ that it
167       used to have in *PARGZ incase realloc() moved the block.  */
168    before = argz + offset;
169
170    /* Move the ARGZ entries starting at BEFORE up into the new
171       space at the end -- making room to copy ENTRY into the
172       resulting gap.  */
173    memmove (before + entry_len, before, *pargz_len - offset);
174    memcpy  (before, entry, entry_len);
175
176    /* Assign new values.  */
177    *pargz = argz;
178    *pargz_len = argz_len;
179  }
180
181  return 0;
182}
183
184
185char *
186argz_next (char *argz, size_t argz_len, const char *entry)
187{
188  assert ((argz && argz_len) || (!argz && !argz_len));
189
190  if (entry)
191    {
192      /* Either ARGZ/ARGZ_LEN is empty, or ENTRY points into an address
193         within the ARGZ vector.  */
194      assert ((!argz && !argz_len)
195              || ((argz <= entry) && (entry < (argz + argz_len))));
196
197      /* Move to the char immediately after the terminating
198         '\0' of ENTRY.  */
199      entry = 1+ strchr (entry, EOS_CHAR);
200
201      /* Return either the new ENTRY, or else NULL if ARGZ is
202         exhausted.  */
203      return (entry >= argz + argz_len) ? 0 : (char *) entry;
204    }
205  else
206    {
207      /* This should probably be flagged as a programmer error,
208         since starting an argz_next loop with the iterator set
209         to ARGZ is safer.  To preserve semantics, handle the NULL
210         case by returning the start of ARGZ (if any).  */
211      if (argz_len > 0)
212        return argz;
213      else
214        return 0;
215    }
216}
217
218
219void
220argz_stringify (char *argz, size_t argz_len, int sep)
221{
222  assert ((argz && argz_len) || (!argz && !argz_len));
223
224  if (sep)
225    {
226      --argz_len;               /* don't stringify the terminating EOS */
227      while (--argz_len > 0)
228        {
229          if (argz[argz_len] == EOS_CHAR)
230            argz[argz_len] = sep;
231        }
232    }
233}
234
235
236/* Count number of elements (null bytes) in argz vector.  */
237
238size_t
239argz_count (const char *argz, size_t argz_len)
240{
241  size_t count = 0;
242
243  assert ((argz && argz_len) || (!argz && !argz_len));
244
245  while (argz_len > 0)
246    {
247      size_t part_len = strlen (argz);
248      argz += part_len + 1;
249      argz_len -= part_len + 1;
250      count++;
251    }
252
253  return count;
254}
Note: See TracBrowser for help on using the browser.