cpp-utilities/io/nativefilestream.cpp

74 lines
1.9 KiB
C++
Raw Normal View History

#include "./nativefilestream.h"
#ifdef PLATFORM_MINGW
2017-05-01 03:13:11 +02:00
#include "catchiofailure.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <windows.h>
#endif
using namespace std;
namespace IoUtilities {
#if !defined(PLATFORM_MINGW) || !defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER)
// just use std::fstream
#else
2017-05-01 03:13:11 +02:00
NativeFileStream::NativeFileStream()
: m_filebuf(new __gnu_cxx::stdio_filebuf<char>)
{
rdbuf(m_filebuf.get());
}
NativeFileStream::~NativeFileStream()
2017-05-01 03:13:11 +02:00
{
}
void NativeFileStream::open(const string &path, ios_base::openmode flags)
{
// convert path to UTF-16
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, path.data(), -1, nullptr, 0);
2017-05-01 03:13:11 +02:00
if (requiredSize <= 0) {
::IoUtilities::throwIoFailure("Unable to calculate buffer size for conversion of path to UTF-16");
}
auto widePath = make_unique<wchar_t[]>(static_cast<size_t>(requiredSize));
requiredSize = MultiByteToWideChar(CP_UTF8, 0, path.data(), -1, widePath.get(), requiredSize);
2017-05-01 03:13:11 +02:00
if (requiredSize <= 0) {
::IoUtilities::throwIoFailure("Unable to convert path to UTF-16");
}
// translate flags
int nativeFlags = (flags & ios_base::binary ? _O_BINARY : 0);
int permissions = 0;
2017-05-01 03:13:11 +02:00
if ((flags & ios_base::out) && (flags & ios_base::in)) {
nativeFlags |= _O_RDWR;
2017-05-01 03:13:11 +02:00
} else if (flags & ios_base::out) {
nativeFlags |= _O_WRONLY | _O_CREAT;
permissions = _S_IREAD | _S_IWRITE;
2017-05-01 03:13:11 +02:00
} else if (flags & ios_base::in) {
nativeFlags |= _O_RDONLY;
}
2017-05-01 03:13:11 +02:00
if (flags & ios_base::trunc) {
nativeFlags |= _O_TRUNC;
}
// initialize stdio_filebuf
int fd = _wopen(widePath.get(), nativeFlags, permissions);
2017-05-01 03:13:11 +02:00
if (fd == -1) {
::IoUtilities::throwIoFailure("_wopen failed");
}
2017-05-04 22:44:00 +02:00
m_filebuf = make_unique<__gnu_cxx::stdio_filebuf<char>>(fd, flags);
rdbuf(m_filebuf.get());
}
void NativeFileStream::close()
{
2017-05-01 03:13:11 +02:00
if (m_filebuf) {
m_filebuf->close();
}
}
#endif
}