view help2html/buffer.c @ 13:43d8b194fb4e

Remove unused variables.
author David A. Holland
date Tue, 31 May 2022 00:59:00 -0400
parents 13d2b8934445
children
line wrap: on
line source

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "must.h"
#include "buffer.h"

static void buffer_provide(struct buffer *b, size_t needed) {
  if (needed <= b->allocsize) {
    return;
  }
  size_t newalloc = b->allocsize*2;
  if (newalloc < 16) {
    newalloc = 16;
  }
  while (newalloc < needed) {
    newalloc *= 2;
  }

  b->text = must_realloc(b->text, newalloc);
  b->allocsize = newalloc;
}

void buffer_clear(struct buffer *b) {
  b->len = 0;
  b->text[b->len] = 0;
}

void buffer_init(struct buffer *b) {
  b->allocsize = 16;
  b->text = must_malloc(b->allocsize);
  buffer_clear(b);
}

void buffer_cleanup(struct buffer *b) {
  free(b->text);
  b->text = NULL;
  b->len = 0;
  b->allocsize = 0;
}

void buffer_append(struct buffer *b, const char *t) {
  size_t tlen = strlen(t);
  buffer_provide(b, b->len + tlen + 1);
  strcpy(b->text + b->len, t);
  b->len += tlen;
}

void buffer_add(struct buffer *b, int ch) {
  buffer_provide(b, b->len + 2);
  b->text[b->len++] = ch;
  b->text[b->len] = 0;
}

void buffer_start(struct buffer *b, int ch) {
  buffer_clear(b);
  buffer_add(b, ch);
}