3#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
23#ifdef PLATFORM_WINDOWS
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>
34#error "Configuration for NativeFileStream backend insufficient."
38#if defined(PLATFORM_UNIX)
43#elif defined(PLATFORM_WINDOWS)
60#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
62#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
63using StreamBuffer = __gnu_cxx::stdio_filebuf<char>;
65using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
68struct NativeFileParams {
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)
79 if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
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;
92 access = GENERIC_READ;
93 shareMode = FILE_SHARE_READ;
94 creation = OPEN_EXISTING;
96 if (cppOpenMode & ios_base::app) {
97 openMode |= _O_APPEND;
100 if (cppOpenMode & ios_base::trunc) {
101 openMode |= _O_TRUNC;
102 creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS;
113 NativeFileParams(ios_base::openmode cppOpenMode)
116 if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
117 if (cppOpenMode & ios_base::app) {
119 openFlags = O_RDWR | O_APPEND;
120 }
else if (cppOpenMode & ios_base::trunc) {
122 openFlags = O_RDWR | O_TRUNC;
127 }
else if (cppOpenMode & ios_base::in) {
129 openFlags = O_RDONLY;
130 }
else if (cppOpenMode & ios_base::out) {
131 if (cppOpenMode & ios_base::app) {
133 openFlags = O_WRONLY | O_APPEND;
134 }
else if (cppOpenMode & ios_base::trunc) {
136 openFlags = O_WRONLY | O_TRUNC | O_CREAT;
139 openFlags = O_WRONLY | O_CREAT;
142 if (cppOpenMode & ios_base::binary) {
147 std::string openMode;
160NativeFileStream::FileBuffer::FileBuffer(std::basic_streambuf<char> *buffer)
169NativeFileStream::FileBuffer::FileBuffer(
const char *path, ios_base::openmode openMode)
171#ifdef PLATFORM_WINDOWS
173 const auto widePath(makeWidePath(path));
177 const NativeFileParams nativeParams(openMode);
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()));
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()));
192 buffer = make_unique<StreamBuffer>(descriptor, openMode);
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(
static_cast<int>(GetLastError()), std::system_category()));
199 buffer = std::make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
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()));
206 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
215NativeFileStream::FileBuffer::FileBuffer(
const std::string &path, ios_base::openmode openMode)
226NativeFileStream::FileBuffer::FileBuffer(
int fileDescriptor, ios_base::openmode openMode)
227 : descriptor(fileDescriptor)
229#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
230 buffer = make_unique<StreamBuffer>(descriptor, openMode);
233#ifdef PLATFORM_WINDOWS
234 handle =
reinterpret_cast<Handle
>(_get_osfhandle(descriptor));
235 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
237 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
245NativeFileStream::NativeFileStream()
246 : iostream(new StreamBuffer)
254NativeFileStream::NativeFileStream(NativeFileStream &&other)
255 : iostream(other.m_data.buffer.release())
258#ifdef PLATFORM_WINDOWS
259 m_data.handle = other.m_data.handle;
261 m_data.descriptor = other.m_data.descriptor;
267NativeFileStream::~NativeFileStream()
274bool NativeFileStream::isOpen()
const
276 return m_data.buffer &&
static_cast<const StreamBuffer *
>(m_data.buffer.get())->is_open();
290void NativeFileStream::open(
const char *path, ios_base::openmode openMode)
292 setData(FileBuffer(path, openMode), openMode);
298void NativeFileStream::open(
const std::string &path, ios_base::openmode openMode)
300 open(path.data(), openMode);
310void NativeFileStream::open(
int fileDescriptor, ios_base::openmode openMode)
312 setData(FileBuffer(fileDescriptor, openMode), openMode);
318void NativeFileStream::close()
321 static_cast<StreamBuffer *
>(m_data.buffer.get())->close();
322#ifdef PLATFORM_WINDOWS
323 m_data.handle =
nullptr;
325 m_data.descriptor = -1;
332void NativeFileStream::setData(FileBuffer data, std::ios_base::openmode openMode)
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)
339 if (m_openMode & ios_base::app) {
340 seekp(0, ios_base::end);
345#ifdef PLATFORM_WINDOWS
350std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(std::string_view path)
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);
358 return std::move(widePath.first);
365std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(
const std::string &path)
367 return makeWidePath(std::string_view(path));
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Contains all utilities provides by the c++utilities library.
std::fstream NativeFileStream