diff options
| author | Xavi Del Campo <xavi.dcr@tutanota.com> | 2020-03-19 18:39:06 +0100 |
|---|---|---|
| committer | Xavi Del Campo <xavi.dcr@tutanota.com> | 2020-03-19 18:40:31 +0100 |
| commit | 73dd849484cee9c46c24582fc6537bc68c18fdce (patch) | |
| tree | 1cf9873fd92713cc47c22f3d24b30b21c3326bd4 /dynstr.c | |
| download | dynstr-73dd849484cee9c46c24582fc6537bc68c18fdce.tar.gz | |
First commit
Diffstat (limited to 'dynstr.c')
| -rw-r--r-- | dynstr.c | 68 |
1 files changed, 68 insertions, 0 deletions
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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> +#include <stddef.h> + +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); +} |
