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. */ | |
252b5132 RH |
59 | static sb_list_vector free_list; |
60 | ||
93a9f991 | 61 | /* Initializes an sb. */ |
252b5132 | 62 | |
87c245cc | 63 | static void |
24361518 | 64 | sb_build (sb *ptr, int size) |
252b5132 | 65 | { |
93a9f991 | 66 | /* See if we can find one to allocate. */ |
252b5132 RH |
67 | sb_element *e; |
68 | ||
69 | if (size > sb_max_power_two) | |
70 | abort (); | |
71 | ||
72 | e = free_list.size[size]; | |
73 | if (!e) | |
74 | { | |
93a9f991 | 75 | /* Nothing there, allocate one and stick into the free list. */ |
252b5132 RH |
76 | e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size)); |
77 | e->next = free_list.size[size]; | |
78 | e->size = 1 << size; | |
79 | free_list.size[size] = e; | |
80 | string_count[size]++; | |
81 | } | |
82 | ||
93a9f991 | 83 | /* Remove from free list. */ |
252b5132 RH |
84 | free_list.size[size] = e->next; |
85 | ||
93a9f991 | 86 | /* Copy into callers world. */ |
252b5132 RH |
87 | ptr->ptr = e->data; |
88 | ptr->pot = size; | |
89 | ptr->len = 0; | |
90 | ptr->item = e; | |
91 | } | |
92 | ||
252b5132 | 93 | void |
24361518 | 94 | sb_new (sb *ptr) |
252b5132 RH |
95 | { |
96 | sb_build (ptr, dsize); | |
97 | } | |
98 | ||
93a9f991 | 99 | /* Deallocate the sb at ptr. */ |
252b5132 RH |
100 | |
101 | void | |
24361518 | 102 | sb_kill (sb *ptr) |
252b5132 | 103 | { |
93a9f991 | 104 | /* Return item to free list. */ |
252b5132 RH |
105 | ptr->item->next = free_list.size[ptr->pot]; |
106 | free_list.size[ptr->pot] = ptr->item; | |
107 | } | |
108 | ||
93a9f991 | 109 | /* Add the sb at s to the end of the sb at ptr. */ |
252b5132 RH |
110 | |
111 | void | |
24361518 | 112 | sb_add_sb (sb *ptr, sb *s) |
252b5132 RH |
113 | { |
114 | sb_check (ptr, s->len); | |
115 | memcpy (ptr->ptr + ptr->len, s->ptr, s->len); | |
116 | ptr->len += s->len; | |
117 | } | |
118 | ||
c19d1205 ZW |
119 | /* Helper for sb_scrub_and_add_sb. */ |
120 | ||
121 | static sb *sb_to_scrub; | |
122 | static char *scrub_position; | |
123 | static int | |
124 | scrub_from_sb (char *buf, int buflen) | |
125 | { | |
126 | int copy; | |
127 | copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr); | |
128 | if (copy > buflen) | |
129 | copy = buflen; | |
130 | memcpy (buf, scrub_position, copy); | |
131 | scrub_position += copy; | |
132 | return copy; | |
133 | } | |
134 | ||
135 | /* Run the sb at s through do_scrub_chars and add the result to the sb | |
136 | at ptr. */ | |
137 | ||
138 | void | |
139 | sb_scrub_and_add_sb (sb *ptr, sb *s) | |
140 | { | |
141 | sb_to_scrub = s; | |
142 | scrub_position = s->ptr; | |
143 | ||
144 | sb_check (ptr, s->len); | |
145 | ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len); | |
146 | ||
147 | sb_to_scrub = 0; | |
148 | scrub_position = 0; | |
149 | } | |
150 | ||
93a9f991 | 151 | /* Make sure that the sb at ptr has room for another len characters, |
f0e652b4 | 152 | and grow it if it doesn't. */ |
252b5132 RH |
153 | |
154 | static void | |
24361518 | 155 | sb_check (sb *ptr, int len) |
252b5132 RH |
156 | { |
157 | if (ptr->len + len >= 1 << ptr->pot) | |
158 | { | |
159 | sb tmp; | |
160 | int pot = ptr->pot; | |
93a9f991 | 161 | |
252b5132 RH |
162 | while (ptr->len + len >= 1 << pot) |
163 | pot++; | |
164 | sb_build (&tmp, pot); | |
165 | sb_add_sb (&tmp, ptr); | |
166 | sb_kill (ptr); | |
167 | *ptr = tmp; | |
168 | } | |
169 | } | |
170 | ||
93a9f991 | 171 | /* Make the sb at ptr point back to the beginning. */ |
252b5132 RH |
172 | |
173 | void | |
24361518 | 174 | sb_reset (sb *ptr) |
252b5132 RH |
175 | { |
176 | ptr->len = 0; | |
177 | } | |
178 | ||
93a9f991 | 179 | /* Add character c to the end of the sb at ptr. */ |
252b5132 RH |
180 | |
181 | void | |
24361518 | 182 | sb_add_char (sb *ptr, int c) |
252b5132 RH |
183 | { |
184 | sb_check (ptr, 1); | |
185 | ptr->ptr[ptr->len++] = c; | |
186 | } | |
187 | ||
93a9f991 | 188 | /* Add null terminated string s to the end of sb at ptr. */ |
252b5132 RH |
189 | |
190 | void | |
24361518 | 191 | sb_add_string (sb *ptr, const char *s) |
252b5132 RH |
192 | { |
193 | int len = strlen (s); | |
194 | sb_check (ptr, len); | |
195 | memcpy (ptr->ptr + ptr->len, s, len); | |
196 | ptr->len += len; | |
197 | } | |
198 | ||
93a9f991 | 199 | /* Add string at s of length len to sb at ptr */ |
3c9aabc7 JB |
200 | |
201 | void | |
202 | sb_add_buffer (sb *ptr, const char *s, int len) | |
203 | { | |
204 | sb_check (ptr, len); | |
205 | memcpy (ptr->ptr + ptr->len, s, len); | |
206 | ptr->len += len; | |
207 | } | |
208 | ||
93a9f991 | 209 | /* Like sb_name, but don't include the null byte in the string. */ |
252b5132 RH |
210 | |
211 | char * | |
24361518 | 212 | sb_terminate (sb *in) |
252b5132 RH |
213 | { |
214 | sb_add_char (in, 0); | |
215 | --in->len; | |
216 | return in->ptr; | |
217 | } | |
218 | ||
93a9f991 NC |
219 | /* Start at the index idx into the string in sb at ptr and skip |
220 | whitespace. return the index of the first non whitespace character. */ | |
252b5132 RH |
221 | |
222 | int | |
24361518 | 223 | sb_skip_white (int idx, sb *ptr) |
252b5132 RH |
224 | { |
225 | while (idx < ptr->len | |
226 | && (ptr->ptr[idx] == ' ' | |
227 | || ptr->ptr[idx] == '\t')) | |
228 | idx++; | |
229 | return idx; | |
230 | } | |
231 | ||
93a9f991 | 232 | /* Start at the index idx into the sb at ptr. skips whitespace, |
47eebc20 | 233 | a comma and any following whitespace. returns the index of the |
f0e652b4 | 234 | next character. */ |
252b5132 RH |
235 | |
236 | int | |
24361518 | 237 | sb_skip_comma (int idx, sb *ptr) |
252b5132 RH |
238 | { |
239 | while (idx < ptr->len | |
240 | && (ptr->ptr[idx] == ' ' | |
241 | || ptr->ptr[idx] == '\t')) | |
242 | idx++; | |
243 | ||
244 | if (idx < ptr->len | |
245 | && ptr->ptr[idx] == ',') | |
246 | idx++; | |
247 | ||
248 | while (idx < ptr->len | |
249 | && (ptr->ptr[idx] == ' ' | |
250 | || ptr->ptr[idx] == '\t')) | |
251 | idx++; | |
252 | ||
253 | return idx; | |
254 | } |