1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFile - encapsulates low-level "file descriptor"
4 // wxTempFile - safely replace the old file
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
20 #include "wx/string.h"
21 #include "wx/filefn.h"
22 #include "wx/strconv.h"
24 // ----------------------------------------------------------------------------
25 // class wxFile: raw file IO
27 // NB: for space efficiency this class has no virtual functions, including
28 // dtor which is _not_ virtual, so it shouldn't be used as a base class.
29 // ----------------------------------------------------------------------------
31 class WXDLLIMPEXP_BASE wxFile
34 // more file constants
35 // -------------------
37 enum OpenMode
{ read
, write
, read_write
, write_append
, write_excl
};
38 // standard values for file descriptor
39 enum { fd_invalid
= -1, fd_stdin
, fd_stdout
, fd_stderr
};
43 // check whether a regular file by this name exists
44 static bool Exists(const wxString
& name
);
45 // check whether we can access the given file in given mode
46 // (only read and write make sense here)
47 static bool Access(const wxString
& name
, OpenMode mode
);
52 wxFile() { m_fd
= fd_invalid
; m_error
= false; }
53 // open specified file (may fail, use IsOpened())
54 wxFile(const wxString
& fileName
, OpenMode mode
= read
);
55 // attach to (already opened) file
56 wxFile(int lfd
) { m_fd
= lfd
; m_error
= false; }
59 // create a new file (with the default value of bOverwrite, it will fail if
60 // the file already exists, otherwise it will overwrite it and succeed)
61 bool Create(const wxString
& fileName
, bool bOverwrite
= false,
62 int access
= wxS_DEFAULT
);
63 bool Open(const wxString
& fileName
, OpenMode mode
= read
,
64 int access
= wxS_DEFAULT
);
65 bool Close(); // Close is a NOP if not opened
67 // assign an existing file descriptor and get it back from wxFile object
68 void Attach(int lfd
) { Close(); m_fd
= lfd
; m_error
= false; }
69 void Detach() { m_fd
= fd_invalid
; }
70 int fd() const { return m_fd
; }
72 // read/write (unbuffered)
73 // returns number of bytes read or wxInvalidOffset on error
74 ssize_t
Read(void *pBuf
, size_t nCount
);
75 // returns the number of bytes written
76 size_t Write(const void *pBuf
, size_t nCount
);
77 // returns true on success
78 bool Write(const wxString
& s
, const wxMBConv
& conv
= wxMBConvUTF8());
79 // flush data not yet written
82 // file pointer operations (return wxInvalidOffset on failure)
83 // move ptr ofs bytes related to start/current offset/end of file
84 wxFileOffset
Seek(wxFileOffset ofs
, wxSeekMode mode
= wxFromStart
);
85 // move ptr to ofs bytes before the end
86 wxFileOffset
SeekEnd(wxFileOffset ofs
= 0) { return Seek(ofs
, wxFromEnd
); }
88 wxFileOffset
Tell() const;
89 // get current file length
90 wxFileOffset
Length() const;
94 bool IsOpened() const { return m_fd
!= fd_invalid
; }
95 // is end of file reached?
97 // has an error occurred?
98 bool Error() const { return m_error
; }
99 // type such as disk or pipe
100 wxFileKind
GetKind() const { return wxGetFileKind(m_fd
); }
102 // dtor closes the file if opened
103 ~wxFile() { Close(); }
106 // copy ctor and assignment operator are private because
107 // it doesn't make sense to copy files this way:
108 // attempt to do it will provoke a compile-time error.
109 wxFile(const wxFile
&);
110 wxFile
& operator=(const wxFile
&);
112 int m_fd
; // file descriptor or INVALID_FD if not opened
113 bool m_error
; // error memory
116 // ----------------------------------------------------------------------------
117 // class wxTempFile: if you want to replace another file, create an instance
118 // of wxTempFile passing the name of the file to be replaced to the ctor. Then
119 // you can write to wxTempFile and call Commit() function to replace the old
120 // file (and close this one) or call Discard() to cancel the modification. If
121 // you call neither of them, dtor will call Discard().
122 // ----------------------------------------------------------------------------
124 class WXDLLIMPEXP_BASE wxTempFile
130 // associates the temp file with the file to be replaced and opens it
131 wxTempFile(const wxString
& strName
);
133 // open the temp file (strName is the name of file to be replaced)
134 bool Open(const wxString
& strName
);
136 // is the file opened?
137 bool IsOpened() const { return m_file
.IsOpened(); }
138 // get current file length
139 wxFileOffset
Length() const { return m_file
.Length(); }
140 // move ptr ofs bytes related to start/current offset/end of file
141 wxFileOffset
Seek(wxFileOffset ofs
, wxSeekMode mode
= wxFromStart
)
142 { return m_file
.Seek(ofs
, mode
); }
143 // get current offset
144 wxFileOffset
Tell() const { return m_file
.Tell(); }
146 // I/O (both functions return true on success, false on failure)
147 bool Write(const void *p
, size_t n
) { return m_file
.Write(p
, n
) == n
; }
148 bool Write(const wxString
& str
, const wxMBConv
& conv
= wxMBConvUTF8())
149 { return m_file
.Write(str
, conv
); }
151 // flush data: can be called before closing file to ensure that data was
152 // correctly written out
153 bool Flush() { return m_file
.Flush(); }
155 // different ways to close the file
156 // validate changes and delete the old file of name m_strName
161 // dtor calls Discard() if file is still opened
165 // no copy ctor/assignment operator
166 wxTempFile(const wxTempFile
&);
167 wxTempFile
& operator=(const wxTempFile
&);
169 wxString m_strName
, // name of the file to replace in Commit()
170 m_strTemp
; // temporary file name
171 wxFile m_file
; // the temporary file
176 #endif // _WX_FILEH__