Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/obj/PullParser.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: 21 апр. 2017 г.
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/PullParser.h>
23
24 #include <lsp-plug.in/common/debug.h>
25 #include <lsp-plug.in/io/InFileStream.h>
26 #include <lsp-plug.in/io/InStringSequence.h>
27 #include <lsp-plug.in/io/InSequence.h>
28 #include <lsp-plug.in/stdlib/locale.h>
29
30 #include <errno.h>
31
32 namespace lsp
33 {
34 namespace obj
35 {
36 PullParser::PullParser()
37 {
38 pIn = NULL;
39 nWFlags = 0;
40 pBuffer = NULL;
41 nBufOff = 0;
42 nBufLen = 0;
43 nLines = 0;
44 nVx = 0;
45 nParVx = 0;
46 nTexVx = 0;
47 nNormVx = 0;
48 sEvent.type = EV_NONE;
49 }
50
51 PullParser::~PullParser()
52 {
53 close();
54 }
55
56 status_t PullParser::open(const char *path, const char *charset)
57 {
58 if (pIn != NULL)
59 return STATUS_BAD_STATE;
60 else if (path == NULL)
61 return STATUS_BAD_ARGUMENTS;
62
63 io::InFileStream *ifs = new io::InFileStream();
64 if (ifs == NULL)
65 return STATUS_NO_MEM;
66 status_t res = ifs->open(path);
67 if (res == STATUS_OK)
68 {
69 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
70 if (res == STATUS_OK)
71 return res;
72 ifs->close();
73 }
74 delete ifs;
75
76 return res;
77 }
78
79 status_t PullParser::open(const LSPString *path, const char *charset)
80 {
81 if (pIn != NULL)
82 return STATUS_BAD_STATE;
83 else if (path == NULL)
84 return STATUS_BAD_ARGUMENTS;
85
86 io::InFileStream *ifs = new io::InFileStream();
87 if (ifs == NULL)
88 return STATUS_NO_MEM;
89 status_t res = ifs->open(path);
90 if (res == STATUS_OK)
91 {
92 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
93 if (res == STATUS_OK)
94 return res;
95 ifs->close();
96 }
97 delete ifs;
98
99 return res;
100 }
101
102 status_t PullParser::open(const io::Path *path, const char *charset)
103 {
104 if (pIn != NULL)
105 return STATUS_BAD_STATE;
106 else if (path == NULL)
107 return STATUS_BAD_ARGUMENTS;
108
109 io::InFileStream *ifs = new io::InFileStream();
110 if (ifs == NULL)
111 return STATUS_NO_MEM;
112 status_t res = ifs->open(path);
113 if (res == STATUS_OK)
114 {
115 res = wrap(ifs, WRAP_CLOSE | WRAP_DELETE, charset);
116 if (res == STATUS_OK)
117 return res;
118 ifs->close();
119 }
120 delete ifs;
121
122 return res;
123 }
124
125 status_t PullParser::wrap(const char *str, const char *charset)
126 {
127 if (pIn != NULL)
128 return STATUS_BAD_STATE;
129 else if (str == NULL)
130 return STATUS_BAD_ARGUMENTS;
131
132 io::InStringSequence *seq = new io::InStringSequence();
133 if (seq == NULL)
134 return STATUS_NO_MEM;
135
136 status_t res = seq->wrap(str, charset);
137 if (res == STATUS_OK)
138 {
139 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
140 return res;
141 seq->close();
142 }
143
144 delete seq;
145 return res;
146 }
147
148 status_t PullParser::wrap(const LSPString *str)
149 {
150 if (pIn != NULL)
151 return STATUS_BAD_STATE;
152 else if (str == NULL)
153 return STATUS_BAD_ARGUMENTS;
154
155 io::InStringSequence *seq = new io::InStringSequence();
156 if (seq == NULL)
157 return STATUS_NO_MEM;
158
159 status_t res = seq->wrap(str);
160 if (res == STATUS_OK)
161 {
162 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
163 return res;
164 seq->close();
165 }
166
167 delete seq;
168 return res;
169 }
170
171 status_t PullParser::wrap(io::IInStream *is, size_t flags, const char *charset)
172 {
173 if (pIn != NULL)
174 return STATUS_BAD_STATE;
175 else if (is == NULL)
176 return STATUS_BAD_ARGUMENTS;
177
178 io::InSequence *seq = new io::InSequence();
179 if (seq == NULL)
180 return STATUS_NO_MEM;
181
182 status_t res = seq->wrap(is, flags, charset);
183 if (res == STATUS_OK)
184 {
185 if ((res = wrap(seq, WRAP_CLOSE | WRAP_DELETE)) == STATUS_OK)
186 return res;
187 seq->close();
188 }
189
190 delete seq;
191 return res;
192 }
193
194 status_t PullParser::wrap(io::IInSequence *seq, size_t flags)
195 {
196 if (pIn != NULL)
197 return STATUS_BAD_STATE;
198 else if (seq == NULL)
199 return STATUS_BAD_ARGUMENTS;
200
201 // Allocate memory
202 lsp_wchar_t *b = static_cast<lsp_wchar_t *>(::malloc(IO_BUF_SIZE * sizeof(lsp_wchar_t)));
203 if (b == NULL)
204 return STATUS_NO_MEM;
205
206 // Initialize state
207 pIn = seq;
208 nWFlags = flags;
209 pBuffer = b;
210 nBufOff = 0;
211 nBufLen = 0;
212 nLines = 0;
213 nVx = 0;
214 nParVx = 0;
215 nTexVx = 0;
216 nNormVx = 0;
217 sEvent.type = EV_NONE;
218
219 return STATUS_OK;
220 }
221
222 status_t PullParser::close()
223 {
224 status_t res = STATUS_OK;
225
226 // Reset internal state
227 if (pBuffer != NULL)
228 {
229 ::free(pBuffer);
230 pBuffer = NULL;
231 }
232 nBufOff = 0;
233 nBufLen = 0;
234 nLines = 0;
235
236 // Release input sequence
237 if (pIn != NULL)
238 {
239 if (nWFlags & WRAP_CLOSE)
240 res = pIn->close();
241
242 if (nWFlags & WRAP_DELETE)
243 delete pIn;
244
245 pIn = NULL;
246 }
247
248 return res;
249 }
250
251 const event_t *PullParser::current() const
252 {
253 return (sEvent.type != EV_NONE) ? &sEvent : NULL;
254 };
255
256 status_t PullParser::current(event_t *ev) const
257 {
258 if (pIn == NULL)
259 return STATUS_CLOSED;
260 if (sEvent.type == EV_NONE)
261 return STATUS_NO_DATA;
262 return copy_event(ev);
263 }
264
265 status_t PullParser::next(event_t *ev)
266 {
267 if (pIn == NULL)
268 return STATUS_CLOSED;
269 status_t res = read_event();
270 if ((res == STATUS_OK) && (ev != NULL))
271 res = copy_event(ev);
272 return res;
273 }
274
275 status_t PullParser::copy_event(event_t *ev) const
276 {
277 // Set values to target structure
278 ev->type = sEvent.type;
279 ev->vertex = sEvent.vertex;
280
281 if (!ev->name.set(&sEvent.name))
282 return STATUS_NO_MEM;
283 if (!ev->ivertex.set(&sEvent.ivertex))
284 return STATUS_NO_MEM;
285 if (!ev->inormal.set(&sEvent.inormal))
286 return STATUS_NO_MEM;
287 if (!ev->itexcoord.set(&sEvent.itexcoord))
288 return STATUS_NO_MEM;
289
290 return STATUS_OK;
291 }
292
293 status_t PullParser::read_event()
294 {
295 status_t res = STATUS_OK;
296
297 // Clear current event
298 sEvent.type = EV_NONE;
299 sEvent.vertex.x = 0.0f;
300 sEvent.vertex.y = 0.0f;
301 sEvent.vertex.z = 0.0f;
302 sEvent.vertex.w = 0.0f;
303 sEvent.name.clear();
304 sEvent.ivertex.clear();
305 sEvent.inormal.clear();
306 sEvent.itexcoord.clear();
307
308 // Read lines
309 while ((res = read_line()) == STATUS_OK)
310 {
311 // Check that line is not empty
312 const char *l = skip_spaces(sLine.get_utf8());
313 if ((l == NULL) || (*l == '\0'))
314 continue;
315
316 // Parse line
317 if ((res = parse_line(l)) != STATUS_OK)
318 return res;
319
320 // Got event?
321 if (sEvent.type != EV_NONE)
322 return STATUS_OK;
323 }
324
325 return res;
326 }
327
328 status_t PullParser::read_line()
329 {
330 // Clear previous line contents
331 sLine.clear();
332
333 while (true)
334 {
335 // Ensure that there is data in buffer
336 if (nBufOff >= nBufLen)
337 {
338 // No data in the buffer, read from input stream
339 ssize_t n = pIn->read(pBuffer, IO_BUF_SIZE);
340 if (n <= 0)
341 {
342 if (sLine.length() > 0)
343 return STATUS_OK;
344
345 return status_t(-n);
346 }
347 nBufLen = n;
348 nBufOff = 0;
349 }
350
351 // Scan for line ending character
352 size_t tail = nBufOff;
353 while (tail < nBufLen)
354 {
355 lsp_wchar_t ch = pBuffer[tail++];
356 if (ch == '\n') // Found!
357 break;
358 }
359
360 // Append data to string and update buffer state
361 sLine.append(&pBuffer[nBufOff], tail - nBufOff);
362 nBufOff = tail;
363
364 // Now analyze last string character
365 size_t len = sLine.length();
366 if (sLine.last() != '\n') // Not end of line?
367 continue;
368 sLine.set_length(--len);
369 if (sLine.last() == '\r') // Remove carriage-return symbol if it is present
370 sLine.set_length(--len);
371
372 // Compute number of terminating '\\' characters
373 ssize_t slashes = 0, xoff = len-1;
374 while ((xoff >= 0) && (sLine.char_at(xoff) == '\\'))
375 {
376 ++slashes;
377 --xoff;
378 }
379
380 // Line has been split into multiple lines?
381 if (slashes & 1)
382 {
383 sLine.set_length(--len);
384 continue;
385 }
386
387 // Alright, now we have complete line and can return it
388 return eliminate_comments();
389 }
390
391 return STATUS_NOT_IMPLEMENTED;
392 }
393
394 status_t PullParser::eliminate_comments()
395 {
396 size_t len = sLine.length(), r=0, w=0;
397 bool slash = false;
398
399 while (r < len)
400 {
401 lsp_wchar_t ch = sLine.at(r);
402 if (slash)
403 {
404 ++r;
405 if ((ch != '#') && (ch != '\\'))
406 sLine.set_at(w++, '\\');
407 sLine.set_at(w++, ch);
408 slash = false;
409 continue;
410 }
411 else if (ch == '#')
412 {
413 sLine.set_length(r);
414 return STATUS_OK;
415 }
416 else if (ch == '\\')
417 {
418 slash = true;
419 ++r;
420 continue;
421 }
422
423 if (r != w)
424 sLine.set_at(w, ch);
425 ++r;
426 ++w;
427 }
428
429 if (slash)
430 sLine.set_at(w++, '\\');
431 sLine.set_length(w);
432
433 return STATUS_OK;
434 }
435
436 inline bool PullParser::is_space(char ch)
437 {
438 return (ch == ' ') || (ch == '\t');
439 }
440
441 bool PullParser::prefix_match(const char *s, const char *prefix)
442 {
443 while (*prefix != '\0')
444 {
445 if (*(s++) != *(prefix++))
446 return false;
447 }
448 return is_space(*s);
449 }
450
451 const char *PullParser::skip_spaces(const char *s)
452 {
453 if (s == NULL)
454 return NULL;
455
456 while (true)
457 {
458 char ch = *s;
459 if ((ch == '\0') || (!is_space(ch)))
460 return s;
461 s++;
462 }
463 }
464
465 bool PullParser::end_of_line(const char *s)
466 {
467 if (s == NULL)
468 return true;
469
470 while (true)
471 {
472 char ch = *s;
473 if (ch == '\0')
474 return true;
475 if (!is_space(ch))
476 return false;
477 s++;
478 }
479 }
480
481 bool PullParser::parse_float(float *dst, const char **s)
482 {
483 if (*s == NULL)
484 return false;
485
486 // Save and update locale
487 SET_LOCALE_SCOPED(LC_NUMERIC, "C");
488
489 // Parse floating-point value
490 errno = 0;
491 char *ptr = NULL;
492 float result = strtof(*s, &ptr);
493 bool success = ((errno == 0) && (ptr > *s));
494 if (success)
495 {
496 *dst = result;
497 *s = ptr;
498 }
499
500 return success;
501 }
502
503 bool PullParser::parse_int(ssize_t *dst, const char **s)
504 {
505 if ((*s == NULL) || (**s == '\0') || (**s == ' '))
506 return false;
507
508 errno = 0;
509 char *ptr = NULL;
510 long result = strtol(*s, &ptr, 10);
511 if ((errno != 0) || (ptr == *s))
512 return false;
513 *dst = result;
514 *s = ptr;
515 return true;
516 }
517
518 status_t PullParser::parse_line(const char *s)
519 {
520 status_t result = ((nLines++) > 0) ? STATUS_CORRUPTED_FILE : STATUS_BAD_FORMAT;
521
522 switch (*(s++))
523 {
524 case 'b': // bmat, bevel
525 if (prefix_match(s, "mat")) // bmat
526 return STATUS_OK;
527 else if (prefix_match(s, "evel")) // bevel
528 return STATUS_OK;
529 break;
530
531 case 'c': // cstype, curv, curv2, con, c_interp, ctech
532 if (prefix_match(s, "stype")) // cstype
533 return STATUS_OK;
534 else if (prefix_match(s, "urv")) // curv
535 return STATUS_OK;
536 else if (prefix_match(s, "urv2")) // curv2
537 return STATUS_OK;
538 else if (prefix_match(s, "on")) // con
539 return STATUS_OK;
540 else if (prefix_match(s, "_interp")) // c_interp
541 return STATUS_OK;
542 else if (prefix_match(s, "tech")) // ctech
543 return STATUS_OK;
544 break;
545
546 case 'd': // deg, d_interp
547 if (prefix_match(s, "eg")) // deg
548 return STATUS_OK;
549 else if (prefix_match(s, "_interp")) // d_interp
550 return STATUS_OK;
551 break;
552
553 case 'e': // end
554 if (prefix_match(s, "nd")) // end
555 return STATUS_OK;
556 break;
557
558 case 'f': // f
559 if (is_space(*s)) // f - face
560 {
561 // Parse face
562 while (true)
563 {
564 ssize_t v = 0, vt = 0, vn = 0;
565
566 // Parse indexes
567 s = skip_spaces(s);
568 if (!parse_int(&v, &s))
569 break;
570 if (*s == '/')
571 {
572 ++s;
573 if (!parse_int(&vt, &s))
574 vt = 0;
575 if (*s == '/')
576 {
577 ++s;
578 if (!parse_int(&vn, &s))
579 vn = 0;
580 }
581 }
582
583 // Ensure that indexes are correct
584 v = (v < 0) ? nVx + v : v - 1;
585 if ((v < 0) || (v >= nVx))
586 return result;
587
588 vt = (vt < 0) ? nTexVx + vt : vt - 1;
589 if ((vt < -1) || (vt >= nTexVx))
590 return result;
591
592 vn = (vn < 0) ? nNormVx + vn : vn - 1;
593 if ((vn < -1) || (vn >= nNormVx))
594 return result;
595
596 // Add items to lists
597 if (!sEvent.ivertex.add(&v))
598 return STATUS_NO_MEM;
599 if (!sEvent.itexcoord.add(&vt))
600 return STATUS_NO_MEM;
601 if (!sEvent.inormal.add(&vn))
602 return STATUS_NO_MEM;
603 }
604
605 if (!end_of_line(s))
606 return result;
607
608 // Check face parameters
609 if (sEvent.ivertex.size() < 3)
610 return STATUS_BAD_FORMAT;
611
612 sEvent.type = EV_FACE;
613 return STATUS_OK;
614 }
615 break;
616
617 case 'g': // g
618 if (is_space(*s)) // g
619 return STATUS_OK;
620 break;
621
622 case 'h': // hole
623 if (prefix_match(s, "ole")) // hole
624 return STATUS_OK;
625 break;
626
627 case 'l': // l, lod
628 if (prefix_match(s, "od")) // lod
629 return STATUS_OK;
630
631 if (is_space(*s)) // l - line
632 {
633 // Parse line
634 while (true)
635 {
636 ssize_t v = 0, vt = 0;
637
638 // Parse indexes
639 s = skip_spaces(s);
640 if (!parse_int(&v, &s))
641 break;
642 if (*(s++) != '/')
643 return result;
644 if (!parse_int(&vt, &s))
645 vt = 0;
646
647 // Ensure that indexes are correct
648 v = (v < 0) ? nVx + v : v - 1;
649 if ((v < 0) || (v >= nVx))
650 return result;
651
652 vt = (vt < 0) ? nTexVx + vt : vt - 1;
653 if ((vt <= -1) || (vt >= nTexVx))
654 return result;
655
656 // Add items to lists
657 if (!sEvent.ivertex.add(&v))
658 return STATUS_NO_MEM;
659 if (!sEvent.itexcoord.add(&vt))
660 return STATUS_NO_MEM;
661 }
662
663 if (!end_of_line(s))
664 return result;
665
666 // Check line parameters
667 if (sEvent.ivertex.size() < 2)
668 return STATUS_BAD_FORMAT;
669
670 // Call parser to handle data
671 sEvent.type = EV_LINE;
672 return STATUS_OK;
673 }
674 break;
675
676 case 'm': // mg, mtllib
677 if (prefix_match(s, "g")) // mg
678 return STATUS_OK;
679 else if (prefix_match(s, "tllib")) // mtllib
680 return STATUS_OK;
681 break;
682
683 case 'o': // o
684 if (is_space(*s)) // o
685 {
686 s = skip_spaces(s + 1);
687 if (!sEvent.name.set_utf8(s))
688 return STATUS_NO_MEM;
689
690 sEvent.type = EV_OBJECT;
691 return STATUS_OK;
692 }
693 break;
694
695 case 'p': // p, parm
696 if (is_space(*s)) // p
697 {
698 // Parse point
699 while (true)
700 {
701 ssize_t v = 0;
702
703 // Parse indexes
704 s = skip_spaces(s);
705 if (!parse_int(&v, &s))
706 break;
707
708 // Ensure that indexes are correct
709 v = (v < 0) ? nVx + v : v - 1;
710 if ((v < 0) || (v >= nVx))
711 return result;
712
713 // Add items to lists
714 if (!sEvent.ivertex.add(&v))
715 return STATUS_NO_MEM;
716 }
717
718 // Check that we reached end of line
719 if (!end_of_line(s))
720 return result;
721
722 sEvent.type = EV_POINT;
723 return STATUS_OK;
724 }
725 else if (prefix_match(s, "arm")) // parm
726 return STATUS_OK;
727 break;
728
729 case 's': // s, step, surf, scrv, sp, shadow_obj, stech
730 if (is_space(*s)) // s
731 return STATUS_OK;
732 else if (prefix_match(s, "tep")) // step
733 return STATUS_OK;
734 else if (prefix_match(s, "urf")) // surf
735 return STATUS_OK;
736 else if (prefix_match(s, "rcv")) // srcv
737 return STATUS_OK;
738 else if (prefix_match(s, "p")) // sp
739 return STATUS_OK;
740 else if (prefix_match(s, "hadow_obj")) // shadow_obj
741 return STATUS_OK;
742 else if (prefix_match(s, "tech")) // stech
743 return STATUS_OK;
744 break;
745
746 case 't': // trim, trace_obj
747 if (prefix_match(s, "rim")) // trim
748 return STATUS_OK;
749 else if (prefix_match(s, "race_obj")) // trace_obj
750 return STATUS_OK;
751 break;
752
753 case 'u': // usemtl
754 if (prefix_match(s, "semtl")) // usemtl
755 return STATUS_OK;
756 break;
757
758 case 'v': // v, vt, vn, vp
759 if (is_space(*s)) // v
760 {
761 s = skip_spaces(s+1);
762 if (!parse_float(&sEvent.vertex.x, &s))
763 return result;
764 s = skip_spaces(s);
765 if (!parse_float(&sEvent.vertex.y, &s))
766 return result;
767 s = skip_spaces(s);
768 if (!parse_float(&sEvent.vertex.z, &s))
769 sEvent.vertex.z = 0.0f; // Extension, strictly required in obj format, for our case facilitated
770 s = skip_spaces(s);
771 if (!parse_float(&sEvent.vertex.w, &s))
772 sEvent.vertex.w = 1.0f;
773
774 if (!end_of_line(s))
775 return result;
776
777 ++nVx;
778 sEvent.type = EV_VERTEX;
779 return STATUS_OK;
780 }
781 else if (prefix_match(s, "n")) // vn
782 {
783 s = skip_spaces(s+2);
784 if (!parse_float(&sEvent.normal.dx, &s))
785 return result;
786 s = skip_spaces(s);
787 if (!parse_float(&sEvent.normal.dy, &s))
788 return result;
789 s = skip_spaces(s);
790 if (!parse_float(&sEvent.normal.dz, &s))
791 sEvent.normal.dz = 0.0f; // Extension, strictly required in obj format, for our case facilitated
792 sEvent.normal.dw = 0.0f;
793
794 if (!end_of_line(s))
795 return result;
796
797 ++nNormVx;
798 sEvent.type = EV_NORMAL;
799 return STATUS_OK;
800 }
801 else if (prefix_match(s, "p")) // vp
802 {
803 s = skip_spaces(s+2);
804 if (parse_float(&sEvent.vertex.x, &s))
805 return result;
806 s = skip_spaces(s);
807 if (!parse_float(&sEvent.vertex.y, &s))
808 sEvent.vertex.y = 0.0f;
809 sEvent.vertex.z = 0.0f;
810 s = skip_spaces(s);
811 if (!parse_float(&sEvent.vertex.w, &s))
812 sEvent.vertex.w = 1.0f;
813
814 if (!end_of_line(s))
815 return result;
816
817 ++nParVx;
818 sEvent.type = EV_PVERTEX;
819 return STATUS_OK;
820 }
821 else if (prefix_match(s, "t")) // vt
822 {
823 s = skip_spaces(s+2);
824 if (!parse_float(&sEvent.texcoord.u, &s))
825 return result;
826 s = skip_spaces(s);
827 if (!parse_float(&sEvent.texcoord.v, &s))
828 sEvent.texcoord.v = 0.0f;
829 s = skip_spaces(s);
830 if (!parse_float(&sEvent.texcoord.w, &s))
831 sEvent.texcoord.w = 0.0f;
832
833 if (!end_of_line(s))
834 return result;
835
836 ++nTexVx;
837 sEvent.type = EV_TEXCOORD;
838 return STATUS_OK;
839 }
840 break;
841
842 default:
843 break;
844 }
845
846 return result;
847 }
848
849 } /* namespace obj */
850 } /* namespace lsp */