| 1 | #ifndef IOUTILITIES_NATIVE_FILE_STREAM |
| 2 | #define IOUTILITIES_NATIVE_FILE_STREAM |
| 3 | |
| 4 | #include "../global.h" |
| 5 | |
| 6 | #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER |
| 7 | #include <iostream> |
| 8 | #include <memory> |
| 9 | #include <streambuf> |
| 10 | #include <string> |
| 11 | #ifdef PLATFORM_WINDOWS |
| 12 | #include <string_view> |
| 13 | #endif |
| 14 | #else |
| 15 | #include <fstream> |
| 16 | #endif |
| 17 | |
| 18 | namespace CppUtilities { |
| 19 | |
| 20 | #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFER |
| 21 | |
| 22 | class CPP_UTILITIES_EXPORT NativeFileStream : public std::iostream { |
| 23 | public: |
| 24 | #ifdef PLATFORM_WINDOWS |
| 25 | using Handle = void *; |
| 26 | #endif |
| 27 | struct CPP_UTILITIES_EXPORT FileBuffer { |
| 28 | FileBuffer(std::basic_streambuf<char> *buffer); |
| 29 | FileBuffer(const char *path, ios_base::openmode openMode); |
| 30 | FileBuffer(const std::string &path, ios_base::openmode openMode); |
| 31 | FileBuffer(int fileDescriptor, ios_base::openmode openMode); |
| 32 | |
| 33 | std::unique_ptr<std::basic_streambuf<char>> buffer; |
| 34 | #ifdef PLATFORM_WINDOWS |
| 35 | Handle handle = nullptr; |
| 36 | #endif |
| 37 | int descriptor = -1; |
| 38 | }; |
| 39 | |
| 40 | NativeFileStream(); |
| 41 | NativeFileStream(const char *path, std::ios_base::openmode openMode); |
| 42 | NativeFileStream(const std::string &path, std::ios_base::openmode openMode); |
| 43 | NativeFileStream(int fileDescriptor, std::ios_base::openmode openMode); |
| 44 | NativeFileStream(NativeFileStream &&); |
| 45 | ~NativeFileStream() override; |
| 46 | |
| 47 | bool is_open() const; |
| 48 | bool isOpen() const; |
| 49 | void open(const char *path, std::ios_base::openmode openMode); |
| 50 | void open(const std::string &path, std::ios_base::openmode openMode); |
| 51 | void open(int fileDescriptor, std::ios_base::openmode openMode); |
| 52 | void close(); |
| 53 | int fileDescriptor(); |
| 54 | #ifdef PLATFORM_WINDOWS |
| 55 | Handle fileHandle(); |
| 56 | static std::unique_ptr<wchar_t[]> makeWidePath(std::string_view path); |
| 57 | static std::unique_ptr<wchar_t[]> makeWidePath(const char *path); |
| 58 | static std::unique_ptr<wchar_t[]> makeWidePath(const std::string &path); |
| 59 | #endif |
| 60 | |
| 61 | private: |
| 62 | void setData(FileBuffer data, std::ios_base::openmode openMode); |
| 63 | |
| 64 | FileBuffer m_data; |
| 65 | std::ios_base::openmode m_openMode; |
| 66 | }; |
| 67 | |
| 68 | /*! |
| 69 | * \brief Constructs a new NativeFileStream. The specified \a path is supposed to be UTF-8 encoded. |
| 70 | */ |
| 71 | inline NativeFileStream::NativeFileStream(const char *path, ios_base::openmode openMode) |
| 72 | : NativeFileStream() |
| 73 | { |
| 74 | open(path, openMode); |
| 75 | } |
| 76 | |
| 77 | /*! |
| 78 | * \brief Constructs a new NativeFileStream. The specified \a path is supposed to be UTF-8 encoded. |
| 79 | */ |
| 80 | inline NativeFileStream::NativeFileStream(const std::string &path, ios_base::openmode openMode) |
| 81 | : NativeFileStream(path.data(), openMode) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | /*! |
| 86 | * \brief Constructs a new NativeFileStream. The specified \a fileDescriptor is either a POSIX file descriptor or a Windows CRT file descriptor. |
| 87 | */ |
| 88 | inline NativeFileStream::NativeFileStream(int fileDescriptor, ios_base::openmode openMode) |
| 89 | : NativeFileStream() |
| 90 | { |
| 91 | open(fileDescriptor, openMode); |
| 92 | } |
| 93 | |
| 94 | /*! |
| 95 | * \brief Returns the native POSIX or Windows CRT file descriptor. |
| 96 | * \remarks Might not be populated if only a Windows file handle is used. |
| 97 | */ |
| 98 | inline int NativeFileStream::fileDescriptor() |
| 99 | { |
| 100 | return m_data.descriptor; |
| 101 | } |
| 102 | |
| 103 | #ifdef PLATFORM_WINDOWS |
| 104 | /*! |
| 105 | * \brief Returns the native Windows file handle. |
| 106 | * \remarks Might not be populated if only a Windows CRT file descriptor is used. |
| 107 | */ |
| 108 | inline NativeFileStream::Handle NativeFileStream::fileHandle() |
| 109 | { |
| 110 | return m_data.handle; |
| 111 | } |
| 112 | |
| 113 | /*! |
| 114 | * \brief Converts the specified UTF-8 encoded \a path to UTF-16 for passing it to WinAPI functions. |
| 115 | * \throws Throws std::ios_base::failure when an encoding error occurs. |
| 116 | */ |
| 117 | inline std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(const char *path) |
| 118 | { |
| 119 | return makeWidePath(std::string_view(path)); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | /*! |
| 124 | * \brief Returns whether the file is open. |
| 125 | * \remarks Same as NativeFileStream::isOpen(); provided for API compatibility with std::fstream. |
| 126 | */ |
| 127 | inline bool NativeFileStream::is_open() const |
| 128 | { |
| 129 | return isOpen(); |
| 130 | } |
| 131 | |
| 132 | #else // CPP_UTILITIES_USE_NATIVE_FILE_BUFFER |
| 133 | |
| 134 | using NativeFileStream = std::fstream; |
| 135 | |
| 136 | #endif |
| 137 | |
| 138 | } // namespace CppUtilities |
| 139 | |
| 140 | #endif // IOUTILITIES_NATIVE_FILE_STREAM |