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 | |
21 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
f0e652b4 | 22 | 02111-1307, 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" | |
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 | ||
87c245cc | 53 | static int dsize = 5; |
24361518 | 54 | static void sb_check (sb *, int); |
252b5132 RH |
55 | |
56 | /* Statistics of sb structures. */ | |
87c245cc | 57 | static int string_count[sb_max_power_two]; |
252b5132 RH |
58 | |
59 | /* Free list of sb structures. */ | |
252b5132 RH |
60 | static sb_list_vector free_list; |
61 | ||
f0e652b4 | 62 | /* initializes an sb. */ |
252b5132 | 63 | |
87c245cc | 64 | static void |
24361518 | 65 | sb_build (sb *ptr, int size) |
252b5132 RH |
66 | { |
67 | /* see if we can find one to allocate */ | |
68 | sb_element *e; | |
69 | ||
70 | if (size > sb_max_power_two) | |
71 | abort (); | |
72 | ||
73 | e = free_list.size[size]; | |
74 | if (!e) | |
75 | { | |
76 | /* nothing there, allocate one and stick into the free list */ | |
77 | e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size)); | |
78 | e->next = free_list.size[size]; | |
79 | e->size = 1 << size; | |
80 | free_list.size[size] = e; | |
81 | string_count[size]++; | |
82 | } | |
83 | ||
84 | /* remove from free list */ | |
85 | ||
86 | free_list.size[size] = e->next; | |
87 | ||
88 | /* copy into callers world */ | |
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 | ||
101 | /* deallocate the sb at ptr */ | |
102 | ||
103 | void | |
24361518 | 104 | sb_kill (sb *ptr) |
252b5132 RH |
105 | { |
106 | /* return item to free list */ | |
107 | ptr->item->next = free_list.size[ptr->pot]; | |
108 | free_list.size[ptr->pot] = ptr->item; | |
109 | } | |
110 | ||
111 | /* add the sb at s to the end of the sb at ptr */ | |
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 | ||
121 | /* make sure that the sb at ptr has room for another len characters, | |
f0e652b4 | 122 | and grow it if it doesn't. */ |
252b5132 RH |
123 | |
124 | static void | |
24361518 | 125 | sb_check (sb *ptr, int len) |
252b5132 RH |
126 | { |
127 | if (ptr->len + len >= 1 << ptr->pot) | |
128 | { | |
129 | sb tmp; | |
130 | int pot = ptr->pot; | |
131 | while (ptr->len + len >= 1 << pot) | |
132 | pot++; | |
133 | sb_build (&tmp, pot); | |
134 | sb_add_sb (&tmp, ptr); | |
135 | sb_kill (ptr); | |
136 | *ptr = tmp; | |
137 | } | |
138 | } | |
139 | ||
140 | /* make the sb at ptr point back to the beginning. */ | |
141 | ||
142 | void | |
24361518 | 143 | sb_reset (sb *ptr) |
252b5132 RH |
144 | { |
145 | ptr->len = 0; | |
146 | } | |
147 | ||
f0e652b4 | 148 | /* add character c to the end of the sb at ptr. */ |
252b5132 RH |
149 | |
150 | void | |
24361518 | 151 | sb_add_char (sb *ptr, int c) |
252b5132 RH |
152 | { |
153 | sb_check (ptr, 1); | |
154 | ptr->ptr[ptr->len++] = c; | |
155 | } | |
156 | ||
f0e652b4 | 157 | /* add null terminated string s to the end of sb at ptr. */ |
252b5132 RH |
158 | |
159 | void | |
24361518 | 160 | sb_add_string (sb *ptr, const char *s) |
252b5132 RH |
161 | { |
162 | int len = strlen (s); | |
163 | sb_check (ptr, len); | |
164 | memcpy (ptr->ptr + ptr->len, s, len); | |
165 | ptr->len += len; | |
166 | } | |
167 | ||
252b5132 RH |
168 | /* like sb_name, but don't include the null byte in the string. */ |
169 | ||
170 | char * | |
24361518 | 171 | sb_terminate (sb *in) |
252b5132 RH |
172 | { |
173 | sb_add_char (in, 0); | |
174 | --in->len; | |
175 | return in->ptr; | |
176 | } | |
177 | ||
178 | /* start at the index idx into the string in sb at ptr and skip | |
179 | whitespace. return the index of the first non whitespace character */ | |
180 | ||
181 | int | |
24361518 | 182 | sb_skip_white (int idx, sb *ptr) |
252b5132 RH |
183 | { |
184 | while (idx < ptr->len | |
185 | && (ptr->ptr[idx] == ' ' | |
186 | || ptr->ptr[idx] == '\t')) | |
187 | idx++; | |
188 | return idx; | |
189 | } | |
190 | ||
191 | /* start at the index idx into the sb at ptr. skips whitespace, | |
47eebc20 | 192 | a comma and any following whitespace. returns the index of the |
f0e652b4 | 193 | next character. */ |
252b5132 RH |
194 | |
195 | int | |
24361518 | 196 | sb_skip_comma (int idx, sb *ptr) |
252b5132 RH |
197 | { |
198 | while (idx < ptr->len | |
199 | && (ptr->ptr[idx] == ' ' | |
200 | || ptr->ptr[idx] == '\t')) | |
201 | idx++; | |
202 | ||
203 | if (idx < ptr->len | |
204 | && ptr->ptr[idx] == ',') | |
205 | idx++; | |
206 | ||
207 | while (idx < ptr->len | |
208 | && (ptr->ptr[idx] == ' ' | |
209 | || ptr->ptr[idx] == '\t')) | |
210 | idx++; | |
211 | ||
212 | return idx; | |
213 | } |