view help2html/buffer.c @ 8:ec2b657edf13

Add explicit lint-comment-style fallthrough annotations. GCC now assumes that if you don't have these you're making a mistake, which is annoying. XXX: This changeset updates the AG output files only (by hand) and is XXX: abusive - rebuilding them will erase the change. However, I need XXX: to get things to build before I can try to get AG to issue the XXX: annotations itself, so this seems like a reasonable expedient.
author David A. Holland
date Mon, 30 May 2022 23:51:43 -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);
}