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
59using StreamBuffer = __gnu_cxx::stdio_filebuf<char>;
61using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
64struct NativeFileParams {
66#ifdef PLATFORM_WINDOWS
67 NativeFileParams(ios_base::openmode cppOpenMode)
68 : openMode(cppOpenMode & ios_base::binary ? _O_BINARY : 0)
69 , flags(cppOpenMode & ios_base::binary ? 0 : _O_TEXT)
75 if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
77 access = GENERIC_READ | GENERIC_WRITE;
78 shareMode = FILE_SHARE_READ;
79 creation = OPEN_EXISTING;
80 }
else if (cppOpenMode & ios_base::out) {
81 openMode |= _O_WRONLY | _O_CREAT;
82 permissions = _S_IREAD | _S_IWRITE;
83 access = GENERIC_WRITE;
84 creation = OPEN_ALWAYS;
85 }
else if (cppOpenMode & ios_base::in) {
86 openMode |= _O_RDONLY;
88 access = GENERIC_READ;
89 shareMode = FILE_SHARE_READ;
90 creation = OPEN_EXISTING;
92 if (cppOpenMode & ios_base::app) {
93 openMode |= _O_APPEND;
96 if (cppOpenMode & ios_base::trunc) {
98 creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS;
109 NativeFileParams(ios_base::openmode cppOpenMode)
112 if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
113 if (cppOpenMode & ios_base::app) {
115 openFlags = O_RDWR | O_APPEND;
116 }
else if (cppOpenMode & ios_base::trunc) {
118 openFlags = O_RDWR | O_TRUNC;
123 }
else if (cppOpenMode & ios_base::in) {
125 openFlags = O_RDONLY;
126 }
else if (cppOpenMode & ios_base::out) {
127 if (cppOpenMode & ios_base::app) {
129 openFlags = O_WRONLY | O_APPEND;
130 }
else if (cppOpenMode & ios_base::trunc) {
132 openFlags = O_WRONLY | O_TRUNC | O_CREAT;
135 openFlags = O_WRONLY | O_CREAT;
138 if (cppOpenMode & ios_base::binary) {
143 std::string openMode;
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();
334 auto widePath = ::CppUtilities::convertMultiByteToWide(ec, path);
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.