* Makefile.am (ALL_EMULATIONS): Add ecrisaout.o, ecriself.o,
[binutils.git] / libiberty / dyn-string.c
blobf4a9d9a74f8368c44874145ebeb4a6f469a7bb65
1 /* An abstract string datatype.
2 Copyright (C) 1998, 1999, 2000 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 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
36 #include "libiberty.h"
37 #include "dyn-string.h"
39 /* Performs in-place initialization of a dyn_string struct. This
40 function can be used with a dyn_string struct on the stack or
41 embedded in another object. The contents of of the string itself
42 are still dynamically allocated. The string initially is capable
43 of holding at least SPACE characeters, including the terminating
44 NUL. If SPACE is 0, it will silently be increated to 1. */
46 void
47 dyn_string_init (ds_struct_ptr, space)
48 struct dyn_string *ds_struct_ptr;
49 int space;
51 /* We need at least one byte in which to store the terminating NUL. */
52 if (space == 0)
53 space = 1;
55 ds_struct_ptr->allocated = space;
56 ds_struct_ptr->s = (char *) xmalloc (space);
57 ds_struct_ptr->length = 0;
58 ds_struct_ptr->s[0] = '\0';
61 /* Create a new dynamic string capable of holding at least SPACE characters,
62 including the terminating NUL. If SPACE is 0, it will be silently
63 increased to 1. */
65 dyn_string_t
66 dyn_string_new (space)
67 int space;
69 dyn_string_t result = (dyn_string_t) xmalloc (sizeof (struct dyn_string));
70 dyn_string_init (result, space);
71 return result;
74 /* Free the memory used by DS. */
76 void
77 dyn_string_delete (ds)
78 dyn_string_t ds;
80 free (ds->s);
81 free (ds);
84 /* Returns the contents of DS in a buffer allocated with malloc. It
85 is the caller's responsibility to deallocate the buffer using free.
86 DS is then set to the empty string. */
88 char*
89 dyn_string_release (ds)
90 dyn_string_t ds;
92 /* Store the old buffer. */
93 char* result = ds->s;
94 /* The buffer is no longer owned by DS. */
95 ds->s = NULL;
96 /* Reinitialize DS to the empty string. */
97 dyn_string_init (ds, 0);
98 /* Return the old buffer. */
99 return result;
102 /* Increase the capacity of DS so it can hold at least SPACE
103 characters, plus the terminating NUL. This function will not (at
104 present) reduce the capacity of DS. */
106 dyn_string_t
107 dyn_string_resize (ds, space)
108 dyn_string_t ds;
109 int space;
111 int new_allocated = ds->allocated;
113 /* Increase SPACE to hold the NUL termination. */
114 ++space;
116 while (space > new_allocated)
117 new_allocated *= 2;
119 if (new_allocated != ds->allocated)
121 /* We actually need more space. */
122 ds->allocated = new_allocated;
123 ds->s = (char *) xrealloc (ds->s, ds->allocated);
126 return ds;
129 /* Sets the contents of DS to the empty string. */
131 void
132 dyn_string_clear (ds)
133 dyn_string_t ds;
135 /* A dyn_string always has room for at least the NUL terminator. */
136 ds->s[0] = '\0';
137 ds->length = 0;
140 /* Makes the contents of DEST the same as the contents of SRC. DEST
141 and SRC must be distinct. */
143 void
144 dyn_string_copy (dest, src)
145 dyn_string_t dest;
146 dyn_string_t src;
148 if (dest == src)
149 abort ();
151 /* Make room in DEST. */
152 dyn_string_resize (dest, src->length);
153 /* Copy DEST into SRC. */
154 strcpy (dest->s, src->s);
155 /* Update the size of DEST. */
156 dest->length = src->length;
159 /* Copies SRC, a NUL-terminated string, into DEST. */
161 void
162 dyn_string_copy_cstr (dest, src)
163 dyn_string_t dest;
164 const char *src;
166 int length = strlen (src);
167 /* Make room in DEST. */
168 dyn_string_resize (dest, length);
169 /* Copy DEST into SRC. */
170 strcpy (dest->s, src);
171 /* Update the size of DEST. */
172 dest->length = length;
175 /* Inserts SRC at the beginning of DEST. DEST is expanded as
176 necessary. SRC and DEST must be distinct. */
178 void
179 dyn_string_prepend (dest, src)
180 dyn_string_t dest;
181 dyn_string_t src;
183 dyn_string_insert (dest, 0, src);
186 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
187 DEST is expanded as necessary. */
189 void
190 dyn_string_prepend_cstr (dest, src)
191 dyn_string_t dest;
192 const char *src;
194 dyn_string_insert_cstr (dest, 0, src);
197 /* Inserts SRC into DEST starting at position POS. DEST is expanded as
198 necessary. SRC and DEST must be distinct. */
200 void
201 dyn_string_insert (dest, pos, src)
202 dyn_string_t dest;
203 int pos;
204 dyn_string_t src;
206 int i;
208 if (src == dest)
209 abort ();
211 dyn_string_resize (dest, dest->length + src->length);
212 /* Make room for the insertion. Be sure to copy the NUL. */
213 for (i = dest->length; i >= pos; --i)
214 dest->s[i + src->length] = dest->s[i];
215 /* Splice in the new stuff. */
216 strncpy (dest->s + pos, src->s, src->length);
217 /* Compute the new length. */
218 dest->length += src->length;
221 /* Inserts SRC, a NUL-terminated string, into DEST starting at
222 position POS. DEST is expanded as necessary. */
224 void
225 dyn_string_insert_cstr (dest, pos, src)
226 dyn_string_t dest;
227 int pos;
228 const char *src;
230 int i;
231 int length = strlen (src);
233 dyn_string_resize (dest, dest->length + length);
234 /* Make room for the insertion. Be sure to copy the NUL. */
235 for (i = dest->length; i >= pos; --i)
236 dest->s[i + length] = dest->s[i];
237 /* Splice in the new stuff. */
238 strncpy (dest->s + pos, src, length);
239 /* Compute the new length. */
240 dest->length += length;
243 /* Append S to DS, resizing DS if necessary. Returns DS. */
245 dyn_string_t
246 dyn_string_append (ds, s)
247 dyn_string_t ds;
248 dyn_string_t s;
250 dyn_string_resize (ds, ds->length + s->length);
251 strcpy (ds->s + ds->length, s->s);
252 ds->length += s->length;
253 return ds;
256 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
257 Returns DS. */
259 dyn_string_t
260 dyn_string_append_cstr (ds, s)
261 dyn_string_t ds;
262 const char *s;
264 int len = strlen (s);
266 /* The new length is the old length plus the size of our string, plus
267 one for the null at the end. */
268 dyn_string_resize (ds, ds->length + len);
269 strcpy (ds->s + ds->length, s);
270 ds->length += len;
272 return ds;
275 /* Appends C to the end of DS. */
277 dyn_string_t
278 dyn_string_append_char (ds, c)
279 dyn_string_t ds;
280 int c;
282 /* Make room for the extra character. */
283 dyn_string_resize (ds, ds->length + 1);
284 /* Append the character; it will overwrite the old NUL. */
285 ds->s[ds->length] = c;
286 /* Add a new NUL at the end. */
287 ds->s[ds->length + 1] = '\0';
288 /* Update the length. */
289 ++(ds->length);
290 return ds;
293 /* Sets the contents of DEST to the substring of SRC starting at START
294 and ending before END. START must be less than or equal to END,
295 and both must be between zero and the length of SRC, inclusive. */
297 void
298 dyn_string_substring (dest, src, start, end)
299 dyn_string_t dest;
300 dyn_string_t src;
301 int start;
302 int end;
304 int i;
305 int length = end - start;
307 if (start > end || start > src->length || end > src->length)
308 abort ();
310 /* Make room for the substring. */
311 dyn_string_resize (dest, length);
312 /* Copy the characters in the substring, */
313 for (i = length; --i >= 0; )
314 dest->s[i] = src->s[start + i];
315 /* NUL-terimate the result. */
316 dest->s[length] = '\0';
317 /* Record the length of the substring. */
318 dest->length = length;
321 /* Returns non-zero if DS1 and DS2 have the same contents. */
324 dyn_string_eq (ds1, ds2)
325 dyn_string_t ds1;
326 dyn_string_t ds2;
328 /* If DS1 and DS2 have different lengths, they must not be the same. */
329 if (ds1->length != ds2->length)
330 return 0;
331 else
332 return !strcmp (ds1->s, ds2->s);