Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/sfz/parse.cpp

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 2 мар. 2023 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/fmt/sfz/parse.h>
23
24 #include <lsp-plug.in/stdlib/locale.h>
25 #include <lsp-plug.in/stdlib/stdlib.h>
26 #include <lsp-plug.in/stdlib/string.h>
27
28 #include <errno.h>
29
30 namespace lsp
31 {
32 namespace sfz
33 {
34 static const char *skip_blank(const char *src, size_t required = 0)
35 {
36 size_t skipped = 0;
37
38 while (true)
39 {
40 switch (*src)
41 {
42 case ' ':
43 case '\n':
44 case '\r':
45 case '\t':
46 case '\v':
47 ++skipped;
48 ++src;
49 break;
50 default:
51 return (skipped >= required) ? src : NULL;
52 }
53 }
54 }
55
56 static inline char tolower(char c)
57 {
58 return ((c >= 'A') && (c <= 'Z')) ? c + 'a' - 'A' : c;
59 }
60
61 static bool check_match(const char *s, const char *pat)
62 {
63 for (;; ++s, ++pat)
64 {
65 if (*s == '\0')
66 return *pat == '\0';
67 else if (*pat == '\0')
68 return true;
69 else if (tolower(*s) != tolower(*pat))
70 return false;
71 }
72 return true;
73 }
74
75 status_t parse_bool(bool *dst, const char *text)
76 {
77 text = skip_blank(text);
78 ssize_t value = 0;
79
80 if (check_match(text, "true"))
81 {
82 value = 1;
83 text += 4;
84 }
85 else if (check_match(text, "on"))
86 {
87 value = 1;
88 text += 2;
89 }
90 else if (check_match(text, "yes"))
91 {
92 value = 1;
93 text += 3;
94 }
95 else if (check_match(text, "t"))
96 {
97 value = 1;
98 text += 1;
99 }
100 else if (check_match(text, "false"))
101 {
102 value = 0;
103 text += 5;
104 }
105 else if (check_match(text, "off"))
106 {
107 value = 0;
108 text += 3;
109 }
110 else if (check_match(text, "no"))
111 {
112 value = 0;
113 text += 2;
114 }
115 else if (check_match(text, "f"))
116 {
117 value = 0;
118 text += 1;
119 }
120 else
121 {
122 status_t res = parse_int(&value, text);
123 if (res != STATUS_OK)
124 return res;
125 }
126
127 // Check that there is no data at the end
128 text = skip_blank(text);
129 if (*text != '\0')
130 return STATUS_INVALID_VALUE;
131
132 // Return result
133 if (dst != NULL)
134 *dst = value != 0;
135 return STATUS_OK;
136 }
137
138 status_t parse_int(ssize_t *dst, const char *text)
139 {
140 // Update locale
141 SET_LOCALE_SCOPED(LC_NUMERIC, "C");
142
143 // Parse the integer value
144 errno = 0;
145 char *end = NULL;
146 long value = ::strtol(text, &end, 10);
147 if ((errno != 0) || (end == text))
148 return STATUS_INVALID_VALUE;
149 text = skip_blank(end);
150
151 // Check that we reached the end of string
152 if (*text != '\0')
153 return STATUS_INVALID_VALUE;
154
155 // Return the result
156 if (dst != NULL)
157 *dst = value;
158 return STATUS_OK;
159 }
160
161 status_t parse_float(float *dst, const char *text)
162 {
163 // Update locale
164 SET_LOCALE_SCOPED(LC_NUMERIC, "C");
165
166 // Parse the floating-point value
167 errno = 0;
168 char *end = NULL;
169 float value = ::strtof(text, &end);
170 if ((errno != 0) || (end == text))
171 return STATUS_INVALID_VALUE;
172 text = skip_blank(end);
173
174 // Check that we reached the end of string
175 if (*text != '\0')
176 return STATUS_INVALID_VALUE;
177
178 // Return the result
179 if (dst != NULL)
180 *dst = value;
181 return STATUS_OK;
182 }
183
184 static status_t parse_note_name(ssize_t *dst, const char *text)
185 {
186 text = skip_blank(text);
187
188 // Parse the note name
189 int note;
190 switch (*(text++))
191 {
192 case 'c': case 'C': note = 0; break;
193 case 'd': case 'D': note = 2; break;
194 case 'e': case 'E': note = 4; break;
195 case 'f': case 'F': note = 5; break;
196 case 'g': case 'G': note = 7; break;
197 case 'a': case 'A': note = 9; break;
198 case 'b': case 'B': note = 11; break;
199 case 'h': case 'H': note = 11; break;
200 default:
201 return STATUS_INVALID_VALUE;
202 }
203
204 // Check alterations
205 if (*text == '#')
206 {
207 ++text;
208 ++note;
209
210 // Double alteration?
211 if (*text == '#')
212 {
213 ++text;
214 ++note;
215 }
216 }
217 else if (*text == 'b')
218 {
219 ++text;
220 --note;
221
222 // Double alteration?
223 if (*text == 'b')
224 {
225 ++text;
226 --note;
227 }
228 }
229
230 // Parse the octave (if specified)
231 text = skip_blank(text);
232 errno = 0;
233 char *end = NULL;
234 long octave = ::strtol(text, &end, 10);
235 if ((errno != 0) || (end == text))
236 {
237 // Unsuccessful parse?
238 if (end != text)
239 return STATUS_INVALID_VALUE;
240 octave = 4;
241 }
242
243 // Check that there are no extra characters at the end
244 text = skip_blank(end);
245 if (*text != '\0')
246 return STATUS_INVALID_VALUE;
247
248 if (dst != NULL)
249 *dst = (octave + 1) * 12 + note;
250 return STATUS_OK;
251 }
252
253 status_t parse_note(ssize_t *dst, const char *text)
254 {
255 ssize_t note = 0;
256
257 // Parse the value
258 status_t res = parse_note_name(&note, text);
259 if (res != STATUS_OK)
260 res = parse_int(&note, text);
261
262 // Validate the result
263 if (res != STATUS_OK)
264 return res;
265 if ((note < 0) || (note > 127))
266 return STATUS_INVALID_VALUE;
267
268 // Return result
269 if (dst != NULL)
270 *dst = note;
271 return res;
272 }
273
274 } /* namespace sfz */
275 } /* namespace lsp */
276