diff options
author | John Keeping <john@keeping.me.uk> | 2013-04-07 22:40:50 (JST) |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2013-04-08 23:10:11 (JST) |
commit | fd00d2f9d6088223f57006949dc6ce7c36316a79 (patch) | |
tree | 46092821a261964a35b36f0e22b924fdd144bf75 /html.c | |
parent | 57d09bf448990b3a67436e928807e854e491756f (diff) | |
download | cgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.zip cgit-fd00d2f9d6088223f57006949dc6ce7c36316a79.tar.gz |
html.c: add various strbuf and varadic helpers
This adds the fmtalloc helper, html_txtf, html_vtxtf, and html_attrf.
These takes a printf style format string like htmlf but escapes the
resulting string. The html_vtxtf variant takes a va_list whereas
html_txtf is variadic.
Signed-off-by: John Keeping <john@keeping.me.uk>
Diffstat (limited to 'html.c')
-rw-r--r-- | html.c | 53 |
1 files changed, 50 insertions, 3 deletions
@@ -63,6 +63,18 @@ char *fmt(const char *format, ...) | |||
63 | return buf[bufidx]; | 63 | return buf[bufidx]; |
64 | } | 64 | } |
65 | 65 | ||
66 | char *fmtalloc(const char *format, ...) | ||
67 | { | ||
68 | struct strbuf sb = STRBUF_INIT; | ||
69 | va_list args; | ||
70 | |||
71 | va_start(args, format); | ||
72 | strbuf_vaddf(&sb, format, args); | ||
73 | va_end(args); | ||
74 | |||
75 | return strbuf_detach(&sb, NULL); | ||
76 | } | ||
77 | |||
66 | void html_raw(const char *data, size_t size) | 78 | void html_raw(const char *data, size_t size) |
67 | { | 79 | { |
68 | if (write(htmlfd, data, size) != size) | 80 | if (write(htmlfd, data, size) != size) |
@@ -76,13 +88,35 @@ void html(const char *txt) | |||
76 | 88 | ||
77 | void htmlf(const char *format, ...) | 89 | void htmlf(const char *format, ...) |
78 | { | 90 | { |
79 | static char buf[65536]; | 91 | va_list args; |
92 | struct strbuf buf = STRBUF_INIT; | ||
93 | |||
94 | va_start(args, format); | ||
95 | strbuf_vaddf(&buf, format, args); | ||
96 | va_end(args); | ||
97 | html(buf.buf); | ||
98 | strbuf_release(&buf); | ||
99 | } | ||
100 | |||
101 | void html_txtf(const char *format, ...) | ||
102 | { | ||
80 | va_list args; | 103 | va_list args; |
81 | 104 | ||
82 | va_start(args, format); | 105 | va_start(args, format); |
83 | vsnprintf(buf, sizeof(buf), format, args); | 106 | html_vtxtf(format, args); |
84 | va_end(args); | 107 | va_end(args); |
85 | html(buf); | 108 | } |
109 | |||
110 | void html_vtxtf(const char *format, va_list ap) | ||
111 | { | ||
112 | va_list cp; | ||
113 | struct strbuf buf = STRBUF_INIT; | ||
114 | |||
115 | va_copy(cp, ap); | ||
116 | strbuf_vaddf(&buf, format, cp); | ||
117 | va_end(cp); | ||
118 | html_txt(buf.buf); | ||
119 | strbuf_release(&buf); | ||
86 | } | 120 | } |
87 | 121 | ||
88 | void html_status(int code, const char *msg, int more_headers) | 122 | void html_status(int code, const char *msg, int more_headers) |
@@ -136,6 +170,19 @@ void html_ntxt(int len, const char *txt) | |||
136 | html("..."); | 170 | html("..."); |
137 | } | 171 | } |
138 | 172 | ||
173 | void html_attrf(const char *fmt, ...) | ||
174 | { | ||
175 | va_list ap; | ||
176 | struct strbuf sb = STRBUF_INIT; | ||
177 | |||
178 | va_start(ap, fmt); | ||
179 | strbuf_vaddf(&sb, fmt, ap); | ||
180 | va_end(ap); | ||
181 | |||
182 | html_attr(sb.buf); | ||
183 | strbuf_release(&sb); | ||
184 | } | ||
185 | |||
139 | void html_attr(const char *txt) | 186 | void html_attr(const char *txt) |
140 | { | 187 | { |
141 | const char *t = txt; | 188 | const char *t = txt; |