summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/common.c19
-rw-r--r--common/common.h4
2 files changed, 23 insertions, 0 deletions
diff --git a/common/common.c b/common/common.c
index e9cd70cf41..6084176610 100644
--- a/common/common.c
+++ b/common/common.c
@@ -16,6 +16,9 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <stdarg.h>
+#include <assert.h>
+
#include <libavutil/common.h>
#include "talloc.h"
@@ -104,6 +107,22 @@ bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2)
return rc->x1 > rc->x0 && rc->y1 > rc->y0;
}
+// This works like snprintf(), except that it starts writing the first output
+// character to str[strlen(str)]. This returns the number of characters the
+// string would have assuming a large enough buffer, will make sure str is
+// null-terminated, and will never write to str[size] or past.
+int mp_snprintf_append(char *str, size_t size, const char *format, ...)
+{
+ size_t len = strnlen(str, size);
+ assert(!size || len < size); // str with no 0-termination is not allowed
+ int r;
+ va_list ap;
+ va_start(ap, format);
+ r = len + vsnprintf(str + len, size - len, format, ap);
+ va_end(ap);
+ return r;
+}
+
// Encode the unicode codepoint as UTF-8, and append to the end of the
// talloc'ed buffer. All guarantees bstr_xappend() give applies, such as
// implicit \0-termination for convenience.
diff --git a/common/common.h b/common/common.h
index aad1460aa2..f7362152c2 100644
--- a/common/common.h
+++ b/common/common.h
@@ -19,6 +19,7 @@
#ifndef MPLAYER_MPCOMMON_H
#define MPLAYER_MPCOMMON_H
+#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
@@ -76,6 +77,9 @@ struct mp_rect {
void mp_rect_union(struct mp_rect *rc, const struct mp_rect *src);
bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2);
+int mp_snprintf_append(char *str, size_t size, const char *format, ...)
+ PRINTF_ATTRIBUTE(3, 4);
+
struct bstr;
void mp_append_utf8_bstr(void *talloc_ctx, struct bstr *buf, uint32_t codepoint);