This source file includes following definitions.
- name
- mode
- check_open
- remove
1 #ifndef libjmmcg_core_file_hpp
2 #define libjmmcg_core_file_hpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "exception.hpp"
30 #include <cassert>
31 #include <fstream>
32 #include <string>
33
34 namespace jmmcg {
35
36 typedef std::basic_fstream<tchar> tfstream;
37 typedef std::basic_ifstream<tchar> tifstream;
38 typedef std::basic_ofstream<tchar> tofstream;
39
40
41
42
43 template <typename basic_file_type,ppd::generic_traits::api_type API_,typename Mdl_>
44 class file : public basic_file_type, protected non_copyable {
45 public:
46 typedef basic_file_type file_type;
47 typedef jmmcg::crt_exception<API_,Mdl_> exception_type;
48
49 file(const tstring &, const std::ios_base::openmode,const bool =true);
50 virtual ~file(void);
51 file &operator=(const file &);
52
53
54
55
56
57
58
59
60
61
62
63
64 virtual const bool operator==(const file &) const noexcept(true) {
65 return false;
66 }
67
68 const tstring &name(void) const noexcept(true) {
69 return fname;
70 }
71
72 private:
73 bool auto_delete;
74 tstring fname;
75 std::ios_base::openmode mode;
76
77 void check_open(void);
78 void remove(void);
79
80
81
82
83
84
85
86 explicit file(const tstring &) noexcept(true);
87 };
88
89 template<typename basic_file_type,ppd::generic_traits::api_type API_,typename Mdl_> inline
90 file<basic_file_type,API_,Mdl_>::file(const tstring &nm, const std::ios_base::openmode flags,const bool ad)
91 : basic_file_type(nm.c_str(), flags), auto_delete(ad), fname(nm), mode(flags) {
92 check_open();
93 }
94
95 template<typename basic_file_type,ppd::generic_traits::api_type API_,typename Mdl_> inline
96 file<basic_file_type,API_,Mdl_>::~file(void) {
97 if (auto_delete) {
98 remove();
99 }
100 }
101
102 template<typename basic_file_type,ppd::generic_traits::api_type API_,typename Mdl_> inline file<basic_file_type,API_,Mdl_> &
103 file<basic_file_type,API_,Mdl_>::operator=(const file<basic_file_type,API_,Mdl_> &tf) {
104 remove();
105 open(tf.fname,mode|=std::ios_base::out);
106 check_open();
107 file_type::operator<<(tf.rdbuf());
108 return *this;
109 }
110
111 template<typename basic_file_type,ppd::generic_traits::api_type API_,typename Mdl_> inline void
112 file<basic_file_type,API_,Mdl_>::check_open(void) noexcept(false) {
113 if (!file_type::is_open()) {
114 jmmcg::info::function desc(__LINE__,__PRETTY_FUNCTION__,typeid(&file<basic_file_type,API_,Mdl_>::check_open),info::function::argument(_T("file name"),fname));
115 desc.add_arg(info::function::argument(_T("open mode"), mode));
116 throw exception(_T("Failed to open the specified file."), desc, JMMCG_REVISION_HDR(_T("$Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/file.hpp 2055 2017-05-13 19:35:47Z jmmcg $")));
117 }
118 }
119
120 template<typename basic_file_type,ppd::generic_traits::api_type API_,typename Mdl_> inline void
121 file<basic_file_type,API_,Mdl_>::remove(void) noexcept(false) {
122 file_type::close();
123 if (::remove(fname.c_str())) {
124 throw exception(_T("Failed to delete the specified file."), jmmcg::info::function(__LINE__,__PRETTY_FUNCTION__,typeid(&file<basic_file_type,API_,Mdl_>::remove), info::function::argument(_T("file name"), fname)), JMMCG_REVISION_HDR(_T("$Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/file.hpp 2055 2017-05-13 19:35:47Z jmmcg $")));
125 }
126 }
127
128 }
129
130 #endif