[PATCH i18n 01/11] Add three convenient format printing functions with \n automatically appended
From: Nguyễn Thái Ngọc Duy <hidden>
Date: 2016-06-15 22:53:36
Subsystem:
the rest · Maintainer:
Linus Torvalds
These functions are helpful when we do not want to expose \n to
translators. For example
printf("hello world\n");
can be converted to
printf_ln(_("hello world"));
---
strbuf.c | 33 +++++++++++++++++++++++++++++++++
strbuf.h | 7 +++++++
2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 5135d59..9fbc75c 100644
--- a/strbuf.c
+++ b/strbuf.c@@ -464,3 +464,36 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s, { strbuf_add_urlencode(sb, s, strlen(s), reserved); } + +void strbuf_addf_ln(struct strbuf *sb, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + strbuf_vaddf(sb, fmt, ap); + va_end(ap); + strbuf_addch(sb, '\n'); +} + +int printf_ln(const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vprintf(fmt, ap); + va_end(ap); + if (ret >= 0) + ret += printf("\n"); + return ret; +} + +int fprintf_ln(FILE *fp, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = vfprintf(fp, fmt, ap); + va_end(ap); + if (ret >= 0) + ret += fprintf(fp, "\n"); + return ret; +}
diff --git a/strbuf.h b/strbuf.h
index 3effaa8..b888d40 100644
--- a/strbuf.h
+++ b/strbuf.h@@ -99,6 +99,8 @@ __attribute__((format (printf,2,3))) extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...); __attribute__((format (printf,2,0))) extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap); +__attribute__((format (printf,2,3))) +extern void strbuf_addf_ln(struct strbuf *sb, const char *fmt, ...); extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
@@ -129,4 +131,9 @@ extern void strbuf_add_urlencode(struct strbuf *, const char *, size_t, extern void strbuf_addstr_urlencode(struct strbuf *, const char *, int reserved); +__attribute__((format (printf,1,2))) +extern int printf_ln(const char *fmt, ...); +__attribute__((format (printf,2,3))) +extern int fprintf_ln(FILE *fp, const char *fmt, ...); + #endif /* STRBUF_H */
--
1.7.3.1.256.g2539c.dirty