(root)/
texinfo-7.1/
tp/
Texinfo/
XS/
parsetexi/
text.c
       1  /* Copyright 2014-2023 Free Software Foundation, Inc.
       2  
       3     This program is free software: you can redistribute it and/or modify
       4     it under the terms of the GNU General Public License as published by
       5     the Free Software Foundation, either version 3 of the License, or
       6     (at your option) any later version.
       7  
       8     This program is distributed in the hope that it will be useful,
       9     but WITHOUT ANY WARRANTY; without even the implied warranty of
      10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      11     GNU General Public License for more details.
      12  
      13     You should have received a copy of the GNU General Public License
      14     along with this program.  If not, see <http://www.gnu.org/licenses/>. */
      15  
      16  #include <config.h>
      17  #include <stdlib.h>
      18  #include <string.h>
      19  #include <stdio.h>
      20  #include <stdarg.h>
      21  
      22  #include "errors.h"
      23  #include "text.h"
      24  
      25  /* Make sure there are LEN free bytes. */
      26  void
      27  text_alloc (TEXT *t, size_t len)
      28  {
      29    if (t->end + len > t->space)
      30      {
      31        t->space = t->end + len;
      32        if (t->space < 10)
      33          t->space = 10;
      34        t->space *= 2;
      35        t->text = realloc (t->text, t->space);
      36        if (!t->text)
      37          fatal ("realloc failed");
      38      }
      39  }
      40  
      41  void
      42  text_printf (TEXT *t, char *format, ...)
      43  {
      44    va_list v;
      45    char *s;
      46  
      47    va_start (v, format);
      48    xvasprintf (&s, format, v);
      49    text_append (t, s);
      50    free (s);
      51    va_end (v);
      52  }
      53  
      54  void
      55  text_append_n (TEXT *t, char *s, size_t len)
      56  {
      57    text_alloc (t, len + 1);
      58    memcpy (t->text + t->end, s, len);
      59    t->end += len;
      60    t->text[t->end] = '\0';
      61  }
      62  
      63  void
      64  text_append (TEXT *t, char *s)
      65  {
      66    size_t len = strlen (s);
      67    text_append_n (t, s, len);
      68  }
      69  
      70  void
      71  text_reset (TEXT *t)
      72  {
      73    if (t->end > 0)
      74      {
      75        t->end = 0;
      76        t->text[0] = 0;
      77      }
      78  }
      79  
      80  void
      81  text_init (TEXT *t)
      82  {
      83    t->end = t->space = 0;
      84    t->text = 0;
      85  }