Commit | Line | Data |
---|---|---|
7e047ac2 ILT |
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 | #include "sb.h" | |
27 | ||
28 | /* These routines are about manipulating strings. | |
29 | ||
30 | They are managed in things called `sb's which is an abbreviation | |
31 | for string buffers. An sb has to be created, things can be glued | |
32 | on to it, and at the end of it's life it should be freed. The | |
33 | contents should never be pointed at whilst it is still growing, | |
34 | since it could be moved at any time | |
35 | ||
36 | eg: | |
37 | sb_new (&foo); | |
38 | sb_grow... (&foo,...); | |
39 | use foo->ptr[*]; | |
40 | sb_kill (&foo); | |
41 | ||
42 | */ | |
43 | ||
44 | #define dsize 5 | |
45 | ||
46 | static void sb_check PARAMS ((sb *, int)); | |
47 | ||
48 | /* Statistics of sb structures. */ | |
49 | ||
50 | int string_count[sb_max_power_two]; | |
51 | ||
52 | /* Free list of sb structures. */ | |
53 | ||
54 | static sb_list_vector free_list; | |
55 | ||
56 | /* initializes an sb. */ | |
57 | ||
58 | void | |
59 | sb_build (ptr, size) | |
60 | sb *ptr; | |
61 | int size; | |
62 | { | |
63 | /* see if we can find one to allocate */ | |
64 | sb_element *e; | |
65 | ||
66 | if (size > sb_max_power_two) | |
67 | abort (); | |
68 | ||
69 | e = free_list.size[size]; | |
70 | if (!e) | |
71 | { | |
72 | /* nothing there, allocate one and stick into the free list */ | |
73 | e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size)); | |
74 | e->next = free_list.size[size]; | |
75 | e->size = 1 << size; | |
76 | free_list.size[size] = e; | |
77 | string_count[size]++; | |
78 | } | |
79 | ||
80 | /* remove from free list */ | |
81 | ||
82 | free_list.size[size] = e->next; | |
83 | ||
84 | /* copy into callers world */ | |
85 | ptr->ptr = e->data; | |
86 | ptr->pot = size; | |
87 | ptr->len = 0; | |
88 | ptr->item = e; | |
89 | } | |
90 | ||
91 | ||
92 | void | |
93 | sb_new (ptr) | |
94 | sb *ptr; | |
95 | { | |
96 | sb_build (ptr, dsize); | |
97 | } | |
98 | ||
99 | /* deallocate the sb at ptr */ | |
100 | ||
101 | void | |
102 | sb_kill (ptr) | |
103 | sb *ptr; | |
104 | { | |
105 | /* return item to free list */ | |
106 | ptr->item->next = free_list.size[ptr->pot]; | |
107 | free_list.size[ptr->pot] = ptr->item; | |
108 | } | |
109 | ||
110 | /* add the sb at s to the end of the sb at ptr */ | |
111 | ||
112 | void | |
113 | sb_add_sb (ptr, s) | |
114 | sb *ptr; | |
115 | sb *s; | |
116 | { | |
117 | sb_check (ptr, s->len); | |
118 | memcpy (ptr->ptr + ptr->len, s->ptr, s->len); | |
119 | ptr->len += s->len; | |
120 | } | |
121 | ||
122 | /* make sure that the sb at ptr has room for another len characters, | |
123 | and grow it if it doesn't. */ | |
124 | ||
125 | static void | |
126 | sb_check (ptr, len) | |
127 | sb *ptr; | |
128 | int len; | |
129 | { | |
130 | if (ptr->len + len >= 1 << ptr->pot) | |
131 | { | |
132 | sb tmp; | |
133 | int pot = ptr->pot; | |
134 | while (ptr->len + len >= 1 << pot) | |
135 | pot++; | |
136 | sb_build (&tmp, pot); | |
137 | sb_add_sb (&tmp, ptr); | |
138 | sb_kill (ptr); | |
139 | *ptr = tmp; | |
140 | } | |
141 | } | |
142 | ||
143 | /* make the sb at ptr point back to the beginning. */ | |
144 | ||
145 | void | |
146 | sb_reset (ptr) | |
147 | sb *ptr; | |
148 | { | |
149 | ptr->len = 0; | |
150 | } | |
151 | ||
152 | /* add character c to the end of the sb at ptr. */ | |
153 | ||
154 | void | |
155 | sb_add_char (ptr, c) | |
156 | sb *ptr; | |
157 | int c; | |
158 | { | |
159 | sb_check (ptr, 1); | |
160 | ptr->ptr[ptr->len++] = c; | |
161 | } | |
162 | ||
163 | /* add null terminated string s to the end of sb at ptr. */ | |
164 | ||
165 | void | |
166 | sb_add_string (ptr, s) | |
167 | sb *ptr; | |
168 | const char *s; | |
169 | { | |
170 | int len = strlen (s); | |
171 | sb_check (ptr, len); | |
172 | memcpy (ptr->ptr + ptr->len, s, len); | |
173 | ptr->len += len; | |
174 | } | |
175 | ||
176 | /* add string at s of length len to sb at ptr */ | |
177 | ||
178 | void | |
179 | sb_add_buffer (ptr, s, len) | |
180 | sb *ptr; | |
181 | const char *s; | |
182 | int len; | |
183 | { | |
184 | sb_check (ptr, len); | |
185 | memcpy (ptr->ptr + ptr->len, s, len); | |
186 | ptr->len += len; | |
187 | } | |
188 | ||
189 | /* print the sb at ptr to the output file */ | |
190 | ||
191 | void | |
192 | sb_print (outfile, ptr) | |
193 | FILE *outfile; | |
194 | sb *ptr; | |
195 | { | |
196 | int i; | |
197 | int nc = 0; | |
198 | ||
199 | for (i = 0; i < ptr->len; i++) | |
200 | { | |
201 | if (nc) | |
202 | { | |
203 | fprintf (outfile, ","); | |
204 | } | |
205 | fprintf (outfile, "%d", ptr->ptr[i]); | |
206 | nc = 1; | |
207 | } | |
208 | } | |
209 | ||
210 | void | |
211 | sb_print_at (outfile, idx, ptr) | |
212 | FILE *outfile; | |
213 | int idx; | |
214 | sb *ptr; | |
215 | { | |
216 | int i; | |
217 | for (i = idx; i < ptr->len; i++) | |
218 | putc (ptr->ptr[i], outfile); | |
219 | } | |
220 | ||
221 | /* put a null at the end of the sb at in and return the start of the | |
222 | string, so that it can be used as an arg to printf %s. */ | |
223 | ||
224 | char * | |
225 | sb_name (in) | |
226 | sb *in; | |
227 | { | |
228 | /* stick a null on the end of the string */ | |
229 | sb_add_char (in, 0); | |
230 | return in->ptr; | |
231 | } | |
232 | ||
233 | /* like sb_name, but don't include the null byte in the string. */ | |
234 | ||
235 | char * | |
236 | sb_terminate (in) | |
237 | sb *in; | |
238 | { | |
239 | sb_add_char (in, 0); | |
240 | --in->len; | |
241 | return in->ptr; | |
242 | } | |
243 | ||
244 | /* start at the index idx into the string in sb at ptr and skip | |
245 | whitespace. return the index of the first non whitespace character */ | |
246 | ||
247 | int | |
248 | sb_skip_white (idx, ptr) | |
249 | int idx; | |
250 | sb *ptr; | |
251 | { | |
252 | while (idx < ptr->len | |
253 | && (ptr->ptr[idx] == ' ' | |
254 | || ptr->ptr[idx] == '\t')) | |
255 | idx++; | |
256 | return idx; | |
257 | } | |
258 | ||
259 | /* start at the index idx into the sb at ptr. skips whitespace, | |
260 | a comma and any following whitespace. returnes the index of the | |
261 | next character. */ | |
262 | ||
263 | int | |
264 | sb_skip_comma (idx, ptr) | |
265 | int idx; | |
266 | sb *ptr; | |
267 | { | |
268 | while (idx < ptr->len | |
269 | && (ptr->ptr[idx] == ' ' | |
270 | || ptr->ptr[idx] == '\t')) | |
271 | idx++; | |
272 | ||
273 | if (idx < ptr->len | |
274 | && ptr->ptr[idx] == ',') | |
275 | idx++; | |
276 | ||
277 | while (idx < ptr->len | |
278 | && (ptr->ptr[idx] == ' ' | |
279 | || ptr->ptr[idx] == '\t')) | |
280 | idx++; | |
281 | ||
282 | return idx; | |
283 | } |