MMgr v1.0.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 handed 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 * keeps 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, and
17 * every later write then finds no room. A line with a flush hands 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 has been corrupted by
21 * something other than the line. A line no write has reached 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, and 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 read.
26 */
27#ifndef MMGR_VERBA_LINEA_H
28#define MMGR_VERBA_LINEA_H
29
30#include "mmgr.h"
31
32EMBED_BEGIN_DECLS
33
34/**
35 * @brief Where a line hands the size bytes at bytes it holds once it is full.
36 *
37 * @param[in] context The line's context [BORROWS].
38 * @param[in] bytes What the line holds [BORROWS].
39 * @param[in] size Bytes at bytes.
40 * @return 1 where the bytes went, 0 where they did not go.
41 */
42typedef int (*VerbaLineaFlush)(void *context, const char *bytes, size_t size);
43
44/**
45 * @brief A line written into out, at bytes so far, and handed to flush as it fills.
46 *
47 * @note A byte of out is reserved for the terminator, and every write stores it. A write that did not
48 * fit leaves at at capacity, with capacity minus one bytes written.
49 * @note flushed counts the bytes the line has handed on.
50 * @warning out must be writable for capacity bytes.
51 */
52typedef struct
53{
54 char *out; /**< The line's buffer [BORROWS]. */
55 size_t capacity; /**< Bytes in out. */
56 size_t at; /**< Bytes written, and capacity once a write did not fit. */
57 VerbaLineaFlush flush; /**< Where the line hands what it holds, or NULL for a line that fills. */
58 void *context; /**< Handed to flush with every call [BORROWS]. */
59 size_t flushed; /**< Bytes handed to flush so far. */
61
62/**
63 * @brief Makes a line over a buffer, of no capacity where the buffer is NULL.
64 *
65 * @param[in] out The line's buffer, or NULL where its take was refused [BORROWS].
66 * @param[in] capacity Bytes in out.
67 * @return The line, nothing written and no flush.
68 * @note A line over a refused take stores nothing and mmgr_line_finish answers 0, and therefore a
69 * caller tests the take once and writes the line either way.
70 */
71VerbaLinea mmgr_line_over(char *out, size_t capacity);
72
73/**
74 * @brief Hands what the line holds to its flush and empties it.
75 *
76 * @param[in,out] line The line [BORROWS].
77 * @return EMBED_TRUE where the flush took the bytes or grew the line, EMBED_FALSE where
78 * the line has no flush, is full, or its flush refused.
79 * @note A flush that refuses leaves the line full.
80 */
81embed_bool mmgr_line_flush(VerbaLinea *line);
82
83/**
84 * @brief Writes length bytes of text.
85 *
86 * @param[in,out] line The line [BORROWS].
87 * @param[in] text Bytes to write [BORROWS].
88 * @param[in] length Bytes of text.
89 * @return Bytes of text the line took, handed on included: length where every byte went.
90 * @note A text past the room left is handed on in pieces where the line has a flush, and fills the
91 * line where it has none, what fits written.
92 * @note A line of fewer than two bytes holds none past its terminator, and so any text fills it.
93 * @warning text must be readable for length bytes.
94 */
95size_t mmgr_line_put_n(VerbaLinea *line, const char *text, size_t length);
96
97/**
98 * @brief Writes a terminated text.
99 *
100 * @param[in,out] line The line [BORROWS].
101 * @param[in] text Terminated text to write [BORROWS].
102 * @return Bytes of text the line took, handed on included.
103 * @note The text is read a piece the size of the room left at a time, never past its terminator, and therefore a
104 * line with a flush takes a text of any length and needs no bound on it. A line with none is
105 * filled by a text longer than its room, what fits written.
106 */
107size_t mmgr_line_put(VerbaLinea *line, const char *text);
108
109/**
110 * @brief Writes a terminated text, at most most bytes of it.
111 *
112 * @param[in,out] line The line [BORROWS].
113 * @param[in] text Text to write [BORROWS].
114 * @param[in] most The most bytes of text read.
115 * @return Bytes of text the line took, handed on included.
116 */
117size_t mmgr_line_put_most(VerbaLinea *line, const char *text, size_t most);
118
119/**
120 * @brief Writes a terminated text and spaces after it out to columns.
121 *
122 * @param[in,out] line The line [BORROWS].
123 * @param[in] text Terminated text to write [BORROWS].
124 * @param[in] columns Bytes the text and its spaces fill. A longer text gets no spaces.
125 * @note The text is read as mmgr_line_put reads it.
126 */
127void mmgr_line_put_columns(VerbaLinea *line, const char *text, unsigned int columns);
128
129/**
130 * @brief Writes one character.
131 *
132 * @param[in,out] line The line [BORROWS].
133 * @param[in] character The character.
134 */
135void mmgr_line_ch(VerbaLinea *line, char character);
136
137/**
138 * @brief Writes value in base ten, padded on the left with zeros to digits_least digits.
139 *
140 * @param[in,out] line The line [BORROWS].
141 * @param[in] value The value.
142 * @param[in] digits_least Fewest digits written, clamped at 255.
143 */
144void mmgr_line_decimal(VerbaLinea *line, uint64_t value, unsigned int digits_least);
145
146/**
147 * @brief Writes value in base ten, right aligned in columns with spaces before it.
148 *
149 * @param[in,out] line The line [BORROWS].
150 * @param[in] value The value.
151 * @param[in] columns Bytes the value and its spaces fill, clamped at 255.
152 */
153void mmgr_line_decimal_columns(VerbaLinea *line, uint64_t value, unsigned int columns);
154
155/**
156 * @brief Writes a signed value in base ten, a minus sign before a negative one.
157 *
158 * @param[in,out] line The line [BORROWS].
159 * @param[in] value The value. INT64_MIN is written whole.
160 */
161void mmgr_line_decimal_signed(VerbaLinea *line, int64_t value);
162
163/**
164 * @brief Writes value in lower case base sixteen, padded on the left with zeros to digits_least digits.
165 *
166 * @param[in,out] line The line [BORROWS].
167 * @param[in] value The value.
168 * @param[in] digits_least Fewest digits written, clamped at 255.
169 */
170void mmgr_line_hex(VerbaLinea *line, uint64_t value, unsigned int digits_least);
171
172/**
173 * @brief Stores the terminator, the one store a line no write has reached still needs.
174 *
175 * @param[in,out] line The line [BORROWS].
176 * @return The length the line holds, or 0 where it is full.
177 * @note A full line stands terminated in its last byte and holds capacity minus one bytes.
178 */
180
181/**
182 * @brief Reports whether every write fit.
183 *
184 * @param[in] line The line [BORROWS].
185 * @return EMBED_TRUE while the line has room, EMBED_FALSE once a write did not fit.
186 */
187embed_bool mmgr_line_ok(const VerbaLinea *line);
188
189EMBED_END_DECLS
190
191#endif
The embedded_types headers every module builds on.
A line written into out, at bytes so far, and handed to flush as it fills.
Definition verba_linea.h:53
size_t flushed
Definition verba_linea.h:59
char * out
Definition verba_linea.h:54
size_t capacity
Definition verba_linea.h:55
VerbaLineaFlush flush
Definition verba_linea.h:57
void * context
Definition verba_linea.h:58
size_t at
Definition verba_linea.h:56
embed_bool mmgr_line_flush(VerbaLinea *line)
Hands what the line holds to its flush and empties it.
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_bool mmgr_line_ok(const VerbaLinea *line)
Reports whether every write fit.
size_t mmgr_line_put(VerbaLinea *line, const char *text)
Writes a terminated text.
size_t mmgr_line_put_most(VerbaLinea *line, const char *text, size_t most)
Writes a terminated text, at most most bytes of it.
VerbaLinea mmgr_line_over(char *out, size_t capacity)
Makes a line over a buffer, of no capacity where the buffer is NULL.
int(*) VerbaLineaFlush(void *context, const char *bytes, size_t size)
Where a line hands the size bytes at bytes it holds once it is full.
Definition verba_linea.h:42
size_t mmgr_line_put_n(VerbaLinea *line, const char *text, size_t length)
Writes length bytes of text.
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.
void mmgr_line_put_columns(VerbaLinea *line, const char *text, unsigned int columns)
Writes a terminated text and spaces after it out to columns.
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.
void mmgr_line_ch(VerbaLinea *line, char character)
Writes one character.
void mmgr_line_decimal_signed(VerbaLinea *line, int64_t value)
Writes a signed value in base ten, a minus sign before a negative one.
size_t mmgr_line_finish(VerbaLinea *line)
Stores the terminator, the one store a line no write has reached still needs.