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)
57#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
59#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
60using StreamBuffer = __gnu_cxx::stdio_filebuf<char>;
62using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
65struct NativeFileParams {
67#ifdef PLATFORM_WINDOWS
68 NativeFileParams(ios_base::openmode cppOpenMode)
69 : openMode(cppOpenMode & ios_base::binary ? _O_BINARY : 0)
70 , flags(cppOpenMode & ios_base::binary ? 0 : _O_TEXT)
76 if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
78 access = GENERIC_READ | GENERIC_WRITE;
79 shareMode = FILE_SHARE_READ;
80 creation = OPEN_EXISTING;
81 }
else if (cppOpenMode & ios_base::out) {
82 openMode |= _O_WRONLY | _O_CREAT;
83 permissions = _S_IREAD | _S_IWRITE;
84 access = GENERIC_WRITE;
85 creation = OPEN_ALWAYS;
86 }
else if (cppOpenMode & ios_base::in) {
87 openMode |= _O_RDONLY;
89 access = GENERIC_READ;
90 shareMode = FILE_SHARE_READ;
91 creation = OPEN_EXISTING;
93 if (cppOpenMode & ios_base::app) {
94 openMode |= _O_APPEND;
97 if (cppOpenMode & ios_base::trunc) {
99 creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS;
110 NativeFileParams(ios_base::openmode cppOpenMode)
113 if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
114 if (cppOpenMode & ios_base::app) {
116 openFlags = O_RDWR | O_APPEND;
117 }
else if (cppOpenMode & ios_base::trunc) {
119 openFlags = O_RDWR | O_TRUNC;
124 }
else if (cppOpenMode & ios_base::in) {
126 openFlags = O_RDONLY;
127 }
else if (cppOpenMode & ios_base::out) {
128 if (cppOpenMode & ios_base::app) {
130 openFlags = O_WRONLY | O_APPEND;
131 }
else if (cppOpenMode & ios_base::trunc) {
133 openFlags = O_WRONLY | O_TRUNC | O_CREAT;
136 openFlags = O_WRONLY | O_CREAT;
139 if (cppOpenMode & ios_base::binary) {
144 std::string openMode;
157NativeFileStream::FileBuffer::FileBuffer(std::basic_streambuf<char> *buffer)
166NativeFileStream::FileBuffer::FileBuffer(
const char *path, ios_base::openmode openMode)
168#ifdef PLATFORM_WINDOWS
170 const auto widePath(makeWidePath(path));
174 const NativeFileParams nativeParams(openMode);
177#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
178#ifdef PLATFORM_WINDOWS
179 descriptor = _wopen(widePath.get(), nativeParams.openMode, nativeParams.permissions);
180 if (descriptor == -1) {
181 throw std::ios_base::failure(
"_wopen failed", std::error_code(errno, std::system_category()));
184 descriptor = ::open(path, nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
185 if (descriptor == -1) {
186 throw std::ios_base::failure(
"open failed", std::error_code(errno, std::system_category()));
189 buffer = make_unique<StreamBuffer>(descriptor, openMode);
191#ifdef PLATFORM_WINDOWS
192 handle = CreateFileW(widePath.get(), nativeParams.access, nativeParams.shareMode,
nullptr, nativeParams.creation, FILE_ATTRIBUTE_NORMAL,
nullptr);
193 if (handle == INVALID_HANDLE_VALUE) {
194 throw std::ios_base::failure(
"CreateFileW failed", std::error_code(GetLastError(), std::system_category()));
196 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
199 descriptor = ::open(path.data(), nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
200 if (descriptor == -1) {
201 throw std::ios_base::failure(
"open failed", std::error_code(errno, std::system_category()));
203 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
212NativeFileStream::FileBuffer::FileBuffer(
const std::string &path, ios_base::openmode openMode)
223NativeFileStream::FileBuffer::FileBuffer(
int fileDescriptor, ios_base::openmode openMode)
224 : descriptor(fileDescriptor)
226#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
227 buffer = make_unique<StreamBuffer>(descriptor, openMode);
230#ifdef PLATFORM_WINDOWS
231 handle =
reinterpret_cast<Handle
>(_get_osfhandle(descriptor));
232 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
234 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
242NativeFileStream::NativeFileStream()
243 : iostream(new StreamBuffer)
251NativeFileStream::NativeFileStream(NativeFileStream &&other)
252 : iostream(other.m_data.buffer.release())
255#ifdef PLATFORM_WINDOWS
256 m_data.handle = other.m_data.handle;
258 m_data.descriptor = other.m_data.descriptor;
264NativeFileStream::~NativeFileStream()
271bool NativeFileStream::isOpen()
const
273 return m_data.buffer &&
static_cast<const StreamBuffer *
>(m_data.buffer.get())->is_open();
287void NativeFileStream::open(
const char *path, ios_base::openmode openMode)
289 setData(FileBuffer(path, openMode), openMode);
295void NativeFileStream::open(
const std::string &path, ios_base::openmode openMode)
297 open(path.data(), openMode);
307void NativeFileStream::open(
int fileDescriptor, ios_base::openmode openMode)
309 setData(FileBuffer(fileDescriptor, openMode), openMode);
315void NativeFileStream::close()
318 static_cast<StreamBuffer *
>(m_data.buffer.get())->close();
319#ifdef PLATFORM_WINDOWS
320 m_data.handle =
nullptr;
322 m_data.descriptor = -1;
329void NativeFileStream::setData(FileBuffer data, std::ios_base::openmode openMode)
331 rdbuf(data.buffer.get());
332 m_data = std::move(data);
333 m_openMode = openMode;
334#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
336 if (m_openMode & ios_base::app) {
337 seekp(0, ios_base::end);
342#ifdef PLATFORM_WINDOWS
347std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(std::string_view path)
349 auto ec = std::error_code();
350 auto size = path.size() <
static_cast<std::size_t
>(std::numeric_limits<int>::max() - 1) ?
static_cast<int>(path.size() + 1) : -1;
351 auto widePath = ::CppUtilities::convertMultiByteToWide(ec, path.data(), size);
352 if (!widePath.first) {
353 throw std::ios_base::failure(
"converting path to UTF-16", ec);
355 return std::move(widePath.first);
362std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(
const std::string &path)
364 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