PLYwoot
Header-only C++17 library for parsing and writing PLY files
Loading...
Searching...
No Matches
ascii_parser_policy.hpp
Go to the documentation of this file.
1/*
2 This file is part of PLYwoot, a header-only PLY parser.
3
4 Copyright (C) 2023-2025, Ton van den Heuvel
5
6 PLYwoot is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef PLYWOOT_ASCII_PARSER_POLICY_HPP
21#define PLYWOOT_ASCII_PARSER_POLICY_HPP
22
24
25#include "buffered_istream.hpp"
26#include "exceptions.hpp"
27#include "reflect.hpp"
28#include "std.hpp"
29#include "types.hpp"
30
31#include <cstdint>
32#include <string>
33
34namespace plywoot {
35
38{
42 ParserException(const std::string &message) : Exception("parser error: " + message) {}
43};
44
47{
49 UnexpectedEof() : ParserException("unexpected end of file") {}
50};
51
52namespace detail {
53
57class AsciiParserPolicy
58{
59public:
63 AsciiParserPolicy(std::istream &is) : is_{is} {}
64
69 void skipElement(const PlyElement &e) const { is_.skipLines(e.size()); }
70
74 void skipProperty(const PlyProperty &p) const
75 {
76 if (p.isList())
77 {
78 // TODO(ton): maybe switch on the list size type...?
79 const auto size = readNumber<int>();
80 for (int i = 0; i < size; ++i) { skipNumber(); }
81 }
82 else { skipNumber(); }
83 }
84
88 template<typename T>
89 T readNumber() const
90 {
91 is_.skipWhitespace();
92 if (is_.eof()) { throw UnexpectedEof(); }
93
94 is_.buffer(256);
95 return detail::to_number<T>(is_.data(), is_.data() + 256, &is_.data());
96 }
97
105 template<typename PlyT, typename DestT, std::size_t N>
106 std::uint8_t *readNumbers(std::uint8_t *dest) const
107 {
108 // TODO(ton): needs to be specialized for improved performance.
109 for (std::size_t i = 0; i < N; ++i, dest += sizeof(DestT))
110 {
111 *reinterpret_cast<DestT *>(dest) = readNumber<PlyT>();
112 }
113 return dest;
114 }
115
117 template<typename T = void>
118 void skipNumber() const
119 {
120 is_.skipWhitespace();
121 is_.skipNonWhitespace();
122 }
123
127 void skipProperties(std::size_t n) const
128 {
129 if (n > 0) is_.skipLines(1);
130 }
131
132private:
134 mutable detail::BufferedIStream is_;
135};
136
137}
138}
139
140#endif
Base class for all exceptions thrown by PLYwoot.
Base class for all parser exceptions.
ParserException(const std::string &message)
Unexpected end-of-file exception.
UnexpectedEof()
Constructs an unexpected end-of-file exception.