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