libpkgconf: add buffer management functions

Signed-off-by: Ariadne Conill <ariadne@ariadne.space>
Ariadne Conill 2024-12-09 19:49:50 -08:00
parent 5c535171a5
commit 10017500ff
4 changed files with 69 additions and 0 deletions

View File

@ -150,6 +150,7 @@ SUFFIXES= .sh
nobase_pkginclude_HEADERS = libpkgconf/bsdstubs.h libpkgconf/iter.h libpkgconf/libpkgconf.h libpkgconf/stdinc.h libpkgconf/libpkgconf-api.h
libpkgconf_la_SOURCES = \
libpkgconf/audit.c \
libpkgconf/buffer.c \
libpkgconf/cache.c \
libpkgconf/client.c \
libpkgconf/pkg.c \

51
libpkgconf/buffer.c Normal file
View File

@ -0,0 +1,51 @@
/*
* buffer.c
* dynamically-managed buffers
*
* Copyright (c) 2024 pkgconf authors (see AUTHORS).
*
* 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.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <libpkgconf/stdinc.h>
#include <libpkgconf/libpkgconf.h>
/*
* !doc
*
* libpkgconf `buffer` module
* ==========================
*
* The libpkgconf `buffer` module contains the functions related to managing
* dynamically-allocated buffers.
*/
void
pkgconf_buffer_append(pkgconf_buffer_t *buffer, const char *text)
{
size_t needed = strlen(text) + 1;
char *newbase = realloc(buffer->base, pkgconf_buffer_len(buffer) + needed);
/* XXX: silently failing here is antisocial */
if (newbase == NULL)
return;
char *newend = newbase + pkgconf_buffer_len(buffer);
pkgconf_strlcpy(newend, text, needed);
buffer->base = newbase;
buffer->end = newend;
}
void
pkgconf_buffer_finalize(pkgconf_buffer_t *buffer)
{
free(buffer->base);
}

View File

@ -423,6 +423,22 @@ PKGCONF_API void pkgconf_path_free(pkgconf_list_t *dirlist);
PKGCONF_API bool pkgconf_path_relocate(char *buf, size_t buflen);
PKGCONF_API void pkgconf_path_copy_list(pkgconf_list_t *dst, const pkgconf_list_t *src);
/* buffer.c */
typedef struct pkgconf_buffer_ {
char *base;
char *end;
} pkgconf_buffer_t;
PKGCONF_API void pkgconf_buffer_append(pkgconf_buffer_t *buffer, const char *text);
PKGCONF_API void pkgconf_buffer_finalize(pkgconf_buffer_t *buffer);
static inline const char *pkgconf_buffer_str(const pkgconf_buffer_t *buffer) {
return buffer->base;
}
static inline size_t pkgconf_buffer_len(const pkgconf_buffer_t *buffer) {
return (size_t)(ptrdiff_t)(buffer->end - buffer->base);
}
#ifdef __cplusplus
}
#endif

View File

@ -82,6 +82,7 @@ endif
libpkgconf = library('pkgconf',
'libpkgconf/argvsplit.c',
'libpkgconf/audit.c',
'libpkgconf/buffer.c',
'libpkgconf/bsdstubs.c',
'libpkgconf/cache.c',
'libpkgconf/client.c',