3 #if defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER) && (defined(PLATFORM_MINGW) || defined(PLATFORM_LINUX)) 24 #if defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER) && (defined(PLATFORM_MINGW) || defined(PLATFORM_UNIX)) 26 struct NativeFileParams {
29 NativeFileParams(ios_base::openmode cppOpenMode)
30 : openMode(cppOpenMode & ios_base::binary ? _O_BINARY : 0)
31 , flags(cppOpenMode & ios_base::binary ? 0 : _O_TEXT)
34 if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) {
36 }
else if (cppOpenMode & ios_base::out) {
37 openMode |= _O_WRONLY | _O_CREAT;
38 permissions = _S_IREAD | _S_IWRITE;
39 }
else if (cppOpenMode & ios_base::in) {
40 openMode |= _O_RDONLY;
43 if (cppOpenMode & ios_base::app) {
44 openMode |= _O_APPEND;
47 if (cppOpenMode & ios_base::trunc) {
57 NativeFileParams(ios_base::openmode cppOpenMode)
59 if ((cppOpenMode & ios_base::in) && (cppOpenMode & ios_base::out)) {
60 if (cppOpenMode & ios_base::app) {
62 }
else if (cppOpenMode & ios_base::trunc) {
67 }
else if (cppOpenMode & ios_base::in) {
69 }
else if (cppOpenMode & ios_base::out) {
70 if (cppOpenMode & ios_base::app) {
72 }
else if (cppOpenMode & ios_base::trunc) {
78 if (cppOpenMode & ios_base::binary) {
88 : m_filebuf(new __gnu_cxx::stdio_filebuf<char>)
90 rdbuf(m_filebuf.get());
94 : m_filebuf(
std::move(other.m_filebuf))
95 , m_cfile(other.m_cfile)
99 NativeFileStream::~NativeFileStream()
103 void NativeFileStream::open(
const string &path, ios_base::openmode openMode)
105 #ifdef PLATFORM_MINGW 107 int requiredSize = MultiByteToWideChar(CP_UTF8, 0, path.data(), -1,
nullptr, 0);
108 if (requiredSize <= 0) {
111 auto widePath = make_unique<wchar_t[]>(
static_cast<size_t>(requiredSize));
112 requiredSize = MultiByteToWideChar(CP_UTF8, 0, path.data(), -1, widePath.get(), requiredSize);
113 if (requiredSize <= 0) {
118 const NativeFileParams nativeParams(openMode);
119 const int fileHandle = _wopen(widePath.get(), nativeParams.openMode, nativeParams.permissions);
120 if (fileHandle == -1) {
126 const NativeFileParams nativeParams(openMode);
127 const auto fileHandle = fopen(path.data(), nativeParams.openMode.data());
132 m_filebuf = make_unique<__gnu_cxx::stdio_filebuf<char>>(fileHandle, openMode);
133 rdbuf(m_filebuf.get());
136 void NativeFileStream::openFromFileDescriptor(
int fileDescriptor, ios_base::openmode openMode)
138 const NativeFileParams nativeParams(openMode);
139 #ifdef PLATFORM_MINGW 140 const auto fileHandle = _get_osfhandle(fileDescriptor);
141 if (fileHandle == -1) {
144 const auto osFileHandle = _open_osfhandle(fileHandle, nativeParams.flags);
145 if (osFileHandle == -1) {
149 const auto fileHandle = fdopen(fileDescriptor, nativeParams.openMode.data());
154 m_filebuf = make_unique<__gnu_cxx::stdio_filebuf<char>>(fileHandle, openMode);
155 rdbuf(m_filebuf.get());
158 void NativeFileStream::close()
CPP_UTILITIES_EXPORT void throwIoFailure(const char *what)
Throws an std::ios_base::failure with the specified message.
std::fstream NativeFileStream
Contains utility classes helping to read and write streams.