Blender V5.3
ply_import_buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BLI_fileops.hh"
12
13#include <algorithm>
14#include <cstdio>
15#include <cstring>
16#include <stdexcept>
17
18namespace blender {
19
20static inline bool is_newline(char ch)
21{
22 return ch == '\n';
23}
24
25namespace io::ply {
26
27PlyReadBuffer::PlyReadBuffer(const char *file_path, size_t read_buffer_size)
28 : buffer_(read_buffer_size), read_buffer_size_(read_buffer_size)
29{
30 file_ = BLI_fopen(file_path, "rb");
31}
32
34{
35 if (file_ != nullptr) {
36 fclose(file_);
37 }
38}
39
40void PlyReadBuffer::after_header(bool is_binary)
41{
42 is_binary_ = is_binary;
43}
44
49
50std::optional<Span<char>> PlyReadBuffer::try_read_line()
51{
52 if (is_binary_) {
53 throw std::runtime_error("PLY read_line should not be used in binary mode");
54 }
55 if (pos_ >= last_newline_) {
56 refill_buffer();
57 }
58 /* Refill again in case the file contained only newlines. */
59 while (pos_ >= buf_used_) {
60 if (!refill_buffer()) {
61 return std::nullopt;
62 }
63 }
64 BLI_assert(last_newline_ <= buffer_.size());
65 int res_begin = pos_;
66 while (pos_ < last_newline_ && !is_newline(buffer_[pos_])) {
67 pos_++;
68 }
69 int res_end = pos_;
70 /* Remove possible trailing CR from the result. */
71 if (res_end > res_begin && buffer_[res_end - 1] == '\r') {
72 --res_end;
73 }
74 /* Move cursor past newline. */
75 if (pos_ < buf_used_ && is_newline(buffer_[pos_])) {
76 pos_++;
77 }
78 return Span<char>(buffer_.data() + res_begin, res_end - res_begin);
79}
80
81bool PlyReadBuffer::read_bytes(void *dst, size_t size)
82{
83 while (size > 0) {
84 if (pos_ + size > buf_used_) {
85 if (!refill_buffer()) {
86 return false;
87 }
88 }
89 const size_t to_copy = std::min(size, size_t(buf_used_));
90 memcpy(dst, buffer_.data() + pos_, to_copy);
91 pos_ += to_copy;
92 dst = static_cast<char *>(dst) + to_copy;
93 size -= to_copy;
94 }
95 return true;
96}
97
98bool PlyReadBuffer::refill_buffer()
99{
100 BLI_assert(pos_ <= buf_used_);
101 BLI_assert(pos_ <= buffer_.size());
102 BLI_assert(buf_used_ <= buffer_.size());
103
104 if (file_ == nullptr || at_eof_) {
105 return false; /* File is fully read. */
106 }
107
108 /* The buffer is only empty before the first read, since reading nothing sets #at_eof_. */
109 const bool is_file_start = buf_used_ == 0;
110
111 /* Move any leftover to start of buffer. */
112 int keep = buf_used_ - pos_;
113 if (keep > 0) {
114 memmove(buffer_.data(), buffer_.data() + pos_, keep);
115 }
116 /* Read in data from the file. */
117 size_t read = fread(buffer_.data() + keep, 1, read_buffer_size_ - keep, file_) + keep;
118 at_eof_ = read < read_buffer_size_;
119 pos_ = 0;
120 buf_used_ = int(read);
121
122 /* Skip past newlines at the start of the file and find last newline. Newlines at the front of
123 * later chunks are kept, as empty lines are valid rows for elements without properties. */
124 if (!is_binary_) {
125 if (is_file_start) {
126 while (pos_ < buf_used_ && is_newline(buffer_[pos_])) {
127 pos_++;
128 }
129 }
130
131 int last_nl = buf_used_;
132 if (!at_eof_) {
133 while (last_nl > 0) {
134 --last_nl;
135 if (is_newline(buffer_[last_nl])) {
136 break;
137 }
138 }
139 if (!is_newline(buffer_[last_nl])) {
140 /* Whole line did not fit into our read buffer. */
141 throw std::runtime_error("PLY text line did not fit into the read buffer");
142 }
143 }
144 last_newline_ = last_nl;
145 }
146
147 return true;
148}
149
150} // namespace io::ply
151} // namespace blender
File and directory operations.
int64_t size() const
Definition BLI_array.hh:267
const T * data() const
Definition BLI_array.hh:323
PlyReadBuffer(const char *file_path, size_t read_buffer_size=64 *1024)
std::optional< Span< char > > try_read_line()
bool read_bytes(void *dst, size_t size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool is_newline(char ch)
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()