Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* sb.c - string buffer manipulation routines |
2da5c037 | 2 | Copyright 1994, 1995, 2000, 2003 Free Software Foundation, Inc. |
252b5132 RH |
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 | |
4b4da160 NC |
21 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
22 | 02110-1301, USA. */ | |
252b5132 RH |
23 | |
24 | #include "config.h" | |
25 | #include <stdio.h> | |
f980ada0 L |
26 | #ifdef HAVE_STDLIB_H |
27 | #include <stdlib.h> | |
28 | #endif | |
252b5132 RH |
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" | |
c19d1205 | 36 | #include "as.h" |
252b5132 RH |
37 | |
38 | /* These routines are about manipulating strings. | |
39 | ||
40 | They are managed in things called `sb's which is an abbreviation | |
41 | for string buffers. An sb has to be created, things can be glued | |
42 | on to it, and at the end of it's life it should be freed. The | |
43 | contents should never be pointed at whilst it is still growing, | |
44 | since it could be moved at any time | |
45 | ||
46 | eg: | |
47 | sb_new (&foo); | |
48 | sb_grow... (&foo,...); | |
49 | use foo->ptr[*]; | |
93a9f991 | 50 | sb_kill (&foo); */ |
252b5132 | 51 | |
87c245cc | 52 | static int dsize = 5; |
24361518 | 53 | static void sb_check (sb *, int); |
252b5132 RH |
54 | |
55 | /* Statistics of sb structures. */ | |
87c245cc | 56 | static int string_count[sb_max_power_two]; |
252b5132 RH |
57 | |
58 | /* Free list of sb structures. */ | |
58633d9a BE |
59 | static struct |
60 | { | |
61 | sb_element *size[sb_max_power_two]; | |
62 | } free_list; | |
252b5132 | 63 | |
93a9f991 | 64 | /* Initializes an sb. */ |
252b5132 | 65 | |
87c245cc | 66 | static void |
24361518 | 67 | sb_build (sb *ptr, int size) |
252b5132 | 68 | { |
93a9f991 | 69 | /* See if we can find one to allocate. */ |
252b5132 RH |
70 | sb_element *e; |
71 | ||
58633d9a | 72 | assert (size < sb_max_power_two); |
252b5132 RH |
73 | |
74 | e = free_list.size[size]; | |
75 | if (!e) | |
76 | { | |
93a9f991 | 77 | /* Nothing there, allocate one and stick into the free list. */ |
252b5132 RH |
78 | e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size)); |
79 | e->next = free_list.size[size]; | |
80 | e->size = 1 << size; | |
81 | free_list.size[size] = e; | |
82 | string_count[size]++; | |
83 | } | |
84 | ||
93a9f991 | 85 | /* Remove from free list. */ |
252b5132 RH |
86 | free_list.size[size] = e->next; |
87 | ||
93a9f991 | 88 | /* Copy into callers world. */ |
252b5132 RH |
89 | ptr->ptr = e->data; |
90 | ptr->pot = size; | |
91 | ptr->len = 0; | |
92 | ptr->item = e; | |
93 | } | |
94 | ||
252b5132 | 95 | void |
24361518 | 96 | sb_new (sb *ptr) |
252b5132 RH |
97 | { |
98 | sb_build (ptr, dsize); | |
99 | } | |
100 | ||
93a9f991 | 101 | /* Deallocate the sb at ptr. */ |
252b5132 RH |
102 | |
103 | void | |
24361518 | 104 | sb_kill (sb *ptr) |
252b5132 | 105 | { |
93a9f991 | 106 | /* Return item to free list. */ |
252b5132 RH |
107 | ptr->item->next = free_list.size[ptr->pot]; |
108 | free_list.size[ptr->pot] = ptr->item; | |
109 | } | |
110 | ||
93a9f991 | 111 | /* Add the sb at s to the end of the sb at ptr. */ |
252b5132 RH |
112 | |
113 | void | |
24361518 | 114 | sb_add_sb (sb *ptr, sb *s) |
252b5132 RH |
115 | { |
116 | sb_check (ptr, s->len); | |
117 | memcpy (ptr->ptr + ptr->len, s->ptr, s->len); | |
118 | ptr->len += s->len; | |
119 | } | |
120 | ||
c19d1205 ZW |
121 | /* Helper for sb_scrub_and_add_sb. */ |
122 | ||
123 | static sb *sb_to_scrub; | |
124 | static char *scrub_position; | |
125 | static int | |
126 | scrub_from_sb (char *buf, int buflen) | |
127 | { | |
128 | int copy; | |
129 | copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr); | |
130 | if (copy > buflen) | |
131 | copy = buflen; | |
132 | memcpy (buf, scrub_position, copy); | |
133 | scrub_position += copy; | |
134 | return copy; | |
135 | } | |
136 | ||
137 | /* Run the sb at s through do_scrub_chars and add the result to the sb | |
138 | at ptr. */ | |
139 | ||
140 | void | |
141 | sb_scrub_and_add_sb (sb *ptr, sb *s) | |
142 | { | |
143 | sb_to_scrub = s; | |
144 | scrub_position = s->ptr; | |
145 | ||
146 | sb_check (ptr, s->len); | |
147 | ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len); | |
148 | ||
149 | sb_to_scrub = 0; | |
150 | scrub_position = 0; | |
151 | } | |
152 | ||
93a9f991 | 153 | /* Make sure that the sb at ptr has room for another len characters, |
f0e652b4 | 154 | and grow it if it doesn't. */ |
252b5132 RH |
155 | |
156 | static void | |
24361518 | 157 | sb_check (sb *ptr, int len) |
252b5132 RH |
158 | { |
159 | if (ptr->len + len >= 1 << ptr->pot) | |
160 | { | |
161 | sb tmp; | |
162 | int pot = ptr->pot; | |
93a9f991 | 163 | |
252b5132 RH |
164 | while (ptr->len + len >= 1 << pot) |
165 | pot++; | |
166 | sb_build (&tmp, pot); | |
167 | sb_add_sb (&tmp, ptr); | |
168 | sb_kill (ptr); | |
169 | *ptr = tmp; | |
170 | } | |
171 | } | |
172 | ||
93a9f991 | 173 | /* Make the sb at ptr point back to the beginning. */ |
252b5132 RH |
174 | |
175 | void | |
24361518 | 176 | sb_reset (sb *ptr) |
252b5132 RH |
177 | { |
178 | ptr->len = 0; | |
179 | } | |
180 | ||
93a9f991 | 181 | /* Add character c to the end of the sb at ptr. */ |
252b5132 RH |
182 | |
183 | void | |
24361518 | 184 | sb_add_char (sb *ptr, int c) |
252b5132 RH |
185 | { |
186 | sb_check (ptr, 1); | |
187 | ptr->ptr[ptr->len++] = c; | |
188 | } | |
189 | ||
93a9f991 | 190 | /* Add null terminated string s to the end of sb at ptr. */ |
252b5132 RH |
191 | |
192 | void | |
24361518 | 193 | sb_add_string (sb *ptr, const char *s) |
252b5132 RH |
194 | { |
195 | int len = strlen (s); | |
196 | sb_check (ptr, len); | |
197 | memcpy (ptr->ptr + ptr->len, s, len); | |
198 | ptr->len += len; | |
199 | } | |
200 | ||
93a9f991 | 201 | /* Add string at s of length len to sb at ptr */ |
3c9aabc7 JB |
202 | |
203 | void | |
204 | sb_add_buffer (sb *ptr, const char *s, int len) | |
205 | { | |
206 | sb_check (ptr, len); | |
207 | memcpy (ptr->ptr + ptr->len, s, len); | |
208 | ptr->len += len; | |
209 | } | |
210 | ||
93a9f991 | 211 | /* Like sb_name, but don't include the null byte in the string. */ |
252b5132 RH |
212 | |
213 | char * | |
24361518 | 214 | sb_terminate (sb *in) |
252b5132 RH |
215 | { |
216 | sb_add_char (in, 0); | |
217 | --in->len; | |
218 | return in->ptr; | |
219 | } | |
220 | ||
93a9f991 NC |
221 | /* Start at the index idx into the string in sb at ptr and skip |
222 | whitespace. return the index of the first non whitespace character. */ | |
252b5132 RH |
223 | |
224 | int | |
24361518 | 225 | sb_skip_white (int idx, sb *ptr) |
252b5132 RH |
226 | { |
227 | while (idx < ptr->len | |
228 | && (ptr->ptr[idx] == ' ' | |
229 | || ptr->ptr[idx] == '\t')) | |
230 | idx++; | |
231 | return idx; | |
232 | } | |
233 | ||
93a9f991 | 234 | /* Start at the index idx into the sb at ptr. skips whitespace, |
47eebc20 | 235 | a comma and any following whitespace. returns the index of the |
f0e652b4 | 236 | next character. */ |
252b5132 RH |
237 | |
238 | int | |
24361518 | 239 | sb_skip_comma (int idx, sb *ptr) |
252b5132 RH |
240 | { |
241 | while (idx < ptr->len | |
242 | && (ptr->ptr[idx] == ' ' | |
243 | || ptr->ptr[idx] == '\t')) | |
244 | idx++; | |
245 | ||
246 | if (idx < ptr->len | |
247 | && ptr->ptr[idx] == ',') | |
248 | idx++; | |
249 | ||
250 | while (idx < ptr->len | |
251 | && (ptr->ptr[idx] == ' ' | |
252 | || ptr->ptr[idx] == '\t')) | |
253 | idx++; | |
254 | ||
255 | return idx; | |
256 | } |