| 1 | #cxx . . $ `<file>/io/nativefilestream.cpp`/include "./nativefilestream.h"cxx . . $ `<file>/io/nativefilestream.h`/ |
| 2 | |
| 3 | #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFERcxx . . $ `<built-in>:483:9`! |
| 4 | |
| 5 | /*! |
| 6 | * \class |
| 7 | * So one gets e.g. "open failed: Permission denied" instead of |
| 8 | just "open failed: iostream error". |
| 9 | */ |
| 10 | |
| 11 | /*! |
| 12 | * \class NativeFileStream |
| 13 | * \brief Provides a standard IO stream instantiated using native APIs. |
| 14 | * |
| 15 | * Using this class instead of `std::fstream` has the following benefits: |
| 16 | * - Under Windows, the specified file path is interpreted as UTF-8 and passed to Windows' unicode API |
| 17 | * to support any kind of non-ASCII characters in file paths. |
| 18 | * - It is possible to open a file from a native file descriptor. This is for instance useful when dealing with |
| 19 | * Android's `content://` URLs. |
| 20 | * - Better error messages at least when opening a file, e.g. "Permission denied" instead of just "basic_ios::clear". |
| 21 | */ |
| 22 | |
| 23 | #ifdef PLATFORM_WINDOWS |
| 24 | #include "../conversion/stringconversion.h" |
| 25 | #endif |
| 26 | |
| 27 | // include header files for file buffer implementation |
| 28 | #if defined(CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUFcxx . . $ `<built-in>:485:9`!) |
| 29 | #include <ext/stdio_filebuf.h> |
| 30 | #elif defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS) |
| 31 | #include <boost/iostreams/device/file_descriptor.hpp> |
| 32 | #include <boost/iostreams/stream.hpp> |
| 33 | #else |
| 34 | #error "Configuration for NativeFileStream backend insufficient." |
| 35 | #endif |
| 36 | |
| 37 | // include platform specific header |
| 38 | #if defined(PLATFORM_UNIXcxx . . $ `application/global.h:56:9`!) |
| 39 | #include <fcntl.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/types.h> |
| 42 | #elif defined(PLATFORM_WINDOWS) |
| 43 | #include <fcntl.h> |
| 44 | #include <io.h> |
| 45 | #include <limits> |
| 46 | #include <sys/stat.h> // yes, this is needed under Windows (https://msdn.microsoft.com/en-US/library/5yhhz3y7.aspx) |
| 47 | #include <windows.h> |
| 48 | #ifdef max |
| 49 | #undef max // see explanation in stringconversion.cpp |
| 50 | #endif |
| 51 | #endif |
| 52 | |
| 53 | #endif |
| 54 | |
| 55 | using namespace std; |
| 56 | |
| 57 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 58 | |
| 59 | #ifdef CPP_UTILITIES_USE_NATIVE_FILE_BUFFERcxx . . $ `<built-in>:483:9`! |
| 60 | |
| 61 | #ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUFcxx . . $ `<built-in>:485:9`! |
| 62 | using StreamBuffercxx . . $ CppUtilities/StreamBuffer# = __gnu_cxxcxx . . $ __gnu_cxx/::stdio_filebufcxx . . $ __gnu_cxx/stdio_filebuf#<char>; |
| 63 | #else // CPP_UTILITIES_USE_BOOST_IOSTREAMS |
| 64 | using StreamBuffer = boost::iostreams::stream_buffer<boost::iostreams::file_descriptor>; |
| 65 | #endif |
| 66 | |
| 67 | struct NativeFileParamscxx . . $ CppUtilities/NativeFileParams# { |
| 68 | |
| 69 | #ifdef PLATFORM_WINDOWS |
| 70 | NativeFileParams(ios_base::openmode cppOpenMode) |
| 71 | : openMode(cppOpenMode & ios_base::binary ? _O_BINARY : 0) |
| 72 | , flags(cppOpenMode & ios_base::binary ? 0 : _O_TEXT) |
| 73 | , permissions(0) |
| 74 | , access(0) |
| 75 | , shareMode(0) |
| 76 | , creation(0) |
| 77 | { |
| 78 | if ((cppOpenMode & ios_base::out) && (cppOpenMode & ios_base::in)) { |
| 79 | openMode |= _O_RDWR; |
| 80 | access = GENERIC_READ | GENERIC_WRITE; |
| 81 | shareMode = FILE_SHARE_READ; |
| 82 | creation = OPEN_EXISTING; |
| 83 | } else if (cppOpenMode & ios_base::out) { |
| 84 | openMode |= _O_WRONLY | _O_CREAT; |
| 85 | permissions = _S_IREAD | _S_IWRITE; |
| 86 | access = GENERIC_WRITE; |
| 87 | creation = OPEN_ALWAYS; |
| 88 | } else if (cppOpenMode & ios_base::in) { |
| 89 | openMode |= _O_RDONLY; |
| 90 | flags |= _O_RDONLY; |
| 91 | access = GENERIC_READ; |
| 92 | shareMode = FILE_SHARE_READ; |
| 93 | creation = OPEN_EXISTING; |
| 94 | } |
| 95 | if (cppOpenMode & ios_base::app) { |
| 96 | openMode |= _O_APPEND; |
| 97 | flags |= _O_APPEND; |
| 98 | } |
| 99 | if (cppOpenMode & ios_base::trunc) { |
| 100 | openMode |= _O_TRUNC; |
| 101 | creation = (cppOpenMode & ios_base::in) ? TRUNCATE_EXISTING : CREATE_ALWAYS; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | int openMode; |
| 106 | int flags; |
| 107 | int permissions; |
| 108 | DWORD access; |
| 109 | DWORD shareMode; |
| 110 | DWORD creation; |
| 111 | #else |
| 112 | NativeFileParamscxx . . $ CppUtilities/NativeFileParams#NativeFileParams(322f8929844464d3).(ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# cppOpenMode) |
| 113 | : openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags.(0) |
| 114 | { |
| 115 | if ((cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::incxx . . $ std/ios_base#in.) && (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::outcxx . . $ std/ios_base#out.)) { |
| 116 | if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::appcxx . . $ std/ios_base#app.) { |
| 117 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "a+"; |
| 118 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_APPENDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:59:10`!; |
| 119 | } else if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::trunccxx . . $ std/ios_base#trunc.) { |
| 120 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "w+"; |
| 121 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_TRUNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:56:10`!; |
| 122 | } else { |
| 123 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "r+"; |
| 124 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!; |
| 125 | } |
| 126 | } else if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::incxx . . $ std/ios_base#in.) { |
| 127 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(63cc1ae870623930). 'r'; |
| 128 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!; |
| 129 | } else if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::outcxx . . $ std/ios_base#out.) { |
| 130 | if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::appcxx . . $ std/ios_base#app.) { |
| 131 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(63cc1ae870623930). 'a'; |
| 132 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`! | O_APPENDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:59:10`!; |
| 133 | } else if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::trunccxx . . $ std/ios_base#trunc.) { |
| 134 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(63cc1ae870623930). 'w'; |
| 135 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`! | O_TRUNCcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:56:10`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!; |
| 136 | } else { |
| 137 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). "w"; |
| 138 | openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags. = O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!; |
| 139 | } |
| 140 | } |
| 141 | if (cppOpenMode &cxx . . $ std/`operator&`(2dd9eb40323da8bf). ios_basecxx . . $ std/ios_base#::binarycxx . . $ std/ios_base#binary.) { |
| 142 | openModecxx . . $ CppUtilities/NativeFileParams#openMode. +=cxx . . $ std/__cxx11/basic_string#`operator+=`(63cc1ae870623930). 'b'; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | stdcxx . . $ std/::stringcxx . . $ std/string# openModecxx . . $ CppUtilities/NativeFileParams#openMode.; |
| 147 | int openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags.; |
| 148 | #endif |
| 149 | }; |
| 150 | |
| 151 | /*! |
| 152 | * \class NativeFileStream::FileBuffer |
| 153 | * \brief The NativeFileStream::FileBuffer class holds an std::basic_streambuf<char> object obtained from a file path or a native file descriptor. |
| 154 | */ |
| 155 | |
| 156 | /*! |
| 157 | * \brief Constructs a new FileBuffer object taking ownership of \a buffer. |
| 158 | */ |
| 159 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#FileBuffer(7926eb18314c3706).(stdcxx . . $ std/::basic_streambufcxx . . $ std/basic_streambuf#<char> *buffer) |
| 160 | : buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer.(buffer) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | /*! |
| 165 | * \brief Opens a file buffer from the specified \a path. |
| 166 | * \remarks See NativeFileStream::open() for remarks on how \a path must be encoded. |
| 167 | */ |
| 168 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#FileBuffer(b8cd37071ce335df).(const char *path, ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 169 | { |
| 170 | #ifdef PLATFORM_WINDOWS |
| 171 | // convert path to UTF-16 |
| 172 | const auto widePath(makeWidePath(path)); |
| 173 | #endif |
| 174 | |
| 175 | // compute native params |
| 176 | const NativeFileParamscxx . . $ CppUtilities/NativeFileParams# nativeParamscxx . . $ CppUtilities/NativeFileParams#NativeFileParams(322f8929844464d3).(openMode); |
| 177 | |
| 178 | // open native file handle or descriptor |
| 179 | #ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUFcxx . . $ `<built-in>:485:9`! |
| 180 | #ifdef PLATFORM_WINDOWS |
| 181 | descriptor = _wopen(widePath.get(), nativeParams.openMode, nativeParams.permissions); |
| 182 | if (descriptor == -1) { |
| 183 | throw std::ios_base::failure("_wopen failed", std::error_code(errno, std::system_category())); |
| 184 | } |
| 185 | #else |
| 186 | descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor. = ::open(path, nativeParams.openFlagscxx . . $ CppUtilities/NativeFileParams#openFlags., S_IRUSRcxx . . $ `<external>/531885356569766532/stat.h:168:9`! | S_IWUSRcxx . . $ `<external>/531885356569766532/stat.h:169:9`! | S_IRGRPcxx . . $ `<external>/531885356569766532/stat.h:180:9`! | S_IROTHcxx . . $ `<external>/531885356569766532/stat.h:186:9`!); |
| 187 | if (descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor. == -1) { |
| 188 | throw stdcxx . . $ std/ios_base#failure#failure(7e40758012d50676).::ios_basecxx . . $ std/ios_base#::failurecxx . . $ std/ios_base#failure#("open failed", stdcxx . . $ std/error_code#error_code(e271fef6a9068ecc).::error_codecxx . . $ std/error_code#(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, stdcxx . . $ std/::system_categorycxx . . $ std/_V2/system_category(6dcee6f53b9e5f1).())); |
| 189 | } |
| 190 | #endif |
| 191 | buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer. =cxx . . $ std/unique_ptr#`operator=`(274635e25750a47f). make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<StreamBuffercxx . . $ CppUtilities/StreamBuffer#>(descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor., openMode); |
| 192 | #else // CPP_UTILITIES_USE_BOOST_IOSTREAMS |
| 193 | #ifdef PLATFORM_WINDOWS |
| 194 | handle = CreateFileW(widePath.get(), nativeParams.access, nativeParams.shareMode, nullptr, nativeParams.creation, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 195 | if (handle == INVALID_HANDLE_VALUE) { |
| 196 | throw std::ios_base::failure("CreateFileW failed", std::error_code(static_cast<int>(GetLastError()), std::system_category())); |
| 197 | } |
| 198 | buffer = std::make_unique<StreamBuffer>(handle, boost::iostreams::close_handle); |
| 199 | // if we wanted to open assign the descriptor as well: descriptor = _open_osfhandle(reinterpret_cast<std::intptr_t>(handle), nativeParams.flags); |
| 200 | #else |
| 201 | descriptor = ::open(path, nativeParams.openFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 202 | if (descriptor == -1) { |
| 203 | throw std::ios_base::failure("open failed", std::error_code(errno, std::system_category())); |
| 204 | } |
| 205 | buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle); |
| 206 | #endif |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | /*! |
| 211 | * \brief Opens a file buffer from the specified \a path. |
| 212 | * \remarks See NativeFileStream::open() for remarks on how \a path must be encoded. |
| 213 | */ |
| 214 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#FileBuffer(be6c264dafcf0985).(const stdcxx . . $ std/::stringcxx . . $ std/string# &path, ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 215 | : NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#(path.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), openMode) |
| 216 | { |
| 217 | } |
| 218 | |
| 219 | /*! |
| 220 | * \brief Opens a file buffer from the specified \a fileDescriptor. |
| 221 | * \remarks |
| 222 | * The specified \a openMode is only used when using __gnu_cxx::stdio_filebuf<char> and must be in accordance with how \a fileDescriptor |
| 223 | * has been opened. |
| 224 | */ |
| 225 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#::FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#FileBuffer(95134c3a2f2e73fe).(int fileDescriptor, ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 226 | : descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor.(fileDescriptor) |
| 227 | { |
| 228 | #ifdef CPP_UTILITIES_USE_GNU_CXX_STDIO_FILEBUFcxx . . $ `<built-in>:485:9`! |
| 229 | buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer. =cxx . . $ std/unique_ptr#`operator=`(274635e25750a47f). make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<StreamBuffercxx . . $ CppUtilities/StreamBuffer#>(descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor., openMode); |
| 230 | #else // CPP_UTILITIES_USE_BOOST_IOSTREAMS |
| 231 | CPP_UTILITIES_UNUSED(openMode) |
| 232 | #ifdef PLATFORM_WINDOWS |
| 233 | handle = reinterpret_cast<Handle>(_get_osfhandle(descriptor)); |
| 234 | buffer = make_unique<StreamBuffer>(handle, boost::iostreams::close_handle); |
| 235 | #else |
| 236 | buffer = make_unique<StreamBuffer>(descriptor, boost::iostreams::close_handle); |
| 237 | #endif |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | /*! |
| 242 | * \brief Constructs a new NativeFileStream which is initially closed. |
| 243 | */ |
| 244 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#NativeFileStream(49f6e7a06ebc5aa8).() |
| 245 | : iostreamcxx . . $ std/iostream#(new StreamBuffercxx . . $ CppUtilities/StreamBuffer#) |
| 246 | , m_datacxx . . $ CppUtilities/NativeFileStream#m_data.(rdbufcxx . . $ std/basic_ios#rdbuf(fae8afa8316d60d6).()) |
| 247 | { |
| 248 | } |
| 249 | |
| 250 | /*! |
| 251 | * \brief Moves the NativeFileStream. |
| 252 | */ |
| 253 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#NativeFileStream(719b5dd2bbc4fb05).(NativeFileStreamcxx . . $ CppUtilities/NativeFileStream# &&other) |
| 254 | : iostreamcxx . . $ std/iostream#(other.m_datacxx . . $ CppUtilities/NativeFileStream#m_data..buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer..releasecxx . . $ std/unique_ptr#release(aad4c3669c5e940c).()) |
| 255 | , m_datacxx . . $ CppUtilities/NativeFileStream#m_data.(rdbufcxx . . $ std/basic_ios#rdbuf(fae8afa8316d60d6).()) |
| 256 | { |
| 257 | #ifdef PLATFORM_WINDOWS |
| 258 | m_data.handle = other.m_data.handle; |
| 259 | #endif |
| 260 | m_datacxx . . $ CppUtilities/NativeFileStream#m_data..descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor. = other.m_datacxx . . $ CppUtilities/NativeFileStream#m_data..descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor.; |
| 261 | } |
| 262 | |
| 263 | /*! |
| 264 | * \brief Destroys the NativeFileStream releasing all underlying resources. |
| 265 | */ |
| 266 | NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::~cxx . . $ CppUtilities/NativeFileStream#`~NativeFileStream`(ced63f7c635d850d).NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#() |
| 267 | { |
| 268 | } |
| 269 | |
| 270 | /*! |
| 271 | * \brief Returns whether the file is open. |
| 272 | */ |
| 273 | bool NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::isOpencxx . . $ CppUtilities/NativeFileStream#isOpen(50ce9a9e25b4a850).() const |
| 274 | { |
| 275 | return m_datacxx . . $ CppUtilities/NativeFileStream#m_data..buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer. && static_cast<const StreamBuffercxx . . $ CppUtilities/StreamBuffer# *>(m_datacxx . . $ CppUtilities/NativeFileStream#m_data..buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())->is_opencxx . . $ std/basic_filebuf#is_open(3482b152b9333168).(); |
| 276 | } |
| 277 | |
| 278 | /*! |
| 279 | * \brief Opens the file referenced by \a path with the specified \a openMode. |
| 280 | * \remarks |
| 281 | * - Under Windows \a path is expected to be UTF-8 encoded. It is automatically converted so non-ASCII |
| 282 | * characters are treated correctly under Windows (in contrast to std::fstream::open() where only the |
| 283 | * current code page is supported). |
| 284 | * - Under other platforms the \a path is just passed through so there are no assumptions made about its |
| 285 | * encoding. |
| 286 | * \todo Maybe use setstate() instead of throwing exceptions directly for consistent error handling |
| 287 | * with std::fstream::open(). However, that makes passing specific error messages difficult. |
| 288 | */ |
| 289 | void NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::opencxx . . $ CppUtilities/NativeFileStream#open(b8cd37071ce335df).(const char *path, ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 290 | { |
| 291 | setDatacxx . . $ CppUtilities/NativeFileStream#setData(91ccaf2b92fd531e).(FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#FileBuffer(b8cd37071ce335df).(path, openMode), openMode); |
| 292 | } |
| 293 | |
| 294 | /*! |
| 295 | * \brief Opens the file referenced by \a path with the specified \a openMode. |
| 296 | */ |
| 297 | void NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::opencxx . . $ CppUtilities/NativeFileStream#open(be6c264dafcf0985).(const stdcxx . . $ std/::stringcxx . . $ std/string# &path, ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 298 | { |
| 299 | opencxx . . $ CppUtilities/NativeFileStream#open(b8cd37071ce335df).(path.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), openMode); |
| 300 | } |
| 301 | |
| 302 | /*! |
| 303 | * \brief Opens the file from the specified \a fileDescriptor with the specified \a openMode. |
| 304 | * \throws Throws std::ios_base::failure in the error case. |
| 305 | * \todo |
| 306 | * - Maybe use setstate() instead of throwing exceptions directly for consistent error handling |
| 307 | * with std::fstream::open(). However, that makes passing specific error messages difficult. |
| 308 | */ |
| 309 | void NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::opencxx . . $ CppUtilities/NativeFileStream#open(95134c3a2f2e73fe).(int fileDescriptor, ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 310 | { |
| 311 | setDatacxx . . $ CppUtilities/NativeFileStream#setData(91ccaf2b92fd531e).(FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#FileBuffer(95134c3a2f2e73fe).(fileDescriptor, openMode), openMode); |
| 312 | } |
| 313 | |
| 314 | /*! |
| 315 | * \brief Closes the file if opened; otherwise does nothing. |
| 316 | */ |
| 317 | void NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::closecxx . . $ CppUtilities/NativeFileStream#close(49f6e7a06ebc5aa8).() |
| 318 | { |
| 319 | if (m_datacxx . . $ CppUtilities/NativeFileStream#m_data..buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer.) { |
| 320 | static_cast<StreamBuffercxx . . $ CppUtilities/StreamBuffer# *>(m_datacxx . . $ CppUtilities/NativeFileStream#m_data..buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())->closecxx . . $ std/basic_filebuf#close(6ba13ed269b6a132).(); |
| 321 | #ifdef PLATFORM_WINDOWS |
| 322 | m_data.handle = nullptr; |
| 323 | #endif |
| 324 | m_datacxx . . $ CppUtilities/NativeFileStream#m_data..descriptorcxx . . $ CppUtilities/NativeFileStream#FileBuffer#descriptor. = -1; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /*! |
| 329 | * \brief Internally called to assign the buffer, file descriptor and handle. |
| 330 | */ |
| 331 | void NativeFileStreamcxx . . $ CppUtilities/NativeFileStream#::setDatacxx . . $ CppUtilities/NativeFileStream#setData(91ccaf2b92fd531e).(FileBuffercxx . . $ CppUtilities/NativeFileStream#FileBuffer# data, stdcxx . . $ std/::ios_basecxx . . $ std/ios_base#::openmodecxx . . $ std/ios_base#openmode# openMode) |
| 332 | { |
| 333 | rdbufcxx . . $ std/basic_ios#rdbuf(47aaed4dceb1139e).(data.buffercxx . . $ CppUtilities/NativeFileStream#FileBuffer#buffer..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 334 | m_datacxx . . $ CppUtilities/NativeFileStream#m_data. =cxx . . $ CppUtilities/NativeFileStream#FileBuffer#`operator=`(690914fb5ec1bc3d). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(data); |
| 335 | m_openModecxx . . $ CppUtilities/NativeFileStream#m_openMode. = openMode; |
| 336 | #if defined(PLATFORM_WINDOWS) && defined(CPP_UTILITIES_USE_BOOST_IOSTREAMS) |
| 337 | // workaround append flag dysfunctioning |
| 338 | if (m_openMode & ios_base::app) { |
| 339 | seekp(0, ios_base::end); |
| 340 | } |
| 341 | #endif |
| 342 | } |
| 343 | |
| 344 | #ifdef PLATFORM_WINDOWS |
| 345 | /*! |
| 346 | * \brief Converts the specified UTF-8 encoded \a path to UTF-16 for passing it to WinAPI functions. |
| 347 | * \throws Throws std::ios_base::failure when an encoding error occurs. |
| 348 | */ |
| 349 | std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(std::string_view path) |
| 350 | { |
| 351 | auto ec = std::error_code(); |
| 352 | auto size = path.size() < static_cast<std::size_t>(std::numeric_limits<int>::max() - 1) ? static_cast<int>(path.size() + 1) : -1; |
| 353 | auto widePath = ::CppUtilities::convertMultiByteToWide(ec, path.data(), size); |
| 354 | if (!widePath.first) { |
| 355 | throw std::ios_base::failure("converting path to UTF-16", ec); |
| 356 | } |
| 357 | return std::move(widePath.first); |
| 358 | } |
| 359 | |
| 360 | /*! |
| 361 | * \brief Converts the specified UTF-8 encoded \a path to UTF-16 for passing it to WinAPI functions. |
| 362 | * \throws Throws std::ios_base::failure when an encoding error occurs. |
| 363 | */ |
| 364 | std::unique_ptr<wchar_t[]> NativeFileStream::makeWidePath(const std::string &path) |
| 365 | { |
| 366 | return makeWidePath(std::string_view(path)); |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | #else |
| 371 | |
| 372 | // std::fstream is used |
| 373 | |
| 374 | #endif |
| 375 | } // namespace CppUtilities |