Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/fmt/sfz/DocumentProcessor.h

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 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 #ifndef LSP_PLUG_IN_FMT_SFZ_DOCUMENTPROCESSOR_H_
23 #define LSP_PLUG_IN_FMT_SFZ_DOCUMENTPROCESSOR_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26
27 #include <lsp-plug.in/fmt/sfz/IDocumentHandler.h>
28 #include <lsp-plug.in/fmt/sfz/PullParser.h>
29 #include <lsp-plug.in/io/InSharedMemoryStream.h>
30 #include <lsp-plug.in/lltl/parray.h>
31 #include <lsp-plug.in/lltl/pphash.h>
32
33 namespace lsp
34 {
35 namespace sfz
36 {
37 /**
38 * SFZ document processor. Allows to perform easy context-aware processing of the SFZ document.
39 * The settings for headers <global>, <master>, <group> become merged into the <region> seciton.
40 * This allows to simplify parse due no need of parsing <global>, <master> and <group> headers
41 * by the document handler.
42 */
43 class LSP_RUNTIME_LIB_PUBLIC DocumentProcessor
44 {
45 protected:
46 enum scope_t
47 {
48 SC_NONE, // No scope
49 SC_CONTROL, // Control scope
50 SC_GLOBAL, // Global scope
51 SC_MASTER, // Master scope
52 SC_GROUP, // Group scope
53 SC_REGION, // Region scope
54 SC_OTHER, // Any other scope
55
56 SC_TOTAL
57 };
58
59 enum other_t
60 {
61 OT_SAMPLE,
62 OT_MIDI,
63 OT_CURVE,
64 OT_EFFECT,
65 OT_CUSTOM
66 };
67
68 typedef struct document_t
69 {
70 char *sPath; // Path to the original file or NULL
71 PullParser *pParser; // Parser of the associated file
72 size_t nWFlags; // Wrapping flags for the processor
73 } document_t;
74
75 typedef struct scope_data_t
76 {
77 lltl::pphash<char, char> vOpcodes; // Opcodes
78 lltl::parray<char> vAllocated; // Allocated opcodes
79
80 scope_t enType; // Type of scope
81 other_t enOther; // The sub-type of SC_OTHER scope
82 char *sName; // The name of header or of the sample
83 io::InSharedMemoryStream sData; // The sample data for <sample>
84 scope_data_t *pPrev; // Pervious scope
85 } scope_data_t;
86
87 protected:
88 lltl::parray<document_t> vTree;
89 lltl::pphash<char, char> vVars; // Variables defined
90 scope_data_t vScopes[SC_TOTAL];
91 scope_data_t *pScope;
92 ssize_t nScopes;
93
94 protected:
95 static document_t *create_document();
96 static status_t destroy_document(document_t *doc);
97 static status_t init_scope(scope_data_t *scope, scope_t type, scope_data_t *prev);
98 static void clear_scope(scope_data_t *scope);
99 static void drop_hash(lltl::pphash<char, char> *data);
100 static scope_t get_scope(const LSPString *header);
101
102 protected:
103 status_t switch_scope(IDocumentHandler *handler, scope_t scope);
104 status_t dispatch_scope(IDocumentHandler *handler, scope_data_t *scope);
105 status_t process_header(IDocumentHandler *handler, event_t *ev);
106 status_t process_opcode(event_t *ev);
107 status_t process_include(IDocumentHandler *handler, event_t *ev);
108 status_t process_define(IDocumentHandler *handler, document_t *doc, event_t *ev);
109 status_t process_sample_data(IDocumentHandler *handler, event_t *ev);
110 status_t main_loop(IDocumentHandler *handler);
111 char *process_value(const LSPString *value);
112
113 public:
114 DocumentProcessor();
115 virtual ~DocumentProcessor();
116
117 public:
118 /**
119 * Open processor
120 * @param path UTF-8 path to the file
121 * @return status of operation
122 */
123 status_t open(const char *path);
124
125 /**
126 * Open processor
127 * @param path string representation of path to the file
128 * @return status of operation
129 */
130 status_t open(const LSPString *path);
131
132 /**
133 * Open processor
134 * @param path path to the file
135 * @return status of operation
136 */
137 status_t open(const io::Path *path);
138
139 /**
140 * Wrap string with processor
141 * @param str string to wrap
142 * @return status of operation
143 */
144 status_t wrap(const char *str);
145
146 /**
147 * Wrap string with parser
148 * @param buf buffer to wrap
149 * @param len length of buffer to wrap
150 * @return status of operation
151 */
152 status_t wrap(const void *buf, size_t len);
153
154 /**
155 * Wrap string with processor
156 * @param str string to wrap
157 * @return status of operation
158 */
159 status_t wrap(const LSPString *str);
160
161 /**
162 * Wrap input stream with processor
163 * @param is input stream
164 * @param flags wrap flags
165 * @return status of operation
166 */
167 status_t wrap(io::IInStream *is, size_t flags = WRAP_NONE);
168
169 /**
170 * Wrap pull parser with the processor
171 * @param parser parser to wrap
172 * @param flags wrap flags
173 * @return status of operation
174 */
175 status_t wrap(PullParser *parser, size_t flags = WRAP_NONE);
176
177 /**
178 * Process the document using the supplied document handler
179 * @param handler document handler to supply the data
180 * @return status of operation
181 */
182 status_t process(IDocumentHandler *handler);
183
184 /**
185 * Close parser
186 * @return status of operation
187 */
188 status_t close();
189 };
190
191 } /* namespace sfz */
192 } /* namespace lsp */
193
194
195 #endif /* LSP_PLUG_IN_FMT_SFZ_DOCUMENTPROCESSOR_H_ */