Mercurial > libbuf
changeset 23:b7690784cd17 0.3.0
misc: update before 0.3.0
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 16 Sep 2021 13:56:31 +0200 |
parents | d87e84936795 |
children | dddf94a30fa1 |
files | CHANGES.md INSTALL.md Makefile README.md buf-clear.c buf-dup.c buf-erase.c buf-finish.c buf-init.c buf-int.h buf-printf.c buf-putc.c buf-puts.c buf-reserve.c buf-resize.c buf-shrink.c buf-sub.c buf-vprintf.c buf.c buf.h |
diffstat | 20 files changed, 251 insertions(+), 581 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES.md Wed Feb 24 11:43:50 2021 +0100 +++ b/CHANGES.md Thu Sep 16 13:56:31 2021 +0200 @@ -1,11 +1,12 @@ libbuf CHANGES ============== -libbuf 0.3.0 2021-02-24 +libbuf 0.3.0 2021-09-16 ----------------------- - Switch to `int` rather than bool (and return -1 in case of failures) as they are less optimized. +- Finally merge all C files into buf.c for convenience. libbuf 0.2.0 2020-12-16 -----------------------
--- a/INSTALL.md Wed Feb 24 11:43:50 2021 +0100 +++ b/INSTALL.md Thu Sep 16 13:56:31 2021 +0200 @@ -13,13 +13,17 @@ Basic installation ------------------ -Quick install. +### Embedded - $ tar xvzf libbuf-x.y.z-tar.xz - $ cd libbuf-x.y.z +Copy buf.c and buf.h files into your projects. + +### System wide (not recommended) + $ make # sudo make install +### Other targets + Alternatively, you can use the following targets as well. - `make test`: run test suite.
--- a/Makefile Wed Feb 24 11:43:50 2021 +0100 +++ b/Makefile Thu Sep 16 13:56:31 2021 +0200 @@ -30,22 +30,9 @@ LIBDIR= ${PREFIX}/lib MANDIR= ${PREFIX}/share/man -VERSION= 0.2.0 +VERSION= 0.3.0 -SRCS= buf-clear.c \ - buf-dup.c \ - buf-erase.c \ - buf-finish.c \ - buf-init.c \ - buf-printf.c \ - buf-putc.c \ - buf-puts.c \ - buf-reserve.c \ - buf-resize.c \ - buf-shrink.c \ - buf-sub.c \ - buf-vprintf.c \ - buf.c +SRCS= buf.c OBJS= ${SRCS:.c=.o} MAN= buf_clear.3 \ @@ -93,7 +80,7 @@ rm -rf libbuf-${VERSION} mkdir libbuf-${VERSION} cp -R test extern libbuf-${VERSION} - cp ${SRCS} buf.h buf-int.h libbuf-${VERSION} + cp ${SRCS} buf.h libbuf-${VERSION} cp ${MAN} libbuf-${VERSION} cp INSTALL.md LICENSE.md README.md Makefile libbuf-${VERSION} tar -cjf libbuf-${VERSION}.tar.xz libbuf-${VERSION}
--- a/README.md Wed Feb 24 11:43:50 2021 +0100 +++ b/README.md Thu Sep 16 13:56:31 2021 +0200 @@ -47,11 +47,6 @@ process. This library can be bundled in your project by just copying all .c files and .h files without any modification. -### Why every function is defined in its own file? - -Because it improves static linking as only symbols you need are bundled into -your final executable. - ### Why not using `open_memstream`? It's a POSIX function that is not available on every operating system, but if
--- a/buf-clear.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -/* - * buf-clear.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> - -#include "buf.h" - -void -buf_clear(struct buf *b) -{ - assert(b); - - if (b->data) - b->data[b->length = 0] = 0; -}
--- a/buf-dup.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -/* - * buf-dup.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <stdlib.h> -#include <string.h> - -#include "buf.h" - -int -buf_dup(struct buf *b, const struct buf *src) -{ - assert(b); - assert(src); - - if (!src->data) - return 0; - if (!(b->data = BUF_MALLOC(src->length + 1))) - return -1; - - strcpy(b->data, src->data); - b->capacity = src->length; - b->length = src->length; - - return 0; -}
--- a/buf-erase.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -/* - * buf-erase.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <string.h> - -#include "buf.h" - -void -buf_erase(struct buf *b, size_t pos, size_t count) -{ - assert(b); - assert(pos <= b->length); - - if (count > b->length - pos) { - /* Optimize whole erase at pos. */ - b->data[pos] = 0; - b->length = pos; - } else { - memmove(&b->data[pos], &b->data[pos + count], b->length - count); - b->length -= count; - } -}
--- a/buf-finish.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* - * buf-finish.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <stdlib.h> - -#include "buf.h" - -void -buf_finish(struct buf *b) -{ - assert(b); - - BUF_FREE(b->data); - b->data = NULL; - b->capacity = b->length = 0; -}
--- a/buf-init.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -/* - * buf-init.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <string.h> - -#include "buf.h" - -void -buf_init(struct buf *b) -{ - assert(b); - - memset(b, 0, sizeof (*b)); -}
--- a/buf-int.h Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -/* - * buf-int.h -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stddef.h> - -struct buf; - -int -_buf_growdbl(struct buf *, size_t); - -int -_buf_growmin(struct buf *, size_t); - -int -_buf_grow(struct buf *, size_t);
--- a/buf-printf.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* - * buf-printf.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> - -#include "buf.h" - -int -buf_printf(struct buf *b, const char *fmt, ...) -{ - assert(b); - assert(fmt); - - va_list ap; - int ret; - - va_start(ap, fmt); - ret = buf_vprintf(b, fmt, ap); - va_end(ap); - - return ret; -}
--- a/buf-putc.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/* - * buf-putc.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> - -#include "buf.h" -#include "buf-int.h" - -int -buf_putc(struct buf *b, char c) -{ - assert(b); - - if (_buf_grow(b, 1) < 0) - return -1; - - b->data[b->length++] = c; - b->data[b->length] = 0; - - return 0; -}
--- a/buf-puts.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - * buf-puts.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <string.h> - -#include "buf.h" -#include "buf-int.h" - -int -buf_puts(struct buf *b, const char *s) -{ - assert(b); - assert(s); - - const size_t len = strlen(s); - - if (_buf_grow(b, len) < 0) - return -1; - - memcpy(&b->data[b->length], s, len + 1); - b->length += len; - - return 0; -}
--- a/buf-reserve.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* - * buf-reserve.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> - -#include "buf.h" -#include "buf-int.h" - -int -buf_reserve(struct buf *b, size_t amount) -{ - assert(b); - - if (_buf_grow(b, amount) < 0) - return -1; - - return 0; -}
--- a/buf-resize.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* - * buf-resize.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <string.h> - -#include "buf.h" -#include "buf-int.h" - -int -buf_resize(struct buf *b, size_t size, char ch) -{ - assert(b); - - /* New size is smaller than curren't length, just update it. */ - if (size < b->length) { - b->data[b->length = size] = 0; - return 0; - } - - /* New size is bigger, data may be reallocated. */ - if (_buf_grow(b, size - b->length) < 0) - return -1; - - memset(&b->data[b->length], ch, size - b->length); - b->length = size; - b->data[b->length] = 0; - - return 0; -}
--- a/buf-shrink.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* - * buf-shrink.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <stdlib.h> - -#include "buf.h" - -int -buf_shrink(struct buf *b) -{ - assert(b); - - void *newptr; - - if (b->length == 0) { - free(b->data); - b->data = NULL; - b->length = b->capacity = 0; - return 0; - } - - if (!(newptr = BUF_REALLOC(b->data, b->length + 1))) - return -1; - - b->data = newptr; - b->capacity = b->length; - - return 0; -}
--- a/buf-sub.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -/* - * buf-sub.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <stdlib.h> -#include <string.h> - -#include "buf.h" - -int -buf_sub(struct buf *b, const struct buf *src, size_t pos, size_t count) -{ - assert(b); - assert(src); - assert(pos <= src->length); - - if (count >= src->length) - count = src->length - pos; - if (!(b->data = BUF_MALLOC(count + 1))) - return -1; - - strncpy(b->data, &src->data[pos], count); - b->length = count; - b->capacity = count; - b->data[b->length] = 0; - - return 0; -}
--- a/buf-vprintf.c Wed Feb 24 11:43:50 2021 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * buf-vprintf.c -- simple string buffer for C - * - * Copyright (c) 2019-2021 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <assert.h> -#include <stdio.h> - -#include "buf.h" -#include "buf-int.h" - -int -buf_vprintf(struct buf *b, const char *fmt, va_list args) -{ - assert(b); - assert(fmt); - - va_list ap; - int amount; - - /* Determine length. */ - va_copy(ap, args); - amount = vsnprintf(NULL, 0, fmt, ap); - va_end(ap); - - if (amount < 0) - return -1; - - /* Do actual copy. */ - if (_buf_grow(b, amount) < 0) - return -1; - - va_copy(ap, args); - amount = vsprintf(&b->data[b->length], fmt, ap); - va_end(ap); - - if (amount < 0) - return -1; - - b->length += amount; - - return 0; -}
--- a/buf.c Wed Feb 24 11:43:50 2021 +0100 +++ b/buf.c Thu Sep 16 13:56:31 2021 +0200 @@ -20,6 +20,8 @@ #include <errno.h> #include <stdint.h> #include <stdlib.h> +#include <stdio.h> +#include <string.h> #include "buf.h" @@ -30,8 +32,8 @@ * Detects overflow and return -1 if happened or reallocation could not * occur. */ -int -_buf_growdbl(struct buf *b, size_t desired) +static int +growdbl(struct buf *b, size_t desired) { size_t newcap = b->capacity; void *newptr; @@ -77,8 +79,8 @@ * * Returns -1 if allocation failed. */ -int -_buf_growmin(struct buf *b, size_t desired) +static int +growmin(struct buf *b, size_t desired) { size_t newcap; void *newptr; @@ -107,8 +109,8 @@ * Entry point for reallocating data. Will try to allocate twice until we have * enough room and then only the minimal amount. */ -int -_buf_grow(struct buf *b, size_t desired) +static int +grow(struct buf *b, size_t desired) { const size_t avail = b->capacity - b->length; @@ -120,8 +122,225 @@ return -1; b->capacity = desired; - } else if (_buf_growdbl(b, desired) < 0 && _buf_growmin(b, desired) < 0) + } else if (growdbl(b, desired) < 0 && growmin(b, desired) < 0) + return -1; + + return 0; +} + +void +buf_init(struct buf *b) +{ + assert(b); + + memset(b, 0, sizeof (*b)); +} + +int +buf_reserve(struct buf *b, size_t amount) +{ + assert(b); + + if (grow(b, amount) < 0) return -1; return 0; } + +int +buf_resize(struct buf *b, size_t size, char ch) +{ + assert(b); + + /* New size is smaller than curren't length, just update it. */ + if (size < b->length) { + b->data[b->length = size] = 0; + return 0; + } + + /* New size is bigger, data may be reallocated. */ + if (grow(b, size - b->length) < 0) + return -1; + + memset(&b->data[b->length], ch, size - b->length); + b->length = size; + b->data[b->length] = 0; + + return 0; +} + +int +buf_shrink(struct buf *b) +{ + assert(b); + + void *newptr; + + if (b->length == 0) { + free(b->data); + b->data = NULL; + b->length = b->capacity = 0; + return 0; + } + + if (!(newptr = BUF_REALLOC(b->data, b->length + 1))) + return -1; + + b->data = newptr; + b->capacity = b->length; + + return 0; +} + +void +buf_erase(struct buf *b, size_t pos, size_t count) +{ + assert(b); + assert(pos <= b->length); + + if (count > b->length - pos) { + /* Optimize whole erase at pos. */ + b->data[pos] = 0; + b->length = pos; + } else { + memmove(&b->data[pos], &b->data[pos + count], b->length - count); + b->length -= count; + } +} + +int +buf_putc(struct buf *b, char c) +{ + assert(b); + + if (grow(b, 1) < 0) + return -1; + + b->data[b->length++] = c; + b->data[b->length] = 0; + + return 0; +} + +int +buf_puts(struct buf *b, const char *s) +{ + assert(b); + assert(s); + + const size_t len = strlen(s); + + if (grow(b, len) < 0) + return -1; + + memcpy(&b->data[b->length], s, len + 1); + b->length += len; + + return 0; +} + +int +buf_printf(struct buf *b, const char *fmt, ...) +{ + assert(b); + assert(fmt); + + va_list ap; + int ret; + + va_start(ap, fmt); + ret = buf_vprintf(b, fmt, ap); + va_end(ap); + + return ret; +} + +int +buf_vprintf(struct buf *b, const char *fmt, va_list args) +{ + assert(b); + assert(fmt); + + va_list ap; + int amount; + + /* Determine length. */ + va_copy(ap, args); + amount = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + if (amount < 0) + return -1; + + /* Do actual copy. */ + if (grow(b, amount) < 0) + return -1; + + va_copy(ap, args); + amount = vsprintf(&b->data[b->length], fmt, ap); + va_end(ap); + + if (amount < 0) + return -1; + + b->length += amount; + + return 0; +} + +int +buf_sub(struct buf *b, const struct buf *src, size_t pos, size_t count) +{ + assert(b); + assert(src); + assert(pos <= src->length); + + if (count >= src->length) + count = src->length - pos; + if (!(b->data = BUF_MALLOC(count + 1))) + return -1; + + strncpy(b->data, &src->data[pos], count); + b->length = count; + b->capacity = count; + b->data[b->length] = 0; + + return 0; +} + +int +buf_dup(struct buf *b, const struct buf *src) +{ + assert(b); + assert(src); + + if (!src->data) + return 0; + if (!(b->data = BUF_MALLOC(src->length + 1))) + return -1; + + memcpy(b->data, src->data, src->length + 1); + b->capacity = src->length; + b->length = src->length; + + return 0; +} + +void +buf_clear(struct buf *b) +{ + assert(b); + + if (b->data) + b->data[b->length = 0] = 0; +} + +void +buf_finish(struct buf *b) +{ + assert(b); + + BUF_FREE(b->data); + b->data = NULL; + b->capacity = b->length = 0; +}
--- a/buf.h Wed Feb 24 11:43:50 2021 +0100 +++ b/buf.h Thu Sep 16 13:56:31 2021 +0200 @@ -45,43 +45,43 @@ }; void -buf_init(struct buf *b); +buf_init(struct buf *); int -buf_reserve(struct buf *b, size_t desired); +buf_reserve(struct buf *, size_t); int -buf_resize(struct buf *b, size_t size, char c); +buf_resize(struct buf *, size_t, char); int -buf_shrink(struct buf *b); +buf_shrink(struct buf *); void -buf_erase(struct buf *b, size_t pos, size_t count); +buf_erase(struct buf *, size_t, size_t); int -buf_putc(struct buf *b, char c); +buf_putc(struct buf *, char); int -buf_puts(struct buf *b, const char *s); +buf_puts(struct buf *, const char *); int -buf_printf(struct buf *b, const char *fmt, ...); +buf_printf(struct buf *, const char *, ...); int -buf_vprintf(struct buf *b, const char *fmt, va_list ap); +buf_vprintf(struct buf *, const char *, va_list); int -buf_sub(struct buf *b, const struct buf *src, size_t pos, size_t count); +buf_sub(struct buf *, const struct buf *, size_t, size_t); int -buf_dup(struct buf *b, const struct buf *src); +buf_dup(struct buf *, const struct buf *); void -buf_clear(struct buf *b); +buf_clear(struct buf *); void -buf_finish(struct buf *b); +buf_finish(struct buf *); #if defined(__cplusplus) }