Debian Codegraph @

src/mpt/io_file/fstream.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_IO_FILE_FSTREAM_HPP
4 #define MPT_IO_FILE_FSTREAM_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/base/span.hpp"
11 #include "mpt/io/base.hpp"
12 #include "mpt/io/io.hpp"
13 #include "mpt/io/io_stdstream.hpp"
14 #include "mpt/path/os_path.hpp"
15 #include "mpt/path/os_path_long.hpp"
16 #include "mpt/string_transcode/transcode.hpp"
17
18 #if defined(MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR)
19 #if MPT_GCC_AT_LEAST(9, 1, 0) && !defined(MPT_COMPILER_QUIRK_NO_FILESYSTEM)
20 #include <filesystem>
21 #endif // MPT_GCC_AT_LEAST(9,1,0) && !defined(MPT_COMPILER_QUIRK_NO_FILESYSTEM)
22 #endif // MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
23 #include <fstream>
24 #include <ios>
25 #include <string>
26 #include <string_view>
27
28 #if MPT_LIBCXX_MS
29 #include <cstdio>
30 #endif // MPT_LIBCXX_MS
31
32
33
34 namespace mpt {
35 inline namespace MPT_INLINE_NS {
36
37
38
39 namespace IO {
40
41
42
43 namespace detail {
44
45 template <typename Tbase>
46 inline void fstream_open(Tbase & base, const mpt::os_path & filename, std::ios_base::openmode mode) {
47 #if defined(MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR)
48 #if MPT_GCC_AT_LEAST(9, 1, 0) && !defined(MPT_COMPILER_QUIRK_NO_FILESYSTEM)
49 base.open(static_cast<std::filesystem::path>(mpt::support_long_path(filename)), mode);
50 #else // !MPT_GCC_AT_LEAST(9,1,0) || MPT_COMPILER_QUIRK_NO_FILESYSTEM
51 // Warning: MinGW with GCC earlier than 9.1 detected. Standard library does neither provide std::fstream wchar_t overloads nor std::filesystem with wchar_t support. Unicode filename support is thus unavailable.
52 base.open(mpt::transcode<std::string>(mpt::logical_encoding::locale, mpt::support_long_path(filename)).c_str(), mode);
53 #endif // MPT_GCC_AT_LEAST(9,1,0) && !MPT_COMPILER_QUIRK_NO_FILESYSTEM
54 #else // !MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
55 base.open(mpt::support_long_path(filename).c_str(), mode);
56 #endif // MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
57 }
58
59 } // namespace detail
60
61 // We cannot rely on implicit conversion of mpt::path to std::filesystem::path when constructing std::fstream
62 // because of broken overload implementation in GCC libstdc++ 8, 9, 10.
63 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95642
64 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90704
65
66 class fstream
67 : public std::fstream {
68 private:
69 using base_type = std::fstream;
70
71 public:
72 explicit fstream(const mpt::os_path & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) {
73 detail::fstream_open<base_type>(*this, filename, mode);
74 }
75 #if MPT_LIBCXX_MS
76 protected:
77 fstream(std::FILE * file)
78 : std::fstream(file) {
79 }
80 #endif // MPT_LIBCXX_MS
81 public:
82 void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
83 void open(const std::string_view & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
84 void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
85 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
86 void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
87 void open(const std::wstring_view & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
88 void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
89 #endif
90 };
91
92 class ifstream
93 : public std::ifstream {
94 private:
95 using base_type = std::ifstream;
96
97 public:
98 explicit ifstream(const mpt::os_path & filename, std::ios_base::openmode mode = std::ios_base::in) {
99 detail::fstream_open<base_type>(*this, filename, mode);
100 }
101 #if MPT_LIBCXX_MS
102 protected:
103 ifstream(std::FILE * file)
104 : std::ifstream(file) {
105 }
106 #endif // MPT_LIBCXX_MS
107 public:
108 void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
109 void open(const std::string_view & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
110 void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
111 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
112 void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
113 void open(const std::wstring_view & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
114 void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
115 #endif
116 };
117
118 class ofstream
119 : public std::ofstream {
120 private:
121 using base_type = std::ofstream;
122
123 public:
124 explicit ofstream(const mpt::os_path & filename, std::ios_base::openmode mode = std::ios_base::out) {
125 detail::fstream_open<base_type>(*this, filename, mode);
126 }
127 #if MPT_LIBCXX_MS
128 protected:
129 ofstream(std::FILE * file)
130 : std::ofstream(file) {
131 }
132 #endif // MPT_LIBCXX_MS
133 public:
134 void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
135 void open(const std::string_view & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
136 void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
137 #if MPT_OS_WINDOWS && !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
138 void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
139 void open(const std::wstring_view & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
140 void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
141 #endif
142 };
143
144
145
146 } // namespace IO
147
148
149
150 } // namespace MPT_INLINE_NS
151 } // namespace mpt
152
153
154
155 #endif // MPT_IO_FILE_FSTREAM_HPP