/src/wxwidgets/include/wx/file.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/file.h |
3 | | // Purpose: wxFile - encapsulates low-level "file descriptor" |
4 | | // wxTempFile - safely replace the old file |
5 | | // Author: Vadim Zeitlin |
6 | | // Created: 29/01/98 |
7 | | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
8 | | // Licence: wxWindows licence |
9 | | ///////////////////////////////////////////////////////////////////////////// |
10 | | |
11 | | #ifndef _WX_FILEH__ |
12 | | #define _WX_FILEH__ |
13 | | |
14 | | #include "wx/defs.h" |
15 | | |
16 | | #if wxUSE_FILE |
17 | | |
18 | | #include "wx/string.h" |
19 | | #include "wx/filefn.h" |
20 | | #include "wx/convauto.h" |
21 | | |
22 | | // ---------------------------------------------------------------------------- |
23 | | // class wxFile: raw file IO |
24 | | // |
25 | | // NB: for space efficiency this class has no virtual functions, including |
26 | | // dtor which is _not_ virtual, so it shouldn't be used as a base class. |
27 | | // ---------------------------------------------------------------------------- |
28 | | |
29 | | class WXDLLIMPEXP_BASE wxFile |
30 | | { |
31 | | public: |
32 | | // more file constants |
33 | | // ------------------- |
34 | | // suppress Xcode 11 warning about shadowing global read() symbol |
35 | | wxCLANG_WARNING_SUPPRESS(shadow) |
36 | | |
37 | | // opening mode |
38 | | enum OpenMode { read, write, read_write, write_append, write_excl }; |
39 | | // standard values for file descriptor |
40 | | enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr }; |
41 | | |
42 | | wxCLANG_WARNING_RESTORE(shadow) |
43 | | |
44 | | // static functions |
45 | | // ---------------- |
46 | | // check whether a regular file by this name exists |
47 | | static bool Exists(const wxString& name); |
48 | | // check whether we can access the given file in given mode |
49 | | // (only read and write make sense here) |
50 | | static bool Access(const wxString& name, OpenMode mode); |
51 | | |
52 | | // ctors |
53 | | // ----- |
54 | | // def ctor |
55 | 0 | wxFile() { m_fd = fd_invalid; m_lasterror = 0; } |
56 | | // open specified file (may fail, use IsOpened()) |
57 | | wxFile(const wxString& fileName, OpenMode mode = read); |
58 | | // attach to (already opened) file |
59 | 0 | wxFile(int lfd) { m_fd = lfd; m_lasterror = 0; } |
60 | | |
61 | | // open/close |
62 | | // create a new file (with the default value of bOverwrite, it will fail if |
63 | | // the file already exists, otherwise it will overwrite it and succeed) |
64 | | bool Create(const wxString& fileName, bool bOverwrite = false, |
65 | | int access = wxS_DEFAULT); |
66 | | bool Open(const wxString& fileName, OpenMode mode = read, |
67 | | int access = wxS_DEFAULT); |
68 | | bool Close(); // Close is a NOP if not opened |
69 | | |
70 | | // assign an existing file descriptor and get it back from wxFile object |
71 | 0 | void Attach(int lfd) { Close(); m_fd = lfd; m_lasterror = 0; } |
72 | 0 | int Detach() { const int fdOld = m_fd; m_fd = fd_invalid; return fdOld; } |
73 | 0 | int fd() const { return m_fd; } |
74 | | |
75 | | // read/write (unbuffered) |
76 | | // read all data from the file into a string (useful for text files) |
77 | | bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto()); |
78 | | // returns number of bytes read or wxInvalidOffset on error |
79 | | ssize_t Read(void *pBuf, size_t nCount); |
80 | | // returns the number of bytes written |
81 | | size_t Write(const void *pBuf, size_t nCount); |
82 | | // returns true on success |
83 | | bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto()); |
84 | | // flush data not yet written |
85 | | bool Flush(); |
86 | | |
87 | | // file pointer operations (return wxInvalidOffset on failure) |
88 | | // move ptr ofs bytes related to start/current offset/end of file |
89 | | wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart); |
90 | | // move ptr to ofs bytes before the end |
91 | 0 | wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); } |
92 | | // get current offset |
93 | | wxFileOffset Tell() const; |
94 | | // get current file length |
95 | | wxFileOffset Length() const; |
96 | | |
97 | | // simple accessors |
98 | | // is file opened? |
99 | 0 | bool IsOpened() const { return m_fd != fd_invalid; } |
100 | | // is end of file reached? |
101 | | bool Eof() const; |
102 | | // has an error occurred? |
103 | 0 | bool Error() const { return m_lasterror != 0; } |
104 | | // get last errno |
105 | 0 | int GetLastError() const { return m_lasterror; } |
106 | | // reset error state |
107 | 0 | void ClearLastError() { m_lasterror = 0; } |
108 | | // type such as disk or pipe |
109 | 0 | wxFileKind GetKind() const { return wxGetFileKind(m_fd); } |
110 | | |
111 | | |
112 | | // dtor closes the file if opened |
113 | 0 | ~wxFile() { Close(); } |
114 | | |
115 | | private: |
116 | | // copy ctor and assignment operator are private because |
117 | | // it doesn't make sense to copy files this way: |
118 | | // attempt to do it will provoke a compile-time error. |
119 | | wxFile(const wxFile&); |
120 | | wxFile& operator=(const wxFile&); |
121 | | |
122 | | // Copy the value of errno into m_lasterror if rc == -1 and return true in |
123 | | // this case (indicating that we've got an error). Otherwise return false. |
124 | | // |
125 | | // Notice that we use the possibly 64 bit wxFileOffset instead of int here so |
126 | | // that it works for checking the result of functions such as tell() too. |
127 | | bool CheckForError(wxFileOffset rc) const; |
128 | | |
129 | | |
130 | | int m_fd; // file descriptor or INVALID_FD if not opened |
131 | | int m_lasterror; // errno value of last error |
132 | | }; |
133 | | |
134 | | // ---------------------------------------------------------------------------- |
135 | | // class wxTempFile: if you want to replace another file, create an instance |
136 | | // of wxTempFile passing the name of the file to be replaced to the ctor. Then |
137 | | // you can write to wxTempFile and call Commit() function to replace the old |
138 | | // file (and close this one) or call Discard() to cancel the modification. If |
139 | | // you call neither of them, dtor will call Discard(). |
140 | | // ---------------------------------------------------------------------------- |
141 | | |
142 | | class WXDLLIMPEXP_BASE wxTempFile |
143 | | { |
144 | | public: |
145 | | // ctors |
146 | | // default |
147 | | wxTempFile() = default; |
148 | | // associates the temp file with the file to be replaced and opens it |
149 | | explicit wxTempFile(const wxString& strName); |
150 | | |
151 | | // open the temp file (strName is the name of file to be replaced) |
152 | | bool Open(const wxString& strName); |
153 | | |
154 | | // is the file opened? |
155 | 0 | bool IsOpened() const { return m_file.IsOpened(); } |
156 | | // get current file length |
157 | 0 | wxFileOffset Length() const { return m_file.Length(); } |
158 | | // move ptr ofs bytes related to start/current offset/end of file |
159 | | wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart) |
160 | 0 | { return m_file.Seek(ofs, mode); } |
161 | | // get current offset |
162 | 0 | wxFileOffset Tell() const { return m_file.Tell(); } |
163 | | |
164 | | // I/O (both functions return true on success, false on failure) |
165 | 0 | bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; } |
166 | | bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8()) |
167 | 0 | { return m_file.Write(str, conv); } |
168 | | |
169 | | // flush data: can be called before closing file to ensure that data was |
170 | | // correctly written out |
171 | 0 | bool Flush() { return m_file.Flush(); } |
172 | | |
173 | | // different ways to close the file |
174 | | // validate changes and delete the old file of name m_strName |
175 | | bool Commit(); |
176 | | // discard changes |
177 | | void Discard(); |
178 | | |
179 | | // dtor calls Discard() if file is still opened |
180 | | ~wxTempFile(); |
181 | | |
182 | | private: |
183 | | // no copy ctor/assignment operator |
184 | | wxTempFile(const wxTempFile&); |
185 | | wxTempFile& operator=(const wxTempFile&); |
186 | | |
187 | | wxString m_strName, // name of the file to replace in Commit() |
188 | | m_strTemp; // temporary file name |
189 | | wxFile m_file; // the temporary file |
190 | | }; |
191 | | |
192 | | #endif // wxUSE_FILE |
193 | | |
194 | | #endif // _WX_FILEH__ |