diff options
Diffstat (limited to 'lib/libcxx/include/fstream')
-rw-r--r-- | lib/libcxx/include/fstream | 348 |
1 files changed, 314 insertions, 34 deletions
diff --git a/lib/libcxx/include/fstream b/lib/libcxx/include/fstream index f57908c8dfa..332b4747c1a 100644 --- a/lib/libcxx/include/fstream +++ b/lib/libcxx/include/fstream @@ -38,6 +38,7 @@ public: bool is_open() const; basic_filebuf* open(const char* s, ios_base::openmode mode); basic_filebuf* open(const string& s, ios_base::openmode mode); + basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17 basic_filebuf* close(); protected: @@ -77,6 +78,8 @@ public: basic_ifstream(); explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in); explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in); + explicit basic_ifstream(const filesystem::path& p, + ios_base::openmode mode = ios_base::in); // C++17 basic_ifstream(basic_ifstream&& rhs); basic_ifstream& operator=(basic_ifstream&& rhs); @@ -86,6 +89,8 @@ public: bool is_open() const; void open(const char* s, ios_base::openmode mode = ios_base::in); void open(const string& s, ios_base::openmode mode = ios_base::in); + void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17 + void close(); }; @@ -110,6 +115,8 @@ public: basic_ofstream(); explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out); explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out); + explicit basic_ofstream(const filesystem::path& p, + ios_base::openmode mode = ios_base::out); // C++17 basic_ofstream(basic_ofstream&& rhs); basic_ofstream& operator=(basic_ofstream&& rhs); @@ -119,6 +126,9 @@ public: bool is_open() const; void open(const char* s, ios_base::openmode mode = ios_base::out); void open(const string& s, ios_base::openmode mode = ios_base::out); + void open(const filesystem::path& p, + ios_base::openmode mode = ios_base::out); // C++17 + void close(); }; @@ -143,6 +153,8 @@ public: basic_fstream(); explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); + explicit basic_fstream(const filesystem::path& p, + ios_base::openmode mode = ios_base::in|ios_base::out); C++17 basic_fstream(basic_fstream&& rhs); basic_fstream& operator=(basic_fstream&& rhs); @@ -152,6 +164,9 @@ public: bool is_open() const; void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); + void open(const filesystem::path& s, + ios_base::openmode mode = ios_base::in|ios_base::out); // C++17 + void close(); }; @@ -170,6 +185,8 @@ typedef basic_fstream<wchar_t> wfstream; #include <istream> #include <__locale> #include <cstdio> +#include <cstdlib> +#include <filesystem> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header @@ -212,12 +229,28 @@ public: bool is_open() const; #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE basic_filebuf* open(const char* __s, ios_base::openmode __mode); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode); +#endif _LIBCPP_INLINE_VISIBILITY basic_filebuf* open(const string& __s, ios_base::openmode __mode); + +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) { + return open(__p.c_str(), __mode); + } +#endif + _LIBCPP_INLINE_VISIBILITY + basic_filebuf* __open(int __fd, ios_base::openmode __mode); #endif basic_filebuf* close(); -protected: + _LIBCPP_INLINE_VISIBILITY + inline static const char* + __make_mdstring(ios_base::openmode __mode) _NOEXCEPT; + + protected: // 27.9.1.5 Overridden virtual functions: virtual int_type underflow(); virtual int_type pbackfail(int_type __c = traits_type::eof()); @@ -231,25 +264,25 @@ protected: virtual void imbue(const locale& __loc); private: - char* __extbuf_; - const char* __extbufnext_; - const char* __extbufend_; - char __extbuf_min_[8]; - size_t __ebs_; - char_type* __intbuf_; - size_t __ibs_; - FILE* __file_; - const codecvt<char_type, char, state_type>* __cv_; - state_type __st_; - state_type __st_last_; - ios_base::openmode __om_; - ios_base::openmode __cm_; - bool __owns_eb_; - bool __owns_ib_; - bool __always_noconv_; - - bool __read_mode(); - void __write_mode(); + char* __extbuf_; + const char* __extbufnext_; + const char* __extbufend_; + char __extbuf_min_[8]; + size_t __ebs_; + char_type* __intbuf_; + size_t __ibs_; + FILE* __file_; + const codecvt<char_type, char, state_type>* __cv_; + state_type __st_; + state_type __st_last_; + ios_base::openmode __om_; + ios_base::openmode __cm_; + bool __owns_eb_; + bool __owns_ib_; + bool __always_noconv_; + + bool __read_mode(); + void __write_mode(); }; template <class _CharT, class _Traits> @@ -470,6 +503,46 @@ basic_filebuf<_CharT, _Traits>::is_open() const return __file_ != 0; } +template <class _CharT, class _Traits> +const char* basic_filebuf<_CharT, _Traits>::__make_mdstring( + ios_base::openmode __mode) _NOEXCEPT { + switch (__mode & ~ios_base::ate) { + case ios_base::out: + case ios_base::out | ios_base::trunc: + return "w"; + case ios_base::out | ios_base::app: + case ios_base::app: + return "a"; + case ios_base::in: + return "r"; + case ios_base::in | ios_base::out: + return "r+"; + case ios_base::in | ios_base::out | ios_base::trunc: + return "w+"; + case ios_base::in | ios_base::out | ios_base::app: + case ios_base::in | ios_base::app: + return "a+"; + case ios_base::out | ios_base::binary: + case ios_base::out | ios_base::trunc | ios_base::binary: + return "wb"; + case ios_base::out | ios_base::app | ios_base::binary: + case ios_base::app | ios_base::binary: + return "ab"; + case ios_base::in | ios_base::binary: + return "rb"; + case ios_base::in | ios_base::out | ios_base::binary: + return "r+b"; + case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: + return "w+b"; + case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: + case ios_base::in | ios_base::app | ios_base::binary: + return "a+b"; + default: + return nullptr; + } + _LIBCPP_UNREACHABLE(); +} + #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE template <class _CharT, class _Traits> basic_filebuf<_CharT, _Traits>* @@ -478,51 +551,104 @@ basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) basic_filebuf<_CharT, _Traits>* __rt = 0; if (__file_ == 0) { + if (const char* __mdstr = __make_mdstring(__mode)) { __rt = this; - const char* __mdstr; + __file_ = fopen(__s, __mdstr); + if (__file_) { + __om_ = __mode; + if (__mode & ios_base::ate) { + if (fseek(__file_, 0, SEEK_END)) { + fclose(__file_); + __file_ = 0; + __rt = 0; + } + } + } else + __rt = 0; + } + } + return __rt; +} + +template <class _CharT, class _Traits> +_LIBCPP_INLINE_VISIBILITY basic_filebuf<_CharT, _Traits>* +basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) { + basic_filebuf<_CharT, _Traits>* __rt = 0; + if (__file_ == 0) { + if (const char* __mdstr = __make_mdstring(__mode)) { + __rt = this; + __file_ = fdopen(__fd, __mdstr); + if (__file_) { + __om_ = __mode; + if (__mode & ios_base::ate) { + if (fseek(__file_, 0, SEEK_END)) { + fclose(__file_); + __file_ = 0; + __rt = 0; + } + } + } else + __rt = 0; + } + } + return __rt; +} + +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +// This is basically the same as the char* overload except that it uses _wfopen +// and long mode strings. +template <class _CharT, class _Traits> +basic_filebuf<_CharT, _Traits>* +basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) +{ + basic_filebuf<_CharT, _Traits>* __rt = 0; + if (__file_ == 0) + { + __rt = this; + const wchar_t* __mdstr; switch (__mode & ~ios_base::ate) { case ios_base::out: case ios_base::out | ios_base::trunc: - __mdstr = "w"; + __mdstr = L"w"; break; case ios_base::out | ios_base::app: case ios_base::app: - __mdstr = "a"; + __mdstr = L"a"; break; case ios_base::in: - __mdstr = "r"; + __mdstr = L"r"; break; case ios_base::in | ios_base::out: - __mdstr = "r+"; + __mdstr = L"r+"; break; case ios_base::in | ios_base::out | ios_base::trunc: - __mdstr = "w+"; + __mdstr = L"w+"; break; case ios_base::in | ios_base::out | ios_base::app: case ios_base::in | ios_base::app: - __mdstr = "a+"; + __mdstr = L"a+"; break; case ios_base::out | ios_base::binary: case ios_base::out | ios_base::trunc | ios_base::binary: - __mdstr = "wb"; + __mdstr = L"wb"; break; case ios_base::out | ios_base::app | ios_base::binary: case ios_base::app | ios_base::binary: - __mdstr = "ab"; + __mdstr = L"ab"; break; case ios_base::in | ios_base::binary: - __mdstr = "rb"; + __mdstr = L"rb"; break; case ios_base::in | ios_base::out | ios_base::binary: - __mdstr = "r+b"; + __mdstr = L"r+b"; break; case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: - __mdstr = "w+b"; + __mdstr = L"w+b"; break; case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: case ios_base::in | ios_base::app | ios_base::binary: - __mdstr = "a+b"; + __mdstr = L"a+b"; break; default: __rt = 0; @@ -530,7 +656,7 @@ basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) } if (__rt) { - __file_ = fopen(__s, __mdstr); + __file_ = _wfopen(__s, __mdstr); if (__file_) { __om_ = __mode; @@ -550,6 +676,7 @@ basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) } return __rt; } +#endif template <class _CharT, class _Traits> inline @@ -1017,8 +1144,17 @@ public: #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE _LIBCPP_INLINE_VISIBILITY explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + _LIBCPP_INLINE_VISIBILITY + explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); +#endif _LIBCPP_INLINE_VISIBILITY explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in); +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) + : basic_ifstream(__p.c_str(), __mode) {} +#endif // _LIBCPP_STD_VER >= 17 #endif #ifndef _LIBCPP_CXX03_LANG _LIBCPP_INLINE_VISIBILITY @@ -1036,7 +1172,20 @@ public: bool is_open() const; #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE void open(const char* __s, ios_base::openmode __mode = ios_base::in); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); +#endif void open(const string& __s, ios_base::openmode __mode = ios_base::in); +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + void open(const filesystem::path& __p, + ios_base::openmode __mode = ios_base::in) { + return open(__p.c_str(), __mode); + } +#endif // _LIBCPP_STD_VER >= 17 + + _LIBCPP_INLINE_VISIBILITY + void __open(int __fd, ios_base::openmode __mode); #endif _LIBCPP_INLINE_VISIBILITY void close(); @@ -1062,6 +1211,17 @@ basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openm this->setstate(ios_base::failbit); } +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +template <class _CharT, class _Traits> +inline +basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode) + : basic_istream<char_type, traits_type>(&__sb_) +{ + if (__sb_.open(__s, __mode | ios_base::in) == 0) + this->setstate(ios_base::failbit); +} +#endif + template <class _CharT, class _Traits> inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode) @@ -1139,6 +1299,18 @@ basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode this->setstate(ios_base::failbit); } +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +template <class _CharT, class _Traits> +void +basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) +{ + if (__sb_.open(__s, __mode | ios_base::in)) + this->clear(); + else + this->setstate(ios_base::failbit); +} +#endif + template <class _CharT, class _Traits> void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) @@ -1148,6 +1320,15 @@ basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mo else this->setstate(ios_base::failbit); } + +template <class _CharT, class _Traits> +void basic_ifstream<_CharT, _Traits>::__open(int __fd, + ios_base::openmode __mode) { + if (__sb_.__open(__fd, __mode | ios_base::in)) + this->clear(); + else + this->setstate(ios_base::failbit); +} #endif template <class _CharT, class _Traits> @@ -1176,8 +1357,19 @@ public: basic_ofstream(); _LIBCPP_INLINE_VISIBILITY explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + _LIBCPP_INLINE_VISIBILITY + explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); +#endif _LIBCPP_INLINE_VISIBILITY explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out); + +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) + : basic_ofstream(__p.c_str(), __mode) {} +#endif // _LIBCPP_STD_VER >= 17 + #ifndef _LIBCPP_CXX03_LANG _LIBCPP_INLINE_VISIBILITY basic_ofstream(basic_ofstream&& __rhs); @@ -1194,7 +1386,19 @@ public: bool is_open() const; #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE void open(const char* __s, ios_base::openmode __mode = ios_base::out); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); +#endif void open(const string& __s, ios_base::openmode __mode = ios_base::out); + +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) + { return open(__p.c_str(), __mode); } +#endif // _LIBCPP_STD_VER >= 17 + + _LIBCPP_INLINE_VISIBILITY + void __open(int __fd, ios_base::openmode __mode); #endif _LIBCPP_INLINE_VISIBILITY void close(); @@ -1220,6 +1424,17 @@ basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openm this->setstate(ios_base::failbit); } +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +template <class _CharT, class _Traits> +inline +basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode) + : basic_ostream<char_type, traits_type>(&__sb_) +{ + if (__sb_.open(__s, __mode | ios_base::out) == 0) + this->setstate(ios_base::failbit); +} +#endif + template <class _CharT, class _Traits> inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode) @@ -1297,6 +1512,18 @@ basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode this->setstate(ios_base::failbit); } +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +template <class _CharT, class _Traits> +void +basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) +{ + if (__sb_.open(__s, __mode | ios_base::out)) + this->clear(); + else + this->setstate(ios_base::failbit); +} +#endif + template <class _CharT, class _Traits> void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) @@ -1306,6 +1533,15 @@ basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mo else this->setstate(ios_base::failbit); } + +template <class _CharT, class _Traits> +void basic_ofstream<_CharT, _Traits>::__open(int __fd, + ios_base::openmode __mode) { + if (__sb_.__open(__fd, __mode | ios_base::out)) + this->clear(); + else + this->setstate(ios_base::failbit); +} #endif template <class _CharT, class _Traits> @@ -1335,8 +1571,19 @@ public: #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE _LIBCPP_INLINE_VISIBILITY explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + _LIBCPP_INLINE_VISIBILITY + explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); +#endif _LIBCPP_INLINE_VISIBILITY explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); + +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) + : basic_fstream(__p.c_str(), __mode) {} +#endif // _LIBCPP_STD_VER >= 17 + #endif #ifndef _LIBCPP_CXX03_LANG _LIBCPP_INLINE_VISIBILITY @@ -1354,7 +1601,17 @@ public: bool is_open() const; #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR + void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); +#endif void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); + +#if _LIBCPP_STD_VER >= 17 + _LIBCPP_INLINE_VISIBILITY + void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out) + { return open(__p.c_str(), __mode); } +#endif // _LIBCPP_STD_VER >= 17 + #endif _LIBCPP_INLINE_VISIBILITY void close(); @@ -1380,6 +1637,17 @@ basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmod this->setstate(ios_base::failbit); } +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +template <class _CharT, class _Traits> +inline +basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode) + : basic_iostream<char_type, traits_type>(&__sb_) +{ + if (__sb_.open(__s, __mode) == 0) + this->setstate(ios_base::failbit); +} +#endif + template <class _CharT, class _Traits> inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode) @@ -1457,6 +1725,18 @@ basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) this->setstate(ios_base::failbit); } +#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR +template <class _CharT, class _Traits> +void +basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) +{ + if (__sb_.open(__s, __mode)) + this->clear(); + else + this->setstate(ios_base::failbit); +} +#endif + template <class _CharT, class _Traits> void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) |