summaryrefslogtreecommitdiff
path: root/ot_fmt.c
blob: 9cab25f73de6a128df39aa0452e457ff47e7a29a (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
42
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

/* System */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

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

void fmt_append(char **cursor, const char *end, const char *format, ...) {
  va_list arguments;
  size_t  room;
  int     written;

  if (*cursor >= end) {
    *cursor = (char *)end;
    return;
  }

  room = (size_t)(end - *cursor);
  va_start(arguments, format);
  written = vsnprintf(*cursor, room, format, arguments);
  va_end(arguments);

  if (written < 0)
    return;
  *cursor = (size_t)written >= room ? (char *)end : *cursor + written;
}

void fmt_bytes(char **cursor, const char *end, const void *bytes, size_t length) {
  if (*cursor >= end || (size_t)(end - *cursor) < length) {
    *cursor = (char *)end;
    return;
  }

  memcpy(*cursor, bytes, length);
  *cursor += length;
}