]> git.draconx.ca Git - cdecl99.git/blob - src/output.c
Generate specifier strings directly from cdecl.h
[cdecl99.git] / src / output.c
1 /*
2  *  Helper functions for outputting text.
3  *  Copyright © 2011, 2021 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 #include <config.h>
19 #include <stdio.h>
20 #include <assert.h>
21
22 #include "cdecl.h"
23 #include "cdecl-internal.h"
24
25 size_t cdecl__advance_(char **buf, size_t *n, size_t amount)
26 {
27         if (amount >= *n) {
28                 *n   = 0;
29                 *buf = 0;
30         } else {
31                 *buf += amount;
32                 *n   -= amount;
33         }
34
35         return amount;
36 }
37
38 size_t cdecl__advance(char **buf, size_t *n, size_t amount)
39 {
40         size_t ret, rc;
41
42         if (!amount)
43                 return 0;
44
45         ret = cdecl__advance_(buf, n, amount);
46         rc = snprintf(*buf, *n, " ");
47         return ret + cdecl__advance_(buf, n, rc);
48 }
49
50 #include "specstr.h"
51
52 static size_t explain_spec(char *buf, size_t n, struct cdecl_declspec *s)
53 {
54         const char *keyword = spec_string(s->type);
55
56         if (keyword[0] && s->ident)
57                 return snprintf(buf, n, "%s %s", keyword, s->ident);
58         else if (s->ident)
59                 return snprintf(buf, n, "%s", s->ident);
60         return snprintf(buf, n, "%s", keyword);
61 }
62
63 /*
64  * Render a list of declaration specifiers.  Only the declaration specifiers
65  * listed in mask, which is the bitwise OR of the desired specifier kinds, are
66  * printed.
67  */
68 size_t cdecl__explain_specs(char *buf, size_t n, struct cdecl_declspec *s,
69                                                  unsigned mask)
70 {
71         size_t ret = 0, rc = 0;
72
73         for (struct cdecl_declspec *c = s; c; c = c->next) {
74                 switch (cdecl_spec_kind(c) & mask) {
75                 case CDECL_SPEC_FUNC:
76                 case CDECL_SPEC_STOR:
77                 case CDECL_SPEC_QUAL:
78                 case CDECL_SPEC_TYPE:
79                         ret += cdecl__advance(&buf, &n, rc);
80                         rc = explain_spec(buf, n, c);
81                 }
82         }
83
84         return ret + rc;
85 }
86
87 /* Renders the storage-class and function specifiers in canonical form. */
88 size_t cdecl__explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
89 {
90         return cdecl__explain_specs(buf, n, s, CDECL_SPEC_FUNC|CDECL_SPEC_STOR);
91 }
92
93 /* Renders the type qualifiers and type specifiers in canonical form. */
94 size_t cdecl__explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
95 {
96         return cdecl__explain_specs(buf, n, s, CDECL_SPEC_QUAL|CDECL_SPEC_TYPE);
97 }