Use mg_putchar_iobuf in mg_vprintf

This commit is contained in:
Sergey Lyubka 2022-06-29 17:42:55 +01:00
parent 017c7290d6
commit 73297c8e65
6 changed files with 36 additions and 16 deletions

View File

@ -109,16 +109,16 @@ riscv: DEFS += -DMG_ENABLE_FILE=0 -DMG_ENABLE_MIP=1 -DMG_ARCH=MG_ARCH_NEWLIB
riscv: mongoose.h $(SRCS)
$(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test
#vc98: VCFLAGS += -DMG_ENABLE_IPV6=1
vc98: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe
$(DOCKER) mdashnet/vc98 wine $@.exe
#vc2017: VCFLAGS += -DMG_ENABLE_IPV6=1
# vc2017: DEFS += -DMG_ENABLE_IPV6=1
vc2017: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc2017 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe
$(DOCKER) mdashnet/vc2017 wine64 $@.exe
# vc22: DEFS += -DMG_ENABLE_IPV6=$(IPV6)
vc22: Makefile mongoose.h $(SRCS)
$(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe
$(DOCKER) mdashnet/vc22 wine64 $@.exe

View File

@ -598,7 +598,6 @@ char *mg_mprintf(const char *fmt, ...) {
return s;
}
size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
size_t len = 0;
va_list ap;
@ -610,7 +609,19 @@ size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
static void mg_putchar_iobuf_static(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch;
if (io->len + 2 <= io->size) {
io->buf[io->len++] = (uint8_t) ch;
io->buf[io->len] = 0;
}
}
static void mg_putchar_iobuf(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len + 2 > io->size) mg_iobuf_resize(io, io->size + 64);
if (io->len + 2 <= io->size) {
io->buf[io->len++] = (uint8_t) ch;
io->buf[io->len] = 0;
}
}
// We don't use realloc() in mongoose, so resort to inefficient calloc
@ -3290,11 +3301,9 @@ struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url,
size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) {
char mem[256], *buf = mem;
size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap);
len = mg_send(c, buf, len);
if (buf != mem) free(buf);
return len;
size_t old = c->send.len;
mg_vrprintf(mg_putchar_iobuf, &c->send, fmt, &ap);
return c->send.len - old;
}
size_t mg_printf(struct mg_connection *c, const char *fmt, ...) {

View File

@ -728,6 +728,7 @@ size_t mg_dtoa(char *buf, size_t len, double d, int width);
typedef void (*mg_pc_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer
void mg_putchar_realloc(char ch, void *param); // Print to malloced str
void mg_putchar_iobuf(char ch, void *param); // Print to iobuf
size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);

View File

@ -47,7 +47,6 @@ char *mg_mprintf(const char *fmt, ...) {
return s;
}
size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
size_t len = 0;
va_list ap;
@ -59,7 +58,19 @@ size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
static void mg_putchar_iobuf_static(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch;
if (io->len + 2 <= io->size) {
io->buf[io->len++] = (uint8_t) ch;
io->buf[io->len] = 0;
}
}
static void mg_putchar_iobuf(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len + 2 > io->size) mg_iobuf_resize(io, io->size + 64);
if (io->len + 2 <= io->size) {
io->buf[io->len++] = (uint8_t) ch;
io->buf[io->len] = 0;
}
}
// We don't use realloc() in mongoose, so resort to inefficient calloc

View File

@ -6,11 +6,9 @@
#include "util.h"
size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list ap) {
char mem[256], *buf = mem;
size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap);
len = mg_send(c, buf, len);
if (buf != mem) free(buf);
return len;
size_t old = c->send.len;
mg_vrprintf(mg_putchar_iobuf, &c->send, fmt, &ap);
return c->send.len - old;
}
size_t mg_printf(struct mg_connection *c, const char *fmt, ...) {

View File

@ -44,6 +44,7 @@ size_t mg_dtoa(char *buf, size_t len, double d, int width);
typedef void (*mg_pc_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer
void mg_putchar_realloc(char ch, void *param); // Print to malloced str
void mg_putchar_iobuf(char ch, void *param); // Print to iobuf
size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);