f345fe13a9a95a8f9fe870ecbe37697a4ef53eba
[deliverable/binutils-gdb.git] / gas / sb.c
1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000, 2003, 2005, 2006, 2007, 2009, 2012
3 Free Software Foundation, Inc.
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
12 the Free Software Foundation; either version 3, or (at your option)
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
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
24
25 #include "as.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 static size_t dsize = 32;
43 static void sb_check (sb *, size_t);
44
45 /* Initializes an sb. */
46
47 static void
48 sb_build (sb *ptr, size_t size)
49 {
50 ptr->ptr = xmalloc (size + 1);
51 ptr->max = size;
52 ptr->len = 0;
53 }
54
55 void
56 sb_new (sb *ptr)
57 {
58 sb_build (ptr, dsize);
59 }
60
61 /* Deallocate the sb at ptr. */
62
63 void
64 sb_kill (sb *ptr)
65 {
66 free (ptr->ptr);
67 }
68
69 /* Add the sb at s to the end of the sb at ptr. */
70
71 void
72 sb_add_sb (sb *ptr, sb *s)
73 {
74 sb_check (ptr, s->len);
75 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
76 ptr->len += s->len;
77 }
78
79 /* Helper for sb_scrub_and_add_sb. */
80
81 static sb *sb_to_scrub;
82 static char *scrub_position;
83 static size_t
84 scrub_from_sb (char *buf, size_t buflen)
85 {
86 size_t copy;
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
111 /* Make sure that the sb at ptr has room for another len characters,
112 and grow it if it doesn't. */
113
114 static void
115 sb_check (sb *ptr, size_t len)
116 {
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)
126 {
127 ptr->max = max;
128 ptr->ptr = xrealloc (ptr->ptr, max + 1);
129 }
130 }
131
132 /* Make the sb at ptr point back to the beginning. */
133
134 void
135 sb_reset (sb *ptr)
136 {
137 ptr->len = 0;
138 }
139
140 /* Add character c to the end of the sb at ptr. */
141
142 void
143 sb_add_char (sb *ptr, size_t c)
144 {
145 sb_check (ptr, 1);
146 ptr->ptr[ptr->len++] = c;
147 }
148
149 /* Add null terminated string s to the end of sb at ptr. */
150
151 void
152 sb_add_string (sb *ptr, const char *s)
153 {
154 size_t len = strlen (s);
155 sb_check (ptr, len);
156 memcpy (ptr->ptr + ptr->len, s, len);
157 ptr->len += len;
158 }
159
160 /* Add string at s of length len to sb at ptr */
161
162 void
163 sb_add_buffer (sb *ptr, const char *s, size_t len)
164 {
165 sb_check (ptr, len);
166 memcpy (ptr->ptr + ptr->len, s, len);
167 ptr->len += len;
168 }
169
170 /* Like sb_name, but don't include the null byte in the string. */
171
172 char *
173 sb_terminate (sb *in)
174 {
175 sb_add_char (in, 0);
176 --in->len;
177 return in->ptr;
178 }
179
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. */
182
183 size_t
184 sb_skip_white (size_t idx, sb *ptr)
185 {
186 while (idx < ptr->len
187 && (ptr->ptr[idx] == ' '
188 || ptr->ptr[idx] == '\t'))
189 idx++;
190 return idx;
191 }
192
193 /* Start at the index idx into the sb at ptr. skips whitespace,
194 a comma and any following whitespace. returns the index of the
195 next character. */
196
197 size_t
198 sb_skip_comma (size_t idx, sb *ptr)
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 }
This page took 0.033834 seconds and 4 git commands to generate.