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