fstream.hpp
7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// boost/filesystem/fstream.hpp ------------------------------------------------------//
// Copyright Beman Dawes 2002
// Copyright Andrey Semashev 2021
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Library home page: http://www.boost.org/libs/filesystem
//--------------------------------------------------------------------------------------//
#ifndef BOOST_FILESYSTEM_FSTREAM_HPP
#define BOOST_FILESYSTEM_FSTREAM_HPP
#include <boost/filesystem/config.hpp>
#include <boost/filesystem/path.hpp>
#include <cstddef>
#include <iosfwd>
#include <fstream>
#include <boost/filesystem/detail/header.hpp> // must be the last #include
#if defined(BOOST_WINDOWS_API)
// On Windows, except for standard libaries known to have wchar_t overloads for
// file stream I/O, use path::string() to get a narrow character c_str()
#if (defined(_CPPLIB_VER) && _CPPLIB_VER >= 405 && !defined(_STLPORT_VERSION)) || \
(defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 7000 && defined(_LIBCPP_HAS_OPEN_WITH_WCHAR))
// Use wide characters directly
// Note: We don't use C++17 std::filesystem::path as a means to pass wide paths
// to file streams because of various problems:
// - std::filesystem is available in gcc 8 but it is broken there (fails to compile path definition
// on Windows). Compilation errors seem to be fixed since gcc 9.
// - In gcc 10.2 and clang 8.0.1 on Cygwin64, the path attempts to convert the wide string to narrow
// and fails in runtime. This may be system locale dependent, and performing character code conversion
// is against the purpose of using std::filesystem::path anyway.
// - Other std::filesystem implementations were not tested, so it is not known if they actually work
// with wide paths.
#define BOOST_FILESYSTEM_C_STR(p) p.c_str()
#else
// Use narrow characters, since wide not available
#define BOOST_FILESYSTEM_C_STR(p) p.string().c_str()
#endif
#endif // defined(BOOST_WINDOWS_API)
#if !defined(BOOST_FILESYSTEM_C_STR)
#define BOOST_FILESYSTEM_C_STR(p) p.c_str()
#endif
#if defined(BOOST_MSVC)
#pragma warning(push)
// 'boost::filesystem::basic_fstream<charT>' : inherits 'std::basic_istream<_Elem,_Traits>::std::basic_istream<_Elem,_Traits>::_Add_vtordisp1' via dominance
#pragma warning(disable : 4250)
#endif
namespace boost {
namespace filesystem {
//--------------------------------------------------------------------------------------//
// basic_filebuf //
//--------------------------------------------------------------------------------------//
template< class charT, class traits = std::char_traits< charT > >
class basic_filebuf :
public std::basic_filebuf< charT, traits >
{
public:
BOOST_DEFAULTED_FUNCTION(basic_filebuf(), {})
BOOST_DELETED_FUNCTION(basic_filebuf(basic_filebuf const&))
BOOST_DELETED_FUNCTION(basic_filebuf const& operator=(basic_filebuf const&))
public:
basic_filebuf< charT, traits >* open(path const& p, std::ios_base::openmode mode)
{
return std::basic_filebuf< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode) ? this : NULL;
}
};
//--------------------------------------------------------------------------------------//
// basic_ifstream //
//--------------------------------------------------------------------------------------//
template< class charT, class traits = std::char_traits< charT > >
class basic_ifstream :
public std::basic_ifstream< charT, traits >
{
public:
BOOST_DEFAULTED_FUNCTION(basic_ifstream(), {})
// use two signatures, rather than one signature with default second
// argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
explicit basic_ifstream(path const& p) :
std::basic_ifstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in) {}
basic_ifstream(path const& p, std::ios_base::openmode mode) :
std::basic_ifstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), mode) {}
BOOST_DELETED_FUNCTION(basic_ifstream(basic_ifstream const&))
BOOST_DELETED_FUNCTION(basic_ifstream const& operator=(basic_ifstream const&))
public:
void open(path const& p)
{
std::basic_ifstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in);
}
void open(path const& p, std::ios_base::openmode mode)
{
std::basic_ifstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode);
}
};
//--------------------------------------------------------------------------------------//
// basic_ofstream //
//--------------------------------------------------------------------------------------//
template< class charT, class traits = std::char_traits< charT > >
class basic_ofstream :
public std::basic_ofstream< charT, traits >
{
public:
BOOST_DEFAULTED_FUNCTION(basic_ofstream(), {})
// use two signatures, rather than one signature with default second
// argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
explicit basic_ofstream(path const& p) :
std::basic_ofstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), std::ios_base::out) {}
basic_ofstream(path const& p, std::ios_base::openmode mode) :
std::basic_ofstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), mode) {}
BOOST_DELETED_FUNCTION(basic_ofstream(basic_ofstream const&))
BOOST_DELETED_FUNCTION(basic_ofstream const& operator=(basic_ofstream const&))
public:
void open(path const& p)
{
std::basic_ofstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::out);
}
void open(path const& p, std::ios_base::openmode mode)
{
std::basic_ofstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode);
}
};
//--------------------------------------------------------------------------------------//
// basic_fstream //
//--------------------------------------------------------------------------------------//
template< class charT, class traits = std::char_traits< charT > >
class basic_fstream :
public std::basic_fstream< charT, traits >
{
public:
BOOST_DEFAULTED_FUNCTION(basic_fstream(), {})
// use two signatures, rather than one signature with default second
// argument, to workaround VC++ 7.1 bug (ID VSWhidbey 38416)
explicit basic_fstream(path const& p) :
std::basic_fstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in | std::ios_base::out) {}
basic_fstream(path const& p, std::ios_base::openmode mode) :
std::basic_fstream< charT, traits >(BOOST_FILESYSTEM_C_STR(p), mode) {}
BOOST_DELETED_FUNCTION(basic_fstream(basic_fstream const&))
BOOST_DELETED_FUNCTION(basic_fstream const& operator=(basic_fstream const&))
public:
void open(path const& p)
{
std::basic_fstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), std::ios_base::in | std::ios_base::out);
}
void open(path const& p, std::ios_base::openmode mode)
{
std::basic_fstream< charT, traits >::open(BOOST_FILESYSTEM_C_STR(p), mode);
}
};
//--------------------------------------------------------------------------------------//
// typedefs //
//--------------------------------------------------------------------------------------//
typedef basic_filebuf< char > filebuf;
typedef basic_ifstream< char > ifstream;
typedef basic_ofstream< char > ofstream;
typedef basic_fstream< char > fstream;
typedef basic_filebuf< wchar_t > wfilebuf;
typedef basic_ifstream< wchar_t > wifstream;
typedef basic_ofstream< wchar_t > wofstream;
typedef basic_fstream< wchar_t > wfstream;
} // namespace filesystem
} // namespace boost
#if defined(BOOST_MSVC)
#pragma warning(pop)
#endif
#include <boost/filesystem/detail/footer.hpp>
#endif // BOOST_FILESYSTEM_FSTREAM_HPP