2 * Helper functions for outputting text.
3 * Copyright © 2011, 2021, 2023-2024 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "cdecl-internal.h"
29 size_t cdecl__advance(struct output_state *dst, size_t amount)
31 size_t x = MIN(amount, dst->dstlen);
40 size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen)
42 size_t srclen = strlen(src);
45 memcpy(dst, src, MIN(dstlen, srclen+1));
53 size_t cdecl__emit(struct output_state *dst, const char *src)
55 size_t rc = cdecl__strlcpy(dst->dst, src, dst->dstlen);
56 return cdecl__advance(dst, rc);
61 * upper bound on number of decimal digits required to convert
64 MAX_UINT_DIGITS = (CHAR_BIT * sizeof (cdecl_uintmax) + 2)/3
67 size_t cdecl__emit_uint(struct output_state *dst, cdecl_uintmax val)
69 char buf[MAX_UINT_DIGITS + 1], *p = &buf[sizeof buf];
73 *(--p) = '0' + val % 10;
77 return cdecl__emit(dst, p);
80 static void explain_spec(struct output_state *dst, struct cdecl_declspec *s)
84 rc = cdecl__emit(dst, spec_string(s->type));
86 cdecl__emit(dst, " " + !rc);
87 cdecl__emit(dst, s->ident);
92 * Render a list of declaration specifiers. Only the declaration specifiers
93 * listed in mask, which is the bitwise OR of the desired specifier kinds, are
96 const char *cdecl__emit_specs(struct output_state *dst,
97 struct cdecl_declspec *s,
100 const char *sep = " ";
103 for (; s; s = s->next) {
104 if (!(s->type & mask))
107 cdecl__emit(dst, sep + empty);
108 explain_spec(dst, s);