]> git.draconx.ca Git - cdecl99.git/blob - src/output.c
Release 1.3.
[cdecl99.git] / src / output.c
1 /*
2  *  Helper functions for outputting text.
3  *  Copyright © 2011, 2021, 2023-2024 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 <limits.h>
21 #include <assert.h>
22
23 #include "cdecl.h"
24 #include "cdecl-internal.h"
25
26 #include "parse.h"
27 #include "specstr.h"
28
29 size_t cdecl__advance(struct output_state *dst, size_t amount)
30 {
31         size_t x = MIN(amount, dst->dstlen);
32
33         dst->dst    += x;
34         dst->dstlen -= x;
35         dst->accum  += amount;
36
37         return amount;
38 }
39
40 size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen)
41 {
42         size_t srclen = strlen(src);
43
44         if (dstlen > 0) {
45                 memcpy(dst, src, MIN(dstlen, srclen+1));
46                 if (dstlen <= srclen)
47                         dst[dstlen-1] = 0;
48         }
49
50         return srclen;
51 }
52
53 size_t cdecl__emit(struct output_state *dst, const char *src)
54 {
55         size_t rc = cdecl__strlcpy(dst->dst, src, dst->dstlen);
56         return cdecl__advance(dst, rc);
57 }
58
59 enum {
60         /*
61          * upper bound on number of decimal digits required to convert
62          * cdecl_uintmax.
63          */
64         MAX_UINT_DIGITS = (CHAR_BIT * sizeof (cdecl_uintmax) + 2)/3
65 };
66
67 size_t cdecl__emit_uint(struct output_state *dst, cdecl_uintmax val)
68 {
69         char buf[MAX_UINT_DIGITS + 1], *p = &buf[sizeof buf];
70
71         *(--p) = 0;
72         while (val > 0) {
73                 *(--p) = '0' + val % 10;
74                 val /= 10;
75         }
76
77         return cdecl__emit(dst, p);
78 }
79
80 static void explain_spec(struct output_state *dst, struct cdecl_declspec *s)
81 {
82         size_t rc;
83
84         rc = cdecl__emit(dst, spec_string(s->type));
85         if (s->ident) {
86                 cdecl__emit(dst, " " + !rc);
87                 cdecl__emit(dst, s->ident);
88         }
89 }
90
91 /*
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
94  * printed.
95  */
96 const char *cdecl__emit_specs(struct output_state *dst,
97                               struct cdecl_declspec *s,
98                               unsigned mask)
99 {
100         const char *sep = " ";
101         int empty = 1;
102
103         for (; s; s = s->next) {
104                 if (!(s->type & mask))
105                         continue;
106
107                 cdecl__emit(dst, sep + empty);
108                 explain_spec(dst, s);
109                 empty = 0;
110         }
111
112         return sep + empty;
113 }