From 8d1317d71c46e27c5073d3429a64af69de0351a6 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Tue, 6 Dec 2005 03:26:28 +0000 Subject: [PATCH] add evbuffer_add_vprintf interface from artur grabowski; add some testing svn:r188 --- buffer.c | 60 ++++++++++++++++++++++++++++---------------------- event.h | 5 ++++- test/regress.c | 53 +++++++++++++++++++++++++++++--------------- 3 files changed, 73 insertions(+), 45 deletions(-) diff --git a/buffer.c b/buffer.c index 10a6c602..66fb53bc 100644 --- a/buffer.c +++ b/buffer.c @@ -124,38 +124,46 @@ evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) } int -evbuffer_add_printf(struct evbuffer *buf, char *fmt, ...) +evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap) { - int res = -1; - char *msg; -#ifndef HAVE_VASPRINTF - static char buffer[4096]; -#endif - va_list ap; + char *buffer; + size_t space; + size_t oldoff = buf->off; + int sz; - va_start(ap, fmt); + for (;;) { + buffer = buf->buffer + buf->off; + space = buf->totallen - buf->misalign - buf->off; -#ifdef HAVE_VASPRINTF - if (vasprintf(&msg, fmt, ap) == -1) - goto end; +#ifdef WIN32 + sz = vsnprintf(buffer, space - 1, fmt, ap); + buffer[space - 1] = '\0'; #else -# ifdef WIN32 - _vsnprintf(buffer, sizeof(buffer) - 1, fmt, ap); - buffer[sizeof(buffer)-1] = '\0'; -# else /* ! WIN32 */ - vsnprintf(buffer, sizeof(buffer), fmt, ap); -# endif - msg = buffer; + sz = vsnprintf(buffer, space, fmt, ap); #endif - - res = strlen(msg); - if (evbuffer_add(buf, msg, res) == -1) - res = -1; -#ifdef HAVE_VASPRINTF - free(msg); + if (sz == -1) + return (-1); + if (sz < space) { + buf->off += sz; + if (buf->cb != NULL) + (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); + return (sz); + } + if (evbuffer_expand(buf, sz) == -1) + return (-1); -end: -#endif + } + /* NOTREACHED */ +} + +int +evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...) +{ + int res = -1; + va_list ap; + + va_start(ap, fmt); + res = evbuffer_add_vprintf(buf, fmt, ap); va_end(ap); return (res); diff --git a/event.h b/event.h index ed478ea5..55af00ee 100644 --- a/event.h +++ b/event.h @@ -31,6 +31,8 @@ extern "C" { #endif +#include + #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -263,7 +265,8 @@ int evbuffer_add(struct evbuffer *, void *, size_t); int evbuffer_remove(struct evbuffer *, void *, size_t); char *evbuffer_readline(struct evbuffer *); int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); -int evbuffer_add_printf(struct evbuffer *, char *fmt, ...); +int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...); +int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap); void evbuffer_drain(struct evbuffer *, size_t); int evbuffer_write(struct evbuffer *, int); int evbuffer_read(struct evbuffer *, int, int); diff --git a/test/regress.c b/test/regress.c index 519a7a02..d8e71ded 100644 --- a/test/regress.c +++ b/test/regress.c @@ -278,7 +278,7 @@ cleanup_test(void) } void -test1(void) +test_simpleread(void) { struct event ev; @@ -297,7 +297,7 @@ test1(void) } void -test2(void) +test_simplewrite(void) { struct event ev; @@ -313,7 +313,7 @@ test2(void) } void -test3(void) +test_multiple(void) { struct event ev, ev2; int i; @@ -342,7 +342,7 @@ test3(void) } void -test4(void) +test_persistent(void) { struct event ev, ev2; int i; @@ -371,7 +371,7 @@ test4(void) } void -test5(void) +test_combined(void) { struct both r1, r2, w1, w2; @@ -406,7 +406,7 @@ test5(void) } void -test6(void) +test_simpletimeout(void) { struct timeval tv; struct event ev; @@ -426,7 +426,7 @@ test6(void) #ifndef WIN32 void -test7(void) +test_simplesignal(void) { struct event ev; struct itimerval itv; @@ -449,7 +449,7 @@ test7(void) #endif void -test8(void) +test_loopexit(void) { struct timeval tv, tv_start, tv_end; struct event ev; @@ -478,6 +478,21 @@ test8(void) cleanup_test(); } +void +test_evbuffer(void) { + setup_test("Evbuffer: "); + + struct evbuffer *evb = evbuffer_new(); + + evbuffer_add_printf(evb, "%s/%d", "hello", 1); + + if (EVBUFFER_LENGTH(evb) == 7 && + strcmp(EVBUFFER_DATA(evb), "hello/1") == 0) + test_ok = 1; + + cleanup_test(); +} + void readcb(struct bufferevent *bev, void *arg) { @@ -501,7 +516,7 @@ errorcb(struct bufferevent *bev, short what, void *arg) } void -test9(void) +test_bufferevent(void) { struct bufferevent *bev1, *bev2; char buffer[8333]; @@ -814,23 +829,25 @@ main (int argc, char **argv) /* Initalize the event library */ event_base = event_init(); - test1(); + test_simpleread(); - test2(); + test_simplewrite(); - test3(); + test_multiple(); - test4(); + test_persistent(); - test5(); + test_combined(); - test6(); + test_simpletimeout(); #ifndef WIN32 - test7(); + test_simplesignal(); #endif - test8(); + test_loopexit(); - test9(); + test_evbuffer(); + + test_bufferevent(); test_priorities(1); test_priorities(2); -- 2.40.0