* som.c (struct som_misc_symbol_info): Add is_comdat, is_common and
[deliverable/binutils-gdb.git] / gas / sb.c
CommitLineData
252b5132 1/* sb.c - string buffer manipulation routines
f7e42eb4 2 Copyright 1994, 1995, 2000 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
53#define dsize 5
54
24361518 55static void sb_check (sb *, int);
252b5132
RH
56
57/* Statistics of sb structures. */
58
59int string_count[sb_max_power_two];
60
61/* Free list of sb structures. */
62
63static sb_list_vector free_list;
64
f0e652b4 65/* initializes an sb. */
252b5132
RH
66
67void
24361518 68sb_build (sb *ptr, int size)
252b5132
RH
69{
70 /* see if we can find one to allocate */
71 sb_element *e;
72
73 if (size > sb_max_power_two)
74 abort ();
75
76 e = free_list.size[size];
77 if (!e)
78 {
79 /* nothing there, allocate one and stick into the free list */
80 e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
81 e->next = free_list.size[size];
82 e->size = 1 << size;
83 free_list.size[size] = e;
84 string_count[size]++;
85 }
86
87 /* remove from free list */
88
89 free_list.size[size] = e->next;
90
91 /* copy into callers world */
92 ptr->ptr = e->data;
93 ptr->pot = size;
94 ptr->len = 0;
95 ptr->item = e;
96}
97
252b5132 98void
24361518 99sb_new (sb *ptr)
252b5132
RH
100{
101 sb_build (ptr, dsize);
102}
103
104/* deallocate the sb at ptr */
105
106void
24361518 107sb_kill (sb *ptr)
252b5132
RH
108{
109 /* return item to free list */
110 ptr->item->next = free_list.size[ptr->pot];
111 free_list.size[ptr->pot] = ptr->item;
112}
113
114/* add the sb at s to the end of the sb at ptr */
115
116void
24361518 117sb_add_sb (sb *ptr, sb *s)
252b5132
RH
118{
119 sb_check (ptr, s->len);
120 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
121 ptr->len += s->len;
122}
123
124/* make sure that the sb at ptr has room for another len characters,
f0e652b4 125 and grow it if it doesn't. */
252b5132
RH
126
127static void
24361518 128sb_check (sb *ptr, int len)
252b5132
RH
129{
130 if (ptr->len + len >= 1 << ptr->pot)
131 {
132 sb tmp;
133 int pot = ptr->pot;
134 while (ptr->len + len >= 1 << pot)
135 pot++;
136 sb_build (&tmp, pot);
137 sb_add_sb (&tmp, ptr);
138 sb_kill (ptr);
139 *ptr = tmp;
140 }
141}
142
143/* make the sb at ptr point back to the beginning. */
144
145void
24361518 146sb_reset (sb *ptr)
252b5132
RH
147{
148 ptr->len = 0;
149}
150
f0e652b4 151/* add character c to the end of the sb at ptr. */
252b5132
RH
152
153void
24361518 154sb_add_char (sb *ptr, int c)
252b5132
RH
155{
156 sb_check (ptr, 1);
157 ptr->ptr[ptr->len++] = c;
158}
159
f0e652b4 160/* add null terminated string s to the end of sb at ptr. */
252b5132
RH
161
162void
24361518 163sb_add_string (sb *ptr, const char *s)
252b5132
RH
164{
165 int len = strlen (s);
166 sb_check (ptr, len);
167 memcpy (ptr->ptr + ptr->len, s, len);
168 ptr->len += len;
169}
170
171/* add string at s of length len to sb at ptr */
172
173void
24361518 174sb_add_buffer (sb *ptr, const char *s, int len)
252b5132
RH
175{
176 sb_check (ptr, len);
177 memcpy (ptr->ptr + ptr->len, s, len);
178 ptr->len += len;
179}
180
181/* print the sb at ptr to the output file */
182
183void
24361518 184sb_print (FILE *outfile, sb *ptr)
252b5132
RH
185{
186 int i;
187 int nc = 0;
188
189 for (i = 0; i < ptr->len; i++)
190 {
191 if (nc)
192 {
193 fprintf (outfile, ",");
194 }
195 fprintf (outfile, "%d", ptr->ptr[i]);
196 nc = 1;
197 }
198}
199
f0e652b4 200void
24361518 201sb_print_at (FILE *outfile, int idx, sb *ptr)
252b5132
RH
202{
203 int i;
204 for (i = idx; i < ptr->len; i++)
205 putc (ptr->ptr[i], outfile);
206}
207
208/* put a null at the end of the sb at in and return the start of the
f0e652b4 209 string, so that it can be used as an arg to printf %s. */
252b5132
RH
210
211char *
24361518 212sb_name (sb *in)
252b5132
RH
213{
214 /* stick a null on the end of the string */
215 sb_add_char (in, 0);
216 return in->ptr;
217}
218
219/* like sb_name, but don't include the null byte in the string. */
220
221char *
24361518 222sb_terminate (sb *in)
252b5132
RH
223{
224 sb_add_char (in, 0);
225 --in->len;
226 return in->ptr;
227}
228
229/* start at the index idx into the string in sb at ptr and skip
230 whitespace. return the index of the first non whitespace character */
231
232int
24361518 233sb_skip_white (int idx, sb *ptr)
252b5132
RH
234{
235 while (idx < ptr->len
236 && (ptr->ptr[idx] == ' '
237 || ptr->ptr[idx] == '\t'))
238 idx++;
239 return idx;
240}
241
242/* start at the index idx into the sb at ptr. skips whitespace,
47eebc20 243 a comma and any following whitespace. returns the index of the
f0e652b4 244 next character. */
252b5132
RH
245
246int
24361518 247sb_skip_comma (int idx, sb *ptr)
252b5132
RH
248{
249 while (idx < ptr->len
250 && (ptr->ptr[idx] == ' '
251 || ptr->ptr[idx] == '\t'))
252 idx++;
253
254 if (idx < ptr->len
255 && ptr->ptr[idx] == ',')
256 idx++;
257
258 while (idx < ptr->len
259 && (ptr->ptr[idx] == ' '
260 || ptr->ptr[idx] == '\t'))
261 idx++;
262
263 return idx;
264}
This page took 0.338019 seconds and 4 git commands to generate.