Blender V5.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
8
9#include "ply_file_buffer.hh"
10
11#include "BLI_fileops.hh"
12
13#include <system_error>
14
15#include "CLG_log.h"
16
17namespace blender {
18
19static CLG_LogRef LOG = {"io.ply"};
20
21namespace io::ply {
22
23FileBuffer::FileBuffer(const char *filepath, size_t buffer_chunk_size)
24 : buffer_chunk_size_(buffer_chunk_size), filepath_(filepath)
25{
26 outfile_ = BLI_fopen(filepath, "wb");
27 if (!outfile_) {
28 throw std::system_error(
29 errno, std::system_category(), "Cannot open file " + std::string(filepath) + ".");
30 }
31}
32
34{
35 for (const VectorChar &b : blocks_) {
36 fwrite(b.data(), 1, b.size(), this->outfile_);
37 }
38 blocks_.clear();
39}
40
42{
43 if (!outfile_) {
44 return;
45 }
46 int close_status = std::fclose(outfile_);
47 if (close_status == EOF) {
48 return;
49 }
50 if (close_status) {
51 CLOG_ERROR(&LOG, "Error: could not close file '%s' properly, it may be corrupted.", filepath_);
52 }
53}
54
56{
57 write_fstring("element {} {}\n", name, count);
58}
60{
61 write_fstring("property {} {}\n", dataType, name);
62}
63
65 StringRef dataType,
66 StringRef name)
67{
68 write_fstring("property list {} {} {}\n", countType, dataType, name);
69}
70
72{
73 write_fstring("{}\n", s);
74}
75
80
82{
83 ensure_space(bytes.size());
84 VectorChar &bb = blocks_.last();
85 bb.insert(bb.end(), bytes.begin(), bytes.end());
86}
87
88} // namespace io::ply
89} // namespace blender
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
void insert(const int64_t insert_index, const T &value)
void ensure_space(size_t at_least)
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)
void write_fstring(fmt::format_string< T... > fmt, T &&...args)
FileBuffer(const char *filepath, size_t buffer_chunk_size=64 *1024)
void write_header_element(StringRef name, int count)
BMesh Iterators.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static CLG_LogRef LOG
Definition logging.cpp:12