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