Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/config/Serializer.cpp

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 1 мая 2020 г.
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/config/Serializer.h>
23 #include <lsp-plug.in/io/OutFileStream.h>
24 #include <lsp-plug.in/io/OutSequence.h>
25 #include <lsp-plug.in/io/OutStringSequence.h>
26 #include <lsp-plug.in/stdlib/locale.h>
27
28 namespace lsp
29 {
30 namespace config
31 {
32
33 Serializer::Serializer()
34 {
35 pOut = NULL;
36 nWFlags = 0;
37 }
38
39 Serializer::~Serializer()
40 {
41 do_close();
42 }
43
44 status_t Serializer::write_key(const LSPString *key)
45 {
46 ssize_t idx = -1;
47
48 // Validate key
49 for (size_t i=0, n=key->length(); i<n; ++i)
50 {
51 lsp_wchar_t ch = key->at(i);
52 if (((ch >= 'a') && (ch <= 'z')) ||
53 ((ch >= 'A') && (ch <= 'Z')) ||
54 ((ch >= '0') && (ch <= '9')) ||
55 (ch == '_'))
56 continue;
57 else if (ch == '/')
58 {
59 if (idx < 0)
60 {
61 if (i != 0)
62 return STATUS_INVALID_VALUE;
63 idx = i;
64 }
65 continue;
66 }
67
68 return STATUS_INVALID_VALUE;
69 }
70
71 // Write key
72 status_t res = pOut->write(key);
73 if (res == STATUS_OK)
74 res = pOut->write_ascii(" = ");
75
76 return res;
77 }
78
79 status_t Serializer::write_uint(uint64_t v, size_t flags)
80 {
81 status_t res;
82 char tmp[64];
83
84 ::snprintf(tmp, sizeof(tmp), "%llu", (unsigned long long)v);
85 tmp[sizeof(tmp)-1] = '\0';
86
87 if (flags & SF_QUOTED)
88 {
89 if ((res = pOut->write('\"')) != STATUS_OK)
90 return res;
91 if ((res = pOut->write_ascii(tmp)) != STATUS_OK)
92 return res;
93 res = pOut->write_ascii("\"\n");
94 }
95 else
96 {
97 if ((res = pOut->write_ascii(tmp)) != STATUS_OK)
98 return res;
99 res = pOut->write('\n');
100 }
101
102 return res;
103 }
104
105 status_t Serializer::write_int(int64_t v, size_t flags)
106 {
107 status_t res;
108 char tmp[64];
109
110 snprintf(tmp, sizeof(tmp), "%lld", (long long)v);
111 tmp[sizeof(tmp)-1] = '\0';
112
113 if (flags & SF_QUOTED)
114 {
115 if ((res = pOut->write('\"')) != STATUS_OK)
116 return res;
117 if ((res = pOut->write_ascii(tmp)) != STATUS_OK)
118 return res;
119 res = pOut->write_ascii("\"\n");
120 }
121 else
122 {
123 if ((res = pOut->write_ascii(tmp)) != STATUS_OK)
124 return res;
125 res = pOut->write('\n');
126 }
127
128 return res;
129 }
130
131 status_t Serializer::write_float(double v, size_t flags)
132 {
133 // Save and update locale
134 SET_LOCALE_SCOPED(LC_NUMERIC, "C");
135
136 status_t res;
137 char tmp[64];
138 const char *fmt = "%f";
139
140 // Analyze format
141 if (flags & SF_DECIBELS)
142 {
143 switch (flags & SF_PREC_MASK)
144 {
145 case SF_PREC_SHORT: fmt = "%.1f db"; break;
146 case SF_PREC_LONG: fmt = "%.4f db"; break;
147 case SF_PREC_SCI: fmt = "%e db"; break;
148 default: fmt = "%.2f db"; break;
149 }
150 }
151 else
152 {
153 switch (flags & SF_PREC_MASK)
154 {
155 case SF_PREC_SHORT: fmt = "%.2f"; break;
156 case SF_PREC_LONG: fmt = "%.10f"; break;
157 case SF_PREC_SCI: fmt = "%e"; break;
158 default: fmt = "%.5f"; break;
159 }
160 }
161 ::snprintf(tmp, sizeof(tmp), fmt, v);
162 tmp[sizeof(tmp)-1] = '\0';
163
164 // Write value
165 if (flags & SF_QUOTED)
166 {
167 if ((res = pOut->write('\"')) != STATUS_OK)
168 return res;
169 if ((res = pOut->write_ascii(tmp)) != STATUS_OK)
170 return res;
171 res = pOut->write_ascii("\"\n");
172 }
173 else
174 {
175 if ((res = pOut->write_ascii(tmp)) != STATUS_OK)
176 return res;
177 res = pOut->write('\n');
178 }
179
180 return res;
181 }
182
183 status_t Serializer::write_bool(bool v, size_t flags)
184 {
185 status_t res;
186
187 // Write value
188 if (flags & SF_QUOTED)
189 {
190 if ((res = pOut->write('\"')) != STATUS_OK)
191 return res;
192 if ((res = pOut->write_ascii((v) ? "true" : "false")) != STATUS_OK)
193 return res;
194 res = pOut->write_ascii("\"\n");
195 }
196 else
197 {
198 if ((res = pOut->write_ascii((v) ? "true" : "false")) != STATUS_OK)
199 return res;
200 res = pOut->write('\n');
201 }
202
203 return res;
204 }
205
206 status_t Serializer::open(const char *path, const char *charset)
207 {
208 if (pOut != NULL)
209 return STATUS_BAD_STATE;
210 else if (path == NULL)
211 return STATUS_BAD_ARGUMENTS;
212
213 io::OutFileStream *ofs = new io::OutFileStream();
214 if (ofs == NULL)
215 return STATUS_NO_MEM;
216 status_t res = ofs->open(path, io::File::FM_WRITE | io::File::FM_TRUNC | io::File::FM_CREATE);
217 if (res == STATUS_OK)
218 {
219 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE, charset);
220 if (res == STATUS_OK)
221 return res;
222 ofs->close();
223 }
224 delete ofs;
225
226 return res;
227 }
228
229 status_t Serializer::open(const LSPString *path, const char *charset)
230 {
231 if (pOut != NULL)
232 return STATUS_BAD_STATE;
233 else if (path == NULL)
234 return STATUS_BAD_ARGUMENTS;
235
236 io::OutFileStream *ofs = new io::OutFileStream();
237 if (ofs == NULL)
238 return STATUS_NO_MEM;
239 status_t res = ofs->open(path, io::File::FM_WRITE | io::File::FM_TRUNC | io::File::FM_CREATE);
240 if (res == STATUS_OK)
241 {
242 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE, charset);
243 if (res == STATUS_OK)
244 return res;
245 ofs->close();
246 }
247 delete ofs;
248
249 return res;
250 }
251
252 status_t Serializer::open(const io::Path *path, const char *charset)
253 {
254 if (pOut != NULL)
255 return STATUS_BAD_STATE;
256 else if (path == NULL)
257 return STATUS_BAD_ARGUMENTS;
258
259 io::OutFileStream *ofs = new io::OutFileStream();
260 if (ofs == NULL)
261 return STATUS_NO_MEM;
262 status_t res = ofs->open(path, io::File::FM_WRITE | io::File::FM_TRUNC | io::File::FM_CREATE);
263 if (res == STATUS_OK)
264 {
265 res = wrap(ofs, WRAP_CLOSE | WRAP_DELETE, charset);
266 if (res == STATUS_OK)
267 return res;
268 ofs->close();
269 }
270 delete ofs;
271
272 return res;
273 }
274
275 status_t Serializer::wrap(LSPString *str)
276 {
277 if (pOut != NULL)
278 return STATUS_BAD_STATE;
279 else if (str == NULL)
280 return STATUS_BAD_ARGUMENTS;
281
282 io::OutStringSequence *seq = new io::OutStringSequence(str, false);
283 if (seq == NULL)
284 return STATUS_NO_MEM;
285
286 status_t res = wrap(seq, WRAP_CLOSE | WRAP_DELETE);
287 if (res == STATUS_OK)
288 return res;
289
290 seq->close();
291 delete seq;
292
293 return res;
294 }
295
296 status_t Serializer::wrap(io::IOutStream *os, size_t flags, const char *charset)
297 {
298 if (pOut != NULL)
299 return STATUS_BAD_STATE;
300 else if (os == NULL)
301 return STATUS_BAD_ARGUMENTS;
302
303 io::OutSequence *seq = new io::OutSequence();
304 if (seq == NULL)
305 return STATUS_NO_MEM;
306
307 status_t res = seq->wrap(os, flags, charset);
308 if (res == STATUS_OK)
309 {
310 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
311 return res;
312 seq->close();
313 }
314
315 delete seq;
316 return res;
317 }
318
319 status_t Serializer::wrap(io::IOutSequence *seq, size_t flags)
320 {
321 if (pOut != NULL)
322 return STATUS_BAD_STATE;
323 else if (seq == NULL)
324 return STATUS_BAD_ARGUMENTS;
325
326 pOut = seq;
327 nWFlags = flags;
328
329 return STATUS_OK;
330 }
331
332 status_t Serializer::do_close()
333 {
334 if (pOut == NULL)
335 return STATUS_OK;
336
337 // Close handles
338 status_t res = STATUS_OK;
339 if (nWFlags & WRAP_CLOSE)
340 res = update_status(res, pOut->close());
341 if (nWFlags & WRAP_DELETE)
342 delete pOut;
343
344 pOut = NULL;
345
346 return res;
347 }
348
349 status_t Serializer::close()
350 {
351 return do_close();
352 }
353
354 status_t Serializer::write_comment(const LSPString *v)
355 {
356 if (pOut == NULL)
357 return STATUS_CLOSED;
358
359 status_t res;
360
361 size_t first = 0;
362 while (true)
363 {
364 if ((res = pOut->write_ascii("# ")) != STATUS_OK)
365 return res;
366 ssize_t split = v->index_of(first, '\n');
367 if (split < 0)
368 {
369 if ((res = pOut->write(v, first)) == STATUS_OK)
370 res = pOut->write('\n');
371 return res;
372 }
373 else
374 {
375 if ((res = pOut->write(v, first, split)) != STATUS_OK)
376 return res;
377 if ((res = pOut->write('\n')) != STATUS_OK)
378 return res;
379 first = split + 1;
380 }
381 }
382 }
383
384 status_t Serializer::write_escaped(const LSPString *key, size_t flags)
385 {
386 status_t res = STATUS_OK;
387
388 if (flags & SF_QUOTED)
389 {
390 if ((res = pOut->write('\"')) != STATUS_OK)
391 return res;
392 }
393
394 size_t first = 0, last = 0, len = key->length();
395 while (last < len)
396 {
397 // Analyze character
398 lsp_wchar_t ch = key->char_at(last);
399 switch (ch)
400 {
401 case '\n': ch = 'n'; break;
402 case '\r': ch = 'r'; break;
403 case '\t': ch = 't'; break;
404
405 case '\"':
406 case '\\':
407 break;
408
409 case '#':
410 case ' ':
411 if (!(flags & SF_QUOTED))
412 break;
413 ++last;
414 continue;
415
416 default:
417 ++last;
418 continue;
419 }
420
421 // Emit characters in queue
422 if (first < last)
423 {
424 if ((res = pOut->write(key, first, last)) != STATUS_OK)
425 return res;
426 }
427
428 // Emit escaped character
429 if ((res = pOut->write('\\')) != STATUS_OK)
430 return res;
431 if ((res = pOut->write(ch)) != STATUS_OK)
432 return res;
433 first = ++last;
434 }
435
436 // Write tail
437 if (first < last)
438 res = pOut->write(key, first, last);
439
440 if (flags & SF_QUOTED)
441 {
442 if ((res = pOut->write('\"')) != STATUS_OK)
443 return res;
444 }
445
446 return res;
447 }
448
449 status_t Serializer::write_comment(const char *v)
450 {
451 LSPString tmp;
452 if (!tmp.set_utf8(v))
453 return STATUS_NO_MEM;
454 return write_comment(&tmp);
455 }
456
457 status_t Serializer::writeln()
458 {
459 return (pOut != NULL) ? pOut->write('\n') : STATUS_CLOSED;
460 }
461
462 status_t Serializer::write(const char *key, const value_t *v, size_t flags)
463 {
464 LSPString tmp;
465 if (!tmp.set_utf8(key))
466 return STATUS_NO_MEM;
467 return write(&tmp, v, flags);
468 }
469
470 status_t Serializer::write(const LSPString *key, const value_t *v, size_t flags)
471 {
472 switch (flags & SF_TYPE_MASK)
473 {
474 case SF_TYPE_I32: return write_i32(key, v->i32, flags);
475 case SF_TYPE_U32: return write_u32(key, v->u32, flags);
476 case SF_TYPE_I64: return write_i64(key, v->i64, flags);
477 case SF_TYPE_U64: return write_u64(key, v->u64, flags);
478 case SF_TYPE_F32: return write_f32(key, v->f32, flags);
479 case SF_TYPE_F64: return write_f64(key, v->f64, flags);
480 case SF_TYPE_BOOL: return write_bool(key, v->bval, flags);
481 case SF_TYPE_STR: return write_string(key, v->str, flags);
482 case SF_TYPE_BLOB: return write_blob(key, &v->blob, flags);
483 default: break;
484 }
485
486 return STATUS_BAD_TYPE;
487 }
488
489 status_t Serializer::write(const config::param_t * param)
490 {
491 if (param == NULL)
492 return STATUS_BAD_ARGUMENTS;
493 return write(*param);
494 }
495
496 status_t Serializer::write(const config::param_t & param)
497 {
498 if (param.flags & SF_COMMENT)
499 {
500 status_t res = write_comment(&param.comment);
501 if (res != STATUS_OK)
502 return res;
503 }
504 return write(&param.name, &param.v, param.flags);
505 }
506
507 status_t Serializer::write_i32(const char *key, int32_t value, size_t flags)
508 {
509 LSPString tmp;
510 if (!tmp.set_utf8(key))
511 return STATUS_NO_MEM;
512 return write_i32(&tmp, value, flags);
513 }
514
515 status_t Serializer::write_i32(const LSPString *key, int32_t value, size_t flags)
516 {
517 if (pOut == NULL)
518 return STATUS_CLOSED;
519 status_t res = write_key(key);
520 if (res != STATUS_OK)
521 return res;
522
523 if (flags & SF_TYPE_SET)
524 {
525 if ((res = pOut->write_ascii("i32:")) != STATUS_OK)
526 return res;
527 }
528
529 return write_int(value, flags);
530 }
531
532 status_t Serializer::write_u32(const char *key, uint32_t value, size_t flags)
533 {
534 LSPString tmp;
535 if (!tmp.set_utf8(key))
536 return STATUS_NO_MEM;
537 return write_u32(&tmp, value, flags);
538 }
539
540 status_t Serializer::write_u32(const LSPString *key, uint32_t value, size_t flags)
541 {
542 if (pOut == NULL)
543 return STATUS_CLOSED;
544 status_t res = write_key(key);
545 if (res != STATUS_OK)
546 return res;
547
548 if (flags & SF_TYPE_SET)
549 {
550 if ((res = pOut->write_ascii("u32:")) != STATUS_OK)
551 return res;
552 }
553
554 return write_uint(value, flags);
555 }
556
557 status_t Serializer::write_f32(const char *key, float value, size_t flags)
558 {
559 LSPString tmp;
560 if (!tmp.set_utf8(key))
561 return STATUS_NO_MEM;
562 return write_f32(&tmp, value, flags);
563 }
564
565 status_t Serializer::write_f32(const LSPString *key, float value, size_t flags)
566 {
567 if (pOut == NULL)
568 return STATUS_CLOSED;
569 status_t res = write_key(key);
570 if (res != STATUS_OK)
571 return res;
572
573 if (flags & SF_TYPE_SET)
574 {
575 if ((res = pOut->write_ascii("f32:")) != STATUS_OK)
576 return res;
577 }
578
579 return write_float(value, flags);
580 }
581
582 status_t Serializer::write_i64(const char *key, int64_t value, size_t flags)
583 {
584 LSPString tmp;
585 if (!tmp.set_utf8(key))
586 return STATUS_NO_MEM;
587 return write_i64(&tmp, value, flags);
588 }
589
590 status_t Serializer::write_i64(const LSPString *key, int64_t value, size_t flags)
591 {
592 if (pOut == NULL)
593 return STATUS_CLOSED;
594 status_t res = write_key(key);
595 if (res != STATUS_OK)
596 return res;
597
598 if (flags & SF_TYPE_SET)
599 {
600 if ((res = pOut->write_ascii("i64:")) != STATUS_OK)
601 return res;
602 }
603
604 return write_int(value, flags);
605 }
606
607 status_t Serializer::write_u64(const char *key, uint64_t value, size_t flags)
608 {
609 LSPString tmp;
610 if (!tmp.set_utf8(key))
611 return STATUS_NO_MEM;
612 return write_u64(&tmp, value, flags);
613 }
614
615 status_t Serializer::write_u64(const LSPString *key, uint64_t value, size_t flags)
616 {
617 if (pOut == NULL)
618 return STATUS_CLOSED;
619 status_t res = write_key(key);
620 if (res != STATUS_OK)
621 return res;
622
623 if (flags & SF_TYPE_SET)
624 {
625 if ((res = pOut->write_ascii("u64:")) != STATUS_OK)
626 return res;
627 }
628
629 return write_uint(value, flags);
630 }
631
632 status_t Serializer::write_f64(const char *key, double value, size_t flags)
633 {
634 LSPString tmp;
635 if (!tmp.set_utf8(key))
636 return STATUS_NO_MEM;
637 return write_f64(&tmp, value, flags);
638 }
639
640 status_t Serializer::write_f64(const LSPString *key, double value, size_t flags)
641 {
642 if (pOut == NULL)
643 return STATUS_CLOSED;
644 status_t res = write_key(key);
645 if (res != STATUS_OK)
646 return res;
647
648 if (flags & SF_TYPE_SET)
649 {
650 if ((res = pOut->write_ascii("f64:")) != STATUS_OK)
651 return res;
652 }
653
654 return write_float(value, flags);
655 }
656
657 status_t Serializer::write_bool(const char *key, bool value, size_t flags)
658 {
659 LSPString tmp;
660 if (!tmp.set_utf8(key))
661 return STATUS_NO_MEM;
662 return write_bool(&tmp, value, flags);
663 }
664
665 status_t Serializer::write_bool(const LSPString *key, bool value, size_t flags)
666 {
667 if (pOut == NULL)
668 return STATUS_CLOSED;
669 status_t res = write_key(key);
670 if (res != STATUS_OK)
671 return res;
672
673 if (flags & SF_TYPE_SET)
674 {
675 if ((res = pOut->write_ascii("bool:")) != STATUS_OK)
676 return res;
677 }
678
679 return write_bool(value, flags);
680 }
681
682 status_t Serializer::write_string_impl(const LSPString *key, const LSPString *v, size_t flags)
683 {
684 if (pOut == NULL)
685 return STATUS_CLOSED;
686 status_t res = write_key(key);
687 if (res != STATUS_OK)
688 return res;
689
690 if (flags & SF_TYPE_SET)
691 {
692 if ((res = pOut->write_ascii("str:")) != STATUS_OK)
693 return res;
694 }
695
696 if ((res = write_escaped(v, flags)) != STATUS_OK)
697 return res;
698
699 return pOut->write('\n');
700 }
701
702 status_t Serializer::write_string(const LSPString *key, const LSPString *v, size_t flags)
703 {
704 return write_string_impl(key, v, flags);
705 }
706
707 status_t Serializer::write_string(const LSPString *key, const char *v, size_t flags)
708 {
709 LSPString tmp;
710 if (!tmp.set_utf8(v))
711 return STATUS_NO_MEM;
712 return write_string_impl(key, &tmp, flags);
713 }
714
715 status_t Serializer::write_string(const char *key, const LSPString *v, size_t flags)
716 {
717 LSPString tmp;
718 if (!tmp.set_utf8(key))
719 return STATUS_NO_MEM;
720 return write_string_impl(&tmp, v, flags);
721 }
722
723 status_t Serializer::write_string(const char *key, const char *v, size_t flags)
724 {
725 LSPString tmp1, tmp2;
726 if ((!tmp1.set_utf8(key)) || (!tmp2.set_utf8(v)))
727 return STATUS_NO_MEM;
728 return write_string_impl(&tmp1, &tmp2, flags);
729 }
730
731 status_t Serializer::write_blob(const LSPString *key, const blob_t *v, size_t flags)
732 {
733 if (pOut == NULL)
734 return STATUS_CLOSED;
735 status_t res = write_key(key);
736 if (res != STATUS_OK)
737 return res;
738
739 if ((res = pOut->write_ascii("blob:")) != STATUS_OK)
740 return res;
741 if ((res = pOut->write('\"')) != STATUS_OK)
742 return res;
743
744 // Write blob data
745 LSPString tmp;
746 if (v->ctype != NULL)
747 {
748 if (!tmp.set_utf8(v->ctype))
749 return STATUS_NO_MEM;
750 }
751 if (!tmp.append(':'))
752 return STATUS_NO_MEM;
753 if (!tmp.fmt_append_ascii("%llu:", (unsigned long long)v->length))
754 return STATUS_NO_MEM;
755 if ((res = write_escaped(&tmp, 0)) != STATUS_OK)
756 return res;
757 if (!tmp.set_utf8(v->data))
758 return STATUS_NO_MEM;
759 if ((res = write_escaped(&tmp, 0)) != STATUS_OK)
760 return res;
761 res = pOut->write_ascii("\"\n");
762
763 return res;
764 }
765
766 status_t Serializer::write_blob(const char *key, const blob_t *v, size_t flags)
767 {
768 LSPString tmp;
769 if (!tmp.set_utf8(key))
770 return STATUS_NO_MEM;
771 return write_blob(&tmp, v, flags);
772 }
773
774 } /* namespace config */
775 } /* namespace lsp */