Blender V5.0
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"
16static CLG_LogRef LOG = {"io.ply"};
17
18namespace blender::io::ply {
19
20FileBuffer::FileBuffer(const char *filepath, size_t buffer_chunk_size)
21 : buffer_chunk_size_(buffer_chunk_size), filepath_(filepath)
22{
23 outfile_ = BLI_fopen(filepath, "wb");
24 if (!outfile_) {
25 throw std::system_error(
26 errno, std::system_category(), "Cannot open file " + std::string(filepath) + ".");
27 }
28}
29
31{
32 for (const VectorChar &b : blocks_) {
33 fwrite(b.data(), 1, b.size(), this->outfile_);
34 }
35 blocks_.clear();
36}
37
39{
40 if (!outfile_) {
41 return;
42 }
43 int close_status = std::fclose(outfile_);
44 if (close_status == EOF) {
45 return;
46 }
47 if (close_status) {
48 CLOG_ERROR(&LOG, "Error: could not close file '%s' properly, it may be corrupted.", filepath_);
49 }
50}
51
53{
54 write_fstring("element {} {}\n", name, count);
55}
57{
58 write_fstring("property {} {}\n", dataType, name);
59}
60
62 StringRef dataType,
64{
65 write_fstring("property list {} {} {}\n", countType, dataType, name);
66}
67
69{
70 write_fstring("{}\n", s);
71}
72
77
79{
80 ensure_space(bytes.size());
81 VectorChar &bb = blocks_.last();
82 bb.insert(bb.end(), bytes.begin(), bytes.end());
83}
84
85} // namespace blender::io::ply
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr const T * end() const
Definition BLI_span.hh:224
constexpr const T * begin() const
Definition BLI_span.hh:220
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)
int count
#define LOG(level)
Definition log.h:97
const char * name