Increase minor version number (to 2.10.91) to help tools detect the new
[deliverable/binutils-gdb.git] / gas / sb.c
1 /* sb.c - string buffer manipulation routines
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3
4 Written by Steve and Judy Chamberlain of Cygnus Support,
5 sac@cygnus.com
6
7 This file is part of GAS, the GNU Assembler.
8
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.
13
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.
18
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, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
23
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"
36
37 /* These routines are about manipulating strings.
38
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
44
45 eg:
46 sb_new (&foo);
47 sb_grow... (&foo,...);
48 use foo->ptr[*];
49 sb_kill (&foo);
50
51 */
52
53 #define dsize 5
54
55 static void sb_check PARAMS ((sb *, int));
56
57 /* Statistics of sb structures. */
58
59 int string_count[sb_max_power_two];
60
61 /* Free list of sb structures. */
62
63 static sb_list_vector free_list;
64
65 /* initializes an sb. */
66
67 void
68 sb_build (ptr, size)
69 sb *ptr;
70 int size;
71 {
72 /* see if we can find one to allocate */
73 sb_element *e;
74
75 if (size > sb_max_power_two)
76 abort ();
77
78 e = free_list.size[size];
79 if (!e)
80 {
81 /* nothing there, allocate one and stick into the free list */
82 e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
83 e->next = free_list.size[size];
84 e->size = 1 << size;
85 free_list.size[size] = e;
86 string_count[size]++;
87 }
88
89 /* remove from free list */
90
91 free_list.size[size] = e->next;
92
93 /* copy into callers world */
94 ptr->ptr = e->data;
95 ptr->pot = size;
96 ptr->len = 0;
97 ptr->item = e;
98 }
99
100
101 void
102 sb_new (ptr)
103 sb *ptr;
104 {
105 sb_build (ptr, dsize);
106 }
107
108 /* deallocate the sb at ptr */
109
110 void
111 sb_kill (ptr)
112 sb *ptr;
113 {
114 /* return item to free list */
115 ptr->item->next = free_list.size[ptr->pot];
116 free_list.size[ptr->pot] = ptr->item;
117 }
118
119 /* add the sb at s to the end of the sb at ptr */
120
121 void
122 sb_add_sb (ptr, s)
123 sb *ptr;
124 sb *s;
125 {
126 sb_check (ptr, s->len);
127 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
128 ptr->len += s->len;
129 }
130
131 /* make sure that the sb at ptr has room for another len characters,
132 and grow it if it doesn't. */
133
134 static void
135 sb_check (ptr, len)
136 sb *ptr;
137 int len;
138 {
139 if (ptr->len + len >= 1 << ptr->pot)
140 {
141 sb tmp;
142 int pot = ptr->pot;
143 while (ptr->len + len >= 1 << pot)
144 pot++;
145 sb_build (&tmp, pot);
146 sb_add_sb (&tmp, ptr);
147 sb_kill (ptr);
148 *ptr = tmp;
149 }
150 }
151
152 /* make the sb at ptr point back to the beginning. */
153
154 void
155 sb_reset (ptr)
156 sb *ptr;
157 {
158 ptr->len = 0;
159 }
160
161 /* add character c to the end of the sb at ptr. */
162
163 void
164 sb_add_char (ptr, c)
165 sb *ptr;
166 int c;
167 {
168 sb_check (ptr, 1);
169 ptr->ptr[ptr->len++] = c;
170 }
171
172 /* add null terminated string s to the end of sb at ptr. */
173
174 void
175 sb_add_string (ptr, s)
176 sb *ptr;
177 const char *s;
178 {
179 int len = strlen (s);
180 sb_check (ptr, len);
181 memcpy (ptr->ptr + ptr->len, s, len);
182 ptr->len += len;
183 }
184
185 /* add string at s of length len to sb at ptr */
186
187 void
188 sb_add_buffer (ptr, s, len)
189 sb *ptr;
190 const char *s;
191 int len;
192 {
193 sb_check (ptr, len);
194 memcpy (ptr->ptr + ptr->len, s, len);
195 ptr->len += len;
196 }
197
198 /* print the sb at ptr to the output file */
199
200 void
201 sb_print (outfile, ptr)
202 FILE *outfile;
203 sb *ptr;
204 {
205 int i;
206 int nc = 0;
207
208 for (i = 0; i < ptr->len; i++)
209 {
210 if (nc)
211 {
212 fprintf (outfile, ",");
213 }
214 fprintf (outfile, "%d", ptr->ptr[i]);
215 nc = 1;
216 }
217 }
218
219 void
220 sb_print_at (outfile, idx, ptr)
221 FILE *outfile;
222 int idx;
223 sb *ptr;
224 {
225 int i;
226 for (i = idx; i < ptr->len; i++)
227 putc (ptr->ptr[i], outfile);
228 }
229
230 /* put a null at the end of the sb at in and return the start of the
231 string, so that it can be used as an arg to printf %s. */
232
233 char *
234 sb_name (in)
235 sb *in;
236 {
237 /* stick a null on the end of the string */
238 sb_add_char (in, 0);
239 return in->ptr;
240 }
241
242 /* like sb_name, but don't include the null byte in the string. */
243
244 char *
245 sb_terminate (in)
246 sb *in;
247 {
248 sb_add_char (in, 0);
249 --in->len;
250 return in->ptr;
251 }
252
253 /* start at the index idx into the string in sb at ptr and skip
254 whitespace. return the index of the first non whitespace character */
255
256 int
257 sb_skip_white (idx, ptr)
258 int idx;
259 sb *ptr;
260 {
261 while (idx < ptr->len
262 && (ptr->ptr[idx] == ' '
263 || ptr->ptr[idx] == '\t'))
264 idx++;
265 return idx;
266 }
267
268 /* start at the index idx into the sb at ptr. skips whitespace,
269 a comma and any following whitespace. returnes the index of the
270 next character. */
271
272 int
273 sb_skip_comma (idx, ptr)
274 int idx;
275 sb *ptr;
276 {
277 while (idx < ptr->len
278 && (ptr->ptr[idx] == ' '
279 || ptr->ptr[idx] == '\t'))
280 idx++;
281
282 if (idx < ptr->len
283 && ptr->ptr[idx] == ',')
284 idx++;
285
286 while (idx < ptr->len
287 && (ptr->ptr[idx] == ' '
288 || ptr->ptr[idx] == '\t'))
289 idx++;
290
291 return idx;
292 }
This page took 0.041172 seconds and 4 git commands to generate.