]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* sb.c - string buffer manipulation routines |
39a45edc | 2 | Copyright 1994, 1995, 2000, 2003, 2005, 2006, 2007, 2009, 2012 |
ec2655a6 | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | |
5 | Written by Steve and Judy Chamberlain of Cygnus Support, | |
6 | sac@cygnus.com | |
7 | ||
8 | This file is part of GAS, the GNU Assembler. | |
9 | ||
10 | GAS is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
ec2655a6 | 12 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
13 | any later version. |
14 | ||
15 | GAS is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
22 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
23 | 02110-1301, USA. */ | |
252b5132 | 24 | |
c19d1205 | 25 | #include "as.h" |
ebd1c875 | 26 | #include "sb.h" |
252b5132 RH |
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[*]; | |
93a9f991 | 40 | sb_kill (&foo); */ |
252b5132 | 41 | |
39a45edc AM |
42 | static size_t dsize = 32; |
43 | static void sb_check (sb *, size_t); | |
252b5132 | 44 | |
93a9f991 | 45 | /* Initializes an sb. */ |
252b5132 | 46 | |
87c245cc | 47 | static void |
39a45edc | 48 | sb_build (sb *ptr, size_t size) |
252b5132 | 49 | { |
39a45edc AM |
50 | ptr->ptr = xmalloc (size + 1); |
51 | ptr->max = size; | |
252b5132 | 52 | ptr->len = 0; |
252b5132 RH |
53 | } |
54 | ||
252b5132 | 55 | void |
24361518 | 56 | sb_new (sb *ptr) |
252b5132 RH |
57 | { |
58 | sb_build (ptr, dsize); | |
59 | } | |
60 | ||
93a9f991 | 61 | /* Deallocate the sb at ptr. */ |
252b5132 RH |
62 | |
63 | void | |
24361518 | 64 | sb_kill (sb *ptr) |
252b5132 | 65 | { |
39a45edc | 66 | free (ptr->ptr); |
252b5132 RH |
67 | } |
68 | ||
93a9f991 | 69 | /* Add the sb at s to the end of the sb at ptr. */ |
252b5132 RH |
70 | |
71 | void | |
24361518 | 72 | sb_add_sb (sb *ptr, sb *s) |
252b5132 RH |
73 | { |
74 | sb_check (ptr, s->len); | |
75 | memcpy (ptr->ptr + ptr->len, s->ptr, s->len); | |
76 | ptr->len += s->len; | |
77 | } | |
78 | ||
c19d1205 ZW |
79 | /* Helper for sb_scrub_and_add_sb. */ |
80 | ||
81 | static sb *sb_to_scrub; | |
82 | static char *scrub_position; | |
39a45edc AM |
83 | static size_t |
84 | scrub_from_sb (char *buf, size_t buflen) | |
c19d1205 | 85 | { |
39a45edc | 86 | size_t copy; |
c19d1205 ZW |
87 | copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr); |
88 | if (copy > buflen) | |
89 | copy = buflen; | |
90 | memcpy (buf, scrub_position, copy); | |
91 | scrub_position += copy; | |
92 | return copy; | |
93 | } | |
94 | ||
95 | /* Run the sb at s through do_scrub_chars and add the result to the sb | |
96 | at ptr. */ | |
97 | ||
98 | void | |
99 | sb_scrub_and_add_sb (sb *ptr, sb *s) | |
100 | { | |
101 | sb_to_scrub = s; | |
102 | scrub_position = s->ptr; | |
103 | ||
104 | sb_check (ptr, s->len); | |
105 | ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len); | |
106 | ||
107 | sb_to_scrub = 0; | |
108 | scrub_position = 0; | |
109 | } | |
110 | ||
93a9f991 | 111 | /* Make sure that the sb at ptr has room for another len characters, |
f0e652b4 | 112 | and grow it if it doesn't. */ |
252b5132 RH |
113 | |
114 | static void | |
39a45edc | 115 | sb_check (sb *ptr, size_t len) |
252b5132 | 116 | { |
39a45edc AM |
117 | size_t max = ptr->max; |
118 | ||
119 | while (ptr->len + len >= max) | |
120 | { | |
121 | max <<= 1; | |
122 | if (max == 0) | |
123 | as_fatal ("string buffer overflow"); | |
124 | } | |
125 | if (max != ptr->max) | |
252b5132 | 126 | { |
39a45edc AM |
127 | ptr->max = max; |
128 | ptr->ptr = xrealloc (ptr->ptr, max + 1); | |
252b5132 RH |
129 | } |
130 | } | |
131 | ||
93a9f991 | 132 | /* Make the sb at ptr point back to the beginning. */ |
252b5132 RH |
133 | |
134 | void | |
24361518 | 135 | sb_reset (sb *ptr) |
252b5132 RH |
136 | { |
137 | ptr->len = 0; | |
138 | } | |
139 | ||
93a9f991 | 140 | /* Add character c to the end of the sb at ptr. */ |
252b5132 RH |
141 | |
142 | void | |
39a45edc | 143 | sb_add_char (sb *ptr, size_t c) |
252b5132 RH |
144 | { |
145 | sb_check (ptr, 1); | |
146 | ptr->ptr[ptr->len++] = c; | |
147 | } | |
148 | ||
93a9f991 | 149 | /* Add null terminated string s to the end of sb at ptr. */ |
252b5132 RH |
150 | |
151 | void | |
24361518 | 152 | sb_add_string (sb *ptr, const char *s) |
252b5132 | 153 | { |
39a45edc | 154 | size_t len = strlen (s); |
252b5132 RH |
155 | sb_check (ptr, len); |
156 | memcpy (ptr->ptr + ptr->len, s, len); | |
157 | ptr->len += len; | |
158 | } | |
159 | ||
93a9f991 | 160 | /* Add string at s of length len to sb at ptr */ |
3c9aabc7 JB |
161 | |
162 | void | |
39a45edc | 163 | sb_add_buffer (sb *ptr, const char *s, size_t len) |
3c9aabc7 JB |
164 | { |
165 | sb_check (ptr, len); | |
166 | memcpy (ptr->ptr + ptr->len, s, len); | |
167 | ptr->len += len; | |
168 | } | |
169 | ||
93a9f991 | 170 | /* Like sb_name, but don't include the null byte in the string. */ |
252b5132 RH |
171 | |
172 | char * | |
24361518 | 173 | sb_terminate (sb *in) |
252b5132 RH |
174 | { |
175 | sb_add_char (in, 0); | |
176 | --in->len; | |
177 | return in->ptr; | |
178 | } | |
179 | ||
93a9f991 NC |
180 | /* Start at the index idx into the string in sb at ptr and skip |
181 | whitespace. return the index of the first non whitespace character. */ | |
252b5132 | 182 | |
39a45edc AM |
183 | size_t |
184 | sb_skip_white (size_t idx, sb *ptr) | |
252b5132 RH |
185 | { |
186 | while (idx < ptr->len | |
187 | && (ptr->ptr[idx] == ' ' | |
188 | || ptr->ptr[idx] == '\t')) | |
189 | idx++; | |
190 | return idx; | |
191 | } | |
192 | ||
93a9f991 | 193 | /* Start at the index idx into the sb at ptr. skips whitespace, |
47eebc20 | 194 | a comma and any following whitespace. returns the index of the |
f0e652b4 | 195 | next character. */ |
252b5132 | 196 | |
39a45edc AM |
197 | size_t |
198 | sb_skip_comma (size_t idx, sb *ptr) | |
252b5132 RH |
199 | { |
200 | while (idx < ptr->len | |
201 | && (ptr->ptr[idx] == ' ' | |
202 | || ptr->ptr[idx] == '\t')) | |
203 | idx++; | |
204 | ||
205 | if (idx < ptr->len | |
206 | && ptr->ptr[idx] == ',') | |
207 | idx++; | |
208 | ||
209 | while (idx < ptr->len | |
210 | && (ptr->ptr[idx] == ' ' | |
211 | || ptr->ptr[idx] == '\t')) | |
212 | idx++; | |
213 | ||
214 | return idx; | |
215 | } |