Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. #ifdef HAVE_LIMITS_H
  29. #include <limits.h>
  30. #endif
  31. #ifndef CHAR_BIT
  32. #define CHAR_BIT 8
  33. #endif
  34.  
  35. /* These routines are about manipulating strings.
  36.  
  37.    They are managed in things called `sb's which is an abbreviation
  38.    for string buffers.  An sb has to be created, things can be glued
  39.    on to it, and at the end of it's life it should be freed.  The
  40.    contents should never be pointed at whilst it is still growing,
  41.    since it could be moved at any time
  42.  
  43.    eg:
  44.    sb_new (&foo);
  45.    sb_grow... (&foo,...);
  46.    use foo->ptr[*];
  47.    sb_kill (&foo);  */
  48.  
  49. /* Buffers start at INIT_ALLOC size, and roughly double each time we
  50.    go over the current allocation.  MALLOC_OVERHEAD is a guess at the
  51.    system malloc overhead.  We aim to not waste any memory in the
  52.    underlying page/chunk allocated by the system malloc.  */
  53. #define MALLOC_OVERHEAD (2 * sizeof (size_t))
  54. #define INIT_ALLOC (64 - MALLOC_OVERHEAD - 1)
  55.  
  56. static void sb_check (sb *, size_t);
  57.  
  58. /* Initializes an sb.  */
  59.  
  60. void
  61. sb_build (sb *ptr, size_t size)
  62. {
  63.   ptr->ptr = xmalloc (size + 1);
  64.   ptr->max = size;
  65.   ptr->len = 0;
  66. }
  67.  
  68. void
  69. sb_new (sb *ptr)
  70. {
  71.   sb_build (ptr, INIT_ALLOC);
  72. }
  73.  
  74. /* Deallocate the sb at ptr.  */
  75.  
  76. void
  77. sb_kill (sb *ptr)
  78. {
  79.   free (ptr->ptr);
  80. }
  81.  
  82. /* Add the sb at s to the end of the sb at ptr.  */
  83.  
  84. void
  85. sb_add_sb (sb *ptr, sb *s)
  86. {
  87.   sb_check (ptr, s->len);
  88.   memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
  89.   ptr->len += s->len;
  90. }
  91.  
  92. /* Helper for sb_scrub_and_add_sb.  */
  93.  
  94. static sb *sb_to_scrub;
  95. static char *scrub_position;
  96. static size_t
  97. scrub_from_sb (char *buf, size_t buflen)
  98. {
  99.   size_t copy;
  100.   copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
  101.   if (copy > buflen)
  102.     copy = buflen;
  103.   memcpy (buf, scrub_position, copy);
  104.   scrub_position += copy;
  105.   return copy;
  106. }
  107.  
  108. /* Run the sb at s through do_scrub_chars and add the result to the sb
  109.    at ptr.  */
  110.  
  111. void
  112. sb_scrub_and_add_sb (sb *ptr, sb *s)
  113. {
  114.   sb_to_scrub = s;
  115.   scrub_position = s->ptr;
  116.  
  117.   sb_check (ptr, s->len);
  118.   ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
  119.  
  120.   sb_to_scrub = 0;
  121.   scrub_position = 0;
  122. }
  123.  
  124. /* Make sure that the sb at ptr has room for another len characters,
  125.    and grow it if it doesn't.  */
  126.  
  127. static void
  128. sb_check (sb *ptr, size_t len)
  129. {
  130.   size_t want = ptr->len + len;
  131.  
  132.   if (want > ptr->max)
  133.     {
  134.       size_t max;
  135.  
  136.       want += MALLOC_OVERHEAD + 1;
  137.       if ((ssize_t) want < 0)
  138.         as_fatal ("string buffer overflow");
  139. #if GCC_VERSION >= 3004
  140.       max = (size_t) 1 << (CHAR_BIT * sizeof (want)
  141.                            - (sizeof (want) <= sizeof (long)
  142.                               ? __builtin_clzl ((long) want)
  143.                               : __builtin_clzll ((long long) want)));
  144. #else
  145.       max = 128;
  146.       while (want > max)
  147.         max <<= 1;
  148. #endif
  149.       max -= MALLOC_OVERHEAD + 1;
  150.       ptr->max = max;
  151.       ptr->ptr = xrealloc (ptr->ptr, max + 1);
  152.     }
  153. }
  154.  
  155. /* Make the sb at ptr point back to the beginning.  */
  156.  
  157. void
  158. sb_reset (sb *ptr)
  159. {
  160.   ptr->len = 0;
  161. }
  162.  
  163. /* Add character c to the end of the sb at ptr.  */
  164.  
  165. void
  166. sb_add_char (sb *ptr, size_t c)
  167. {
  168.   sb_check (ptr, 1);
  169.   ptr->ptr[ptr->len++] = c;
  170. }
  171.  
  172. /* Add null terminated string s to the end of sb at ptr.  */
  173.  
  174. void
  175. sb_add_string (sb *ptr, const char *s)
  176. {
  177.   size_t len = strlen (s);
  178.   sb_check (ptr, len);
  179.   memcpy (ptr->ptr + ptr->len, s, len);
  180.   ptr->len += len;
  181. }
  182.  
  183. /* Add string at s of length len to sb at ptr */
  184.  
  185. void
  186. sb_add_buffer (sb *ptr, const char *s, size_t len)
  187. {
  188.   sb_check (ptr, len);
  189.   memcpy (ptr->ptr + ptr->len, s, len);
  190.   ptr->len += len;
  191. }
  192.  
  193. /* Write terminating NUL and return string.  */
  194.  
  195. char *
  196. sb_terminate (sb *in)
  197. {
  198.   in->ptr[in->len] = 0;
  199.   return in->ptr;
  200. }
  201.  
  202. /* Start at the index idx into the string in sb at ptr and skip
  203.    whitespace. return the index of the first non whitespace character.  */
  204.  
  205. size_t
  206. sb_skip_white (size_t idx, sb *ptr)
  207. {
  208.   while (idx < ptr->len
  209.          && (ptr->ptr[idx] == ' '
  210.              || ptr->ptr[idx] == '\t'))
  211.     idx++;
  212.   return idx;
  213. }
  214.  
  215. /* Start at the index idx into the sb at ptr. skips whitespace,
  216.    a comma and any following whitespace. returns the index of the
  217.    next character.  */
  218.  
  219. size_t
  220. sb_skip_comma (size_t idx, sb *ptr)
  221. {
  222.   while (idx < ptr->len
  223.          && (ptr->ptr[idx] == ' '
  224.              || ptr->ptr[idx] == '\t'))
  225.     idx++;
  226.  
  227.   if (idx < ptr->len
  228.       && ptr->ptr[idx] == ',')
  229.     idx++;
  230.  
  231.   while (idx < ptr->len
  232.          && (ptr->ptr[idx] == ' '
  233.              || ptr->ptr[idx] == '\t'))
  234.     idx++;
  235.  
  236.   return idx;
  237. }
  238.