YSTest: FileIO.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

FileIO.h
浏览该文件的文档.
1 /*
2 © 2011-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YCL_INC_FileIO_h_
29 #define YCL_INC_FileIO_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YCLib_Platform
33 #include YFM_YBaseMacro
34 #include <ystdex/string.hpp> // for std::uint32_t, std::uint64_t, ynothrow,
35 // ystdex::totally_ordered, std::FILE, ystdex::enable_for_string_class_t;
36 #include YFM_YCLib_Debug // for Nonnull, string, u16string, array;
37 #include <chrono> // for std::chrono::nanoseconds;
38 #include YFM_YCLib_Reference // for unique_ptr;
39 #include <ios> // for std::ios_base::sync_with_stdio;
40 #include <fstream> // for std::filebuf;
41 #if __GLIBCXX__
42 # include <ext/stdio_filebuf.h> // for __gnu_cxx::stdio_filebuf;
43 # include <ystdex/utility.hpp> // for ystdex::swap_underlying,
44 // ystdex::exchange;
45 #endif
46 #include <system_error> // for std::system_error;
47 #include <ystdex/container.hpp> // for ystdex::retry_for_vector;
48
49 namespace platform
50 {
51
58 inline PDefH(string, MakePathString, const char* s)
60 ImplRet(Nonnull(s))
61 inline PDefH(const string&, MakePathString, const string& s)
62 ImplRet(s)
64 YF_API string
65 MakePathString(const char16_t*);
67 inline PDefH(string, MakePathString, u16string_view sv)
68 ImplRet(MakePathString(sv.data()))
70
71
73
74
76 #if YCL_Win32
77 using FileNodeID = pair<std::uint32_t, std::uint64_t>;
78 #else
79 using FileNodeID = pair<std::uint64_t, std::uint64_t>;
80 #endif
81
82
88 yconstfn PDefHOp(bool, ==, const FileNodeID& x, const FileNodeID& y)
89 ImplRet(x.first == y.first && x.second == y.second)
90 yconstfn PDefHOp(bool, !=, const FileNodeID& x, const FileNodeID& y)
91 ImplRet(!(x == y))
93
94
95
97 using FileTime = std::chrono::nanoseconds;
98
99
105 #if YCL_Win32
106 using mode_t = unsigned short;
107 #else
108 using mode_t = ::mode_t;
109 #endif
110
111
112
117 enum class NodeCategory : std::uint_least32_t
118 {
119 Empty = 0,
121
122 Invalid = 1 << 0,
123 Regular = 1 << 1,
125 Unknown = Invalid | Regular,
127
128 Device = 1 << 9,
129 Block = Device,
130 Character = Device | 1 << 7,
131 Communicator = 2 << 9,
132 FIFO = Communicator | 1 << 6,
133 Socket = Communicator | 2 << 6,
135 SymbolicLink = 1 << 12,
136 MountPoint = 2 << 12,
137 Junction = MountPoint,
139
140 Link = SymbolicLink | Junction,
142 Directory = 1 << 15,
144
145 Missing = 1 << 16,
146 Special = Link | Missing
148 };
149
151
152 DefBitmaskEnum(NodeCategory)
154
155 YF_API NodeCategory
157 CategorizeNode(mode_t) ynothrow;
159
160
171 class YF_API FileDescriptor : private ystdex::totally_ordered<FileDescriptor>
172 {
173 public:
178 struct YF_API Deleter
179 {
180 using pointer = FileDescriptor;
181
183 void
184 operator()(pointer) const ynothrow;
185 };
186
187 private:
189 int desc = -1;
190
191 public:
192 yconstfn DefDeCtor(FileDescriptor)
193 yconstfn
194 FileDescriptor(int fd) ynothrow
195 : desc(fd)
196 {}
198 yconstfn
199 FileDescriptor(nullptr_t) ynothrow
200 {}
207 FileDescriptor(std::FILE*) ynothrow;
208
210 PDefHOp(int, *, ) const ynothrow
211 ImplRet(desc)
212
214 PDefHOp(FileDescriptor*, ->, ) ynothrow
215 ImplRet(this)
217 PDefHOp(const FileDescriptor*, ->, ) const ynothrow
218 ImplRet(this)
219
220 explicit DefCvt(const ynothrow, bool, desc != -1)
221
223 friend yconstfn YB_PURE PDefHOp(bool,
224 ==, const FileDescriptor& x, const FileDescriptor& y) ynothrow
225 ImplRet(x.desc == y.desc)
226
228 friend yconstfn YB_PURE PDefHOp(bool,
229 <, const FileDescriptor& x, const FileDescriptor& y) ynothrow
230 ImplRet(x.desc < y.desc)
231
233
234
240 FileTime
242 GetAccessTime() const;
248 NodeCategory
249 GetCategory() const ynothrow;
251
252
256 int
257 GetFlags() const ynothrow;
259 mode_t
260 GetMode() const ynothrow;
262 FileTime
264 GetModificationTime() const;
266 array<FileTime, 2>
267 GetModificationAndAccessTime() const;
269
274 FileNodeID
276 GetNodeID() const ynothrow;
278 size_t
279 GetNumberOfLinks() const ynothrow;
281
287 std::uint64_t
288 GetSize() const;
290
298 void
299 SetAccessTime(FileTime) const;
310 bool
311 SetBlocking() const ynothrow;
313 bool
314 SetFlags(int) const ynothrow;
316 bool
317 SetMode(mode_t) const ynothrow;
319
327 void
329 SetModificationTime(FileTime) const;
331 void
332 SetModificationAndAccessTime(array<FileTime, 2>) const;
334
345 bool
346 SetNonblocking() const ynothrow;
348
350
357 bool
358 SetSize(size_t) ynothrow;
365 int
366 SetTranslationMode(int) const ynothrow;
367
369
370
378 YB_NONNULL(2) size_t
379 FullRead(void*, size_t) ynothrowv;
380
385 YB_NONNULL(2) size_t
386 FullWrite(const void*, size_t) ynothrowv;
388
396 YB_NONNULL(2) size_t
397 Read(void*, size_t) ynothrowv;
398
399 YB_NONNULL(2) size_t
400 Write(const void*, size_t) ynothrowv;
402
403
404
412 static YB_NONNULL(3) void
414 WriteContent(FileDescriptor, FileDescriptor, byte*, size_t);
419 static void
420 WriteContent(FileDescriptor, FileDescriptor,
421 size_t = yimpl(size_t(BUFSIZ << 4)));
423 };
424
425
427 using UniqueFile = unique_ptr_from<FileDescriptor::Deleter>;
428
429
434 YF_API YB_STATELESS mode_t
435 DefaultPMode() ynothrow;
436
443 YF_API void
444 SetBinaryIO(std::FILE*) ynothrowv;
445
452 inline PDefH(void, SetupBinaryStdIO, std::FILE* in = stdin,
453 std::FILE* out = stdout, bool sync = {}) ynothrowv
454 ImplExpr(SetBinaryIO(in), SetBinaryIO(out),
455 std::ios_base::sync_with_stdio(sync))
457
458
465 YF_API YB_STATELESS int
467 omode_conv(std::ios_base::openmode) ynothrow;
468
473 YF_API YB_STATELESS int
474 omode_convb(std::ios_base::openmode) ynothrow;
476
490 YF_API YB_NONNULL(1) int
491 uaccess(const char* path, int amode) ynothrowv;
492 YF_API YB_NONNULL(1) int
493 uaccess(const char16_t* path, int amode) ynothrowv;
495
502 YF_API YB_NONNULL(1) int
504 uopen(const char* filename, int oflag, mode_t pmode = 0) ynothrowv;
506 YF_API YB_NONNULL(1) int
507 uopen(const char16_t* filename, int oflag, mode_t pmode = 0) ynothrowv;
509
511
512
517 YF_API YB_NONNULL(1, 2) std::FILE*
519 ufopen(const char* filename, const char* mode) ynothrowv;
521 YF_API YB_NONNULL(1, 2) std::FILE*
522 ufopen(const char16_t* filename, const char16_t* mode) ynothrowv;
524
526 YF_API YB_NONNULL(1) std::FILE*
528 ufopen(const char* filename, std::ios_base::openmode mode) ynothrowv;
530 YF_API YB_NONNULL(1) std::FILE*
531 ufopen(const char16_t* filename, std::ios_base::openmode mode) ynothrowv;
533
535
536 YF_API YB_NONNULL(1) bool
538 ufexists(const char*) ynothrowv;
540 YF_API YB_NONNULL(1) bool
541 ufexists(const char16_t*) ynothrowv;
543
544
551 YF_API YB_NONNULL(1) int
552 upclose(std::FILE*) ynothrowv;
553
555
556
565 YF_API YB_NONNULL(1, 2) std::FILE*
567 upopen(const char* filename, const char* mode) ynothrowv;
569 YF_API YB_NONNULL(1, 2) std::FILE*
570 upopen(const char16_t* filename, const char16_t* mode) ynothrowv;
572
581 YF_API YB_NONNULL(1) char*
583 ugetcwd(char* buf, size_t size) ynothrowv;
585 YF_API YB_NONNULL(1) char16_t*
586 ugetcwd(char16_t* buf, size_t size) ynothrowv;
588
602 YF_API YB_NONNULL(1) bool
603 uchdir(const char*) ynothrowv;
604
609 YF_API YB_NONNULL(1) bool
610 umkdir(const char*) ynothrowv;
611
616 YF_API YB_NONNULL(1) bool
617 urmdir(const char*) ynothrowv;
618
624 YF_API YB_NONNULL(1) bool
625 uunlink(const char*) ynothrowv;
626
633 YF_API YB_NONNULL(1) bool
634 uremove(const char*) ynothrowv;
636
637
638
639
641
642 #if __GLIBCXX__
643 template<typename _tChar, class _tTraits = std::char_traits<_tChar>>
644 class basic_filebuf : public yimpl(__gnu_cxx::stdio_filebuf<_tChar, _tTraits>)
645 {
646 public:
647 using char_type = _tChar;
648 using int_type = typename _tTraits::int_type;
649 using pos_type = typename _tTraits::pos_type;
650 using off_type = typename _tTraits::off_type;
651 using traits = _tTraits;
652
654 DefDeCtor(basic_filebuf)
655 using yimpl(__gnu_cxx::stdio_filebuf<_tChar, _tTraits>::stdio_filebuf);
656 # if YB_IMPL_GNUCPP && !(YB_IMPL_GNUCPP >= 50000 && __GLIBCXX__ > 20140922)
657
659 DefDelCopyCtor(basic_filebuf)
660 basic_filebuf(basic_filebuf&& rhs)
661 : __gnu_cxx::stdio_filebuf<_tChar, _tTraits>()
662 {
663 assign(std::move(rhs)),
664 yunseq(this->_M_pback = rhs._M_pback,
665 this->_M_codecvt = rhs._M_codecvt);
666 }
667
668 DefDelCopyAssignment(basic_filebuf)
669 basic_filebuf&
670 operator=(basic_filebuf&& rhs)
671 {
672 this->close();
673 ystdex::swap_underlying(this->_M_file, rhs._M_file),
674 assign(std::move(rhs));
675 return *this;
676 }
677
678 private:
679 void
680 assign(basic_filebuf&& rhs)
681 {
682 // static_cast<std::basic_streambuf<_tChar, _tTraits>&>(*this) = rhs;
683 yunseq(
684 this->_M_mode
685 = ystdex::exchange(rhs._M_mode, std::ios_base::openmode(0)),
686 this->_M_state_beg = std::move(rhs._M_state_beg),
687 this->_M_state_cur = std::move(rhs._M_state_cur),
688 this->_M_state_last = std::move(rhs._M_state_last),
689 this->_M_buf = ystdex::exchange(rhs._M_buf, {}),
690 this->_M_buf_size = ystdex::exchange(rhs._M_buf_size, 1U),
691 this->_M_buf_allocated = ystdex::exchange(rhs._M_buf_allocated, {}),
692 this->_M_ext_buf = ystdex::exchange(rhs._M_ext_buf, {}),
693 this->_M_ext_buf_size = ystdex::exchange(rhs._M_ext_buf_size, 0),
694 this->_M_ext_next = ystdex::exchange(rhs._M_ext_next, {}),
695 this->_M_ext_end = ystdex::exchange(rhs._M_ext_end, {}),
696 this->_M_reading = ystdex::exchange(rhs._M_reading, {}),
697 this->_M_writing = ystdex::exchange(rhs._M_writing, {}),
698 this->_M_pback_cur_save = ystdex::exchange(rhs._M_pback_cur_save, {}),
699 this->_M_pback_end_save = ystdex::exchange(rhs._M_pback_end_save, {}),
700 this->_M_pback_init = ystdex::exchange(rhs._M_pback_init, {})
701 );
702 rhs._M_set_buffer(-1);
703 yunseq(rhs._M_state_last = rhs._M_state_beg,
704 rhs._M_state_cur = rhs._M_state_beg);
705 }
707 # endif
708
709 public:
711 std::basic_filebuf<_tChar, _tTraits>*
712 open(UniqueFile p, std::ios_base::openmode mode)
713 {
714 if(p)
715 {
716 this->_M_file.sys_open(*p.get(), mode);
717
718 if(open_check(mode))
719 {
720 p.release();
721 return this;
722 }
723 }
724 return {};
725 }
726 template<typename _tPathChar>
727 std::basic_filebuf<_tChar, _tTraits>*
728 open(const _tPathChar* s, std::ios_base::openmode mode)
729 {
730 if(!this->is_open())
731 {
732 this->_M_file.sys_open(uopen(s, omode_convb(mode), DefaultPMode()),
733 mode);
734 if(open_check(mode))
735 return this;
736 }
737 return {};
738 }
740 template<class _tString,
741 yimpl(typename = ystdex::enable_for_string_class_t<_tString>)>
742 std::basic_filebuf<_tChar, _tTraits>*
743 open(const _tString& s, std::ios_base::openmode mode)
744 {
745 return open(s.c_str(), mode);
746 }
747
748 private:
750 bool
751 open_check(std::ios_base::openmode mode)
752 {
753 if(this->is_open())
754 {
755 this->_M_allocate_internal_buffer();
756 this->_M_mode = mode;
757 yunseq(this->_M_reading = {}, this->_M_writing = {});
758 this->_M_set_buffer(-1);
759 yunseq(this->_M_state_cur = this->_M_state_beg,
760 this->_M_state_last = this->_M_state_beg);
761 if((mode & std::ios_base::ate) && this->seekoff(0,
762 std::ios_base::end) == pos_type(off_type(-1)))
763 this->close();
764 else
765 return true;
766 }
767 return {};
768 }
769 };
770
771
772 //extern template class YF_API basic_filebuf<char>;
773 //extern template class YF_API basic_filebuf<wchar_t>;
774
775 using filebuf = basic_filebuf<char>;
776 using wfilebuf = basic_filebuf<wchar_t>;
777
778
780
781 template<typename _tChar, class _tTraits = std::char_traits<_tChar>>
782 class basic_ifstream : public std::basic_istream<_tChar, _tTraits>
783 {
784 public:
785 using char_type = _tChar;
786 using int_type = typename _tTraits::int_type;
787 using pos_type = typename _tTraits::pos_type;
788 using off_type = typename _tTraits::off_type;
789 using traits_type = _tTraits;
790
791 private:
792 using base_type = std::basic_istream<char_type, traits_type>;
793
794 mutable basic_filebuf<_tChar, _tTraits> fbuf{};
795
796 public:
797 basic_ifstream()
798 : base_type({})
799 {
800 this->init(&fbuf);
801 }
803 template<typename _tParam,
804 yimpl(typename = ystdex::exclude_self_t<basic_ifstream, _tParam>)>
805 explicit
806 basic_ifstream(_tParam&& s,
807 std::ios_base::openmode mode = std::ios_base::in)
808 : base_type({})
809 {
810 this->init(&fbuf);
811 this->open(yforward(s), mode);
812 }
813 DefDelCopyCtor(basic_ifstream)
814 basic_ifstream(basic_ifstream&& rhs)
815 : base_type(std::move(rhs)),
816 fbuf(std::move(rhs.fbuf))
817 {
818 base_type::set_rdbuf(&fbuf);
819 }
820 DefDeDtor(basic_ifstream)
821
822 DefDelCopyAssignment(basic_ifstream)
823 basic_ifstream&
824 operator=(basic_ifstream&& rhs)
825 {
826 base_type::operator=(std::move(rhs));
827 fbuf = std::move(rhs.fbuf);
828 return *this;
829 }
830
831 # if YB_IMPL_GNUCPP && YB_IMPL_GNUCPP >= 50000 && __GLIBCXX__ > 20140922
832 void
833 swap(basic_ifstream& rhs)
834 {
835 base_type::swap(rhs),
836 fbuf.swap(rhs.fbuf);
837 }
838 # endif
839
840 void
841 close()
842 {
843 if(!fbuf.close())
844 this->setstate(std::ios_base::failbit);
845 }
846
847 bool
848 is_open() const
849 {
850 return fbuf.is_open();
851 }
852
854 template<typename _tParam>
855 void
856 open(_tParam&& s,
857 std::ios_base::openmode mode = std::ios_base::in)
858 {
859 if(fbuf.open(yforward(s), mode))
860 this->clear();
861 else
862 this->setstate(std::ios_base::failbit);
863 }
864
865 std::basic_filebuf<_tChar, _tTraits>*
866 rdbuf() const
867 {
868 return &fbuf;
869 }
870 };
871
872 # if __GLIBCXX__ > 20140922
873 template<typename _tChar, class _tTraits>
874 inline DefSwap(, basic_ifstream<_tChar YPP_Comma _tTraits>)
875 # endif
876
877
878 template<typename _tChar, class _tTraits = std::char_traits<_tChar>>
879 class basic_ofstream : public std::basic_ostream<_tChar, _tTraits>
880 {
881 public:
882 using char_type = _tChar;
883 using int_type = typename _tTraits::int_type;
884 using pos_type = typename _tTraits::pos_type;
885 using off_type = typename _tTraits::off_type;
886 using traits_type = _tTraits;
887
888 private:
889 using base_type = std::basic_ostream<char_type, traits_type>;
890
892 mutable basic_filebuf<_tChar, _tTraits> fbuf{};
893
894 public:
895 basic_ofstream()
896 : base_type({})
897 {
898 this->init(&fbuf);
899 }
901 template<typename _tParam,
902 yimpl(typename = ystdex::exclude_self_t<basic_ofstream, _tParam>)>
903 explicit
904 basic_ofstream(_tParam&& s,
905 std::ios_base::openmode mode = std::ios_base::out)
906 : base_type({})
907 {
908 this->init(&fbuf);
909 this->open(yforward(s), mode);
910 }
911 DefDelCopyCtor(basic_ofstream)
912 basic_ofstream(basic_ofstream&& rhs)
913 : base_type(std::move(rhs)),
914 fbuf(std::move(rhs.fbuf))
915 {
916 base_type::set_rdbuf(&fbuf);
917 }
918 DefDeDtor(basic_ofstream)
919
920 DefDelCopyAssignment(basic_ofstream)
921 basic_ofstream&
922 operator=(basic_ofstream&& rhs)
923 {
924 base_type::operator=(std::move(rhs));
925 fbuf = std::move(rhs.fbuf);
926 return *this;
927 }
928
929 # if YB_IMPL_GNUCPP && YB_IMPL_GNUCPP >= 50000 && __GLIBCXX__ > 20140922
930 void
931 swap(basic_ofstream& rhs)
932 {
933 base_type::swap(rhs),
934 fbuf.swap(rhs.fbuf);
935 }
936 # endif
937
938 void
939 close()
940 {
941 if(!fbuf.close())
942 this->setstate(std::ios_base::failbit);
943 }
944
945 bool
946 is_open() const
947 {
948 return fbuf.is_open();
949 }
950
952 template<typename _tParam>
953 void
954 open(_tParam&& s, std::ios_base::openmode mode = std::ios_base::out)
955 {
956 if(fbuf.open(yforward(s), mode))
957 this->clear();
958 else
959 this->setstate(std::ios_base::failbit);
960 }
961
962 std::basic_filebuf<_tChar, _tTraits>*
963 rdbuf() const
964 {
965 return &fbuf;
966 }
967 };
968
969 # if __GLIBCXX__ > 20140922
970 template<typename _tChar, class _tTraits>
971 inline DefSwap(, basic_ofstream<_tChar YPP_Comma _tTraits>)
972 # endif
973
974
975
976 template<typename _tChar, class _tTraits = std::char_traits<_tChar>>
977 class basic_fstream : public std::basic_iostream<_tChar, _tTraits>
978 {
979 public:
980 using char_type = _tChar;
981 using int_type = typename _tTraits::int_type;
982 using pos_type = typename _tTraits::pos_type;
983 using off_type = typename _tTraits::off_type;
984 using traits_type = _tTraits;
985
986 private:
987 using base_type = std::basic_iostream<char_type, traits_type>;
988
990 mutable basic_filebuf<_tChar, _tTraits> fbuf{};
991
992 public:
993 basic_fstream()
994 : base_type({})
995 {
996 this->init(&fbuf);
997 }
998 template<typename _tParam,
999 yimpl(typename = ystdex::exclude_self_t<basic_fstream, _tParam>)>
1000 explicit
1001 basic_fstream(_tParam&& s,
1002 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
1003 : base_type({})
1004 {
1005 this->init(&fbuf);
1006 this->open(yforward(s), mode);
1007 }
1008 DefDelCopyCtor(basic_fstream)
1009 basic_fstream(basic_fstream&& rhs)
1010 : base_type(std::move(rhs)),
1011 fbuf(std::move(rhs.fbuf))
1012 {
1013 base_type::set_rdbuf(&fbuf);
1014 }
1015 DefDeDtor(basic_fstream)
1016
1017 DefDelCopyAssignment(basic_fstream)
1018 basic_fstream&
1019 operator=(basic_fstream&& rhs)
1020 {
1021 base_type::operator=(std::move(rhs));
1022 fbuf = std::move(rhs.fbuf);
1023 return *this;
1024 }
1025
1026 # if YB_IMPL_GNUCPP && YB_IMPL_GNUCPP >= 50000 && __GLIBCXX__ > 20140922
1027 void
1028 swap(basic_fstream& rhs)
1029 {
1030 base_type::swap(rhs),
1031 fbuf.swap(rhs.fbuf);
1032 }
1033 # endif
1034
1035 void
1036 close()
1037 {
1038 if(!fbuf.close())
1039 this->setstate(std::ios_base::failbit);
1040 }
1041
1042 bool
1043 is_open() const
1044 {
1045 return fbuf.is_open();
1046 }
1047
1049 template<typename _tParam>
1050 void
1051 open(_tParam&& s,
1052 std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
1053 {
1054 if(fbuf.open(yforward(s), mode))
1055 this->clear();
1056 else
1057 this->setstate(std::ios_base::failbit);
1058 }
1059
1060 std::basic_filebuf<_tChar, _tTraits>*
1061 rdbuf() const
1062 {
1063 return &fbuf;
1064 }
1065 };
1066
1067 # if __GLIBCXX__ > 20140922
1068 template<typename _tChar, class _tTraits>
1069 inline DefSwap(, basic_fstream<_tChar YPP_Comma _tTraits>)
1070 # endif
1071
1072
1073 //extern template class YF_API basic_fstream<char>;
1074 //extern template class YF_API basic_fstream<wchar_t>;
1075
1077
1078 using ifstream = basic_ifstream<char>;
1079 using ofstream = basic_ofstream<char>;
1081 using fstream = basic_fstream<char>;
1082 using wifstream = basic_ifstream<wchar_t>;
1083 using wofstream = basic_ofstream<wchar_t>;
1085 using wfstream = basic_fstream<wchar_t>;
1086 #else
1087 // TODO: Use VC++ extensions to support %char16_t path initialization.
1088 using std::basic_filebuf;
1089 using std::filebuf;
1090 using std::wfilebuf;
1092
1093 using std::basic_ifstream;
1094 using std::basic_ofstream;
1096 using std::basic_fstream;
1097 using std::ifstream;
1098 using std::ofstream;
1100 using std::fstream;
1101 using std::wifstream;
1102 using std::wofstream;
1104 using std::wfstream;
1105 #endif
1106
1107
1108
1115 template<typename _tChar>
1116 basic_string<_tChar>
1117 TryGetCurrentWorkingDirectory(size_t init)
1118 {
1119 return ystdex::retry_for_vector<basic_string<_tChar>>(init,
1120 [](basic_string<_tChar>& res, size_t) -> bool{
1121 const auto r(ugetcwd(&res[0], res.length()));
1122
1123 if(r)
1124 {
1125 res = r;
1126 return {};
1127 }
1128
1129 const auto e(errno);
1130
1131 if(e != ERANGE)
1132 ystdex::throw_error(e);
1133 return true;
1134 });
1135 }
1136
1137
1142 class YF_API FileOperationFailure : public std::system_error
1143 {
1144 public:
1146 using system_error::system_error;
1147
1149 DefDeCopyCtor(FileOperationFailure)
1154 ~FileOperationFailure() override;
1155 };
1156
1157
1164 template<typename _tParam>
1165 YB_NORETURN void
1166 ThrowFileOperationFailure(_tParam&& arg)
1167 {
1168 ystdex::throw_error<FileOperationFailure>(errno, yforward(arg));
1169 }
1170
1171
1173
1174
1179 inline YB_NONNULL(1) PDefH(FileTime, GetFileAccessTimeOf, std::FILE* fp)
1180 ImplRet(FileDescriptor(fp).GetAccessTime())
1186 YF_API YB_NONNULL(1) FileTime
1187 GetFileAccessTimeOf(const char*, bool = {});
1188 YF_API YB_NONNULL(1) FileTime
1189 GetFileAccessTimeOf(const char16_t*, bool = {});
1191
1192
1194
1199 inline YB_NONNULL(1) PDefH(FileTime, GetFileModificationTimeOf, std::FILE* fp)
1200 ImplRet(FileDescriptor(fp).GetModificationTime())
1201
1207 YF_API YB_NONNULL(1) FileTime
1208 GetFileModificationTimeOf(const char*, bool = {});
1209 YF_API YB_NONNULL(1) FileTime
1210 GetFileModificationTimeOf(const char16_t*, bool = {});
1212
1213
1215
1220 inline YB_NONNULL(1) PDefH(array<FileTime YPP_Comma 2>,
1221 GetFileModificationAndAccessTimeOf, std::FILE* fp)
1222 ImplRet(FileDescriptor(fp).GetModificationAndAccessTime())
1228 YF_API YB_NONNULL(1) array<FileTime, 2>
1229 GetFileModificationAndAccessTimeOf(const char*, bool = {});
1230 YF_API YB_NONNULL(1) array<FileTime, 2>
1231 GetFileModificationAndAccessTimeOf(const char16_t*, bool = {});
1233
1234
1235
1243 YB_NONNULL(1) size_t
1244 FetchNumberOfLinks(const char*, bool = true) ynothrowv;
1245 YB_NONNULL(1) size_t
1246 FetchNumberOfLinks(const char16_t*, bool = true) ynothrowv;
1248
1249
1261 YF_API YB_NONNULL(1) UniqueFile
1262 EnsureUniqueFile(const char*, mode_t = DefaultPMode(), size_t = 1, bool = {});
1264
1275 YF_API YB_NONNULL(1, 2) bool
1277 HaveSameContents(const char*, const char*, mode_t = DefaultPMode());
1285 YF_API YB_NONNULL(3, 4) bool
1286 HaveSameContents(UniqueFile, UniqueFile, const char*, const char*);
1288
1295 yconstfn YB_PURE PDefH(bool, IsNodeShared, const FileNodeID& x,
1296 const FileNodeID& y) ynothrow
1297 ImplRet(x != FileNodeID() && x == y)
1304 YF_API YB_NONNULL(1, 2) bool
1305 IsNodeShared(const char*, const char*, bool = true) ynothrowv;
1306 YF_API YB_NONNULL(1, 2) bool
1307 IsNodeShared(const char16_t*, const char16_t*, bool = true) ynothrowv;
1309
1313 bool
1314 IsNodeShared(FileDescriptor, FileDescriptor) ynothrow;
1316
1317 } // namespace platform;
1318
1319 #endif
1320
platform::NodeCategory::Directory
platform::DefaultPMode
YF_API YB_STATELESS mode_t DefaultPMode() ynothrow
取默认权限。
Definition: FileIO.cpp:716
platform::p
while p p
Definition: YCLib/FileSystem.h:225
platform::SetBinaryIO
YF_API void SetBinaryIO(std::FILE *) ynothrowv
设置标准库流二进制输入/输出模式。
Definition: FileIO.cpp:728
ystdex::cpp2014::exchange
_type exchange(_type &obj, _type2 &&new_val)
交换值并返回旧值。
Definition: utility.hpp:86
YB_NORETURN
#define YB_NORETURN
指定无返回值函数。
Definition: ydef.h:478
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
platform::NodeCategory::Communicator
platform::mode_t
::mode_t mode_t
文件模式类型。
Definition: FileIO.h:108
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:522
platform::NodeCategory::Special
platform::Nonnull
_type && Nonnull(_type &&p) ynothrow
断言并返回非空指针。
Definition: Debug.h:411
YSLib::Text::buf
const char * buf
Definition: TextFile.h:77
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
platform::NodeCategory::Empty
ystdex::swap_underlying
void swap_underlying(_type &x, _type &y)
交换相同标准布局类型可修改左值的存储。
Definition: utility.hpp:61
DefDelCopyCtor
#define DefDelCopyCtor(_t)
Definition: YBaseMacro.h:163
DefBitmaskEnum
#define DefBitmaskEnum(_tEnum)
Definition: YBaseMacro.h:475
platform::NodeCategory::Missing
utility.hpp
实用设施。
platform::DefCvt
yconstfn DefCvt(const ynothrow, typename Trait::IntegerType, Integer) yconstfn DefGetter(const ynothrow
platform::NodeCategory::Invalid
YSLib::IO::FileDescriptor
YF_API const char *YF_API FileDescriptor
Definition: YSLib/Service/FileSystem.h:575
platform::NodeCategory::Regular
platform::NodeCategory::Block
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
platform::NodeCategory
NodeCategory
文件节点类别。
Definition: FileIO.h:117
platform::NodeCategory::Character
platform::CategorizeNode
NodeCategory CategorizeNode(mode_t st_mode) ynothrow
Definition: FileIO.cpp:376
platform::NodeCategory::Link
platform::size_t
YF_API size_t
Definition: FileIO.h:1262
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
platform::IsNodeShared
bool IsNodeShared(FileDescriptor, FileDescriptor) ynothrow
Definition: FileIO.cpp:1219
platform::GetFileModificationTimeOf
GetFileModificationTimeOf
Definition: FileIO.h:1199
platform::uchdir
bool uchdir(const char *path) ynothrowv
Definition: FileIO.cpp:1023
platform::uopen
int uopen(const char *filename, int oflag, mode_t pmode) ynothrowv
Definition: FileIO.cpp:804
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:708
DefDelCopyAssignment
#define DefDelCopyAssignment(_t)
Definition: YBaseMacro.h:187
platform::NodeCategory::Unknown
platform::uaccess
int uaccess(const char *path, int amode) ynothrowv
Definition: FileIO.cpp:781
platform::MakePathString
MakePathString
Definition: FileIO.h:61
platform::ufexists
bool ufexists(const char *filename) ynothrowv
Definition: FileIO.cpp:875
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
CHRLib::char16_t
char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:501
ystdex::assign
void assign(_tCon &con, _tParams &&...args)
插入参数指定的元素到容器。
Definition: container.hpp:412
platform::NodeCategory::Device
platform::UniqueFile
unique_ptr_from< FileDescriptor::Deleter > UniqueFile
Definition: FileIO.h:427
ystdex::setstate
void setstate(std::basic_ios< _tChar, _tTraits > &ios, std::ios_base::iostate state)
设置流状态。
Definition: ios.hpp:44
platform::upclose
int upclose(std::FILE *fp) ynothrowv
Definition: FileIO.cpp:892
ystdex::enable_for_string_class_t
enable_if_t< is_string_class< decay_t< _tParam >>::value, _type > enable_for_string_class_t
选择字符串类类型的特定重载避免和其它非字符串类型冲突。
Definition: string.hpp:219
container.hpp
通用容器操作。
platform::NodeCategory::SymbolicLink
ystdex::throw_error
void throw_error(std::error_code ec, _tParams &&...args)
抛出错误： std::system_error 或允许相同构造函数参数的异常。
Definition: ystdex/exception.h:162
platform::umkdir
bool umkdir(const char *path) ynothrowv
Definition: FileIO.cpp:1025
platform
默认平台命名空间。
Definition: Container.h:47
std
Definition: memory.hpp:1100
platform::NodeCategory::MountPoint
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:303
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
platform::references::unique_ptr_from
unique_ptr< ystdex::defer_element< ystdex::unique_ptr_pointer< void, _tDeleter >, _tDefault >, _tDeleter > unique_ptr_from
取删除器对应的 unique_ptr 实例。
Definition: Reference.h:83
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:161
platform::uunlink
bool uunlink(const char *path) ynothrowv
Definition: FileIO.cpp:1035
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:996
YSLib::path
YF_API const char * path
Definition: Initialization.h:74
platform::DefDeCtor
DefDeCtor(BGRA) yconstfn BGRA(typename Trait
Definition: Video.h:152
ystdex
特性测试宏。
Definition: ydef.h:796
platform::FileOperationFailure
表示文件操作失败的异常。
Definition: FileIO.h:1142
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
platform::uremove
bool uremove(const char *path) ynothrowv
Definition: FileIO.cpp:1044
platform::TryGetCurrentWorkingDirectory
basic_string< _tChar > TryGetCurrentWorkingDirectory(size_t init)
尝试按指定的起始缓冲区大小取当前工作目录的路径。
Definition: FileIO.h:1117
YModules.h
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
fp
tidy_ptr< std::istream > fp
Definition: TextManager.cpp:86
platform::pmode
YF_API int mode_t pmode
Definition: FileIO.h:504
ystdex::u16string_view
basic_string_view< char16_t > u16string_view
Definition: string_view.hpp:416
YPP_Comma
#define YPP_Comma
替换为逗号的预处理记号。
Definition: ydef.h:225
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
platform::ImplRet
const string &s ImplRet(s) YF_API string MakePathString(const char16_t *)
platform::NodeCategory::Junction
platform::HaveSameContents
bool HaveSameContents(const char *path_a, const char *path_b, mode_t mode)
Definition: FileIO.cpp:1155
DefDeDtor
#define DefDeDtor(_t)
Definition: YBaseMacro.h:176
string.hpp
ISO C++ 标准字符串扩展。
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
platform::omode_convb
std::ios_base::sync_with_stdio(sync)) YF_API YB_STATELESS int omode_conv(std YF_API YB_STATELESS int omode_convb(std::ios_base::openmode) ynothrow
ISO C++ 标准输入输出接口打开模式转换为 POSIX 文件打开模式。
Definition: FileIO.cpp:768
platform::urmdir
bool urmdir(const char *path) ynothrowv
Definition: FileIO.cpp:1033
platform::YB_NONNULL
YF_API YB_NONNULL(1) int uaccess(const char *path
测试路径可访问性。
Definition: YCLib/FileSystem.h:144
platform::ugetcwd
char * ugetcwd(char *buf, size_t size) ynothrowv
Definition: FileIO.cpp:939
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
platform::ynothrowv
YF_API int amode ynothrowv
Definition: FileIO.h:491
YF_API
#define YF_API
Definition: Platform.h:56
platform::NodeCategory::Socket
ystdex::dep_ops::totally_ordered
details::flat_ops< _type, _type2, _tOpt, less_than_comparable, equality_comparable > totally_ordered
Definition: operators.hpp:367
platform::upopen
std::FILE * upopen(const char *filename, const char *mode) ynothrowv
Definition: FileIO.cpp:906
platform::GetFileModificationAndAccessTimeOf
GetFileModificationAndAccessTimeOf
Definition: FileIO.h:1221
platform::NodeCategory::FIFO
platform::ufopen
std::FILE * ufopen(const char *filename, const char *mode) ynothrowv
Definition: FileIO.cpp:828
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
platform::oflag
YF_API int oflag
Definition: FileIO.h:504
platform::EnsureUniqueFile
UniqueFile EnsureUniqueFile(const char *dst, mode_t mode, size_t allowed_links, bool share)
Definition: FileIO.cpp:1122
platform::GetFileAccessTimeOf
GetFileAccessTimeOf
Definition: FileIO.h:1179
platform::FileDescriptor::FileDescriptor
yconstfn FileDescriptor(nullptr_t) ynothrow
Definition: FileIO.h:199
platform::omode_conv
int omode_conv(std::ios_base::openmode mode) ynothrow
Definition: FileIO.cpp:740
platform::FileDescriptor
文件描述符包装类。
Definition: FileIO.h:171
platform::FileDescriptor::Deleter
删除器。
Definition: FileIO.h:178
platform::PDefH
PDefH(string, MakePathString, const char *s) ImplRet(Nonnull(s)) inline PDefH(const string &
构造适合表示路径的字符串。