MMgr v2.3.0
Zero-heap memory manager in C11. One borrowed buffer, carved by asserted offset.
Loading...
Searching...
No Matches
verba_linea.h
Go to the documentation of this file.
1/* MMgr - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial OR LicenseRef-Educational
3 *
4 * Every use falls under AGPL-3.0-or-later unless you hold explicit permission, which is either a
5 * negotiated commercial licensing contract or an educator's license issued to you personally.
6 */
7/**
8 * @file verba_linea.h
9 * @brief A line written into a caller's buffer and passed on as it fills, one field at a time.
10 * @author dstroy0 (Douglas Quigg) <dquigg123@gmail.com>
11 * @date 2026-10-23
12 *
13 * @note Every entry takes the line and the value it writes as plain arguments, and therefore a C++ translation
14 * unit calls them as they are. This header includes mmgr.h alone and defines no table, which
15 * retains it free of the designated initializers verba_scribo.h carries.
16 * @note A write that does not fit a line with no flush writes what fits and leaves at at capacity.
17 * Every later write then finds no room. A line with a flush passes what it holds to the flush as it
18 * fills and goes on.
19 * @note Every write leaves the buffer terminated: at at while the line has room, in its last byte once a
20 * write did not fit. A written line whose buffer holds no terminator took damage from
21 * something other than the line. A line no write reached yet has what its buffer contained until
22 * mmgr_line_finish runs.
23 * @note A text entry answers the bytes of the text the line took. Where that is fewer than the text holds,
24 * the line filled. Text plus the answer is where the caller goes on from, in the same room with
25 * at set to zero once those bytes are out.
26 */
27#ifndef MMGR_VERBA_LINEA_H
28#define MMGR_VERBA_LINEA_H
29
30/** @addtogroup mod_verba
31 * @{ */
32
33#include "mmgr.h"
34
35EMBED_BEGIN_DECLS
36
37/**
38 * @brief Where a line passes the size bytes at bytes it holds once it is full.
39 *
40 * @param[in] context The line's context [BORROWS].
41 * @param[in] bytes What the line holds [BORROWS].
42 * @param[in] size Bytes at bytes.
43 * @return 1 where the bytes went, 0 where they did not go.
44 */
45typedef int (*VerbaLineaFlush)(void *context, const char *bytes, size_t size);
46
47/**
48 * @brief A line written into out, at bytes so far, and passed to flush as it fills.
49 *
50 * @note One byte of out stays reserved for the terminator, and every write stores it. A write that did not
51 * fit leaves at at capacity, with capacity minus one bytes written.
52 * @note flushed counts the bytes the line passed on.
53 * @warning out must be writable for capacity bytes.
54 */
55typedef struct
56{
57 char *out; /**< The line's buffer [BORROWS]. */
58 size_t capacity; /**< Bytes in out. */
59 size_t at; /**< Bytes written, and capacity once a write did not fit. */
60 VerbaLineaFlush flush; /**< Where the line passes what it holds, or NULL for a line that fills. */
61 void *context; /**< Passed to flush with every call [BORROWS]. */
62 size_t flushed; /**< Bytes passed to flush so far. */
64
65/**
66 * @brief Makes a line over a buffer, of no capacity where the buffer is NULL.
67 *
68 * @param[in] out The line's buffer, or NULL where its take failed [BORROWS].
69 * @param[in] capacity Bytes in out.
70 * @return The line, nothing written and no flush.
71 * @note A line over a refused take stores nothing and mmgr_line_finish answers 0, and therefore a
72 * caller tests the take once and writes the line either way.
73 */
74EMBED_HOST_DEVICE VerbaLinea mmgr_line_over(char *out, size_t capacity);
75
76/**
77 * @brief Passes what the line holds to its flush and empties it.
78 *
79 * @param[in,out] line The line [BORROWS].
80 * @return EMBED_TRUE where the flush took the bytes or grew the line, EMBED_FALSE where
81 * the line has no flush, is full, or its flush refused.
82 * @note A flush that refuses leaves the line full.
83 */
84EMBED_HOST_DEVICE embed_bool mmgr_line_flush(VerbaLinea *line);
85
86/**
87 * @brief Writes length bytes of text.
88 *
89 * @param[in,out] line The line [BORROWS].
90 * @param[in] text Bytes to write [BORROWS].
91 * @param[in] length Bytes of text.
92 * @return Bytes of text the line took, passed on included: length where every byte went.
93 * @note A text past the room left goes on in pieces where the line has a flush. It fills the
94 * line where it has none, what fits written.
95 * @note A line of fewer than two bytes holds none past its terminator, and so any text fills it.
96 * @warning text must be readable for length bytes.
97 */
98EMBED_HOST_DEVICE size_t mmgr_line_put_n(VerbaLinea *line, const char *text, size_t length);
99
100/**
101 * @brief Writes a terminated text.
102 *
103 * @param[in,out] line The line [BORROWS].
104 * @param[in] text Terminated text to write [BORROWS].
105 * @return Bytes of text the line took, passed on included.
106 * @note The read of the text goes a piece the size of the room left at a time, never past its terminator. So a
107 * line with a flush takes a text of any length and needs no bound on it. A text longer than its
108 * room fills a line with none, what fits written.
109 */
110EMBED_HOST_DEVICE size_t mmgr_line_put(VerbaLinea *line, const char *text);
111
112/**
113 * @brief Writes a terminated text, at max max bytes of it.
114 *
115 * @param[in,out] line The line [BORROWS].
116 * @param[in] text Text to write [BORROWS].
117 * @param[in] max The max bytes of text read.
118 * @return Bytes of text the line took, passed on included.
119 */
120EMBED_HOST_DEVICE size_t mmgr_line_put_max(VerbaLinea *line, const char *text, size_t max);
121
122/**
123 * @brief Writes a terminated text and spaces after it out to columns.
124 *
125 * @param[in,out] line The line [BORROWS].
126 * @param[in] text Terminated text to write [BORROWS].
127 * @param[in] columns Bytes the text and its spaces fill. A longer text gets no spaces.
128 * @note The read of the text goes as mmgr_line_put reads it.
129 */
130EMBED_HOST_DEVICE void mmgr_line_put_columns(VerbaLinea *line, const char *text, unsigned int columns);
131
132/**
133 * @brief Writes spaces out to columns and then a terminated text, and so the text is right aligned.
134 *
135 * @param[in,out] line The line [BORROWS].
136 * @param[in] text Terminated text to write [BORROWS].
137 * @param[in] columns Bytes the spaces and the text fill. A text of columns bytes or more gets no spaces.
138 * @note The measure of the text reads at max columns bytes of it. The text then goes as mmgr_line_put reads it.
139 */
140EMBED_HOST_DEVICE void mmgr_line_put_right(VerbaLinea *line, const char *text, unsigned int columns);
141
142/**
143 * @brief Writes one character.
144 *
145 * @param[in,out] line The line [BORROWS].
146 * @param[in] character The character.
147 */
148EMBED_HOST_DEVICE void mmgr_line_ch(VerbaLinea *line, char character);
149
150/**
151 * @brief Writes value in base ten, padded on the left with zeros to digits_least digits.
152 *
153 * @param[in,out] line The line [BORROWS].
154 * @param[in] value The value.
155 * @param[in] digits_least Fewest digits written, clamped at 255.
156 */
157EMBED_HOST_DEVICE void mmgr_line_decimal(VerbaLinea *line, uint64_t value, unsigned int digits_least);
158
159/**
160 * @brief Writes value in base ten, right aligned in columns with spaces before it.
161 *
162 * @param[in,out] line The line [BORROWS].
163 * @param[in] value The value.
164 * @param[in] columns Bytes the value and its spaces fill, clamped at 255.
165 */
166EMBED_HOST_DEVICE void mmgr_line_decimal_columns(VerbaLinea *line, uint64_t value, unsigned int columns);
167
168/**
169 * @brief Writes a signed value in base ten, a minus sign before a negative one.
170 *
171 * @param[in,out] line The line [BORROWS].
172 * @param[in] value The value. INT64_MIN goes out whole.
173 */
174EMBED_HOST_DEVICE void mmgr_line_decimal_signed(VerbaLinea *line, int64_t value);
175
176/**
177 * @brief Writes value in lower case base sixteen, padded on the left with zeros to digits_least digits.
178 *
179 * @param[in,out] line The line [BORROWS].
180 * @param[in] value The value.
181 * @param[in] digits_least Fewest digits written, clamped at 255.
182 */
183EMBED_HOST_DEVICE void mmgr_line_hex(VerbaLinea *line, uint64_t value, unsigned int digits_least);
184
185/**
186 * @brief Writes a double with decimals digits after the point, right aligned in columns with spaces before it.
187 *
188 * @param[in,out] line The line [BORROWS].
189 * @param[in] value The value.
190 * @param[in] decimals Digits after the point, clamped at MMGR_FIXED_MAX_DECIMALS. A count of 0 writes no point.
191 * @param[in] columns Bytes the value, its sign and its spaces fill, clamped at 255.
192 * @param[in] plus EMBED_TRUE writes a plus sign before a value whose sign bit is clear.
193 * @note The fraction rounds from the exact value of the double, as mmgr_verba_fixed rounds it. An exact tie goes to
194 * the even digit, as the C library rounds it. A negative zero writes its minus sign.
195 * @warning A magnitude too large for 64 bits of integer part goes out as mmgr_verba_g writes it, at ten
196 * significant digits.
197 */
198EMBED_HOST_DEVICE void mmgr_line_fixed(VerbaLinea *line, double value, unsigned int decimals, unsigned int columns,
199 embed_bool plus);
200
201/**
202 * @brief Writes a double as one digit, a point, decimals digits, e, a sign and a power of two digits or more, right
203 * aligned in columns with spaces before it.
204 *
205 * @param[in,out] line The line [BORROWS].
206 * @param[in] value The value.
207 * @param[in] decimals Digits after the point, clamped at MMGR_G_MAX_SIG less one. A count of 0 writes no point.
208 * @param[in] columns Bytes the value, its sign and its spaces fill, clamped at 255.
209 * @param[in] plus EMBED_TRUE writes a plus sign before a value whose sign bit is clear.
210 * @note The digits round from the exact value of the double. An exact tie goes to the even digit, as the C library
211 * rounds it. A zero writes zeros and a power of +00.
212 * @note An infinity or a NaN goes out as mmgr_verba_fixed writes it.
213 */
214EMBED_HOST_DEVICE void mmgr_line_exponent(VerbaLinea *line, double value, unsigned int decimals, unsigned int columns,
215 embed_bool plus);
216
217/**
218 * @brief Writes a double to significant digits in the shorter form that printf's %g chooses, right aligned in columns
219 * with spaces before it.
220 *
221 * @param[in,out] line The line [BORROWS].
222 * @param[in] value The value.
223 * @param[in] significant Digits kept, 1 where it is 0, clamped at MMGR_G_MAX_SIG.
224 * @param[in] columns Bytes the value, its sign and its spaces fill, clamped at 255.
225 * @param[in] plus EMBED_TRUE writes a plus sign before a value whose sign bit is clear.
226 * @note The digits round as mmgr_line_exponent rounds them. A power below -4, or of significant or more, writes the
227 * exponent form, and any other power writes the plain form. Each form drops the zeros at the end of its digits,
228 * and a point with no digit after it.
229 * @note A zero writes 0. An infinity or a NaN goes out as mmgr_verba_fixed writes it.
230 */
231EMBED_HOST_DEVICE void mmgr_line_general(VerbaLinea *line, double value, unsigned int significant,
232 unsigned int columns, embed_bool plus);
233
234/**
235 * @brief Stores the terminator, the one store a line no write reached yet still needs.
236 *
237 * @param[in,out] line The line [BORROWS].
238 * @return The length the line holds, or 0 where it is full.
239 * @note A full line holds the terminator in its last byte, and capacity minus one bytes before it.
240 */
241EMBED_HOST_DEVICE size_t mmgr_line_finish(VerbaLinea *line);
242
243/**
244 * @brief Reports whether every write fit.
245 *
246 * @param[in] line The line [BORROWS].
247 * @return EMBED_TRUE while the line has room, EMBED_FALSE once a write did not fit.
248 */
249EMBED_HOST_DEVICE embed_bool mmgr_line_ok(const VerbaLinea *line);
250
251EMBED_END_DECLS
252
253/** @} */
254
255#endif
EMBED_HOST_DEVICE size_t mmgr_line_finish(VerbaLinea *line)
Stores the terminator, the one store a line no write reached yet still needs.
EMBED_HOST_DEVICE size_t mmgr_line_put_max(VerbaLinea *line, const char *text, size_t max)
Writes a terminated text, at max max bytes of it.
EMBED_HOST_DEVICE size_t mmgr_line_put(VerbaLinea *line, const char *text)
Writes a terminated text.
int(*) VerbaLineaFlush(void *context, const char *bytes, size_t size)
Where a line passes the size bytes at bytes it holds once it is full.
Definition verba_linea.h:45
EMBED_HOST_DEVICE void mmgr_line_general(VerbaLinea *line, double value, unsigned int significant, unsigned int columns, embed_bool plus)
Writes a double to significant digits in the shorter form that printf's g chooses,...
EMBED_HOST_DEVICE void mmgr_line_ch(VerbaLinea *line, char character)
Writes one character.
EMBED_HOST_DEVICE VerbaLinea mmgr_line_over(char *out, size_t capacity)
Makes a line over a buffer, of no capacity where the buffer is NULL.
EMBED_HOST_DEVICE void mmgr_line_fixed(VerbaLinea *line, double value, unsigned int decimals, unsigned int columns, embed_bool plus)
Writes a double with decimals digits after the point, right aligned in columns with spaces before it.
EMBED_HOST_DEVICE void mmgr_line_hex(VerbaLinea *line, uint64_t value, unsigned int digits_least)
Writes value in lower case base sixteen, padded on the left with zeros to digits_least digits.
EMBED_HOST_DEVICE void mmgr_line_exponent(VerbaLinea *line, double value, unsigned int decimals, unsigned int columns, embed_bool plus)
Writes a double as one digit, a point, decimals digits, e, a sign and a power of two digits or more,...
EMBED_HOST_DEVICE embed_bool mmgr_line_flush(VerbaLinea *line)
Passes what the line holds to its flush and empties it.
EMBED_HOST_DEVICE void mmgr_line_decimal_signed(VerbaLinea *line, int64_t value)
Writes a signed value in base ten, a minus sign before a negative one.
EMBED_HOST_DEVICE size_t mmgr_line_put_n(VerbaLinea *line, const char *text, size_t length)
Writes length bytes of text.
EMBED_HOST_DEVICE void mmgr_line_decimal(VerbaLinea *line, uint64_t value, unsigned int digits_least)
Writes value in base ten, padded on the left with zeros to digits_least digits.
EMBED_HOST_DEVICE void mmgr_line_put_columns(VerbaLinea *line, const char *text, unsigned int columns)
Writes a terminated text and spaces after it out to columns.
EMBED_HOST_DEVICE void mmgr_line_decimal_columns(VerbaLinea *line, uint64_t value, unsigned int columns)
Writes value in base ten, right aligned in columns with spaces before it.
EMBED_HOST_DEVICE void mmgr_line_put_right(VerbaLinea *line, const char *text, unsigned int columns)
Writes spaces out to columns and then a terminated text, and so the text is right aligned.
EMBED_HOST_DEVICE embed_bool mmgr_line_ok(const VerbaLinea *line)
Reports whether every write fit.
The embedded_types headers every module builds on.
A line written into out, at bytes so far, and passed to flush as it fills.
Definition verba_linea.h:56
size_t flushed
Definition verba_linea.h:62
char * out
Definition verba_linea.h:57
size_t capacity
Definition verba_linea.h:58
VerbaLineaFlush flush
Definition verba_linea.h:60
void * context
Definition verba_linea.h:61
size_t at
Definition verba_linea.h:59