Add missing \n character to end of warning message created by the previous delta
[binutils.git] / gas / sb.c
blob9957c7bdfb3d70b73b13158d54074566df04997c
1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000, 2003 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
5 sac@cygnus.com
7 This file is part of GAS, the GNU Assembler.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
24 #include "config.h"
25 #include <stdio.h>
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #else
32 #include <strings.h>
33 #endif
34 #include "libiberty.h"
35 #include "sb.h"
37 /* These routines are about manipulating strings.
39 They are managed in things called `sb's which is an abbreviation
40 for string buffers. An sb has to be created, things can be glued
41 on to it, and at the end of it's life it should be freed. The
42 contents should never be pointed at whilst it is still growing,
43 since it could be moved at any time
45 eg:
46 sb_new (&foo);
47 sb_grow... (&foo,...);
48 use foo->ptr[*];
49 sb_kill (&foo); */
51 static int dsize = 5;
52 static void sb_check (sb *, int);
54 /* Statistics of sb structures. */
55 static int string_count[sb_max_power_two];
57 /* Free list of sb structures. */
58 static sb_list_vector free_list;
60 /* Initializes an sb. */
62 static void
63 sb_build (sb *ptr, int size)
65 /* See if we can find one to allocate. */
66 sb_element *e;
68 if (size > sb_max_power_two)
69 abort ();
71 e = free_list.size[size];
72 if (!e)
74 /* Nothing there, allocate one and stick into the free list. */
75 e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
76 e->next = free_list.size[size];
77 e->size = 1 << size;
78 free_list.size[size] = e;
79 string_count[size]++;
82 /* Remove from free list. */
83 free_list.size[size] = e->next;
85 /* Copy into callers world. */
86 ptr->ptr = e->data;
87 ptr->pot = size;
88 ptr->len = 0;
89 ptr->item = e;
92 void
93 sb_new (sb *ptr)
95 sb_build (ptr, dsize);
98 /* Deallocate the sb at ptr. */
100 void
101 sb_kill (sb *ptr)
103 /* Return item to free list. */
104 ptr->item->next = free_list.size[ptr->pot];
105 free_list.size[ptr->pot] = ptr->item;
108 /* Add the sb at s to the end of the sb at ptr. */
110 void
111 sb_add_sb (sb *ptr, sb *s)
113 sb_check (ptr, s->len);
114 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
115 ptr->len += s->len;
118 /* Make sure that the sb at ptr has room for another len characters,
119 and grow it if it doesn't. */
121 static void
122 sb_check (sb *ptr, int len)
124 if (ptr->len + len >= 1 << ptr->pot)
126 sb tmp;
127 int pot = ptr->pot;
129 while (ptr->len + len >= 1 << pot)
130 pot++;
131 sb_build (&tmp, pot);
132 sb_add_sb (&tmp, ptr);
133 sb_kill (ptr);
134 *ptr = tmp;
138 /* Make the sb at ptr point back to the beginning. */
140 void
141 sb_reset (sb *ptr)
143 ptr->len = 0;
146 /* Add character c to the end of the sb at ptr. */
148 void
149 sb_add_char (sb *ptr, int c)
151 sb_check (ptr, 1);
152 ptr->ptr[ptr->len++] = c;
155 /* Add null terminated string s to the end of sb at ptr. */
157 void
158 sb_add_string (sb *ptr, const char *s)
160 int len = strlen (s);
161 sb_check (ptr, len);
162 memcpy (ptr->ptr + ptr->len, s, len);
163 ptr->len += len;
166 /* Add string at s of length len to sb at ptr */
168 void
169 sb_add_buffer (sb *ptr, const char *s, int len)
171 sb_check (ptr, len);
172 memcpy (ptr->ptr + ptr->len, s, len);
173 ptr->len += len;
176 /* Like sb_name, but don't include the null byte in the string. */
178 char *
179 sb_terminate (sb *in)
181 sb_add_char (in, 0);
182 --in->len;
183 return in->ptr;
186 /* Start at the index idx into the string in sb at ptr and skip
187 whitespace. return the index of the first non whitespace character. */
190 sb_skip_white (int idx, sb *ptr)
192 while (idx < ptr->len
193 && (ptr->ptr[idx] == ' '
194 || ptr->ptr[idx] == '\t'))
195 idx++;
196 return idx;
199 /* Start at the index idx into the sb at ptr. skips whitespace,
200 a comma and any following whitespace. returns the index of the
201 next character. */
204 sb_skip_comma (int idx, sb *ptr)
206 while (idx < ptr->len
207 && (ptr->ptr[idx] == ' '
208 || ptr->ptr[idx] == '\t'))
209 idx++;
211 if (idx < ptr->len
212 && ptr->ptr[idx] == ',')
213 idx++;
215 while (idx < ptr->len
216 && (ptr->ptr[idx] == ' '
217 || ptr->ptr[idx] == '\t'))
218 idx++;
220 return idx;