diff options
Diffstat (limited to 'src/ot_fmt.c')
| -rw-r--r-- | src/ot_fmt.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/ot_fmt.c b/src/ot_fmt.c new file mode 100644 index 0000000..362c9b5 --- /dev/null +++ b/src/ot_fmt.c | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
| 3 | |||
| 4 | /* System */ | ||
| 5 | #include <stdarg.h> | ||
| 6 | #include <stdio.h> | ||
| 7 | #include <string.h> | ||
| 8 | |||
| 9 | /* Opentracker */ | ||
| 10 | #include "ot_fmt.h" | ||
| 11 | |||
| 12 | void fmt_append(char **cursor, const char *end, const char *format, ...) { | ||
| 13 | va_list arguments; | ||
| 14 | size_t room; | ||
| 15 | int written; | ||
| 16 | |||
| 17 | if (*cursor >= end) { | ||
| 18 | *cursor = (char *)end; | ||
| 19 | return; | ||
| 20 | } | ||
| 21 | |||
| 22 | room = (size_t)(end - *cursor); | ||
| 23 | va_start(arguments, format); | ||
| 24 | written = vsnprintf(*cursor, room, format, arguments); | ||
| 25 | va_end(arguments); | ||
| 26 | |||
| 27 | if (written < 0) | ||
| 28 | return; | ||
| 29 | *cursor = (size_t)written >= room ? (char *)end : *cursor + written; | ||
| 30 | } | ||
| 31 | |||
| 32 | void fmt_bytes(char **cursor, const char *end, const void *bytes, size_t length) { | ||
| 33 | if (*cursor >= end || (size_t)(end - *cursor) < length) { | ||
| 34 | *cursor = (char *)end; | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | memcpy(*cursor, bytes, length); | ||
| 39 | *cursor += length; | ||
| 40 | } | ||
