Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/obj/PushParser.cpp

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: 22 апр. 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/obj/PushParser.h>
23
24 namespace lsp
25 {
26 namespace obj
27 {
28
29 PushParser::PushParser()
30 {
31 }
32
33 PushParser::~PushParser()
34 {
35 }
36
37 status_t PushParser::parse_file(IObjHandler *handler, const char *path, const char *charset)
38 {
39 IObjHandler stub;
40 status_t res = sParser.open(path, charset);
41 if (res == STATUS_OK)
42 res = parse_document((handler != NULL) ? handler : &stub);
43 if (res == STATUS_OK)
44 res = sParser.close();
45 else
46 sParser.close();
47 return res;
48 }
49
50 status_t PushParser::parse_file(IObjHandler *handler, const LSPString *path, const char *charset)
51 {
52 IObjHandler stub;
53 status_t res = sParser.open(path, charset);
54 if (res == STATUS_OK)
55 res = parse_document((handler != NULL) ? handler : &stub);
56 if (res == STATUS_OK)
57 res = sParser.close();
58 else
59 sParser.close();
60 return res;
61 }
62
63 status_t PushParser::parse_file(IObjHandler *handler, const io::Path *path, const char *charset)
64 {
65 IObjHandler stub;
66 status_t res = sParser.open(path, charset);
67 if (res == STATUS_OK)
68 res = parse_document((handler != NULL) ? handler : &stub);
69 if (res == STATUS_OK)
70 res = sParser.close();
71 else
72 sParser.close();
73 return res;
74 }
75
76 status_t PushParser::parse_data(IObjHandler *handler, io::IInStream *is, size_t flags, const char *charset)
77 {
78 IObjHandler stub;
79 status_t res = sParser.wrap(is, flags, charset);
80 if (res == STATUS_OK)
81 res = parse_document((handler != NULL) ? handler : &stub);
82 if (res == STATUS_OK)
83 res = sParser.close();
84 else
85 sParser.close();
86 return res;
87 }
88
89 status_t PushParser::parse_data(IObjHandler *handler, const char *str, const char *charset)
90 {
91 IObjHandler stub;
92 status_t res = sParser.wrap(str, charset);
93 if (res == STATUS_OK)
94 res = parse_document((handler != NULL) ? handler : &stub);
95 if (res == STATUS_OK)
96 res = sParser.close();
97 else
98 sParser.close();
99 return res;
100 }
101
102 status_t PushParser::parse_data(IObjHandler *handler, const LSPString *str)
103 {
104 IObjHandler stub;
105 status_t res = sParser.wrap(str);
106 if (res == STATUS_OK)
107 res = parse_document((handler != NULL) ? handler : &stub);
108 if (res == STATUS_OK)
109 res = sParser.close();
110 else
111 sParser.close();
112 return res;
113 }
114
115 status_t PushParser::parse_data(IObjHandler *handler, io::IInSequence *seq, size_t flags)
116 {
117 IObjHandler stub;
118 status_t res = sParser.wrap(seq, flags);
119 if (res == STATUS_OK)
120 res = parse_document((handler != NULL) ? handler : &stub);
121 if (res == STATUS_OK)
122 res = sParser.close();
123 else
124 sParser.close();
125 return res;
126 }
127
128 status_t PushParser::parse_document(IObjHandler *handler)
129 {
130 ssize_t object_id;
131 status_t res = STATUS_OK;
132 size_t nobj = 0;
133
134 while ((res = sParser.next()) == STATUS_OK)
135 {
136 // Fetch event
137 const event_t *ev = sParser.current();
138 if (ev == NULL)
139 return STATUS_BAD_STATE;
140
141 // Analyze event
142 switch (ev->type)
143 {
144 case EV_OBJECT:
145 if (nobj++)
146 {
147 if ((res = handler->end_object()) != STATUS_OK)
148 return res;
149 }
150 if ((res = handler->begin_object(&ev->name)) != STATUS_OK)
151 return res;
152 break;
153
154 case EV_VERTEX:
155 if ((object_id = handler->add_vertex(ev->vertex.x, ev->vertex.y, ev->vertex.z, ev->vertex.w)) < 0)
156 return status_t(-object_id);
157 break;
158
159 case EV_PVERTEX:
160 if ((object_id = handler->add_param_vertex(ev->vertex.x, ev->vertex.y, ev->vertex.z, ev->vertex.w)) < 0)
161 return status_t(-object_id);
162 break;
163
164 case EV_NORMAL:
165 if ((object_id = handler->add_normal(ev->normal.dx, ev->normal.dy, ev->normal.dz, ev->normal.dw)) < 0)
166 return status_t(-object_id);
167 break;
168
169 case EV_TEXCOORD:
170 if ((object_id = handler->add_texture_vertex(ev->texcoord.u, ev->texcoord.v, ev->texcoord.w)) < 0)
171 return status_t(-object_id);
172 break;
173
174 case EV_FACE:
175 if ((object_id = handler->add_face(ev->ivertex.array(), ev->inormal.array(), ev->itexcoord.array(), ev->ivertex.size())) < 0)
176 return status_t(-object_id);
177 break;
178
179 case EV_LINE:
180 if ((object_id = handler->add_line(ev->ivertex.array(), ev->itexcoord.array(), ev->ivertex.size())) < 0)
181 return status_t(-object_id);
182 break;
183
184 case EV_POINT:
185 if ((object_id = handler->add_points(ev->ivertex.array(), ev->ivertex.size())) < 0)
186 return status_t(-object_id);
187 break;
188
189 default:
190 return STATUS_UNSUPPORTED_FORMAT;
191 }
192 }
193
194 if (res == STATUS_EOF)
195 res = STATUS_OK;
196
197 // Post-process data
198 if (res == STATUS_OK)
199 {
200 if (nobj)
201 {
202 if ((res = handler->end_object()) != STATUS_OK)
203 return res;
204 }
205
206 res = handler->end_of_data();
207 }
208
209 return res;
210 }
211 } /* namespace obj */
212 } /* namespace lsp */