2016-12-19 23:29:54 +01:00
|
|
|
#ifndef IOUTILITIES_NATIVE_FILE_STREAM
|
|
|
|
#define IOUTILITIES_NATIVE_FILE_STREAM
|
|
|
|
|
|
|
|
#include "../global.h"
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
#ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2018-09-29 18:05:02 +02:00
|
|
|
#include <streambuf>
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <string>
|
2016-12-19 23:29:54 +01:00
|
|
|
#endif
|
2018-09-05 00:21:40 +02:00
|
|
|
#include <fstream>
|
2016-12-19 23:29:54 +01:00
|
|
|
|
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
|
2016-12-19 23:29:54 +01:00
|
|
|
|
2017-05-01 03:13:11 +02:00
|
|
|
class CPP_UTILITIES_EXPORT NativeFileStream : public std::iostream {
|
2016-12-19 23:29:54 +01:00
|
|
|
public:
|
2019-08-13 00:27:03 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
using Handle = void *;
|
|
|
|
#endif
|
|
|
|
struct CPP_UTILITIES_EXPORT FileBuffer {
|
|
|
|
FileBuffer(std::basic_streambuf<char> *buffer);
|
|
|
|
FileBuffer(const std::string &path, ios_base::openmode openMode);
|
|
|
|
FileBuffer(int fileDescriptor, ios_base::openmode openMode);
|
|
|
|
|
|
|
|
std::unique_ptr<std::basic_streambuf<char>> buffer;
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
Handle handle = nullptr;
|
|
|
|
#endif
|
|
|
|
int descriptor = -1;
|
|
|
|
};
|
|
|
|
|
2016-12-19 23:29:54 +01:00
|
|
|
NativeFileStream();
|
2018-09-29 18:05:02 +02:00
|
|
|
NativeFileStream(const std::string &path, std::ios_base::openmode openMode);
|
|
|
|
NativeFileStream(int fileDescriptor, std::ios_base::openmode openMode);
|
2018-09-05 00:21:40 +02:00
|
|
|
NativeFileStream(NativeFileStream &&);
|
2016-12-19 23:29:54 +01:00
|
|
|
~NativeFileStream();
|
|
|
|
|
|
|
|
bool is_open() const;
|
2019-08-13 00:27:03 +02:00
|
|
|
bool isOpen() const;
|
2018-09-05 00:21:40 +02:00
|
|
|
void open(const std::string &path, std::ios_base::openmode openMode);
|
2018-10-04 17:58:38 +02:00
|
|
|
void open(int fileDescriptor, std::ios_base::openmode openMode);
|
2016-12-19 23:29:54 +01:00
|
|
|
void close();
|
2019-08-13 00:27:03 +02:00
|
|
|
int fileDescriptor();
|
2018-09-29 18:05:02 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2019-08-13 00:27:03 +02:00
|
|
|
Handle fileHandle();
|
2018-09-29 18:05:02 +02:00
|
|
|
static std::unique_ptr<wchar_t[]> makeWidePath(const std::string &path);
|
|
|
|
#endif
|
2016-12-19 23:29:54 +01:00
|
|
|
|
|
|
|
private:
|
2019-08-13 00:27:03 +02:00
|
|
|
void setData(FileBuffer data, std::ios_base::openmode openMode);
|
2018-09-29 18:05:02 +02:00
|
|
|
|
2019-08-13 00:27:03 +02:00
|
|
|
FileBuffer m_data;
|
|
|
|
std::ios_base::openmode m_openMode;
|
2016-12-19 23:29:54 +01:00
|
|
|
};
|
|
|
|
|
2019-08-13 00:27:03 +02:00
|
|
|
/*!
|
|
|
|
* \brief Constructs a new NativeFileStream. The specified \a path is supposed to be UTF-8 encoded.
|
|
|
|
*/
|
2018-09-29 18:05:02 +02:00
|
|
|
inline NativeFileStream::NativeFileStream(const std::string &path, ios_base::openmode openMode)
|
2018-10-07 19:13:57 +02:00
|
|
|
: NativeFileStream()
|
2016-12-19 23:29:54 +01:00
|
|
|
{
|
2018-09-29 18:05:02 +02:00
|
|
|
open(path, openMode);
|
2016-12-19 23:29:54 +01:00
|
|
|
}
|
|
|
|
|
2019-08-13 00:27:03 +02:00
|
|
|
/*!
|
|
|
|
* \brief Constructs a new NativeFileStream. The specified \a fileDescriptor is either a POSIX file descriptor or a Windows CRT file descriptor.
|
|
|
|
*/
|
2018-09-29 18:05:02 +02:00
|
|
|
inline NativeFileStream::NativeFileStream(int fileDescriptor, ios_base::openmode openMode)
|
2018-10-07 19:13:57 +02:00
|
|
|
: NativeFileStream()
|
2018-09-05 00:21:40 +02:00
|
|
|
{
|
2018-10-04 17:58:38 +02:00
|
|
|
open(fileDescriptor, openMode);
|
2018-09-05 00:21:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 00:27:03 +02:00
|
|
|
/*!
|
|
|
|
* \brief Returns the native POSIX or Windows CRT file descriptor.
|
|
|
|
* \remarks Might not be populated if only a Windows file handle is used.
|
|
|
|
*/
|
|
|
|
inline int NativeFileStream::fileDescriptor()
|
|
|
|
{
|
|
|
|
return m_data.descriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
/*!
|
|
|
|
* \brief Returns the native Windows file handle.
|
|
|
|
* \remarks Might not be populated if only a Windows CRT file descriptor is used.
|
|
|
|
*/
|
|
|
|
inline NativeFileStream::Handle NativeFileStream::fileHandle()
|
|
|
|
{
|
|
|
|
return m_data.handle;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Returns whether the file is open.
|
|
|
|
* \remarks Same as NativeFileStream::isOpen(); provided for API compatibility with std::fstream.
|
|
|
|
*/
|
|
|
|
inline bool NativeFileStream::is_open() const
|
|
|
|
{
|
|
|
|
return isOpen();
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:05:02 +02:00
|
|
|
#else // CPP_UTILITIES_USE_NATIVE_FILE_BUFFER
|
2018-09-05 00:21:40 +02:00
|
|
|
|
2018-09-16 00:25:16 +02:00
|
|
|
using NativeFileStream = std::fstream;
|
2018-09-05 00:21:40 +02:00
|
|
|
|
2016-12-19 23:29:54 +01:00
|
|
|
#endif
|
2018-09-05 00:21:40 +02:00
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
} // namespace CppUtilities
|
2016-12-19 23:29:54 +01:00
|
|
|
|
|
|
|
#endif // IOUTILITIES_NATIVE_FILE_STREAM
|