Back to index

cell-binutils  2.17cvs20070401
sb.c
Go to the documentation of this file.
00001 /* sb.c - string buffer manipulation routines
00002    Copyright 1994, 1995, 2000, 2003, 2006 Free Software Foundation, Inc.
00003 
00004    Written by Steve and Judy Chamberlain of Cygnus Support,
00005       sac@cygnus.com
00006 
00007    This file is part of GAS, the GNU Assembler.
00008 
00009    GAS is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2, or (at your option)
00012    any later version.
00013 
00014    GAS is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with GAS; see the file COPYING.  If not, write to the Free
00021    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00022    02110-1301, USA.  */
00023 
00024 #include "as.h"
00025 #include "sb.h"
00026 
00027 /* These routines are about manipulating strings.
00028 
00029    They are managed in things called `sb's which is an abbreviation
00030    for string buffers.  An sb has to be created, things can be glued
00031    on to it, and at the end of it's life it should be freed.  The
00032    contents should never be pointed at whilst it is still growing,
00033    since it could be moved at any time
00034 
00035    eg:
00036    sb_new (&foo);
00037    sb_grow... (&foo,...);
00038    use foo->ptr[*];
00039    sb_kill (&foo);  */
00040 
00041 static int dsize = 5;
00042 static void sb_check (sb *, int);
00043 
00044 /* Statistics of sb structures.  */
00045 static int string_count[sb_max_power_two];
00046 
00047 /* Free list of sb structures.  */
00048 static struct
00049 {
00050   sb_element *size[sb_max_power_two];
00051 } free_list;
00052 
00053 /* Initializes an sb.  */
00054 
00055 static void
00056 sb_build (sb *ptr, int size)
00057 {
00058   /* See if we can find one to allocate.  */
00059   sb_element *e;
00060 
00061   assert (size < sb_max_power_two);
00062 
00063   e = free_list.size[size];
00064   if (!e)
00065     {
00066       /* Nothing there, allocate one and stick into the free list.  */
00067       e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
00068       e->next = free_list.size[size];
00069       e->size = 1 << size;
00070       free_list.size[size] = e;
00071       string_count[size]++;
00072     }
00073 
00074   /* Remove from free list.  */
00075   free_list.size[size] = e->next;
00076 
00077   /* Copy into callers world.  */
00078   ptr->ptr = e->data;
00079   ptr->pot = size;
00080   ptr->len = 0;
00081   ptr->item = e;
00082 }
00083 
00084 void
00085 sb_new (sb *ptr)
00086 {
00087   sb_build (ptr, dsize);
00088 }
00089 
00090 /* Deallocate the sb at ptr.  */
00091 
00092 void
00093 sb_kill (sb *ptr)
00094 {
00095   /* Return item to free list.  */
00096   ptr->item->next = free_list.size[ptr->pot];
00097   free_list.size[ptr->pot] = ptr->item;
00098 }
00099 
00100 /* Add the sb at s to the end of the sb at ptr.  */
00101 
00102 void
00103 sb_add_sb (sb *ptr, sb *s)
00104 {
00105   sb_check (ptr, s->len);
00106   memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
00107   ptr->len += s->len;
00108 }
00109 
00110 /* Helper for sb_scrub_and_add_sb.  */
00111 
00112 static sb *sb_to_scrub;
00113 static char *scrub_position;
00114 static int
00115 scrub_from_sb (char *buf, int buflen)
00116 {
00117   int copy;
00118   copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
00119   if (copy > buflen)
00120     copy = buflen;
00121   memcpy (buf, scrub_position, copy);
00122   scrub_position += copy;
00123   return copy;
00124 }
00125 
00126 /* Run the sb at s through do_scrub_chars and add the result to the sb
00127    at ptr.  */
00128 
00129 void
00130 sb_scrub_and_add_sb (sb *ptr, sb *s)
00131 {
00132   sb_to_scrub = s;
00133   scrub_position = s->ptr;
00134   
00135   sb_check (ptr, s->len);
00136   ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
00137 
00138   sb_to_scrub = 0;
00139   scrub_position = 0;
00140 }
00141 
00142 /* Make sure that the sb at ptr has room for another len characters,
00143    and grow it if it doesn't.  */
00144 
00145 static void
00146 sb_check (sb *ptr, int len)
00147 {
00148   if (ptr->len + len >= 1 << ptr->pot)
00149     {
00150       sb tmp;
00151       int pot = ptr->pot;
00152 
00153       while (ptr->len + len >= 1 << pot)
00154        pot++;
00155       sb_build (&tmp, pot);
00156       sb_add_sb (&tmp, ptr);
00157       sb_kill (ptr);
00158       *ptr = tmp;
00159     }
00160 }
00161 
00162 /* Make the sb at ptr point back to the beginning.  */
00163 
00164 void
00165 sb_reset (sb *ptr)
00166 {
00167   ptr->len = 0;
00168 }
00169 
00170 /* Add character c to the end of the sb at ptr.  */
00171 
00172 void
00173 sb_add_char (sb *ptr, int c)
00174 {
00175   sb_check (ptr, 1);
00176   ptr->ptr[ptr->len++] = c;
00177 }
00178 
00179 /* Add null terminated string s to the end of sb at ptr.  */
00180 
00181 void
00182 sb_add_string (sb *ptr, const char *s)
00183 {
00184   int len = strlen (s);
00185   sb_check (ptr, len);
00186   memcpy (ptr->ptr + ptr->len, s, len);
00187   ptr->len += len;
00188 }
00189 
00190 /* Add string at s of length len to sb at ptr */
00191 
00192 void
00193 sb_add_buffer (sb *ptr, const char *s, int len)
00194 {
00195   sb_check (ptr, len);
00196   memcpy (ptr->ptr + ptr->len, s, len);
00197   ptr->len += len;
00198 }
00199 
00200 /* Like sb_name, but don't include the null byte in the string.  */
00201 
00202 char *
00203 sb_terminate (sb *in)
00204 {
00205   sb_add_char (in, 0);
00206   --in->len;
00207   return in->ptr;
00208 }
00209 
00210 /* Start at the index idx into the string in sb at ptr and skip
00211    whitespace. return the index of the first non whitespace character.  */
00212 
00213 int
00214 sb_skip_white (int idx, sb *ptr)
00215 {
00216   while (idx < ptr->len
00217         && (ptr->ptr[idx] == ' '
00218             || ptr->ptr[idx] == '\t'))
00219     idx++;
00220   return idx;
00221 }
00222 
00223 /* Start at the index idx into the sb at ptr. skips whitespace,
00224    a comma and any following whitespace. returns the index of the
00225    next character.  */
00226 
00227 int
00228 sb_skip_comma (int idx, sb *ptr)
00229 {
00230   while (idx < ptr->len
00231         && (ptr->ptr[idx] == ' '
00232             || ptr->ptr[idx] == '\t'))
00233     idx++;
00234 
00235   if (idx < ptr->len
00236       && ptr->ptr[idx] == ',')
00237     idx++;
00238 
00239   while (idx < ptr->len
00240         && (ptr->ptr[idx] == ' '
00241             || ptr->ptr[idx] == '\t'))
00242     idx++;
00243 
00244   return idx;
00245 }