2016-12-19 23:29:54 +01:00
|
|
|
#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>
|
2016-12-19 23:29:54 +01:00
|
|
|
#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>)
|
2016-12-19 23:29:54 +01:00
|
|
|
{
|
|
|
|
rdbuf(m_filebuf.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
NativeFileStream::~NativeFileStream()
|
2017-05-01 03:13:11 +02:00
|
|
|
{
|
|
|
|
}
|
2016-12-19 23:29:54 +01: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) {
|
2016-12-19 23:29:54 +01:00
|
|
|
::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) {
|
2016-12-19 23:29:54 +01:00
|
|
|
::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)) {
|
2016-12-19 23:29:54 +01:00
|
|
|
nativeFlags |= _O_RDWR;
|
2017-05-01 03:13:11 +02:00
|
|
|
} else if (flags & ios_base::out) {
|
2016-12-19 23:29:54 +01:00
|
|
|
nativeFlags |= _O_WRONLY | _O_CREAT;
|
|
|
|
permissions = _S_IREAD | _S_IWRITE;
|
2017-05-01 03:13:11 +02:00
|
|
|
} else if (flags & ios_base::in) {
|
2016-12-19 23:29:54 +01:00
|
|
|
nativeFlags |= _O_RDONLY;
|
|
|
|
}
|
2017-05-01 03:13:11 +02:00
|
|
|
if (flags & ios_base::trunc) {
|
2016-12-19 23:29:54 +01:00
|
|
|
nativeFlags |= _O_TRUNC;
|
|
|
|
}
|
|
|
|
// initialize stdio_filebuf
|
|
|
|
int fd = _wopen(widePath.get(), nativeFlags, permissions);
|
2017-05-01 03:13:11 +02:00
|
|
|
if (fd == -1) {
|
2016-12-19 23:29:54 +01:00
|
|
|
::IoUtilities::throwIoFailure("_wopen failed");
|
|
|
|
}
|
2017-05-04 22:44:00 +02:00
|
|
|
m_filebuf = make_unique<__gnu_cxx::stdio_filebuf<char>>(fd, flags);
|
2016-12-19 23:29:54 +01:00
|
|
|
rdbuf(m_filebuf.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NativeFileStream::close()
|
|
|
|
{
|
2017-05-01 03:13:11 +02:00
|
|
|
if (m_filebuf) {
|
2016-12-19 23:29:54 +01:00
|
|
|
m_filebuf->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2018-03-24 17:00:30 +01:00
|
|
|
} // namespace IoUtilities
|