numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
file_wrapper.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cerrno>
23#include <cstdio>
24#include <string>
25#include <string_view>
26#include <utility>
27
30
32
39public:
43 file_wrapper() noexcept = default;
44
52 file_wrapper(std::FILE* file, bool close_on_destruction)
53 : file_(file), close_on_destruction_(close_on_destruction) {}
54
61 file_wrapper(const std::string& filepath, const char* mode) {
62 open(filepath, mode);
63 }
64
65 file_wrapper(const file_wrapper&) = delete;
66 auto operator=(const file_wrapper&) = delete;
67
73 file_wrapper(file_wrapper&& obj) noexcept
74 : file_(std::exchange(obj.file_, nullptr)),
76 std::exchange(obj.close_on_destruction_, false)) {}
77
84 auto operator=(file_wrapper&& obj) noexcept -> file_wrapper& {
85 swap(obj);
86 return *this;
87 }
88
92 ~file_wrapper() noexcept { close(); }
93
99 void swap(file_wrapper& obj) noexcept {
100 std::swap(file_, obj.file_);
101 std::swap(close_on_destruction_, obj.close_on_destruction_);
102 }
103
110 void open(const std::string& filepath, const char* mode) {
111 close();
112 errno = 0;
113 file_ = std::fopen(filepath.c_str(), mode);
114 if (file_ == nullptr) {
116 "Failed to open {} with mode \"{}\"", filepath, mode));
117 }
119 }
120
124 void set_stdout() {
125 close();
126 file_ = stdout;
127 }
128
132 void set_stderr() {
133 close();
134 file_ = stderr;
135 }
136
140 void close() noexcept {
141 if (file_ != nullptr && close_on_destruction_) {
142 (void)std::fclose(file_);
143 }
144 file_ = nullptr;
145 close_on_destruction_ = false;
146 }
147
153 void write(std::string_view data) {
154 if (file_ == nullptr) [[unlikely]] {
155 throw file_error("Failed to write to file: file is not opened.");
156 }
157
158 errno = 0;
159 const std::size_t written_size =
160 std::fwrite(data.data(), 1, data.size(), file_);
161 if (written_size != data.size()) [[unlikely]] {
162 throw file_error(util::format_errno("Failed to write to file"));
163 }
164 }
165
169 void flush() {
170 if (file_ == nullptr) [[unlikely]] {
171 throw file_error("Failed to write to file: file is not opened.");
172 }
173
174 errno = 0;
175 const int result = std::fflush(file_);
176 if (result != 0) [[unlikely]] {
177 throw file_error(util::format_errno("Failed to write to file"));
178 }
179 }
180
186 [[nodiscard]] auto file() const noexcept -> std::FILE* { return file_; }
187
188private:
190 std::FILE* file_{nullptr};
191
194};
195
196} // namespace num_collect::logging::sinks
Class of exception on errors in files.
Definition exception.h:101
auto operator=(file_wrapper &&obj) noexcept -> file_wrapper &
Move assignment operator.
auto file() const noexcept -> std::FILE *
Get the file pointer.
void set_stderr()
Set this file to standard error.
void set_stdout()
Set this file to standard output.
void swap(file_wrapper &obj) noexcept
Swap with another object.
void write(std::string_view data)
Write data.
file_wrapper(const std::string &filepath, const char *mode)
Constructor to open a file.
file_wrapper(file_wrapper &&obj) noexcept
Move constructor.
void open(const std::string &filepath, const char *mode)
Open a file.
void close() noexcept
Close this file.
file_wrapper() noexcept=default
Constructor.
bool close_on_destruction_
Whether to close the file when destructed.
Definition of exceptions.
Definition of format_errno function.
Namespace of log sinks.
auto format_errno(fmt::format_string< Args... > format, Args &&... args) -> std::string
Format a message with error message determined by errno.
STL namespace.