summaryrefslogtreecommitdiff
path: root/ot_buf.c
blob: eb00edf446171ebd19c9dc57248d4abf1b2f1320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

/* System */
#include <stdlib.h>
#include <string.h>

/* Opentracker */
#include "ot_buf.h"

int buf_cat(ot_buf *buf, const void *src, size_t len) {
  char *data;

  if (!len)
    return 0;

  if (buf->size + len < buf->size)
    return -1;

  if (!(data = realloc(buf->data, buf->size + len)))
    return -1;

  buf->data = data;
  memcpy(data + buf->size, src, len);
  buf->size += len;

  return 0;
}

void buf_truncate(ot_buf *buf, size_t len) {
  if (len < buf->size)
    buf->size = len;
}

void buf_reset(ot_buf *buf) {
  free(buf->data);
  buf->data = NULL;
  buf->size = 0;
}