From 73dd849484cee9c46c24582fc6537bc68c18fdce Mon Sep 17 00:00:00 2001 From: Xavi Del Campo Date: Thu, 19 Mar 2020 18:39:06 +0100 Subject: First commit --- dynstr.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 dynstr.c (limited to 'dynstr.c') diff --git a/dynstr.c b/dynstr.c new file mode 100644 index 0000000..35d47b1 --- /dev/null +++ b/dynstr.c @@ -0,0 +1,68 @@ +/* + Copyright 2020 Xavier Del Campo Romero + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "dynstr.h" +#include +#include +#include +#include +#include + +void dynstr_init(struct dynstr *const d) +{ + memset(d, 0, sizeof *d); +} + +int dynstr_append(struct dynstr *const d, const char *const format, ...) +{ + va_list ap; + + va_start(ap, format); + const size_t src_len = vsnprintf(NULL, 0, format, ap); + size_t new_len; + va_end(ap); + + if (!d->str) + { + new_len = src_len + 1; + d->str = calloc(src_len + 1, sizeof *d->str); + } + else + { + new_len = d->len + src_len + 1; + d->str = realloc(d->str, (d->len + src_len) * sizeof *d->str); + } + + if (d->str) + { + va_start(ap, format); + vsprintf(d->str + d->len, format, ap); + va_end(ap); + d->len += src_len; + } + else + { + return 1; + } + + return 0; +} + +void dynstr_free(struct dynstr *const d) +{ + free(d->str); + memset(d, 0, sizeof *d); +} -- cgit v1.2.3