vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_demangle / dyn-string.c
blob89ce8e12cc1da41f97affd9c0859a921bffbd61e
1 /* An abstract string datatype.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 GNU CC is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with GNU CC; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #if 0 /* in valgrind */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #endif /* ! in valgrind */
37 #if 0 /* in valgrind */
38 #include <stdio.h>
39 #endif /* ! in valgrind */
41 #if 0 /* in valgrind */
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #endif
45 #endif /* ! in valgrind */
47 #if 0 /* in valgrind */
48 #ifdef HAVE_STDLIB_H
49 #include <stdlib.h>
50 #endif
51 #endif /* ! in valgrind */
53 #if 0 /* in valgrind */
54 #include "libiberty.h"
55 #endif /* ! in valgrind */
57 #include "vg_libciface.h"
59 #include "dyn-string.h"
61 /* Performs in-place initialization of a dyn_string struct. This
62 function can be used with a dyn_string struct on the stack or
63 embedded in another object. The contents of of the string itself
64 are still dynamically allocated. The string initially is capable
65 of holding at least SPACE characeters, including the terminating
66 NUL. If SPACE is 0, it will silently be increated to 1.
68 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
69 fails, returns 0. Otherwise returns 1. */
71 int
72 dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
74 /* We need at least one byte in which to store the terminating NUL. */
75 if (space == 0)
76 space = 1;
78 #ifdef RETURN_ON_ALLOCATION_FAILURE
79 ds_struct_ptr->s = (char *) malloc (space);
80 if (ds_struct_ptr->s == NULL)
81 return 0;
82 #else
83 ds_struct_ptr->s = XNEWVEC (char, space);
84 #endif
85 ds_struct_ptr->allocated = space;
86 ds_struct_ptr->length = 0;
87 ds_struct_ptr->s[0] = '\0';
89 return 1;
92 /* Create a new dynamic string capable of holding at least SPACE
93 characters, including the terminating NUL. If SPACE is 0, it will
94 be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
95 defined and memory allocation fails, returns NULL. Otherwise
96 returns the newly allocated string. */
98 dyn_string_t
99 dyn_string_new (int space)
101 dyn_string_t result;
102 #ifdef RETURN_ON_ALLOCATION_FAILURE
103 result = (dyn_string_t) malloc (sizeof (struct dyn_string));
104 if (result == NULL)
105 return NULL;
106 if (!dyn_string_init (result, space))
108 free (result);
109 return NULL;
111 #else
112 result = XNEW (struct dyn_string);
113 dyn_string_init (result, space);
114 #endif
115 return result;
118 /* Free the memory used by DS. */
120 void
121 dyn_string_delete (dyn_string_t ds)
123 free (ds->s);
124 free (ds);
127 /* Returns the contents of DS in a buffer allocated with malloc. It
128 is the caller's responsibility to deallocate the buffer using free.
129 DS is then set to the empty string. Deletes DS itself. */
131 char*
132 dyn_string_release (dyn_string_t ds)
134 /* Store the old buffer. */
135 char* result = ds->s;
136 /* The buffer is no longer owned by DS. */
137 ds->s = NULL;
138 /* Delete DS. */
139 free (ds);
140 /* Return the old buffer. */
141 return result;
144 /* Increase the capacity of DS so it can hold at least SPACE
145 characters, plus the terminating NUL. This function will not (at
146 present) reduce the capacity of DS. Returns DS on success.
148 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
149 operation fails, deletes DS and returns NULL. */
151 dyn_string_t
152 dyn_string_resize (dyn_string_t ds, int space)
154 int new_allocated = ds->allocated;
156 /* Increase SPACE to hold the NUL termination. */
157 ++space;
159 /* Increase allocation by factors of two. */
160 while (space > new_allocated)
161 new_allocated *= 2;
163 if (new_allocated != ds->allocated)
165 ds->allocated = new_allocated;
166 /* We actually need more space. */
167 #ifdef RETURN_ON_ALLOCATION_FAILURE
168 ds->s = (char *) realloc (ds->s, ds->allocated);
169 if (ds->s == NULL)
171 free (ds);
172 return NULL;
174 #else
175 ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
176 #endif
179 return ds;
182 /* Sets the contents of DS to the empty string. */
184 void
185 dyn_string_clear (dyn_string_t ds)
187 /* A dyn_string always has room for at least the NUL terminator. */
188 ds->s[0] = '\0';
189 ds->length = 0;
192 /* Makes the contents of DEST the same as the contents of SRC. DEST
193 and SRC must be distinct. Returns 1 on success. On failure, if
194 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
197 dyn_string_copy (dyn_string_t dest, dyn_string_t src)
199 if (dest == src)
200 abort ();
202 /* Make room in DEST. */
203 if (dyn_string_resize (dest, src->length) == NULL)
204 return 0;
205 /* Copy DEST into SRC. */
206 strcpy (dest->s, src->s);
207 /* Update the size of DEST. */
208 dest->length = src->length;
209 return 1;
212 /* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
213 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
214 and returns 0. */
217 dyn_string_copy_cstr (dyn_string_t dest, const char *src)
219 int length = strlen (src);
220 /* Make room in DEST. */
221 if (dyn_string_resize (dest, length) == NULL)
222 return 0;
223 /* Copy DEST into SRC. */
224 strcpy (dest->s, src);
225 /* Update the size of DEST. */
226 dest->length = length;
227 return 1;
230 /* Inserts SRC at the beginning of DEST. DEST is expanded as
231 necessary. SRC and DEST must be distinct. Returns 1 on success.
232 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
233 returns 0. */
236 dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
238 return dyn_string_insert (dest, 0, src);
241 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
242 DEST is expanded as necessary. Returns 1 on success. On failure,
243 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
246 dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
248 return dyn_string_insert_cstr (dest, 0, src);
251 /* Inserts SRC into DEST starting at position POS. DEST is expanded
252 as necessary. SRC and DEST must be distinct. Returns 1 on
253 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
254 and returns 0. */
257 dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
259 int i;
261 if (src == dest)
262 abort ();
264 if (dyn_string_resize (dest, dest->length + src->length) == NULL)
265 return 0;
266 /* Make room for the insertion. Be sure to copy the NUL. */
267 for (i = dest->length; i >= pos; --i)
268 dest->s[i + src->length] = dest->s[i];
269 /* Splice in the new stuff. */
270 strncpy (dest->s + pos, src->s, src->length);
271 /* Compute the new length. */
272 dest->length += src->length;
273 return 1;
276 /* Inserts SRC, a NUL-terminated string, into DEST starting at
277 position POS. DEST is expanded as necessary. Returns 1 on
278 success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST
279 and returns 0. */
282 dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
284 int i;
285 int length = strlen (src);
287 if (dyn_string_resize (dest, dest->length + length) == NULL)
288 return 0;
289 /* Make room for the insertion. Be sure to copy the NUL. */
290 for (i = dest->length; i >= pos; --i)
291 dest->s[i + length] = dest->s[i];
292 /* Splice in the new stuff. */
293 memcpy (dest->s + pos, src, length);
294 /* Compute the new length. */
295 dest->length += length;
296 return 1;
299 /* Inserts character C into DEST starting at position POS. DEST is
300 expanded as necessary. Returns 1 on success. On failure,
301 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
304 dyn_string_insert_char (dyn_string_t dest, int pos, int c)
306 int i;
308 if (dyn_string_resize (dest, dest->length + 1) == NULL)
309 return 0;
310 /* Make room for the insertion. Be sure to copy the NUL. */
311 for (i = dest->length; i >= pos; --i)
312 dest->s[i + 1] = dest->s[i];
313 /* Add the new character. */
314 dest->s[pos] = c;
315 /* Compute the new length. */
316 ++dest->length;
317 return 1;
320 /* Append S to DS, resizing DS if necessary. Returns 1 on success.
321 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
322 returns 0. */
325 dyn_string_append (dyn_string_t dest, dyn_string_t s)
327 if (dyn_string_resize (dest, dest->length + s->length) == 0)
328 return 0;
329 strcpy (dest->s + dest->length, s->s);
330 dest->length += s->length;
331 return 1;
334 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
335 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
336 deletes DEST and returns 0. */
339 dyn_string_append_cstr (dyn_string_t dest, const char *s)
341 int len = strlen (s);
343 /* The new length is the old length plus the size of our string, plus
344 one for the null at the end. */
345 if (dyn_string_resize (dest, dest->length + len) == NULL)
346 return 0;
347 strcpy (dest->s + dest->length, s);
348 dest->length += len;
349 return 1;
352 /* Appends C to the end of DEST. Returns 1 on success. On failure,
353 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
356 dyn_string_append_char (dyn_string_t dest, int c)
358 /* Make room for the extra character. */
359 if (dyn_string_resize (dest, dest->length + 1) == NULL)
360 return 0;
361 /* Append the character; it will overwrite the old NUL. */
362 dest->s[dest->length] = c;
363 /* Add a new NUL at the end. */
364 dest->s[dest->length + 1] = '\0';
365 /* Update the length. */
366 ++(dest->length);
367 return 1;
370 /* Sets the contents of DEST to the substring of SRC starting at START
371 and ending before END. START must be less than or equal to END,
372 and both must be between zero and the length of SRC, inclusive.
373 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
374 deletes DEST and returns 0. */
377 dyn_string_substring (dyn_string_t dest, dyn_string_t src,
378 int start, int end)
380 int i;
381 int length = end - start;
383 if (start > end || start > src->length || end > src->length)
384 abort ();
386 /* Make room for the substring. */
387 if (dyn_string_resize (dest, length) == NULL)
388 return 0;
389 /* Copy the characters in the substring, */
390 for (i = length; --i >= 0; )
391 dest->s[i] = src->s[start + i];
392 /* NUL-terimate the result. */
393 dest->s[length] = '\0';
394 /* Record the length of the substring. */
395 dest->length = length;
397 return 1;
400 /* Returns non-zero if DS1 and DS2 have the same contents. */
403 dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
405 /* If DS1 and DS2 have different lengths, they must not be the same. */
406 if (ds1->length != ds2->length)
407 return 0;
408 else
409 return !strcmp (ds1->s, ds2->s);