1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
4 // wxTempFile - safely replace the old file
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
19 #include "wx/string.h"
20 #include "wx/filefn.h"
21 #include "wx/convauto.h"
23 // ----------------------------------------------------------------------------
24 // class wxFile: raw file IO
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 // ----------------------------------------------------------------------------
30 class WXDLLIMPEXP_BASE wxFile
33 // more file constants
34 // -------------------
36 enum OpenMode
{ read
, write
, read_write
, write_append
, write_excl
};
37 // standard values for file descriptor
38 enum { fd_invalid
= -1, fd_stdin
, fd_stdout
, fd_stderr
};
42 // check whether a regular file by this name exists
43 static bool Exists(const wxString
& name
);
44 // check whether we can access the given file in given mode
45 // (only read and write make sense here)
46 static bool Access(const wxString
& name
, OpenMode mode
);
51 wxFile() { m_fd
= fd_invalid
; m_lasterror
= 0; }
52 // open specified file (may fail, use IsOpened())
53 wxFile(const wxString
& fileName
, OpenMode mode
= read
);
54 // attach to (already opened) file
55 wxFile(int lfd
) { m_fd
= lfd
; m_lasterror
= 0; }
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)
60 bool Create(const wxString
& fileName
, bool bOverwrite
= false,
61 int access
= wxS_DEFAULT
);
62 bool Open(const wxString
& fileName
, OpenMode mode
= read
,
63 int access
= wxS_DEFAULT
);
64 bool Close(); // Close is a NOP if not opened
66 // assign an existing file descriptor and get it back from wxFile object
67 void Attach(int lfd
) { Close(); m_fd
= lfd
; m_lasterror
= 0; }
68 void Detach() { m_fd
= fd_invalid
; }
69 int fd() const { return m_fd
; }
71 // read/write (unbuffered)
72 // read all data from the file into a string (useful for text files)
73 bool ReadAll(wxString
*str
, const wxMBConv
& conv
= wxConvAuto());
74 // returns number of bytes read or wxInvalidOffset on error
75 ssize_t
Read(void *pBuf
, size_t nCount
);
76 // returns the number of bytes written
77 size_t Write(const void *pBuf
, size_t nCount
);
78 // returns true on success
79 bool Write(const wxString
& s
, const wxMBConv
& conv
= wxConvAuto());
80 // flush data not yet written
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
);
86 // move ptr to ofs bytes before the end
87 wxFileOffset
SeekEnd(wxFileOffset ofs
= 0) { return Seek(ofs
, wxFromEnd
); }
89 wxFileOffset
Tell() const;
90 // get current file length
91 wxFileOffset
Length() const;
95 bool IsOpened() const { return m_fd
!= fd_invalid
; }
96 // is end of file reached?
98 // has an error occurred?
99 bool Error() const { return m_lasterror
!= 0; }
101 int GetLastError() const { return m_lasterror
; }
103 void ClearLastError() { m_lasterror
= 0; }
104 // type such as disk or pipe
105 wxFileKind
GetKind() const { return wxGetFileKind(m_fd
); }
108 // dtor closes the file if opened
109 ~wxFile() { Close(); }
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
&);
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.
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;
126 int m_fd
; // file descriptor or INVALID_FD if not opened
127 int m_lasterror
; // errno value of last error
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 // ----------------------------------------------------------------------------
138 class WXDLLIMPEXP_BASE wxTempFile
144 // associates the temp file with the file to be replaced and opens it
145 wxTempFile(const wxString
& strName
);
147 // open the temp file (strName is the name of file to be replaced)
148 bool Open(const wxString
& strName
);
150 // is the file opened?
151 bool IsOpened() const { return m_file
.IsOpened(); }
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(); }
160 // I/O (both functions return true on success, false on failure)
161 bool Write(const void *p
, size_t n
) { return m_file
.Write(p
, n
) == n
; }
162 bool Write(const wxString
& str
, const wxMBConv
& conv
= wxMBConvUTF8())
163 { return m_file
.Write(str
, conv
); }
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(); }
169 // different ways to close the file
170 // validate changes and delete the old file of name m_strName
175 // dtor calls Discard() if file is still opened
179 // no copy ctor/assignment operator
180 wxTempFile(const wxTempFile
&);
181 wxTempFile
& operator=(const wxTempFile
&);
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
190 #endif // _WX_FILEH__