2016-12-19 23:29:54 +01:00
|
|
|
#include "./nativefilestream.h"
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
|
2018-09-05 00:21:40 +02:00
|
|
|
|
2018-10-03 21:26:00 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
#include "../conversion/stringconversion.h"
|
|
|
|
#endif
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
// include header files for file buffer implementation
|
|
|
|
#if defined(CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF)
|
|
|
|
#include <ext/stdio_filebuf.h>
|
|
|
|
#elif defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS)
|
|
|
|
#include <boost/iostreams/device/file_descriptor.hpp>
|
|
|
|
#include <boost/iostreams/stream.hpp>
|
|
|
|
#else
|
|
|
|
#error "Configuration for NativeFileStream backend insufficient."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// include platform specific header
|
|
|
|
#if defined(PLATFORM_UNIX)
|
|
|
|
#include <cstdio>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#elif defined(PLATFORM_WINDOWS)
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <fcntl.h>
|
2018-09-05 00:21:40 +02:00
|
|
|
#include <io.h>
|
|
|
|
#include <sys/stat.h> // yes, this is needed under Windows (https://msdn.microsoft.com/en-US/library/5yhhz3y7.aspx)
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <windows.h>
|
2016-12-19 23:29:54 +01:00
|
|
|
#endif
|
|
|
|
|
2018-09-05 00:21:40 +02:00
|
|
|
#endif
|
|
|
|
|
2016-12-19 23:29:54 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
namespace CppUtilities {
|
2016-12-19 23:29:54 +01:00
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
|
|
|
|
|
|
|
|
#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
|
|
|
|
using StreamBuffer = __gnu_cxx::stdio_filebuf<char>;
|
|
|
|
#else // CPP_UTILITIES_USE_BOOST_IOSTREAMS
|
|
|
|
using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>;
|
|
|
|
#endif
|
2018-09-05 00:21:40 +02:00
|
|
|
|
|
|
|
struct NativeFileParams {
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2018-09-05 00:21:40 +02:00
|
|
|
NativeFileParams(ios_base::openmode cppOpenMode)
|
|
|
|
: openMode(cppOpenMode & ios_base::binary ? _O_BINARY : 0)
|
|
|
|
, flags(cppOpenMode & ios_base::binary ? 0 : _O_TEXT)
|
|
|
|
, permissions(0)
|
2018-09-29 18:05:02 +02:00
|
|
|
, access(0)
|
|
|
|
, shareMode(0)
|
|
|
|
, creation(0)
|
2018-09-05 00:21:40 +02:00
|
|
|
{
|
|
|
|
if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
|
|
|
|
openMode |= _O_RDWR;
|
2018-09-29 18:05:02 +02:00
|
|
|
access = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
shareMode = FILE_SHARE_READ;
|
|
|
|
creation = OPEN_EXISTING;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else if (cppOpenMode & ios_base::out) {
|
|
|
|
openMode |= _O_WRONLY | _O_CREAT;
|
|
|
|
permissions = _S_IREAD | _S_IWRITE;
|
2018-09-29 18:05:02 +02:00
|
|
|
access = GENERIC_WRITE;
|
|
|
|
creation = OPEN_ALWAYS;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else if (cppOpenMode & ios_base::in) {
|
|
|
|
openMode |= _O_RDONLY;
|
|
|
|
flags |= _O_RDONLY;
|
2018-09-29 18:05:02 +02:00
|
|
|
access = GENERIC_READ;
|
|
|
|
shareMode = FILE_SHARE_READ;
|
|
|
|
creation = OPEN_EXISTING;
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
if (cppOpenMode & ios_base::app) {
|
|
|
|
openMode |= _O_APPEND;
|
|
|
|
flags |= _O_APPEND;
|
|
|
|
}
|
|
|
|
if (cppOpenMode & ios_base::trunc) {
|
|
|
|
openMode |= _O_TRUNC;
|
2018-09-29 18:05:02 +02:00
|
|
|
creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS;
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 23:29:54 +01:00
|
|
|
|
2018-09-05 00:21:40 +02:00
|
|
|
int openMode;
|
|
|
|
int flags;
|
|
|
|
int permissions;
|
2018-09-29 18:05:02 +02:00
|
|
|
DWORD access;
|
|
|
|
DWORD shareMode;
|
|
|
|
DWORD creation;
|
2016-12-19 23:29:54 +01:00
|
|
|
#else
|
2018-09-05 00:21:40 +02:00
|
|
|
NativeFileParams(ios_base::openmode cppOpenMode)
|
2018-09-29 18:05:02 +02:00
|
|
|
: openFlags(0)
|
2018-09-05 00:21:40 +02:00
|
|
|
{
|
|
|
|
if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
|
|
|
|
if (cppOpenMode & ios_base::app) {
|
|
|
|
openMode = "a+";
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_RDWR | O_APPEND;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else if (cppOpenMode & ios_base::trunc) {
|
|
|
|
openMode = "w+";
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_RDWR | O_TRUNC;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else {
|
|
|
|
openMode = "r+";
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_RDWR;
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
} else if (cppOpenMode & ios_base::in) {
|
|
|
|
openMode = 'r';
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_RDONLY;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else if (cppOpenMode & ios_base::out) {
|
|
|
|
if (cppOpenMode & ios_base::app) {
|
|
|
|
openMode = 'a';
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_WRONLY | O_APPEND;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else if (cppOpenMode & ios_base::trunc) {
|
|
|
|
openMode = 'w';
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_WRONLY | O_TRUNC | O_CREAT;
|
2018-09-05 00:21:40 +02:00
|
|
|
} else {
|
2018-11-02 23:06:30 +01:00
|
|
|
openMode = "w";
|
2018-09-29 18:05:02 +02:00
|
|
|
openFlags = O_WRONLY | O_CREAT;
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cppOpenMode & ios_base::binary) {
|
|
|
|
openMode += 'b';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string openMode;
|
2018-09-29 18:05:02 +02:00
|
|
|
int openFlags;
|
2018-09-05 00:21:40 +02:00
|
|
|
#endif
|
|
|
|
};
|
2016-12-19 23:29:54 +01:00
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
/*!
|
|
|
|
* \brief Constructs a new NativeFileStream which is initially closed.
|
|
|
|
*/
|
2017-05-01 03:13:11 +02:00
|
|
|
NativeFileStream::NativeFileStream()
|
2018-10-07 19:13:57 +02:00
|
|
|
: iostream(new StreamBuffer)
|
|
|
|
, m_filebuf(rdbuf())
|
2016-12-19 23:29:54 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
/*!
|
|
|
|
* \brief Moves the NativeFileStream.
|
|
|
|
*/
|
2018-09-05 00:21:40 +02:00
|
|
|
NativeFileStream::NativeFileStream(NativeFileStream &&other)
|
2018-10-07 19:13:57 +02:00
|
|
|
: iostream(other.m_filebuf.release())
|
|
|
|
, m_filebuf(rdbuf())
|
2018-09-05 00:21:40 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
/*!
|
|
|
|
* \brief Destroys the NativeFileStream releasing all underlying resources.
|
|
|
|
*/
|
2016-12-19 23:29:54 +01:00
|
|
|
NativeFileStream::~NativeFileStream()
|
2017-05-01 03:13:11 +02:00
|
|
|
{
|
|
|
|
}
|
2016-12-19 23:29:54 +01:00
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
/*!
|
|
|
|
* \brief Returns whether the file is open.
|
|
|
|
*/
|
|
|
|
bool NativeFileStream::is_open() const
|
|
|
|
{
|
|
|
|
return m_filebuf && static_cast<const StreamBuffer *>(m_filebuf.get())->is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Opens the file referenced by \a path with the specified \a openMode.
|
|
|
|
* \remarks
|
|
|
|
* Under Windows \a path is expected to be UTF-8 encoded. It is automatically converted so non-ASCII
|
|
|
|
* characters are treated correctly under Windows (in contrast to std::fstream::open() where only the
|
|
|
|
* current code page is supported).
|
|
|
|
*
|
|
|
|
* Under other platforms the \a path is just passed through so there are no assumptions made about its
|
|
|
|
* encoding.
|
|
|
|
* \throws Throws std::ios_base::failure in the error case.
|
|
|
|
* \todo Maybe use setstate() instead of throwing exceptions directly for consistent error handling
|
|
|
|
* with std::fstream::open(). However, that makes passing specific error messages difficult.
|
|
|
|
*/
|
2018-09-05 00:21:40 +02:00
|
|
|
void NativeFileStream::open(const string &path, ios_base::openmode openMode)
|
2016-12-19 23:29:54 +01:00
|
|
|
{
|
2018-09-29 18:05:02 +02:00
|
|
|
setFileBuffer(makeFileBuffer(path, openMode));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Opens the file from the specified \a fileDescriptor with the specified \a openMode.
|
|
|
|
* \throws Throws std::ios_base::failure in the error case.
|
|
|
|
* \todo
|
|
|
|
* - Maybe use setstate() instead of throwing exceptions directly for consistent error handling
|
|
|
|
* with std::fstream::open(). However, that makes passing specific error messages difficult.
|
|
|
|
*/
|
2018-10-04 17:58:38 +02:00
|
|
|
void NativeFileStream::open(int fileDescriptor, ios_base::openmode openMode)
|
2018-09-29 18:05:02 +02:00
|
|
|
{
|
|
|
|
setFileBuffer(makeFileBuffer(fileDescriptor, openMode));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Closes the file if opened; otherwise does nothing.
|
|
|
|
*/
|
|
|
|
void NativeFileStream::close()
|
|
|
|
{
|
|
|
|
if (m_filebuf) {
|
|
|
|
static_cast<StreamBuffer *>(m_filebuf.get())->close();
|
2016-12-19 23:29:54 +01:00
|
|
|
}
|
2018-09-29 18:05:02 +02:00
|
|
|
}
|
2018-09-05 00:21:40 +02:00
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
/*!
|
|
|
|
* \brief Internally called to assign the \a buffer taking. Takes ownership over \a buffer.
|
|
|
|
*/
|
|
|
|
void NativeFileStream::setFileBuffer(std::unique_ptr<std::basic_streambuf<char>> buffer)
|
|
|
|
{
|
|
|
|
rdbuf(buffer.get());
|
|
|
|
m_filebuf = std::move(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief \brief Internally called by open().
|
|
|
|
*/
|
|
|
|
std::unique_ptr<std::basic_streambuf<char>> NativeFileStream::makeFileBuffer(const string &path, ios_base::openmode openMode)
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
// convert path to UTF-16
|
|
|
|
const auto widePath(makeWidePath(path));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// compute native params
|
2018-09-05 00:21:40 +02:00
|
|
|
const NativeFileParams nativeParams(openMode);
|
2018-09-29 18:05:02 +02:00
|
|
|
|
|
|
|
#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
|
|
|
|
// open file handle to initialize stdio_filebuf
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
2018-09-05 00:21:40 +02:00
|
|
|
const int fileHandle = _wopen(widePath.get(), nativeParams.openMode, nativeParams.permissions);
|
|
|
|
if (fileHandle == -1) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("_wopen failed");
|
2016-12-19 23:29:54 +01:00
|
|
|
}
|
2018-09-05 00:21:40 +02:00
|
|
|
#else
|
|
|
|
const auto fileHandle = fopen(path.data(), nativeParams.openMode.data());
|
|
|
|
if (!fileHandle) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("fopen failed");
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-09-29 18:05:02 +02:00
|
|
|
return make_unique<StreamBuffer>(fileHandle, openMode);
|
|
|
|
|
|
|
|
#else // CPP_UTILITIES_USE_BOOST_IOSTREAMS
|
|
|
|
// create raw file descriptor to initialize boost::iostreams::file_descriptor
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
const auto fileDescriptor
|
|
|
|
= CreateFileW(widePath.get(), nativeParams.access, nativeParams.shareMode, nullptr, nativeParams.creation, FILE_ATTRIBUTE_NORMAL);
|
|
|
|
if (fileDescriptor == INVALID_HANDLE_VALUE) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("CreateFileW failed");
|
2018-09-29 18:05:02 +02:00
|
|
|
}
|
|
|
|
#else
|
2018-10-07 19:16:41 +02:00
|
|
|
const auto fileDescriptor = ::open(path.data(), nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2018-09-29 18:05:02 +02:00
|
|
|
if (fileDescriptor == -1) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("open failed");
|
2018-09-29 18:05:02 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return make_unique<StreamBuffer>(fileDescriptor, boost::iostreams::close_handle);
|
|
|
|
#endif
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
/*!
|
2018-10-04 17:58:38 +02:00
|
|
|
* \brief Internally called by open().
|
2018-09-29 18:05:02 +02:00
|
|
|
*/
|
|
|
|
std::unique_ptr<std::basic_streambuf<char>> NativeFileStream::makeFileBuffer(int fileDescriptor, ios_base::openmode openMode)
|
2018-09-05 00:21:40 +02:00
|
|
|
{
|
2018-09-29 18:05:02 +02:00
|
|
|
// compute native params
|
2018-09-05 00:21:40 +02:00
|
|
|
const NativeFileParams nativeParams(openMode);
|
2018-09-29 18:05:02 +02:00
|
|
|
|
|
|
|
#ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUF
|
|
|
|
// open file handle to initialize stdio_filebuf
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
2018-09-05 00:21:40 +02:00
|
|
|
const auto fileHandle = _get_osfhandle(fileDescriptor);
|
|
|
|
if (fileHandle == -1) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("_get_osfhandle failed");
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
const auto osFileHandle = _open_osfhandle(fileHandle, nativeParams.flags);
|
|
|
|
if (osFileHandle == -1) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("_open_osfhandle failed");
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
const auto fileHandle = fdopen(fileDescriptor, nativeParams.openMode.data());
|
|
|
|
if (!fileHandle) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("fdopen failed");
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-09-29 18:05:02 +02:00
|
|
|
return make_unique<StreamBuffer>(fileDescriptor, openMode);
|
|
|
|
|
|
|
|
#else // CPP_UTILITIES_USE_BOOST_IOSTREAMS
|
|
|
|
// initialize boost::iostreams::file_descriptor from the specified fileDescriptor
|
|
|
|
return make_unique<StreamBuffer>(fileDescriptor, boost::iostreams::close_handle);
|
|
|
|
#endif
|
2016-12-19 23:29:54 +01:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2018-10-03 22:15:08 +02:00
|
|
|
/*!
|
|
|
|
* \brief Converts the specified UTF-8 encoded \a path to UTF-16 for passing it to WinAPI functions.
|
|
|
|
* \throws Throws std::ios_base::failure when an encoding error occurs.
|
|
|
|
*/
|
2018-09-29 18:05:02 +02:00
|
|
|
std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(const std::string &path)
|
2016-12-19 23:29:54 +01:00
|
|
|
{
|
2019-06-25 10:20:48 +02:00
|
|
|
auto widePath = ::CppUtilities::convertMultiByteToWide(path);
|
2018-10-03 22:15:08 +02:00
|
|
|
if (!widePath.first) {
|
2018-10-04 18:07:30 +02:00
|
|
|
throw std::ios_base::failure("Unable to convert path to UTF-16");
|
2016-12-19 23:29:54 +01:00
|
|
|
}
|
2018-10-03 22:15:08 +02:00
|
|
|
return std::move(widePath.first);
|
2016-12-19 23:29:54 +01:00
|
|
|
}
|
2018-09-29 18:05:02 +02:00
|
|
|
#endif
|
2016-12-19 23:29:54 +01:00
|
|
|
|
2018-09-05 00:21:40 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
// std::fstream is used
|
|
|
|
|
2016-12-19 23:29:54 +01:00
|
|
|
#endif
|
2019-06-10 21:56:46 +02:00
|
|
|
} // namespace CppUtilities
|