From 4e39ca2e319e2baab49fc4342b168fc8acbad5a0 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 20 May 2012 20:53:34 -0300 Subject: [PATCH] b2/: move variable-length strings to vstring.[ch], for sharing --- b2/Makefile | 2 +- b2/subex.c | 28 +--------------------------- b2/vstring.c | 37 +++++++++++++++++++++++++++++++++++++ b2/vstring.h | 25 +++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 b2/vstring.c create mode 100644 b2/vstring.h diff --git a/b2/Makefile b/b2/Makefile index 351515f..beae1b9 100644 --- a/b2/Makefile +++ b/b2/Makefile @@ -14,7 +14,7 @@ SHELL = /bin/bash CFLAGS = -g -Wall $(shell pkg-config --cflags glib-2.0) SLOPPY = -Wno-unused -Wno-implicit-function-declaration OBJS = boom.o chr.o comp.o db.o dump.o eval.o param.o subex.o subst.o util.o \ - lex.yy.o y.tab.o + vstring.o lex.yy.o y.tab.o LDLIBS = -lfl $(shell pkg-config --libs glib-2.0) YACC = bison -y diff --git a/b2/subex.c b/b2/subex.c index 45a1556..97dd9dd 100644 --- a/b2/subex.c +++ b/b2/subex.c @@ -17,6 +17,7 @@ #include #include "util.h" +#include "vstring.h" #include "lang.h" #include "subst.h" #include "subex.h" @@ -31,33 +32,6 @@ static const char *fn = NULL, *f[FIELDS]; static struct subst jump_end; -static void append_n(char **res, int *res_len, const char *s, int len) -{ - if (!len) - return; - *res = realloc(*res, *res_len+len+1); - if (!*res) - abort(); - memcpy(*res+*res_len, s, len); - *res_len += len; - (*res)[*res_len] = 0; -} - - -static void append(char **res, int *res_len, const char *s) -{ - append_n(res, res_len, s, strlen(s)); -} - - -#if 0 -static void append_char(char **res, int *res_len, char c) -{ - append_n(res, res_len, &c, 1); -} -#endif - - /* * TODO: decide what to do with units */ diff --git a/b2/vstring.c b/b2/vstring.c new file mode 100644 index 0000000..71c27af --- /dev/null +++ b/b2/vstring.c @@ -0,0 +1,37 @@ +/* + * vstring.c - Variable-length strings + * + * Copyright 2012 by Werner Almesberger + * + * This program 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 of the License, or + * (at your option) any later version. + */ + + +#include +#include + +#include "vstring.h" + + +void append_n(char **res, int *res_len, const char *s, int len) +{ + if (!len) + return; + if (!*res) + *res_len = 0; + *res = realloc(*res, *res_len+len+1); + if (!*res) + abort(); + memcpy(*res+*res_len, s, len); + *res_len += len; + (*res)[*res_len] = 0; +} + + +void append(char **res, int *res_len, const char *s) +{ + append_n(res, res_len, s, strlen(s)); +} diff --git a/b2/vstring.h b/b2/vstring.h new file mode 100644 index 0000000..f2241d8 --- /dev/null +++ b/b2/vstring.h @@ -0,0 +1,25 @@ +/* + * vstring.h - Variable-length strings + * + * Copyright 2012 by Werner Almesberger + * + * This program 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 of the License, or + * (at your option) any later version. + */ + +#ifndef VSTRING_H +#define VSTRING_H + + +void append_n(char **res, int *res_len, const char *s, int len); +void append(char **res, int *res_len, const char *s); + + +static inline void append_char(char **res, int *res_len, char c) +{ + append_n(res, res_len, &c, 1); +} + +#endif /* !VSTRING_H */