libpkgconf: buffer: fix memory allocation logic, add push/trim byte functions
Fixes: 1001750
("libpkgconf: add buffer management functions")
Signed-off-by: Ariadne Conill <ariadne@ariadne.space>
master
parent
bd043cae9f
commit
83a2ae0434
|
@ -30,8 +30,9 @@ void
|
|||
pkgconf_buffer_append(pkgconf_buffer_t *buffer, const char *text)
|
||||
{
|
||||
size_t needed = strlen(text) + 1;
|
||||
size_t newsize = pkgconf_buffer_len(buffer) + needed;
|
||||
|
||||
char *newbase = realloc(buffer->base, pkgconf_buffer_len(buffer) + needed);
|
||||
char *newbase = realloc(buffer->base, newsize);
|
||||
|
||||
/* XXX: silently failing here is antisocial */
|
||||
if (newbase == NULL)
|
||||
|
@ -40,10 +41,39 @@ pkgconf_buffer_append(pkgconf_buffer_t *buffer, const char *text)
|
|||
char *newend = newbase + pkgconf_buffer_len(buffer);
|
||||
pkgconf_strlcpy(newend, text, needed);
|
||||
|
||||
buffer->base = newbase;
|
||||
buffer->end = newend + needed;
|
||||
}
|
||||
|
||||
void
|
||||
pkgconf_buffer_push_byte(pkgconf_buffer_t *buffer, char byte)
|
||||
{
|
||||
size_t newsize = pkgconf_buffer_len(buffer) + 1;
|
||||
char *newbase = realloc(buffer->base, newsize);
|
||||
|
||||
/* XXX: silently failing here remains antisocial */
|
||||
if (newbase == NULL)
|
||||
return;
|
||||
|
||||
char *newend = newbase + newsize;
|
||||
*(newend - 1) = byte;
|
||||
*newend = '\0';
|
||||
|
||||
buffer->base = newbase;
|
||||
buffer->end = newend;
|
||||
}
|
||||
|
||||
void
|
||||
pkgconf_buffer_trim_byte(pkgconf_buffer_t *buffer)
|
||||
{
|
||||
size_t newsize = pkgconf_buffer_len(buffer) - 1;
|
||||
char *newbase = realloc(buffer->base, newsize);
|
||||
|
||||
buffer->base = newbase;
|
||||
buffer->end = newbase + newsize;
|
||||
*(buffer->end) = '\0';
|
||||
}
|
||||
|
||||
void
|
||||
pkgconf_buffer_finalize(pkgconf_buffer_t *buffer)
|
||||
{
|
||||
|
|
|
@ -430,6 +430,8 @@ typedef struct pkgconf_buffer_ {
|
|||
} pkgconf_buffer_t;
|
||||
|
||||
PKGCONF_API void pkgconf_buffer_append(pkgconf_buffer_t *buffer, const char *text);
|
||||
PKGCONF_API void pkgconf_buffer_push_byte(pkgconf_buffer_t *buffer, char byte);
|
||||
PKGCONF_API void pkgconf_buffer_trim_byte(pkgconf_buffer_t *buffer);
|
||||
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;
|
||||
|
@ -439,8 +441,20 @@ static inline size_t pkgconf_buffer_len(const pkgconf_buffer_t *buffer) {
|
|||
return (size_t)(ptrdiff_t)(buffer->end - buffer->base);
|
||||
}
|
||||
|
||||
static inline char pkgconf_buffer_lastc(const pkgconf_buffer_t *buffer) {
|
||||
return *(buffer->end - 1);
|
||||
}
|
||||
|
||||
#define PKGCONF_BUFFER_INITIALIZER { NULL, NULL }
|
||||
|
||||
static inline void pkgconf_buffer_reset(pkgconf_buffer_t *buffer) {
|
||||
pkgconf_buffer_finalize(buffer);
|
||||
buffer->base = buffer->end = NULL;
|
||||
}
|
||||
|
||||
/* fileio.c */
|
||||
PKGCONF_API char *pkgconf_fgetline(pkgconf_buffer_t *buffer, FILE *stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue