Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/sfz/DocumentProcessor.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: 23 февр. 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/common/debug.h>
23 #include <lsp-plug.in/fmt/sfz/DocumentProcessor.h>
24
25 namespace lsp
26 {
27 namespace sfz
28 {
29 DocumentProcessor::DocumentProcessor()
30 {
31 nScopes = -1;
32 pScope = NULL;
33
34 for (size_t i=0; i<SC_TOTAL; ++i)
35 {
36 scope_data_t *s = &vScopes[i];
37 s->enType = SC_NONE;
38 s->enOther = OT_CUSTOM;
39 s->sName = NULL;
40 s->pPrev = NULL;
41 }
42 }
43
44 DocumentProcessor::~DocumentProcessor()
45 {
46 close();
47 }
48
49 status_t DocumentProcessor::open(const char *path)
50 {
51 if (nScopes >= 0)
52 return STATUS_OPENED;
53
54 io::Path tmp;
55 status_t res = tmp.set(path);
56 if (res == STATUS_OK)
57 res = open(&tmp);
58
59 return res;
60 }
61
62 status_t DocumentProcessor::open(const LSPString *path)
63 {
64 if (nScopes >= 0)
65 return STATUS_OPENED;
66
67 io::Path tmp;
68 status_t res = tmp.set(path);
69 if (res == STATUS_OK)
70 res = open(&tmp);
71
72 return res;
73 }
74
75 status_t DocumentProcessor::open(const io::Path *path)
76 {
77 if (nScopes >= 0)
78 return STATUS_OPENED;
79
80 // Get last name of the file path
81 LSPString last;
82 status_t res = path->get_last(&last);
83 if (res != STATUS_OK)
84 return res;
85
86 // Create and open parser
87 PullParser *pp = new PullParser();
88 if (pp == NULL)
89 return STATUS_NO_MEM;
90 lsp_finally {
91 if (pp != NULL)
92 {
93 pp->close();
94 delete pp;
95 }
96 };
97 if ((res = pp->open(path)) != STATUS_OK)
98 return res;
99
100 // Create the document entry
101 document_t *doc = create_document();
102 if (doc == NULL)
103 return STATUS_NO_MEM;
104 lsp_finally { destroy_document(doc); };
105
106 // Initialize document
107 if ((doc->sPath = last.clone_utf8()) == NULL)
108 return STATUS_NO_MEM;
109 if (!vTree.add(doc))
110 return STATUS_NO_MEM;
111 lsp::swap(doc->pParser, pp);
112 doc->nWFlags = WRAP_CLOSE | WRAP_DELETE;
113 doc = NULL;
114
115 // Mark processor open
116 nScopes = 0;
117
118 return res;
119 }
120
121 status_t DocumentProcessor::wrap(const char *str)
122 {
123 if (nScopes >= 0)
124 return STATUS_OPENED;
125 PullParser *pp = new PullParser();
126 if (pp == NULL)
127 return STATUS_NO_MEM;
128 status_t res = pp->wrap(str);
129 if (res != STATUS_OK)
130 {
131 pp->close();
132 delete pp;
133 return res;
134 }
135 res = wrap(pp, WRAP_CLOSE | WRAP_DELETE);
136 if (res != STATUS_OK)
137 {
138 pp->close();
139 delete pp;
140 }
141 return res;
142 }
143
144 status_t DocumentProcessor::wrap(const void *buf, size_t len)
145 {
146 if (nScopes >= 0)
147 return STATUS_OPENED;
148 PullParser *pp = new PullParser();
149 if (pp == NULL)
150 return STATUS_NO_MEM;
151 status_t res = pp->wrap(buf, len);
152 if (res != STATUS_OK)
153 {
154 pp->close();
155 delete pp;
156 return res;
157 }
158 res = wrap(pp, WRAP_CLOSE | WRAP_DELETE);
159 if (res != STATUS_OK)
160 {
161 pp->close();
162 delete pp;
163 }
164 return res;
165 }
166
167 status_t DocumentProcessor::wrap(const LSPString *str)
168 {
169 if (nScopes >= 0)
170 return STATUS_OPENED;
171 PullParser *pp = new PullParser();
172 if (pp == NULL)
173 return STATUS_NO_MEM;
174 status_t res = pp->wrap(str);
175 if (res != STATUS_OK)
176 {
177 pp->close();
178 delete pp;
179 return res;
180 }
181 res = wrap(pp, WRAP_CLOSE | WRAP_DELETE);
182 if (res != STATUS_OK)
183 {
184 pp->close();
185 delete pp;
186 }
187 return res;
188 }
189
190 status_t DocumentProcessor::wrap(io::IInStream *is, size_t flags)
191 {
192 if (nScopes >= 0)
193 return STATUS_OPENED;
194 PullParser *pp = new PullParser();
195 if (pp == NULL)
196 return STATUS_NO_MEM;
197 status_t res = pp->wrap(is, flags);
198 if (res != STATUS_OK)
199 {
200 pp->close();
201 delete pp;
202 return res;
203 }
204 res = wrap(pp, WRAP_CLOSE | WRAP_DELETE);
205 if (res != STATUS_OK)
206 {
207 pp->close();
208 delete pp;
209 }
210
211 return res;
212 }
213
214 status_t DocumentProcessor::wrap(PullParser *parser, size_t flags)
215 {
216 if (nScopes >= 0)
217 return STATUS_OPENED;
218
219 document_t *doc = create_document();
220 if (doc == NULL)
221 return STATUS_NO_MEM;
222 lsp_finally { destroy_document(doc); };
223
224 // Initialize document
225 if (!vTree.add(doc))
226 return STATUS_NO_MEM;
227 doc->pParser = parser;
228 doc->nWFlags = flags;
229 doc->sPath = NULL;
230 doc = NULL;
231
232 // Mark processor open
233 nScopes = 0;
234
235 return STATUS_OK;
236 }
237
238 status_t DocumentProcessor::close()
239 {
240 if (nScopes < 0)
241 return STATUS_OK;
242
243 // Destroy documents
244 status_t res = STATUS_OK;
245 for (size_t i=vTree.size(); i > 0; )
246 res = update_status(res, destroy_document(vTree.uget(--i)));
247 vTree.flush();
248
249 // Destroy defined variables if present
250 drop_hash(&vVars);
251
252 // Cleanup scopes
253 for (size_t i=0; i<SC_TOTAL; ++i)
254 clear_scope(&vScopes[i]);
255
256 // Reset scope and open flag
257 nScopes = -1;
258 pScope = NULL;
259
260 return res;
261 }
262
263 status_t DocumentProcessor::process_header(IDocumentHandler *handler, event_t *ev)
264 {
265 // Obtain the type and sub-type of the scope
266 scope_t scope = SC_OTHER;
267 other_t other = OT_CUSTOM;
268
269 if (ev->name.equals_ascii(HDR_REGION))
270 scope = SC_REGION;
271 else if (ev->name.equals_ascii(HDR_GROUP))
272 scope = SC_GROUP;
273 else if (ev->name.equals_ascii(HDR_MASTER))
274 scope = SC_MASTER;
275 else if (ev->name.equals_ascii(HDR_GLOBAL))
276 scope = SC_GLOBAL;
277 else if (ev->name.equals_ascii(HDR_CONTROL))
278 scope = SC_CONTROL;
279 else
280 {
281 if (ev->name.equals_ascii(HDR_CURVE))
282 other = OT_CURVE;
283 else if (ev->name.equals_ascii(HDR_EFFECT))
284 other = OT_EFFECT;
285 else if (ev->name.equals_ascii(HDR_SAMPLE))
286 other = OT_SAMPLE;
287 else if (ev->name.equals_ascii(HDR_MIDI))
288 other = OT_MIDI;
289 }
290
291 // Switch to the desired scope
292 status_t res = switch_scope(handler, scope);
293 if (res != STATUS_OK)
294 return res;
295
296 // Complete the scope
297 pScope->enOther = other;
298 if ((scope == SC_OTHER) && (other == OT_CUSTOM))
299 {
300 if ((pScope->sName = ev->name.clone_utf8()) == NULL)
301 return STATUS_NO_MEM;
302 }
303
304 return STATUS_OK;
305 }
306
307 status_t DocumentProcessor::process_opcode(event_t *ev)
308 {
309 // We can not do anything without the scope
310 if (pScope == NULL)
311 return STATUS_CORRUPTED;
312
313 // Process the opcode and the value
314 char *opcode = process_value(&ev->name);
315 if (opcode == NULL)
316 return STATUS_NO_MEM;
317 lsp_finally { free(opcode); };
318
319 char *value = process_value(&ev->value);
320 if (value == NULL)
321 return STATUS_NO_MEM;
322 if (!pScope->vAllocated.add(value))
323 {
324 free(value);
325 return STATUS_NO_MEM;
326 }
327
328 // Put the opcode to the list
329 char *old_value = NULL;
330 if (!pScope->vOpcodes.put(opcode, value, &old_value))
331 return STATUS_NO_MEM;
332
333 return STATUS_OK;
334 }
335
336 status_t DocumentProcessor::process_include(IDocumentHandler *handler, event_t *ev)
337 {
338 // Obtain the name of the file
339 const char *fname = ev->name.get_utf8();
340 if (fname == NULL)
341 return STATUS_NO_MEM;
342
343 // Ensure that file wasn't previously present
344 for (size_t i=0, n=vTree.size(); i<n; ++i)
345 {
346 document_t *doc = vTree.uget(i);
347 const char *path = (doc->sPath != NULL) ? doc->sPath : handler->root_file_name();
348 if (path == NULL)
349 continue;
350 if (strcmp(fname, path) == 0)
351 return STATUS_OVERFLOW;
352 }
353
354 // Now we can allocate new document
355 document_t *doc = create_document();
356 if (doc == NULL)
357 return STATUS_NO_MEM;
358 lsp_finally { destroy_document(doc); };
359
360 // Save file name
361 if ((doc->sPath = strdup(fname)) == NULL)
362 return STATUS_NO_MEM;
363
364 // Allocate parser
365 if ((doc->pParser = new PullParser()) == NULL)
366 return STATUS_NO_MEM;
367 doc->nWFlags = WRAP_CLOSE | WRAP_DELETE;
368
369 // Call the handler for opening the file
370 status_t res = handler->include(doc->pParser, doc->sPath);
371 if (res != STATUS_OK)
372 return res;
373
374 // Put the document to the tree
375 if (!vTree.push(doc))
376 return STATUS_NO_MEM;
377
378 // Return success
379 doc = NULL;
380
381 return STATUS_OK;
382 }
383
384 status_t DocumentProcessor::process_define(IDocumentHandler *handler, document_t *doc, event_t *ev)
385 {
386 const char *var_name = ev->name.get_utf8(1);
387 if (var_name == NULL)
388 return STATUS_NO_MEM;
389
390 char *var_value = ev->value.clone_utf8();
391 if (var_value == NULL)
392 return STATUS_NO_MEM;
393 lsp_finally {
394 if (var_value != NULL)
395 free(var_value);
396 };
397
398 return (vVars.put(var_name, var_value, &var_value)) ? STATUS_OK : STATUS_NO_MEM;
399 }
400
401 status_t DocumentProcessor::process_sample_data(IDocumentHandler *handler, event_t *ev)
402 {
403 // This should happen only for specific state
404 if ((pScope == NULL) || (pScope->enType != SC_OTHER) || (pScope->enOther != OT_SAMPLE))
405 return STATUS_CORRUPTED;
406
407 // Already exists?
408 if (((pScope->sName != NULL) || (pScope->sData.references()) > 0))
409 return STATUS_CORRUPTED;
410
411 // Bind the sample name
412 if ((pScope->sName = ev->name.clone_utf8()) == NULL)
413 return STATUS_NO_MEM;
414 pScope->sData.wrap(ev->blob);
415
416 return STATUS_OK;
417 }
418
419 status_t DocumentProcessor::main_loop(IDocumentHandler *handler)
420 {
421 // Main stuff
422 status_t res;
423 event_t ev;
424 while (!vTree.is_empty())
425 {
426 // Obtain the current document
427 document_t *doc = vTree.last();
428 if (doc == NULL)
429 return STATUS_CORRUPTED;
430
431 // Fetch next event
432 if ((res = doc->pParser->next(&ev)) != STATUS_OK)
433 {
434 // If not end of file, then we have troubles
435 if (res != STATUS_EOF)
436 return res;
437
438 // End of file, close current document first
439 res = destroy_document(doc);
440 if (!vTree.pop())
441 {
442 if (res == STATUS_OK)
443 res = STATUS_NO_MEM;
444 }
445 if (res != STATUS_OK)
446 return res;
447
448 // Then, continue the loop
449 continue;
450 }
451
452 // Dispatch event
453 switch (ev.type)
454 {
455 case EVENT_COMMENT:
456 res = STATUS_OK;
457 break;
458 case EVENT_HEADER:
459 res = process_header(handler, &ev);
460 break;
461 case EVENT_OPCODE:
462 res = process_opcode(&ev);
463 break;
464 case EVENT_INCLUDE:
465 res = process_include(handler, &ev);
466 break;
467 case EVENT_DEFINE:
468 res = process_define(handler, doc, &ev);
469 break;
470 case EVENT_SAMPLE:
471 res = process_sample_data(handler, &ev);
472 break;
473 case EVENT_NONE:
474 default:
475 res = STATUS_CORRUPTED;
476 break;
477 }
478
479 // Dispatch event
480 if (res != STATUS_OK)
481 return res;
482 }
483
484 return STATUS_OK;
485 }
486
487 status_t DocumentProcessor::process(IDocumentHandler *handler)
488 {
489 // Check status and arguments
490 if (handler == NULL)
491 return STATUS_BAD_ARGUMENTS;
492 if (nScopes < 0)
493 return STATUS_CLOSED;
494
495 // Notify handler about the start of document
496 status_t res = handler->begin();
497 if (res != STATUS_OK)
498 return res;
499 lsp_finally {
500 if (handler != NULL)
501 handler->end(res);
502 };
503
504 // Perform main loop and cleanup
505 res = main_loop(handler);
506 if (res == STATUS_OK)
507 res = switch_scope(handler, SC_NONE);
508
509 // Notify handler about end of document
510 status_t res2 = handler->end(res);
511 handler = NULL;
512 return (res == STATUS_OK) ? res2 : res;
513 }
514
515 DocumentProcessor::document_t *DocumentProcessor::create_document()
516 {
517 document_t *doc = new document_t;
518 doc->sPath = NULL;
519 doc->pParser = NULL;
520 doc->nWFlags = WRAP_NONE;
521
522 return doc;
523 }
524
525 status_t DocumentProcessor::destroy_document(document_t *document)
526 {
527 status_t res = STATUS_OK;
528 if (document == NULL)
529 return res;
530
531 // Destroy parser if present
532 if (document->pParser)
533 {
534 if (document->nWFlags & WRAP_CLOSE)
535 res = document->pParser->close();
536 if (document->nWFlags & WRAP_DELETE)
537 delete document->pParser;
538
539 document->pParser = NULL;
540 document->nWFlags = WRAP_NONE;
541 }
542
543 // Destroy path if present
544 if (document->sPath != NULL)
545 {
546 free(document->sPath);
547 document->sPath = NULL;
548 }
549
550 // Delete the document
551 delete document;
552
553 return res;
554 }
555
556 status_t DocumentProcessor::init_scope(scope_data_t *scope, scope_t type, scope_data_t *prev)
557 {
558 clear_scope(scope);
559 scope->enType = type;
560 scope->pPrev = prev;
561
562 if (prev == NULL)
563 return STATUS_OK;
564
565 // Do not copy contents from the CONTROL scope or to ANY scope
566 if ((prev->enType == SC_CONTROL) || (prev->enType == SC_OTHER))
567 return STATUS_OK;
568
569 // Copy the opcodes from the source scope
570 lltl::parray<char> vk, vv;
571 if (!prev->vOpcodes.items(&vk, &vv))
572 return STATUS_NO_MEM;
573
574 for (size_t i=0, n=vk.size(); i<n; ++i)
575 {
576 char *k = vk.uget(i);
577 char *v = vv.uget(i);
578 if ((k == NULL) || (v == NULL))
579 return STATUS_CORRUPTED;
580 if (!scope->vOpcodes.create(k, v))
581 {
582 clear_scope(scope);
583 return STATUS_NO_MEM;
584 }
585 }
586
587 return STATUS_OK;
588 }
589
590 void DocumentProcessor::clear_scope(scope_data_t *scope)
591 {
592 if (scope == NULL)
593 return;
594
595 scope->vOpcodes.clear();
596 for (size_t i=0, n=scope->vAllocated.size(); i<n; ++i)
597 {
598 char *str = scope->vAllocated.uget(i);
599 if (str != NULL)
600 free(str);
601 }
602 scope->vAllocated.clear();
603 if (scope->sName!= NULL)
604 {
605 free(scope->sName);
606 scope->sName = NULL;
607 }
608
609 scope->sData.close();
610
611 scope->enType = SC_NONE;
612 scope->enOther = OT_CUSTOM;
613 scope->pPrev = NULL;
614 }
615
616 void DocumentProcessor::drop_hash(lltl::pphash<char, char> *data)
617 {
618 if (data == NULL)
619 return;
620
621 lltl::parray<char> vv;
622 data->values(&vv);
623 data->flush();
624
625 for (size_t i=0, n=vv.size(); i<n; ++i)
626 {
627 char *str = vv.uget(i);
628 if (str != NULL)
629 free(str);
630 }
631 vv.flush();
632 }
633
634 DocumentProcessor::scope_t DocumentProcessor::get_scope(const LSPString *header)
635 {
636 if (header->equals_ascii(HDR_CONTROL))
637 return SC_CONTROL;
638 if (header->equals_ascii(HDR_GLOBAL))
639 return SC_GLOBAL;
640 if (header->equals_ascii(HDR_MASTER))
641 return SC_MASTER;
642 if (header->equals_ascii(HDR_GROUP))
643 return SC_GROUP;
644 if (header->equals_ascii(HDR_REGION))
645 return SC_REGION;
646
647 return SC_OTHER;
648 }
649
650 status_t DocumentProcessor::dispatch_scope(IDocumentHandler *handler, scope_data_t *scope)
651 {
652 switch (scope->enType)
653 {
654 case SC_NONE:
655 case SC_GLOBAL:
656 case SC_MASTER:
657 case SC_GROUP:
658 return STATUS_OK;
659 default:
660 break;
661 }
662
663 // Prepare lists of opcodes
664 lltl::parray<char> vk, vv;
665 if (!scope->vOpcodes.items(&vk, &vv))
666 return STATUS_NO_MEM;
667 if (!vk.add(static_cast<char *>(NULL)))
668 return STATUS_NO_MEM;
669 if (!vv.add(static_cast<char *>(NULL)))
670 return STATUS_NO_MEM;
671
672 const char **opcodes = const_cast<const char **>(vk.array());
673 const char **values = const_cast<const char **>(vv.array());
674
675 // Process main scopes
676 switch (scope->enType)
677 {
678 case SC_CONTROL:
679 return handler->control(opcodes, values);
680 case SC_REGION:
681 return handler->region(opcodes, values);
682 default: // SC_OTHER
683 break;
684 }
685
686 // Process sub-types of SC_OTHER scope
687 switch (scope->enOther)
688 {
689 case OT_SAMPLE:
690 return handler->sample(scope->sName, &scope->sData, opcodes, values);
691 case OT_MIDI:
692 return handler->midi(opcodes, values);
693 case OT_CURVE:
694 return handler->curve(opcodes, values);
695 case OT_EFFECT:
696 return handler->effect(opcodes, values);
697 default: // OT_CUSTOM
698 break;
699 }
700
701 return handler->custom_header(scope->sName, opcodes, values);
702 }
703
704 status_t DocumentProcessor::switch_scope(IDocumentHandler *handler, scope_t scope)
705 {
706 status_t res;
707
708 // Walk back if there is necessity
709 while ((pScope != NULL) && (pScope->enType >= scope))
710 {
711 // Call the dispatcher
712 if (pScope->enType != SC_CONTROL)
713 {
714 if ((res = dispatch_scope(handler, pScope)) != STATUS_OK)
715 return res;
716 }
717
718 // Free the scope
719 scope_data_t *prev = pScope->pPrev;
720 clear_scope(pScope);
721 --nScopes;
722 pScope = prev;
723 }
724
725 // Allocate new scope and link to previous
726 if (nScopes >= SC_TOTAL)
727 return STATUS_OVERFLOW;
728
729 // Special case: we're switching from SC_CONTROL scope
730 if ((pScope != NULL) && (pScope->enType == SC_CONTROL))
731 {
732 // Call the dispatcher
733 if ((res = dispatch_scope(handler, pScope)) != STATUS_OK)
734 return res;
735 }
736
737 // Check that we switched to SC_NONE scope
738 if (scope == SC_NONE)
739 return STATUS_OK;
740
741 // Allocate new scope
742 scope_data_t *s = &vScopes[nScopes++];
743 if ((res = init_scope(s, scope, pScope)) != STATUS_OK)
744 {
745 --nScopes;
746 clear_scope(s);
747 return res;
748 }
749
750 // Update the current scope pointer
751 pScope = s;
752
753 return STATUS_OK;
754 }
755
756 char *DocumentProcessor::process_value(const LSPString *value)
757 {
758 LSPString res;
759
760 for (size_t i=0, n=value->length(); i < n; )
761 {
762 lsp_wchar_t ch = value->char_at(i++);
763 if (ch == '$')
764 {
765 // Substitute variable
766 size_t first = i;
767 bool found = false;
768 while (i < n)
769 {
770 // New variable name?
771 char ch = value->char_at(i);
772 if (ch == '$')
773 break;
774
775 // Find the variable and substitute if it was found
776 const char *var = value->get_utf8(first, ++i);
777 const char *val = vVars.get(var);
778 if (val != NULL)
779 {
780 if (!res.append_utf8(val))
781 return NULL;
782 found = true;
783 break;
784 }
785 }
786
787 // No substitution was made?
788 if (!found)
789 {
790 if (!res.append(value, first-1, i))
791 return NULL;
792 }
793 }
794 else
795 {
796 if (!res.append(ch))
797 return NULL;
798 }
799 }
800
801 return res.clone_utf8();
802 }
803
804 } /* namespace sfz */
805 } /* namespace lsp */
806
807
808
809