Add wxTempFFile, similar to wxTempFile but using buffered I/O

Also add wxTempFFileOutputStream.

Closes #18673.
This commit is contained in:
Dummy
2020-02-21 14:52:40 +01:00
committed by Vadim Zeitlin
parent 8c574e5c2b
commit 3e0780e811
6 changed files with 395 additions and 2 deletions

View File

@@ -103,6 +103,64 @@ private:
wxString m_name; // the name of the file (for diagnostic messages)
};
// ----------------------------------------------------------------------------
// class wxTempFFile: if you want to replace another file, create an instance
// of wxTempFFile passing the name of the file to be replaced to the ctor. Then
// you can write to wxTempFFile and call Commit() function to replace the old
// file (and close this one) or call Discard() to cancel the modification. If
// you call neither of them, dtor will call Discard().
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_BASE wxTempFFile
{
public:
// ctors
// default
wxTempFFile() { }
// associates the temp file with the file to be replaced and opens it
wxTempFFile(const wxString& strName);
// open the temp file (strName is the name of file to be replaced)
bool Open(const wxString& strName);
// is the file opened?
bool IsOpened() const { return m_file.IsOpened(); }
// get current file length
wxFileOffset Length() const { return m_file.Length(); }
// move ptr ofs bytes related to start/current pos/end of file
bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
{ return m_file.Seek(ofs, mode); }
// get current position in the file
wxFileOffset Tell() const { return m_file.Tell(); }
// I/O (both functions return true on success, false on failure)
bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
{ return m_file.Write(str, conv); }
// flush data: can be called before closing file to ensure that data was
// correctly written out
bool Flush() { return m_file.Flush(); }
// different ways to close the file
// validate changes and delete the old file of name m_strName
bool Commit();
// discard changes
void Discard();
// dtor calls Discard() if file is still opened
~wxTempFFile();
private:
// no copy ctor/assignment operator
wxTempFFile(const wxTempFFile&);
wxTempFFile& operator=(const wxTempFFile&);
wxString m_strName, // name of the file to replace in Commit()
m_strTemp; // temporary file name
wxFFile m_file; // the temporary file
};
#endif // wxUSE_FFILE
#endif // _WX_FFILE_H_