File: buffer.c

package info (click to toggle)
pkgconf 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,820 kB
  • sloc: ansic: 8,111; sh: 6,689; makefile: 247; python: 157
file content (87 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * 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.
 */

static inline size_t
target_allocation_size(size_t target_size)
{
	return 4096 + (4096 * (target_size / 4096));
}

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, target_allocation_size(newsize));

	/* 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 + 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, target_allocation_size(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, target_allocation_size(newsize));

	buffer->base = newbase;
	buffer->end = newbase + newsize;
	*(buffer->end) = '\0';
}

void
pkgconf_buffer_finalize(pkgconf_buffer_t *buffer)
{
	free(buffer->base);
}