OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [gas/] [sb.c] - Diff between revs 156 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 156 Rev 816
/* sb.c - string buffer manipulation routines
/* sb.c - string buffer manipulation routines
   Copyright 1994, 1995, 2000, 2003, 2006, 2007
   Copyright 1994, 1995, 2000, 2003, 2006, 2007
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
   Written by Steve and Judy Chamberlain of Cygnus Support,
   Written by Steve and Judy Chamberlain of Cygnus Support,
      sac@cygnus.com
      sac@cygnus.com
 
 
   This file is part of GAS, the GNU Assembler.
   This file is part of GAS, the GNU Assembler.
 
 
   GAS is free software; you can redistribute it and/or modify
   GAS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
   any later version.
 
 
   GAS is distributed in the hope that it will be useful,
   GAS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with GAS; see the file COPYING.  If not, write to the Free
   along with GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */
   02110-1301, USA.  */
 
 
#include "as.h"
#include "as.h"
#include "sb.h"
#include "sb.h"
 
 
/* These routines are about manipulating strings.
/* These routines are about manipulating strings.
 
 
   They are managed in things called `sb's which is an abbreviation
   They are managed in things called `sb's which is an abbreviation
   for string buffers.  An sb has to be created, things can be glued
   for string buffers.  An sb has to be created, things can be glued
   on to it, and at the end of it's life it should be freed.  The
   on to it, and at the end of it's life it should be freed.  The
   contents should never be pointed at whilst it is still growing,
   contents should never be pointed at whilst it is still growing,
   since it could be moved at any time
   since it could be moved at any time
 
 
   eg:
   eg:
   sb_new (&foo);
   sb_new (&foo);
   sb_grow... (&foo,...);
   sb_grow... (&foo,...);
   use foo->ptr[*];
   use foo->ptr[*];
   sb_kill (&foo);  */
   sb_kill (&foo);  */
 
 
static int dsize = 5;
static int dsize = 5;
static void sb_check (sb *, int);
static void sb_check (sb *, int);
 
 
/* Statistics of sb structures.  */
/* Statistics of sb structures.  */
static int string_count[sb_max_power_two];
static int string_count[sb_max_power_two];
 
 
/* Free list of sb structures.  */
/* Free list of sb structures.  */
static struct
static struct
{
{
  sb_element *size[sb_max_power_two];
  sb_element *size[sb_max_power_two];
} free_list;
} free_list;
 
 
/* Initializes an sb.  */
/* Initializes an sb.  */
 
 
static void
static void
sb_build (sb *ptr, int size)
sb_build (sb *ptr, int size)
{
{
  /* See if we can find one to allocate.  */
  /* See if we can find one to allocate.  */
  sb_element *e;
  sb_element *e;
 
 
  assert (size < sb_max_power_two);
  assert (size < sb_max_power_two);
 
 
  e = free_list.size[size];
  e = free_list.size[size];
  if (!e)
  if (!e)
    {
    {
      /* Nothing there, allocate one and stick into the free list.  */
      /* Nothing there, allocate one and stick into the free list.  */
      e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
      e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
      e->next = free_list.size[size];
      e->next = free_list.size[size];
      e->size = 1 << size;
      e->size = 1 << size;
      free_list.size[size] = e;
      free_list.size[size] = e;
      string_count[size]++;
      string_count[size]++;
    }
    }
 
 
  /* Remove from free list.  */
  /* Remove from free list.  */
  free_list.size[size] = e->next;
  free_list.size[size] = e->next;
 
 
  /* Copy into callers world.  */
  /* Copy into callers world.  */
  ptr->ptr = e->data;
  ptr->ptr = e->data;
  ptr->pot = size;
  ptr->pot = size;
  ptr->len = 0;
  ptr->len = 0;
  ptr->item = e;
  ptr->item = e;
}
}
 
 
void
void
sb_new (sb *ptr)
sb_new (sb *ptr)
{
{
  sb_build (ptr, dsize);
  sb_build (ptr, dsize);
}
}
 
 
/* Deallocate the sb at ptr.  */
/* Deallocate the sb at ptr.  */
 
 
void
void
sb_kill (sb *ptr)
sb_kill (sb *ptr)
{
{
  /* Return item to free list.  */
  /* Return item to free list.  */
  ptr->item->next = free_list.size[ptr->pot];
  ptr->item->next = free_list.size[ptr->pot];
  free_list.size[ptr->pot] = ptr->item;
  free_list.size[ptr->pot] = ptr->item;
}
}
 
 
/* Add the sb at s to the end of the sb at ptr.  */
/* Add the sb at s to the end of the sb at ptr.  */
 
 
void
void
sb_add_sb (sb *ptr, sb *s)
sb_add_sb (sb *ptr, sb *s)
{
{
  sb_check (ptr, s->len);
  sb_check (ptr, s->len);
  memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
  memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
  ptr->len += s->len;
  ptr->len += s->len;
}
}
 
 
/* Helper for sb_scrub_and_add_sb.  */
/* Helper for sb_scrub_and_add_sb.  */
 
 
static sb *sb_to_scrub;
static sb *sb_to_scrub;
static char *scrub_position;
static char *scrub_position;
static int
static int
scrub_from_sb (char *buf, int buflen)
scrub_from_sb (char *buf, int buflen)
{
{
  int copy;
  int copy;
  copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
  copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
  if (copy > buflen)
  if (copy > buflen)
    copy = buflen;
    copy = buflen;
  memcpy (buf, scrub_position, copy);
  memcpy (buf, scrub_position, copy);
  scrub_position += copy;
  scrub_position += copy;
  return copy;
  return copy;
}
}
 
 
/* Run the sb at s through do_scrub_chars and add the result to the sb
/* Run the sb at s through do_scrub_chars and add the result to the sb
   at ptr.  */
   at ptr.  */
 
 
void
void
sb_scrub_and_add_sb (sb *ptr, sb *s)
sb_scrub_and_add_sb (sb *ptr, sb *s)
{
{
  sb_to_scrub = s;
  sb_to_scrub = s;
  scrub_position = s->ptr;
  scrub_position = s->ptr;
 
 
  sb_check (ptr, s->len);
  sb_check (ptr, s->len);
  ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
  ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
 
 
  sb_to_scrub = 0;
  sb_to_scrub = 0;
  scrub_position = 0;
  scrub_position = 0;
}
}
 
 
/* Make sure that the sb at ptr has room for another len characters,
/* Make sure that the sb at ptr has room for another len characters,
   and grow it if it doesn't.  */
   and grow it if it doesn't.  */
 
 
static void
static void
sb_check (sb *ptr, int len)
sb_check (sb *ptr, int len)
{
{
  if (ptr->len + len >= 1 << ptr->pot)
  if (ptr->len + len >= 1 << ptr->pot)
    {
    {
      sb tmp;
      sb tmp;
      int pot = ptr->pot;
      int pot = ptr->pot;
 
 
      while (ptr->len + len >= 1 << pot)
      while (ptr->len + len >= 1 << pot)
        pot++;
        pot++;
      sb_build (&tmp, pot);
      sb_build (&tmp, pot);
      sb_add_sb (&tmp, ptr);
      sb_add_sb (&tmp, ptr);
      sb_kill (ptr);
      sb_kill (ptr);
      *ptr = tmp;
      *ptr = tmp;
    }
    }
}
}
 
 
/* Make the sb at ptr point back to the beginning.  */
/* Make the sb at ptr point back to the beginning.  */
 
 
void
void
sb_reset (sb *ptr)
sb_reset (sb *ptr)
{
{
  ptr->len = 0;
  ptr->len = 0;
}
}
 
 
/* Add character c to the end of the sb at ptr.  */
/* Add character c to the end of the sb at ptr.  */
 
 
void
void
sb_add_char (sb *ptr, int c)
sb_add_char (sb *ptr, int c)
{
{
  sb_check (ptr, 1);
  sb_check (ptr, 1);
  ptr->ptr[ptr->len++] = c;
  ptr->ptr[ptr->len++] = c;
}
}
 
 
/* Add null terminated string s to the end of sb at ptr.  */
/* Add null terminated string s to the end of sb at ptr.  */
 
 
void
void
sb_add_string (sb *ptr, const char *s)
sb_add_string (sb *ptr, const char *s)
{
{
  int len = strlen (s);
  int len = strlen (s);
  sb_check (ptr, len);
  sb_check (ptr, len);
  memcpy (ptr->ptr + ptr->len, s, len);
  memcpy (ptr->ptr + ptr->len, s, len);
  ptr->len += len;
  ptr->len += len;
}
}
 
 
/* Add string at s of length len to sb at ptr */
/* Add string at s of length len to sb at ptr */
 
 
void
void
sb_add_buffer (sb *ptr, const char *s, int len)
sb_add_buffer (sb *ptr, const char *s, int len)
{
{
  sb_check (ptr, len);
  sb_check (ptr, len);
  memcpy (ptr->ptr + ptr->len, s, len);
  memcpy (ptr->ptr + ptr->len, s, len);
  ptr->len += len;
  ptr->len += len;
}
}
 
 
/* Like sb_name, but don't include the null byte in the string.  */
/* Like sb_name, but don't include the null byte in the string.  */
 
 
char *
char *
sb_terminate (sb *in)
sb_terminate (sb *in)
{
{
  sb_add_char (in, 0);
  sb_add_char (in, 0);
  --in->len;
  --in->len;
  return in->ptr;
  return in->ptr;
}
}
 
 
/* Start at the index idx into the string in sb at ptr and skip
/* Start at the index idx into the string in sb at ptr and skip
   whitespace. return the index of the first non whitespace character.  */
   whitespace. return the index of the first non whitespace character.  */
 
 
int
int
sb_skip_white (int idx, sb *ptr)
sb_skip_white (int idx, sb *ptr)
{
{
  while (idx < ptr->len
  while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
         && (ptr->ptr[idx] == ' '
             || ptr->ptr[idx] == '\t'))
             || ptr->ptr[idx] == '\t'))
    idx++;
    idx++;
  return idx;
  return idx;
}
}
 
 
/* Start at the index idx into the sb at ptr. skips whitespace,
/* Start at the index idx into the sb at ptr. skips whitespace,
   a comma and any following whitespace. returns the index of the
   a comma and any following whitespace. returns the index of the
   next character.  */
   next character.  */
 
 
int
int
sb_skip_comma (int idx, sb *ptr)
sb_skip_comma (int idx, sb *ptr)
{
{
  while (idx < ptr->len
  while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
         && (ptr->ptr[idx] == ' '
             || ptr->ptr[idx] == '\t'))
             || ptr->ptr[idx] == '\t'))
    idx++;
    idx++;
 
 
  if (idx < ptr->len
  if (idx < ptr->len
      && ptr->ptr[idx] == ',')
      && ptr->ptr[idx] == ',')
    idx++;
    idx++;
 
 
  while (idx < ptr->len
  while (idx < ptr->len
         && (ptr->ptr[idx] == ' '
         && (ptr->ptr[idx] == ' '
             || ptr->ptr[idx] == '\t'))
             || ptr->ptr[idx] == '\t'))
    idx++;
    idx++;
 
 
  return idx;
  return idx;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.