]> git.draconx.ca Git - cdecl99.git/blob - src/output.c
libcdecl: Avoid snprintf for integer conversions.
[cdecl99.git] / src / output.c
1 /*
2  *  Helper functions for outputting text.
3  *  Copyright © 2011, 2021, 2023 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 #include "parse.h"
26 #include "specstr.h"
27
28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
29
30 size_t cdecl__advance(struct output_state *dst, size_t amount)
31 {
32         size_t x = MIN(amount, dst->dstlen);
33
34         dst->dst    += x;
35         dst->dstlen -= x;
36         dst->accum  += amount;
37
38         return amount;
39 }
40
41 size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen)
42 {
43         if (dst)
44                 snprintf(dst, dstlen, "%s", src);
45         return strlen(src);
46 }
47
48 size_t cdecl__emit(struct output_state *dst, const char *src)
49 {
50         size_t rc = cdecl__strlcpy(dst->dst, src, dst->dstlen);
51         return cdecl__advance(dst, rc);
52 }
53
54 /*
55  * 31 decimal digits is enough for values up to 2^102 - 1.
56  * 63 decimal digits is enough for values up to 2^209 - 1.
57  *
58  * We can't portably write numbers this large in preprocessor conditionals,
59  * but since the maximum values of unsigned integer types are always one
60  * less than a power of two, we can use a sequence of small shifts to infer
61  * the bounds.
62  *
63  * All known implementations have 64-bit uintmax_t.  Leave some headroom
64  * to support a possible future implementatons with 128-bit uintmax_t.
65  */
66 enum {
67 #if (UINTMAX_MAX >> 27 >> 27 >> 26 >> 26) == 0
68         MAX_UINT_DIGITS = 31
69 #elif (UINTMAX_MAX >> 27 >> 26 >> 26 >> 26 >> 26 >> 26 >> 26 >> 26) == 0
70         MAX_UINT_DIGITS = 63
71 #else
72 #       error UINTMAX_MAX is too large, please report a bug.
73 #endif
74 };
75
76 size_t cdecl__emit_uint(struct output_state *dst, uintmax_t val)
77 {
78         char buf[MAX_UINT_DIGITS + 1], *p = &buf[sizeof buf];
79
80         *(--p) = 0;
81         while (val > 0) {
82                 *(--p) = '0' + val % 10;
83                 val /= 10;
84         }
85
86         return cdecl__emit(dst, p);
87 }
88
89 static void explain_spec(struct output_state *dst, struct cdecl_declspec *s)
90 {
91         size_t rc;
92
93         rc = cdecl__emit(dst, spec_string(s->type));
94         if (s->ident) {
95                 cdecl__emit(dst, " " + !rc);
96                 cdecl__emit(dst, s->ident);
97         }
98 }
99
100 /*
101  * Render a list of declaration specifiers.  Only the declaration specifiers
102  * listed in mask, which is the bitwise OR of the desired specifier kinds, are
103  * printed.
104  */
105 const char *cdecl__emit_specs(struct output_state *dst,
106                               struct cdecl_declspec *s,
107                               unsigned mask)
108 {
109         const char *sep = " ";
110         int empty = 1;
111
112         for (; s; s = s->next) {
113                 if (!(s->type & mask))
114                         continue;
115
116                 cdecl__emit(dst, sep + empty);
117                 explain_spec(dst, s);
118                 empty = 0;
119         }
120
121         return sep + empty;
122 }