Blender V4.3
ply_file_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
9#include "ply_file_buffer.hh"
10
11#include "BLI_fileops.hh"
12
13#include <iostream>
14#include <system_error>
15
16namespace blender::io::ply {
17
18FileBuffer::FileBuffer(const char *filepath, size_t buffer_chunk_size)
19 : buffer_chunk_size_(buffer_chunk_size), filepath_(filepath)
20{
21 outfile_ = BLI_fopen(filepath, "wb");
22 if (!outfile_) {
23 throw std::system_error(
24 errno, std::system_category(), "Cannot open file " + std::string(filepath) + ".");
25 }
26}
27
29{
30 for (const VectorChar &b : blocks_) {
31 fwrite(b.data(), 1, b.size(), this->outfile_);
32 }
33 blocks_.clear();
34}
35
37{
38 int close_status = std::fclose(outfile_);
39 if (close_status == EOF) {
40 return;
41 }
42 if (outfile_ && close_status) {
43 std::cerr << "Error: could not close the file '" << this->filepath_
44 << "' properly, it may be corrupted." << std::endl;
45 }
46}
47
49{
50 write_fstring("element {} {}\n", name, count);
51}
53{
54 write_fstring("property {} {}\n", dataType, name);
55}
56
58 StringRef dataType,
59 StringRef name)
60{
61 write_fstring("property list {} {} {}\n", countType, dataType, name);
62}
63
65{
66 write_fstring("{}\n", s);
67}
68
73
75{
76 ensure_space(bytes.size());
77 VectorChar &bb = blocks_.last();
78 bb.insert(bb.end(), bytes.begin(), bytes.end());
79}
80
81} // namespace blender::io::ply
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
File and directory operations.
constexpr int64_t size() const
Definition BLI_span.hh:253
constexpr const T * end() const
Definition BLI_span.hh:225
constexpr const T * begin() const
Definition BLI_span.hh:221
void insert(const int64_t insert_index, const T &value)
void ensure_space(size_t at_least)
void write_fstring(const char *fmt, T &&...args)
void write_header_scalar_property(StringRef dataType, StringRef name)
void write_bytes(Span< char > bytes)
void write_header_list_property(StringRef countType, StringRef dataType, StringRef name)
FileBuffer(const char *filepath, size_t buffer_chunk_size=64 *1024)
void write_header_element(StringRef name, int count)
local_group_size(16, 16) .push_constant(Type b
int count