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)
56#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
58#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
61using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
66#ifdef PLATFORM_WINDOWS
156NativeFileStream::FileBuffer::FileBuffer(std::basic_streambuf<char> *buffer)
165NativeFileStream::FileBuffer::FileBuffer(
const string &path, ios_base::openmode openMode)
167#ifdef PLATFORM_WINDOWS
169 const auto widePath(makeWidePath(path));
173 const NativeFileParams nativeParams(openMode);
176#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
177#ifdef PLATFORM_WINDOWS
178 descriptor = _wopen(widePath.get(), nativeParams.openMode, nativeParams.permissions);
179 if (descriptor == -1) {
180 throw std::ios_base::failure(
"_wopen failed", std::error_code(errno, std::system_category()));
183 descriptor = ::open(path.data(), nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
184 if (descriptor == -1) {
185 throw std::ios_base::failure(
"open failed", std::error_code(errno, std::system_category()));
188 buffer = make_unique<StreamBuffer>(descriptor, openMode);
190#ifdef PLATFORM_WINDOWS
191 handle = CreateFileW(widePath.get(), nativeParams.access, nativeParams.shareMode,
nullptr, nativeParams.creation, FILE_ATTRIBUTE_NORMAL,
nullptr);
192 if (handle == INVALID_HANDLE_VALUE) {
193 throw std::ios_base::failure(
"CreateFileW failed", std::error_code(GetLastError(), std::system_category()));
195 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
198 descriptor = ::open(path.data(), nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
199 if (descriptor == -1) {
200 throw std::ios_base::failure(
"open failed", std::error_code(errno, std::system_category()));
202 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
213NativeFileStream::FileBuffer::FileBuffer(
int fileDescriptor, ios_base::openmode openMode)
214 : descriptor(fileDescriptor)
216#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
217 buffer = make_unique<StreamBuffer>(descriptor, openMode);
220#ifdef PLATFORM_WINDOWS
221 handle =
reinterpret_cast<Handle
>(_get_osfhandle(descriptor));
222 buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle);
224 buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle);
232NativeFileStream::NativeFileStream()
233 : iostream(new StreamBuffer)
241NativeFileStream::NativeFileStream(NativeFileStream &&other)
242 : iostream(other.m_data.buffer.release())
245#ifdef PLATFORM_WINDOWS
246 m_data.handle = other.m_data.handle;
248 m_data.descriptor = other.m_data.descriptor;
254NativeFileStream::~NativeFileStream()
261bool NativeFileStream::isOpen()
const
263 return m_data.buffer &&
static_cast<const StreamBuffer *
>(m_data.buffer.get())->is_open();
279void NativeFileStream::open(
const string &path, ios_base::openmode openMode)
281 setData(FileBuffer(path, openMode), openMode);
291void NativeFileStream::open(
int fileDescriptor, ios_base::openmode openMode)
293 setData(FileBuffer(fileDescriptor, openMode), openMode);
299void NativeFileStream::close()
302 static_cast<StreamBuffer *
>(m_data.buffer.get())->close();
303#ifdef PLATFORM_WINDOWS
304 m_data.handle =
nullptr;
306 m_data.descriptor = -1;
313void NativeFileStream::setData(FileBuffer data, std::ios_base::openmode openMode)
315 rdbuf(data.buffer.get());
316 m_data = std::move(data);
317 m_openMode = openMode;
318#if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
320 if (m_openMode & ios_base::app) {
321 seekp(0, ios_base::end);
326#ifdef PLATFORM_WINDOWS
331std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(
const std::string &path)
333 auto ec = std::error_code();
335 if (!widePath.first) {
336 throw std::ios_base::failure(
"converting path to UTF-16", ec);
338 return std::move(widePath.first);
#define CPP_UTILITIES_UNUSED(x)
Prevents warnings about unused variables.
Contains all utilities provides by the c++utilities library.
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned/signed number assuming string uses the specified base.