mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-16 02:30:52 +00:00
lib: add sbuf
Signed-off-by: Christian Franke <chris@opensourcerouting.org>
This commit is contained in:
parent
dacb17162a
commit
31bfa0624d
107
lib/sbuf.c
Normal file
107
lib/sbuf.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Simple string buffer
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Christian Franke
|
||||||
|
*
|
||||||
|
* This file is part of FRR.
|
||||||
|
*
|
||||||
|
* FRR is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2, or (at your option) any
|
||||||
|
* later version.
|
||||||
|
*
|
||||||
|
* FRR is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with FRR; see the file COPYING. If not, write to the Free
|
||||||
|
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
||||||
|
* 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
#include <zebra.h>
|
||||||
|
|
||||||
|
#include "sbuf.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
void sbuf_init(struct sbuf *dest, char *buf, size_t size)
|
||||||
|
{
|
||||||
|
dest->fixed = (size > 0);
|
||||||
|
if (dest->fixed) {
|
||||||
|
dest->buf = buf;
|
||||||
|
dest->size = size;
|
||||||
|
} else {
|
||||||
|
dest->buf = XMALLOC(MTYPE_TMP, 4096);
|
||||||
|
dest->size = 4096;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest->pos = 0;
|
||||||
|
dest->buf[0] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
void sbuf_reset(struct sbuf *dest)
|
||||||
|
{
|
||||||
|
dest->pos = 0;
|
||||||
|
dest->buf[0] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *sbuf_buf(struct sbuf *buf)
|
||||||
|
{
|
||||||
|
return buf->buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sbuf_free(struct sbuf *buf)
|
||||||
|
{
|
||||||
|
if (!buf->fixed)
|
||||||
|
XFREE(MTYPE_TMP, buf->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sbuf_push(struct sbuf *buf, int indent, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int written;
|
||||||
|
|
||||||
|
if (!buf->fixed) {
|
||||||
|
char dummy;
|
||||||
|
int written1, written2;
|
||||||
|
size_t new_size;
|
||||||
|
|
||||||
|
written1 = snprintf(&dummy, 0, "%*s", indent, "");
|
||||||
|
va_start(args, format);
|
||||||
|
written2 = vsnprintf(&dummy, 0, format, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
new_size = buf->size;
|
||||||
|
if (written1 >= 0 && written2 >= 0) {
|
||||||
|
while (buf->pos + written1 + written2 >= new_size)
|
||||||
|
new_size *= 2;
|
||||||
|
if (new_size > buf->size) {
|
||||||
|
buf->buf =
|
||||||
|
XREALLOC(MTYPE_TMP, buf->buf, new_size);
|
||||||
|
buf->size = new_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
written = snprintf(buf->buf + buf->pos, buf->size - buf->pos, "%*s",
|
||||||
|
indent, "");
|
||||||
|
|
||||||
|
if (written >= 0)
|
||||||
|
buf->pos += written;
|
||||||
|
if (buf->pos > buf->size)
|
||||||
|
buf->pos = buf->size;
|
||||||
|
|
||||||
|
va_start(args, format);
|
||||||
|
written = vsnprintf(buf->buf + buf->pos, buf->size - buf->pos, format,
|
||||||
|
args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
if (written >= 0)
|
||||||
|
buf->pos += written;
|
||||||
|
if (buf->pos > buf->size)
|
||||||
|
buf->pos = buf->size;
|
||||||
|
|
||||||
|
if (buf->pos == buf->size)
|
||||||
|
assert(!"Buffer filled up!");
|
||||||
|
}
|
77
lib/sbuf.h
Normal file
77
lib/sbuf.h
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Simple string buffer
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Christian Franke
|
||||||
|
*
|
||||||
|
* This file is part of FRR.
|
||||||
|
*
|
||||||
|
* FRR is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2, or (at your option) any
|
||||||
|
* later version.
|
||||||
|
*
|
||||||
|
* FRR is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with FRR; see the file COPYING. If not, write to the Free
|
||||||
|
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
||||||
|
* 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
#ifndef SBUF_H
|
||||||
|
#define SBUF_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* sbuf provides a simple string buffer. One application where this comes
|
||||||
|
* in handy is the parsing of binary data: If there is an error in the parsing
|
||||||
|
* process due to invalid input data, printing an error message explaining what
|
||||||
|
* went wrong is definitely useful. However, just printing the actual error,
|
||||||
|
* without any information about the previous parsing steps, is usually not very
|
||||||
|
* helpful.
|
||||||
|
* Using sbuf, the parser can log the whole parsing process into a buffer using
|
||||||
|
* a printf like API. When an error ocurrs, all the information about previous
|
||||||
|
* parsing steps is there in the log, without any need for backtracking, and can
|
||||||
|
* be used to give a detailed and useful error description.
|
||||||
|
* When parsing completes successfully without any error, the log can just be
|
||||||
|
* discarded unless debugging is turned on, to not spam the log.
|
||||||
|
*
|
||||||
|
* For the described usecase, the code would look something like this:
|
||||||
|
*
|
||||||
|
* int sbuf_example(..., char **parser_log)
|
||||||
|
* {
|
||||||
|
* struct sbuf logbuf;
|
||||||
|
*
|
||||||
|
* sbuf_init(&logbuf, NULL, 0);
|
||||||
|
* sbuf_push(&logbuf, 0, "Starting parser\n");
|
||||||
|
*
|
||||||
|
* int rv = do_parse(&logbuf, ...);
|
||||||
|
*
|
||||||
|
* *parser_log = sbuf_buf(&logbuf);
|
||||||
|
*
|
||||||
|
* return 1;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* In this case, sbuf_example uses a string buffer with undefined size, which will
|
||||||
|
* be allocated on the heap by sbuf. The caller of sbuf_example is expected to free
|
||||||
|
* the string returned in parser_log.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct sbuf {
|
||||||
|
bool fixed;
|
||||||
|
char *buf;
|
||||||
|
size_t size;
|
||||||
|
size_t pos;
|
||||||
|
int indent;
|
||||||
|
};
|
||||||
|
|
||||||
|
void sbuf_init(struct sbuf *dest, char *buf, size_t size);
|
||||||
|
void sbuf_reset(struct sbuf *buf);
|
||||||
|
const char *sbuf_buf(struct sbuf *buf);
|
||||||
|
void sbuf_free(struct sbuf *buf);
|
||||||
|
#include "lib/log.h"
|
||||||
|
void sbuf_push(struct sbuf *buf, int indent, const char *format, ...)
|
||||||
|
PRINTF_ATTRIBUTE(3, 4);
|
||||||
|
|
||||||
|
#endif
|
@ -51,6 +51,7 @@ lib_libfrr_la_SOURCES = \
|
|||||||
lib/ptm_lib.c \
|
lib/ptm_lib.c \
|
||||||
lib/qobj.c \
|
lib/qobj.c \
|
||||||
lib/routemap.c \
|
lib/routemap.c \
|
||||||
|
lib/sbuf.c \
|
||||||
lib/sha256.c \
|
lib/sha256.c \
|
||||||
lib/sigevent.c \
|
lib/sigevent.c \
|
||||||
lib/skiplist.c \
|
lib/skiplist.c \
|
||||||
@ -125,6 +126,7 @@ pkginclude_HEADERS += \
|
|||||||
lib/qobj.h \
|
lib/qobj.h \
|
||||||
lib/route_types.h \
|
lib/route_types.h \
|
||||||
lib/routemap.h \
|
lib/routemap.h \
|
||||||
|
lib/sbuf.h \
|
||||||
lib/sha256.h \
|
lib/sha256.h \
|
||||||
lib/sigevent.h \
|
lib/sigevent.h \
|
||||||
lib/skiplist.h \
|
lib/skiplist.h \
|
||||||
|
Loading…
Reference in New Issue
Block a user