C++ Utilities 5.31.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
nativefilestream.cpp
Go to the documentation of this file.
2
3#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
4
10
22
23#ifdef PLATFORM_WINDOWS
25#endif
26
27// include header files for file buffer implementation
28#if defined(CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF)
29#include <ext/stdio_filebuf.h>
30#elif defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
31#include <boost/iostreams/device/file_descriptor.hpp>
32#include <boost/iostreams/stream.hpp>
33#else
34#error "Configuration for NativeFileStream backend insufficient."
35#endif
36
37// include platform specific header
38#if defined(PLATFORM_UNIX)
39#include <fcntl.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42#elif defined(PLATFORM_WINDOWS)
43#include <fcntl.h>
44#include <io.h>
45#include <limits>
46#include <sys/stat.h> // yes, this is needed under Windows (https://msdn.microsoft.com/en-US/library/5yhhz3y7.aspx)
47#include <windows.h>
48#ifdef max
49#undef max // see explanation in stringconversion.cpp
50#endif
51#endif
52
53#endif
54
55using namespace std;
56
57namespace CppUtilities {
58
59#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
60
61#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
62using StreamBuffer = __gnu_cxx::stdio_filebuf<char>;
63#else // CPP_UTILITIES_USE_BOOST_IOSTREAMS
64using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
65#endif
66
67struct NativeFileParams {
68
69#ifdef PLATFORM_WINDOWS
70 NativeFileParams(ios_base::openmode cppOpenMode)
71 : openMode(cppOpenMode & ios_base::binary ? _O_BINARY : 0)
72 , flags(cppOpenMode & ios_base::binary ? 0 : _O_TEXT)
73 , permissions(0)
74 , access(0)
75 , shareMode(0)
76 , creation(0)
77 {
78 if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
79 openMode |= _O_RDWR;
80 access = GENERIC_READ | GENERIC_WRITE;
81 shareMode = FILE_SHARE_READ;
82 creation = OPEN_EXISTING;
83 } else if (cppOpenMode & ios_base::out) {
84 openMode |= _O_WRONLY | _O_CREAT;
85 permissions = _S_IREAD | _S_IWRITE;
86 access = GENERIC_WRITE;
87 creation = OPEN_ALWAYS;
88 } else if (cppOpenMode & ios_base::in) {
89 openMode |= _O_RDONLY;
90 flags |= _O_RDONLY;
91 access = GENERIC_READ;
92 shareMode = FILE_SHARE_READ;
93 creation = OPEN_EXISTING;
94 }
95 if (cppOpenMode & ios_base::app) {
96 openMode |= _O_APPEND;
97 flags |= _O_APPEND;
98 }
99 if (cppOpenMode & ios_base::trunc) {
100 openMode |= _O_TRUNC;
101 creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS;
102 }
103 }
104
105 int openMode;
106 int flags;
107 int permissions;
108 DWORD access;
109 DWORD shareMode;
110 DWORD creation;
111#else
112 NativeFileParams(ios_base::openmode cppOpenMode)
113 : openFlags(0)
114 {
115 if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
116 if (cppOpenMode & ios_base::app) {
117 openMode = "a+";
118 openFlags = O_RDWR | O_APPEND;
119 } else if (cppOpenMode & ios_base::trunc) {
120 openMode = "w+";
121 openFlags = O_RDWR | O_TRUNC;
122 } else {
123 openMode = "r+";
124 openFlags = O_RDWR;
125 }
126 } else if (cppOpenMode & ios_base::in) {
127 openMode = 'r';
128 openFlags = O_RDONLY;
129 } else if (cppOpenMode & ios_base::out) {
130 if (cppOpenMode & ios_base::app) {
131 openMode = 'a';
132 openFlags = O_WRONLY | O_APPEND;
133 } else if (cppOpenMode & ios_base::trunc) {
134 openMode = 'w';
135 openFlags = O_WRONLY | O_TRUNC | O_CREAT;
136 } else {
137 openMode = "w";
138 openFlags = O_WRONLY | O_CREAT;
139 }
140 }
141 if (cppOpenMode & ios_base::binary) {
142 openMode += 'b';
143 }
144 }
145
146 std::string openMode;
147 int openFlags;
148#endif
149};
150
155
159NativeFileStream::FileBuffer::FileBuffer(std::basic_streambuf<char> *buffer)
160 : buffer(buffer)
161{
162}
163
168NativeFileStream::FileBuffer::FileBuffer(const char *path, ios_base::openmode openMode)
169{
170#ifdef PLATFORM_WINDOWS
171 // convert path to UTF-16
172 const auto widePath(makeWidePath(path));
173#endif
174
175 // compute native params
176 const NativeFileParams nativeParams(openMode);
177
178// open native file handle or descriptor
179#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
180#ifdef PLATFORM_WINDOWS
181 descriptor = _wopen(widePath.get(), nativeParams.openMode, nativeParams.permissions);
182 if (descriptor == -1) {
183 throw std::ios_base::failure("_wopen failed", std::error_code(errno, std::system_category()));
184 }
185#else
186 descriptor = ::open(path, nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
187 if (descriptor == -1) {
188 throw std::ios_base::failure("open failed", std::error_code(errno, std::system_category()));
189 }
190#endif
191 buffer = make_unique<StreamBuffer>(descriptor, openMode);
192#else // CPP_UTILITIES_USE_BOOST_IOSTREAMS
193#ifdef PLATFORM_WINDOWS
194 handle = CreateFileW(widePath.get(), nativeParams.access, nativeParams.shareMode, nullptr, nativeParams.creation, FILE_ATTRIBUTE_NORMAL, nullptr);
195 if (handle == INVALID_HANDLE_VALUE) {
196 throw std::ios_base::failure("CreateFileW failed", std::error_code(static_cast<int>(GetLastError()), std::system_category()));
197 }
198 buffer = std::make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
199 // if we wanted to open assign the descriptor as well: descriptor = _open_osfhandle(reinterpret_cast<std::intptr_t>(handle), nativeParams.flags);
200#else
201 descriptor = ::open(path, nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
202 if (descriptor == -1) {
203 throw std::ios_base::failure("open failed", std::error_code(errno, std::system_category()));
204 }
205 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
206#endif
207#endif
208}
209
214NativeFileStream::FileBuffer::FileBuffer(const std::string &path, ios_base::openmode openMode)
215 : NativeFileStream::FileBuffer(path.data(), openMode)
216{
217}
218
225NativeFileStream::FileBuffer::FileBuffer(int fileDescriptor, ios_base::openmode openMode)
226 : descriptor(fileDescriptor)
227{
228#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
229 buffer = make_unique<StreamBuffer>(descriptor, openMode);
230#else // CPP_UTILITIES_USE_BOOST_IOSTREAMS
231 CPP_UTILITIES_UNUSED(openMode)
232#ifdef PLATFORM_WINDOWS
233 handle = reinterpret_cast<Handle>(_get_osfhandle(descriptor));
234 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
235#else
236 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
237#endif
238#endif
239}
240
244NativeFileStream::NativeFileStream()
245 : iostream(new StreamBuffer)
246 , m_data(rdbuf())
247{
248}
249
253NativeFileStream::NativeFileStream(NativeFileStream &&other)
254 : iostream(other.m_data.buffer.release())
255 , m_data(rdbuf())
256{
257#ifdef PLATFORM_WINDOWS
258 m_data.handle = other.m_data.handle;
259#endif
260 m_data.descriptor = other.m_data.descriptor;
261}
262
266NativeFileStream::~NativeFileStream()
267{
268}
269
273bool NativeFileStream::isOpen() const
274{
275 return m_data.buffer && static_cast<const StreamBuffer *>(m_data.buffer.get())->is_open();
276}
277
289void NativeFileStream::open(const char *path, ios_base::openmode openMode)
290{
291 setData(FileBuffer(path, openMode), openMode);
292}
293
297void NativeFileStream::open(const std::string &path, ios_base::openmode openMode)
298{
299 open(path.data(), openMode);
300}
301
309void NativeFileStream::open(int fileDescriptor, ios_base::openmode openMode)
310{
311 setData(FileBuffer(fileDescriptor, openMode), openMode);
312}
313
317void NativeFileStream::close()
318{
319 if (m_data.buffer) {
320 static_cast<StreamBuffer *>(m_data.buffer.get())->close();
321#ifdef PLATFORM_WINDOWS
322 m_data.handle = nullptr;
323#endif
324 m_data.descriptor = -1;
325 }
326}
327
331void NativeFileStream::setData(FileBuffer data, std::ios_base::openmode openMode)
332{
333 rdbuf(data.buffer.get());
334 m_data = std::move(data);
335 m_openMode = openMode;
336#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
337 // workaround append flag dysfunctioning
338 if (m_openMode & ios_base::app) {
339 seekp(0, ios_base::end);
340 }
341#endif
342}
343
344#ifdef PLATFORM_WINDOWS
349std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(std::string_view path)
350{
351 auto ec = std::error_code();
352 auto size = path.size() < static_cast<std::size_t>(std::numeric_limits<int>::max() - 1) ? static_cast<int>(path.size() + 1) : -1;
353 auto widePath = ::CppUtilities::convertMultiByteToWide(ec, path.data(), size);
354 if (!widePath.first) {
355 throw std::ios_base::failure("converting path to UTF-16", ec);
356 }
357 return std::move(widePath.first);
358}
359
364std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(const std::string &path)
365{
366 return makeWidePath(std::string_view(path));
367}
368#endif
369
370#else
371
372// std::fstream is used
373
374#endif
375} // namespace CppUtilities
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Definition global.h:108
Contains all utilities provided by the c++utilities library.
std::fstream NativeFileStream
STL namespace.