summaryrefslogtreecommitdiffstats
path: root/src/format.c
blob: 458d493128ea219a19eb6a5bd7a1c3049dc72c80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * This file is part of libplacebo.
 *
 * libplacebo is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libplacebo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libplacebo.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <math.h>

#include "common.h"

void pl_str_append_asprintf_c(void *alloc, pl_str *str, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    pl_str_append_vasprintf_c(alloc, str, fmt, ap);
    va_end(ap);
}

void pl_str_append_vasprintf_c(void *alloc, pl_str *str, const char *fmt,
                               va_list ap)
{
    for (const char *c; (c = strchr(fmt, '%')) != NULL; fmt = c + 1) {
        // Append the preceding string literal
        pl_str_append_raw(alloc, str, fmt, c - fmt);
        c++; // skip '%'

        char buf[32];
        int len;

        // The format character follows the % sign
        switch (c[0]) {
        case '%':
            pl_str_append_raw(alloc, str, c, 1);
            continue;
        case 's': {
            const char *arg = va_arg(ap, const char *);
            pl_str_append_raw(alloc, str, arg, strlen(arg));
            continue;
        }
        case '.': { // only used for %.*s
            assert(c[1] == '*');
            assert(c[2] == 's');
            len = va_arg(ap, int);
            pl_str_append_raw(alloc, str, va_arg(ap, char *), len);
            c += 2; // skip '*s'
            continue;
        }
        case 'c':
            buf[0] = (char) va_arg(ap, int);
            len = 1;
            break;
        case 'd':
            len = pl_str_print_int(buf, sizeof(buf), va_arg(ap, int));
            break;
        case 'h': ; // only used for %hx
            assert(c[1] == 'x');
            len = pl_str_print_hex(buf, sizeof(buf), (unsigned short) va_arg(ap, unsigned int));
            c++;
            break;
        case 'u':
            len = pl_str_print_uint(buf, sizeof(buf), va_arg(ap, unsigned int));
            break;
        case 'l':
            assert(c[1] == 'l');
            switch (c[2]) {
            case 'u':
                len = pl_str_print_uint64(buf, sizeof(buf), va_arg(ap, unsigned long long));
                break;
            case 'd':
                len = pl_str_print_int64(buf, sizeof(buf), va_arg(ap, long long));
                break;
            default: pl_unreachable();
            }
            c += 2;
            break;
        case 'z':
            assert(c[1] == 'u');
            len = pl_str_print_uint64(buf, sizeof(buf), va_arg(ap, size_t));
            c++;
            break;
        case 'f':
            len = pl_str_print_double(buf, sizeof(buf), va_arg(ap, double));
            break;
        default:
            fprintf(stderr, "Invalid conversion character: '%c'!\n", c[0]);
            abort();
        }

        pl_str_append_raw(alloc, str, buf, len);
    }

    // Append the remaining string literal
    pl_str_append(alloc, str, pl_str0(fmt));
}

size_t pl_str_append_memprintf_c(void *alloc, pl_str *str, const char *fmt,
                                 const void *args)
{
    const uint8_t *ptr = args;

    for (const char *c; (c = strchr(fmt, '%')) != NULL; fmt = c + 1) {
        pl_str_append_raw(alloc, str, fmt, c - fmt);
        c++;

        char buf[32];
        int len;

#define LOAD(var)                           \
  do {                                      \
      memcpy(&(var), ptr, sizeof(var));     \
      ptr += sizeof(var);                   \
  } while (0)

        switch (c[0]) {
        case '%':
            pl_str_append_raw(alloc, str, c, 1);
            continue;
        case 's': {
            len = strlen((const char *) ptr);
            pl_str_append_raw(alloc, str, ptr, len);
            ptr += len + 1; // also skip \0
            continue;
        }
        case '.': {
            assert(c[1] == '*');
            assert(c[2] == 's');
            LOAD(len);
            pl_str_append_raw(alloc, str, ptr, len);
            ptr += len; // no trailing \0
            c += 2;
            continue;
        }
        case 'c':
            LOAD(buf[0]);
            len = 1;
            break;
        case 'd': ;
            int d;
            LOAD(d);
            len = pl_str_print_int(buf, sizeof(buf), d);
            break;
        case 'h': ;
            assert(c[1] == 'x');
            unsigned short hx;
            LOAD(hx);
            len = pl_str_print_hex(buf, sizeof(buf), hx);
            c++;
            break;
        case 'u': ;
            unsigned u;
            LOAD(u);
            len = pl_str_print_uint(buf, sizeof(buf), u);
            break;
        case 'l':
            assert(c[1] == 'l');
            switch (c[2]) {
            case 'u': ;
                long long unsigned llu;
                LOAD(llu);
                len = pl_str_print_uint64(buf, sizeof(buf), llu);
                break;
            case 'd': ;
                long long int lld;
                LOAD(lld);
                len = pl_str_print_int64(buf, sizeof(buf), lld);
                break;
            default: pl_unreachable();
            }
            c += 2;
            break;
        case 'z': ;
            assert(c[1] == 'u');
            size_t zu;
            LOAD(zu);
            len = pl_str_print_uint64(buf, sizeof(buf), zu);
            c++;
            break;
        case 'f': ;
            double f;
            LOAD(f);
            len = pl_str_print_double(buf, sizeof(buf), f);
            break;
        default:
            fprintf(stderr, "Invalid conversion character: '%c'!\n", c[0]);
            abort();
        }

        pl_str_append_raw(alloc, str, buf, len);
    }
#undef LOAD

    pl_str_append(alloc, str, pl_str0(fmt));
    return (uintptr_t) ptr - (uintptr_t) args;
}