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)
42#elif defined(PLATFORM_WINDOWS)
59#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
61#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
62using StreamBuffer = __gnu_cxx::stdio_filebuf<char>;
64using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
67struct NativeFileParams {
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)
78 if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
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;
91 access = GENERIC_READ;
92 shareMode = FILE_SHARE_READ;
93 creation = OPEN_EXISTING;
95 if (cppOpenMode & ios_base::app) {
96 openMode |= _O_APPEND;
99 if (cppOpenMode & ios_base::trunc) {
100 openMode |= _O_TRUNC;
101 creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS;
112 NativeFileParams(ios_base::openmode cppOpenMode)
115 if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
116 if (cppOpenMode & ios_base::app) {
118 openFlags = O_RDWR | O_APPEND;
119 }
else if (cppOpenMode & ios_base::trunc) {
121 openFlags = O_RDWR | O_TRUNC;
126 }
else if (cppOpenMode & ios_base::in) {
128 openFlags = O_RDONLY;
129 }
else if (cppOpenMode & ios_base::out) {
130 if (cppOpenMode & ios_base::app) {
132 openFlags = O_WRONLY | O_APPEND;
133 }
else if (cppOpenMode & ios_base::trunc) {
135 openFlags = O_WRONLY | O_TRUNC | O_CREAT;
138 openFlags = O_WRONLY | O_CREAT;
141 if (cppOpenMode & ios_base::binary) {
146 std::string openMode;
159NativeFileStream::FileBuffer::FileBuffer(std::basic_streambuf<char> *buffer)
168NativeFileStream::FileBuffer::FileBuffer(
const char *path, ios_base::openmode openMode)
170#ifdef PLATFORM_WINDOWS
172 const auto widePath(makeWidePath(path));
176 const NativeFileParams nativeParams(openMode);
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()));
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()));
191 buffer = make_unique<StreamBuffer>(descriptor, openMode);
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()));
198 buffer = std::make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
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()));
205 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
214NativeFileStream::FileBuffer::FileBuffer(
const std::string &path, ios_base::openmode openMode)
225NativeFileStream::FileBuffer::FileBuffer(
int fileDescriptor, ios_base::openmode openMode)
226 : descriptor(fileDescriptor)
228#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
229 buffer = make_unique<StreamBuffer>(descriptor, openMode);
232#ifdef PLATFORM_WINDOWS
233 handle =
reinterpret_cast<Handle
>(_get_osfhandle(descriptor));
234 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
236 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
244NativeFileStream::NativeFileStream()
245 : iostream(new StreamBuffer)
253NativeFileStream::NativeFileStream(NativeFileStream &&other)
254 : iostream(other.m_data.buffer.release())
257#ifdef PLATFORM_WINDOWS
258 m_data.handle = other.m_data.handle;
260 m_data.descriptor = other.m_data.descriptor;
266NativeFileStream::~NativeFileStream()
273bool NativeFileStream::isOpen()
const
275 return m_data.buffer &&
static_cast<const StreamBuffer *
>(m_data.buffer.get())->is_open();
289void NativeFileStream::open(
const char *path, ios_base::openmode openMode)
291 setData(FileBuffer(path, openMode), openMode);
297void NativeFileStream::open(
const std::string &path, ios_base::openmode openMode)
299 open(path.data(), openMode);
309void NativeFileStream::open(
int fileDescriptor, ios_base::openmode openMode)
311 setData(FileBuffer(fileDescriptor, openMode), openMode);
317void NativeFileStream::close()
320 static_cast<StreamBuffer *
>(m_data.buffer.get())->close();
321#ifdef PLATFORM_WINDOWS
322 m_data.handle =
nullptr;
324 m_data.descriptor = -1;
331void NativeFileStream::setData(FileBuffer data, std::ios_base::openmode openMode)
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)
338 if (m_openMode & ios_base::app) {
339 seekp(0, ios_base::end);
344#ifdef PLATFORM_WINDOWS
349std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(std::string_view path)
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);
357 return std::move(widePath.first);
364std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(
const std::string &path)
366 return makeWidePath(std::string_view(path));
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Contains all utilities provided by the c++utilities library.
std::fstream NativeFileStream