Debian Codegraph @

src/mpt/io_file/fileref.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_FILE_FILEREF_HPP
4 #define MPT_IO_FILE_FILEREF_HPP
5
6
7
8 #include "mpt/base/alloc.hpp"
9 #include "mpt/base/memory.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/base/saturate_cast.hpp"
12 #include "mpt/base/span.hpp"
13 #include "mpt/io/base.hpp"
14 #include "mpt/io/io.hpp"
15 #include "mpt/io/io_stdstream.hpp"
16 #include "mpt/io_file/fstream.hpp"
17 #include "mpt/path/os_path.hpp"
18
19 #include <algorithm>
20 #include <string>
21 #include <vector>
22
23 #include <cstddef>
24
25
26
27 namespace mpt {
28 inline namespace MPT_INLINE_NS {
29
30
31
32 namespace IO {
33
34
35
36 // FileRef is a simple reference to an on-disk file by the means of a
37 // filename which allows easy assignment of the whole file contents to and from
38 // byte buffers.
39
40 class FileRef {
41 private:
42 const mpt::os_path m_Filename;
43
44 public:
45 FileRef(const mpt::os_path & filename)
46 : m_Filename(filename) {
47 return;
48 }
49
50 public:
51 FileRef & operator=(const std::vector<std::byte> & data) {
52 mpt::IO::ofstream file(m_Filename, std::ios::binary);
53 file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
54 mpt::IO::WriteRaw(file, mpt::as_span(data));
55 mpt::IO::Flush(file);
56 return *this;
57 }
58
59 FileRef & operator=(const std::vector<char> & data) {
60 mpt::IO::ofstream file(m_Filename, std::ios::binary);
61 file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
62 mpt::IO::WriteRaw(file, mpt::as_span(data));
63 mpt::IO::Flush(file);
64 return *this;
65 }
66
67 FileRef & operator=(const std::string & data) {
68 mpt::IO::ofstream file(m_Filename, std::ios::binary);
69 file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
70 mpt::IO::WriteRaw(file, mpt::as_span(data));
71 mpt::IO::Flush(file);
72 return *this;
73 }
74
75 operator std::vector<std::byte>() const {
76 mpt::IO::ifstream file(m_Filename, std::ios::binary);
77 if (!mpt::IO::IsValid(file)) {
78 return std::vector<std::byte>();
79 }
80 file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
81 mpt::IO::SeekEnd(file);
82 std::vector<std::byte> buf(mpt::saturate_cast<std::size_t>(mpt::IO::TellRead(file)));
83 mpt::IO::SeekBegin(file);
84 mpt::IO::ReadRaw(file, mpt::as_span(buf));
85 return buf;
86 }
87
88 operator std::vector<char>() const {
89 mpt::IO::ifstream file(m_Filename, std::ios::binary);
90 if (!mpt::IO::IsValid(file)) {
91 return std::vector<char>();
92 }
93 file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
94 mpt::IO::SeekEnd(file);
95 std::vector<char> buf(mpt::saturate_cast<std::size_t>(mpt::IO::TellRead(file)));
96 mpt::IO::SeekBegin(file);
97 mpt::IO::ReadRaw(file, mpt::as_span(buf));
98 return buf;
99 }
100
101 operator std::string() const {
102 mpt::IO::ifstream file(m_Filename, std::ios::binary);
103 if (!mpt::IO::IsValid(file)) {
104 return std::string();
105 }
106 file.exceptions(std::ios_base::failbit | std::ios_base::badbit);
107 mpt::IO::SeekEnd(file);
108 std::vector<char> buf(mpt::saturate_cast<std::size_t>(mpt::IO::TellRead(file)));
109 mpt::IO::SeekBegin(file);
110 mpt::IO::ReadRaw(file, mpt::as_span(buf));
111 return mpt::buffer_cast<std::string>(buf);
112 }
113 };
114
115
116
117 } // namespace IO
118
119
120
121 } // namespace MPT_INLINE_NS
122 } // namespace mpt
123
124
125
126 #endif // MPT_IO_FILE_FILEREF_HPP