2 * Helper functions for outputting text.
3 * Copyright © 2011, 2021, 2023 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/>.
23 #include "cdecl-internal.h"
25 size_t cdecl__advance_(char **buf, size_t *n, size_t amount)
38 size_t cdecl__advance(char **buf, size_t *n, size_t amount)
45 ret = cdecl__advance_(buf, n, amount);
46 rc = snprintf(*buf, *n, " ");
47 return ret + cdecl__advance_(buf, n, rc);
50 size_t cdecl__emit(char **dst, size_t *dstlen, const char *src)
52 size_t rc = snprintf(*dst, *dstlen, "%s", src);
53 return cdecl__advance_(dst, dstlen, rc);
58 static size_t explain_spec(char *buf, size_t n, struct cdecl_declspec *s)
60 const char *keyword = spec_string(s->type);
62 if (keyword[0] && s->ident)
63 return snprintf(buf, n, "%s %s", keyword, s->ident);
65 return snprintf(buf, n, "%s", s->ident);
66 return snprintf(buf, n, "%s", keyword);
70 * Render a list of declaration specifiers. Only the declaration specifiers
71 * listed in mask, which is the bitwise OR of the desired specifier kinds, are
74 size_t cdecl__explain_specs(char *buf, size_t n, struct cdecl_declspec *s,
77 size_t ret = 0, rc = 0;
79 for (; s; s = s->next) {
80 if (!(s->type & mask))
83 ret += cdecl__advance(&buf, &n, rc);
84 rc = explain_spec(buf, n, s);