libratbox: implement rb_linebuf_putprefix() which joins a format string to a static string
This commit is contained in:
parent
33085472a2
commit
5abeae60b8
2 changed files with 67 additions and 2 deletions
|
@ -73,6 +73,7 @@ void rb_linebuf_donebuf(buf_head_t *);
|
||||||
int rb_linebuf_parse(buf_head_t *, char *, int, int);
|
int rb_linebuf_parse(buf_head_t *, char *, int, int);
|
||||||
int rb_linebuf_get(buf_head_t *, char *, int, int, int);
|
int rb_linebuf_get(buf_head_t *, char *, int, int, int);
|
||||||
void rb_linebuf_putmsg(buf_head_t *, const char *, va_list *, const char *, ...);
|
void rb_linebuf_putmsg(buf_head_t *, const char *, va_list *, const char *, ...);
|
||||||
|
void rb_linebuf_putprefix(buf_head_t *, const char *, va_list *, const char *);
|
||||||
void rb_linebuf_put(buf_head_t *, const char *, ...);
|
void rb_linebuf_put(buf_head_t *, const char *, ...);
|
||||||
void rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer);
|
void rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer);
|
||||||
void rb_linebuf_attach(buf_head_t *, buf_head_t *);
|
void rb_linebuf_attach(buf_head_t *, buf_head_t *);
|
||||||
|
|
|
@ -494,8 +494,6 @@ rb_linebuf_attach(buf_head_t * bufhead, buf_head_t * new)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* rb_linebuf_putmsg
|
* rb_linebuf_putmsg
|
||||||
*
|
*
|
||||||
|
@ -568,6 +566,72 @@ rb_linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args,
|
||||||
bufhead->len += len;
|
bufhead->len += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* rb_linebuf_putprefix
|
||||||
|
*
|
||||||
|
* Similar to rb_linebuf_put, but designed for use by send.c.
|
||||||
|
*
|
||||||
|
* prefix is inserted first, then format/va_args is appended to the buffer.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
rb_linebuf_putprefix(buf_head_t * bufhead, const char *format, va_list * va_args,
|
||||||
|
const char *prefix)
|
||||||
|
{
|
||||||
|
buf_line_t *bufline;
|
||||||
|
int len = 0;
|
||||||
|
|
||||||
|
/* make sure the previous line is terminated */
|
||||||
|
#ifndef NDEBUG
|
||||||
|
if(bufhead->list.tail)
|
||||||
|
{
|
||||||
|
bufline = bufhead->list.tail->data;
|
||||||
|
lrb_assert(bufline->terminated);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
/* Create a new line */
|
||||||
|
bufline = rb_linebuf_new_line(bufhead);
|
||||||
|
|
||||||
|
if(prefix != NULL)
|
||||||
|
len = rb_strlcpy(bufline->buf, prefix, BUF_DATA_SIZE);
|
||||||
|
|
||||||
|
if(va_args != NULL)
|
||||||
|
{
|
||||||
|
len += vsnprintf((bufline->buf + len), (BUF_DATA_SIZE - len), format, *va_args);
|
||||||
|
}
|
||||||
|
|
||||||
|
bufline->terminated = 1;
|
||||||
|
|
||||||
|
/* Truncate the data if required */
|
||||||
|
if(rb_unlikely(len > 510))
|
||||||
|
{
|
||||||
|
len = 510;
|
||||||
|
bufline->buf[len++] = '\r';
|
||||||
|
bufline->buf[len++] = '\n';
|
||||||
|
}
|
||||||
|
else if(rb_unlikely(len == 0))
|
||||||
|
{
|
||||||
|
bufline->buf[len++] = '\r';
|
||||||
|
bufline->buf[len++] = '\n';
|
||||||
|
bufline->buf[len] = '\0';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Chop trailing CRLF's .. */
|
||||||
|
while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n')
|
||||||
|
|| (bufline->buf[len] == '\0'))
|
||||||
|
{
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
bufline->buf[++len] = '\r';
|
||||||
|
bufline->buf[++len] = '\n';
|
||||||
|
bufline->buf[++len] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
bufline->len = len;
|
||||||
|
bufhead->len += len;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer)
|
rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue