}
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);
extern "C" {
#endif
+#include <stdarg.h>
+
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
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);
}
void
-test1(void)
+test_simpleread(void)
{
struct event ev;
}
void
-test2(void)
+test_simplewrite(void)
{
struct event ev;
}
void
-test3(void)
+test_multiple(void)
{
struct event ev, ev2;
int i;
}
void
-test4(void)
+test_persistent(void)
{
struct event ev, ev2;
int i;
}
void
-test5(void)
+test_combined(void)
{
struct both r1, r2, w1, w2;
}
void
-test6(void)
+test_simpletimeout(void)
{
struct timeval tv;
struct event ev;
#ifndef WIN32
void
-test7(void)
+test_simplesignal(void)
{
struct event ev;
struct itimerval itv;
#endif
void
-test8(void)
+test_loopexit(void)
{
struct timeval tv, tv_start, tv_end;
struct event ev;
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)
{
}
void
-test9(void)
+test_bufferevent(void)
{
struct bufferevent *bev1, *bev2;
char buffer[8333];
/* 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);