aboutsummaryrefslogtreecommitdiff
path: root/gcc/dyn-string.c
diff options
context:
space:
mode:
authorMark Mitchell <mark@markmitchell.com>1998-07-30 12:30:30 +0000
committerMark Mitchell <mmitchel@gcc.gnu.org>1998-07-30 12:30:30 +0000
commitb170964afd32aec3de313c4f1e4c64c3f14dbeb2 (patch)
tree304a4a5b0923ec3d13e6852847a161df2f910e2e /gcc/dyn-string.c
parent07417085a14349cde788c5cc10663815da40c26f (diff)
downloadgcc-b170964afd32aec3de313c4f1e4c64c3f14dbeb2.zip
gcc-b170964afd32aec3de313c4f1e4c64c3f14dbeb2.tar.gz
gcc-b170964afd32aec3de313c4f1e4c64c3f14dbeb2.tar.bz2
dyn-string.h: New file.
* dyn-string.h: New file. * dyn-string.c: Likewise. * Makefile.in (OBJS): Add dyn-string.o. (dwarf2out.o): Add dyn-string.h dependency. (dyn-string.o): List dependencies. * dwarf2out.c: Include dyn-string.h. (ASM_NAME_TO_STRING): Use dyn_string_append, rather than strcpy. (addr_const_to_string): Take a dyn_string_t, not a char* as a prototype. Use dyn_string_append rather than strcat, throughout. (addr_to_string): Use dyn_string_t. From-SVN: r21496
Diffstat (limited to 'gcc/dyn-string.c')
-rw-r--r--gcc/dyn-string.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/gcc/dyn-string.c b/gcc/dyn-string.c
new file mode 100644
index 0000000..cfcace9
--- /dev/null
+++ b/gcc/dyn-string.c
@@ -0,0 +1,100 @@
+/* An abstract string datatype.
+ Copyright (C) 1998 Free Software Foundation, Inc.
+ Contributed by Mark Mitchell (mark@markmitchell.com).
+
+ This file is part of GNU CC.
+
+ GNU CC is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ GNU CC is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU CC; see the file COPYING. If not, write to the Free
+ Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include "config.h"
+#include "system.h"
+#include "gansidecl.h"
+#include "dyn-string.h"
+
+extern char *xmalloc ();
+extern char *xrealloc ();
+
+/* Create a new dynamic string capable of holding at least SPACE
+ characters, including the terminating NUL. If SPACE is 0, it
+ will be silently increased to 1. */
+
+dyn_string_t
+dyn_string_new (space)
+ int space;
+{
+ dyn_string_t result = (dyn_string_t) xmalloc (sizeof (struct dyn_string));
+
+ if (space == 0)
+ /* We need at least one byte in which to store the terminating
+ NUL. */
+ space = 1;
+
+ result->allocated = space;
+ result->s = (char*) xmalloc (space);
+ result->length = 0;
+ result->s[0] = '\0';
+
+ return result;
+}
+
+/* Free the memory used by DS. */
+
+void
+dyn_string_delete (ds)
+ dyn_string_t ds;
+{
+ free (ds->s);
+ free (ds);
+}
+
+/* Append the NUL-terminated string S to DS, resizing DS if
+ necessary. */
+
+dyn_string_t
+dyn_string_append (ds, s)
+ dyn_string_t ds;
+ char *s;
+{
+ int len = strlen (s);
+ dyn_string_resize (ds, ds->length + len + 1 /* '\0' */);
+ strcpy (ds->s + ds->length, s);
+ ds->length += len;
+
+ return ds;
+}
+
+/* Increase the capacity of DS so that it can hold at least SPACE
+ characters, including the terminating NUL. This function will not
+ (at present) reduce the capacity of DS. */
+
+dyn_string_t
+dyn_string_resize (ds, space)
+ dyn_string_t ds;
+ int space;
+{
+ int new_allocated = ds->allocated;
+
+ while (space > new_allocated)
+ new_allocated *= 2;
+
+ if (new_allocated != ds->allocated)
+ {
+ /* We actually need more space. */
+ ds->allocated = new_allocated;
+ ds->s = (char*) xrealloc (ds->s, ds->allocated);
+ }
+
+ return ds;
+}