summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-05-05 23:55:06 +0200
committerwm4 <wm4@nowhere>2014-05-05 23:56:12 +0200
commita8b267540ecca0fa7d136078c7b84e2c6cd87e06 (patch)
treeea5ccfd368c105d0dd99ec21aa7db5da56932b89
parent7fb5df0d3e67230ec8f962c30ed36ea9b9c71ee7 (diff)
downloadmpv-a8b267540ecca0fa7d136078c7b84e2c6cd87e06.tar.bz2
mpv-a8b267540ecca0fa7d136078c7b84e2c6cd87e06.tar.xz
common: change mp_snprintf_append semantics
Make it more suitable for chaining. This means a function formatting a value to a string using a static buffer can work exactly like mp_snprintf_append itself. Also rename it to mp_snprintf_cat, because that's shorter.
-rw-r--r--common/common.c15
-rw-r--r--common/common.h2
-rw-r--r--player/command.c4
3 files changed, 14 insertions, 7 deletions
diff --git a/common/common.c b/common/common.c
index 6084176610..afab994d72 100644
--- a/common/common.c
+++ b/common/common.c
@@ -109,16 +109,23 @@ bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2)
// 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, ...)
+// string would have *appended* assuming a large enough buffer, will make sure
+// str is null-terminated, and will never write to str[size] or past.
+// Example:
+// int example(char *buf, size_t buf_size, double num, char *str) {
+// int n = 0;
+// n += mp_snprintf_cat(buf, size, "%f", num);
+// n += mp_snprintf_cat(buf, size, "%s", str);
+// return n; }
+// Note how this can be chained with functions similar in style.
+int mp_snprintf_cat(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);
+ r = vsnprintf(str + len, size - len, format, ap);
va_end(ap);
return r;
}
diff --git a/common/common.h b/common/common.h
index f7362152c2..564d1f3ea0 100644
--- a/common/common.h
+++ b/common/common.h
@@ -77,7 +77,7 @@ 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, ...)
+int mp_snprintf_cat(char *str, size_t size, const char *format, ...)
PRINTF_ATTRIBUTE(3, 4);
struct bstr;
diff --git a/player/command.c b/player/command.c
index 413181b530..8077e0369f 100644
--- a/player/command.c
+++ b/player/command.c
@@ -1561,8 +1561,8 @@ static int probe_deint_filters(struct MPContext *mpctx)
if (pref > 0 && pref <= 4) {
const char *types[] =
{"", "first-field", "bob", "temporal", "temporal-spatial"};
- mp_snprintf_append(filter, sizeof(filter), ":deint-mode=%s",
- types[pref]);
+ mp_snprintf_cat(filter, sizeof(filter), ":deint-mode=%s",
+ types[pref]);
}
probe_deint_filter(mpctx, filter);