diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 22:32:52 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 22:32:52 +0100 |
| commit | 1f9494979268b62ed4b4588fb7d9e790c0e12100 (patch) | |
| tree | ed4f78369fe610b5c9188cb4159a23370d50a799 /ot_fmt.c | |
| parent | 6d5485b97cd46f81512e9198ac7c59c7ba1d9247 (diff) | |
replace all calls to sprintf with a format helper that advances a cursor. Also convert the meta-formatter (i.e. results printer) to this scheme
Diffstat (limited to 'ot_fmt.c')
| -rw-r--r-- | ot_fmt.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/ot_fmt.c b/ot_fmt.c new file mode 100644 index 0000000..9cab25f --- /dev/null +++ b/ot_fmt.c | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <stdarg.h> | ||
| 8 | #include <stdio.h> | ||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | /* Opentracker */ | ||
| 12 | #include "ot_fmt.h" | ||
| 13 | |||
| 14 | void fmt_append(char **cursor, const char *end, const char *format, ...) { | ||
| 15 | va_list arguments; | ||
| 16 | size_t room; | ||
| 17 | int written; | ||
| 18 | |||
| 19 | if (*cursor >= end) { | ||
| 20 | *cursor = (char *)end; | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | |||
| 24 | room = (size_t)(end - *cursor); | ||
| 25 | va_start(arguments, format); | ||
| 26 | written = vsnprintf(*cursor, room, format, arguments); | ||
| 27 | va_end(arguments); | ||
| 28 | |||
| 29 | if (written < 0) | ||
| 30 | return; | ||
| 31 | *cursor = (size_t)written >= room ? (char *)end : *cursor + written; | ||
| 32 | } | ||
| 33 | |||
| 34 | void fmt_bytes(char **cursor, const char *end, const void *bytes, size_t length) { | ||
| 35 | if (*cursor >= end || (size_t)(end - *cursor) < length) { | ||
| 36 | *cursor = (char *)end; | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | memcpy(*cursor, bytes, length); | ||
| 41 | *cursor += length; | ||
| 42 | } | ||
