]> git.saurik.com Git - wxWidgets.git/blame - include/wx/file.h
Added wxDECLARE_ANY_TYPE(CLS, DECL) and documented for what kind of situation it...
[wxWidgets.git] / include / wx / file.h
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
a1b82138 2// Name: 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
KB
23
24// ----------------------------------------------------------------------------
25// constants
26// ----------------------------------------------------------------------------
27
246037e2
VZ
28// we redefine these constants here because S_IREAD &c are _not_ standard
29// however, we do assume that the values correspond to the Unix umask bits
a18d02ec
FM
30enum wxPosixPermissions
31{
32 // standard Posix names for these permission flags:
33 wxS_IRUSR = 00400,
34 wxS_IWUSR = 00200,
35 wxS_IXUSR = 00100,
36
37 wxS_IRGRP = 00040,
38 wxS_IWGRP = 00020,
39 wxS_IXGRP = 00010,
40
41 wxS_IROTH = 00004,
42 wxS_IWOTH = 00002,
43 wxS_IXOTH = 00001,
44
45 // longer but more readable synonims for the constants above:
46 wxPOSIX_USER_READ = wxS_IRUSR,
47 wxPOSIX_USER_WRITE = wxS_IWUSR,
48 wxPOSIX_USER_EXECUTE = wxS_IXUSR,
49
50 wxPOSIX_GROUP_READ = wxS_IRGRP,
51 wxPOSIX_GROUP_WRITE = wxS_IWGRP,
52 wxPOSIX_GROUP_EXECUTE = wxS_IXGRP,
53
54 wxPOSIX_OTHERS_READ = wxS_IROTH,
55 wxPOSIX_OTHERS_WRITE = wxS_IWOTH,
56 wxPOSIX_OTHERS_EXECUTE = wxS_IXOTH,
57
58 // default mode for the new files: allow reading/writing them to everybody but
59 // the effective file mode will be set after anding this value with umask and
60 // so won't include wxS_IW{GRP,OTH} for the default 022 umask value
61 wxS_DEFAULT = (wxPOSIX_USER_READ | wxPOSIX_USER_WRITE | \
62 wxPOSIX_GROUP_READ | wxPOSIX_GROUP_WRITE | \
63 wxPOSIX_OTHERS_READ | wxPOSIX_OTHERS_WRITE),
64
65 // default mode for the new directories (see wxFileName::Mkdir): allow
66 // reading/writing/executing them to everybody, but just like wxS_DEFAULT
67 // the effective directory mode will be set after anding this value with umask
68 wxS_DIR_DEFAULT = (wxPOSIX_USER_READ | wxPOSIX_USER_WRITE | wxPOSIX_USER_EXECUTE | \
69 wxPOSIX_GROUP_READ | wxPOSIX_GROUP_WRITE | wxPOSIX_GROUP_EXECUTE | \
70 wxPOSIX_OTHERS_READ | wxPOSIX_OTHERS_WRITE | wxPOSIX_OTHERS_EXECUTE)
71};
246037e2 72
c801d85f
KB
73// ----------------------------------------------------------------------------
74// class wxFile: raw file IO
75//
76// NB: for space efficiency this class has no virtual functions, including
77// dtor which is _not_ virtual, so it shouldn't be used as a base class.
78// ----------------------------------------------------------------------------
886dd7d2 79
bddd7a8d 80class WXDLLIMPEXP_BASE wxFile
c801d85f
KB
81{
82public:
83 // more file constants
84 // -------------------
c801d85f 85 // opening mode
68164137 86 enum OpenMode { read, write, read_write, write_append, write_excl };
c801d85f
KB
87 // standard values for file descriptor
88 enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
c801d85f
KB
89
90 // static functions
91 // ----------------
d4a00e06 92 // check whether a regular file by this name exists
fcea31d5 93 static bool Exists(const wxString& name);
87b6002d 94 // check whether we can access the given file in given mode
d4a00e06 95 // (only read and write make sense here)
fcea31d5 96 static bool Access(const wxString& name, OpenMode mode);
c801d85f
KB
97
98 // ctors
99 // -----
100 // def ctor
47831c68 101 wxFile() { m_fd = fd_invalid; m_error = false; }
c801d85f 102 // open specified file (may fail, use IsOpened())
11aac4ba 103 wxFile(const wxString& fileName, OpenMode mode = read);
c801d85f 104 // attach to (already opened) file
222702b1 105 wxFile(int lfd) { m_fd = lfd; m_error = false; }
c801d85f
KB
106
107 // open/close
246037e2
VZ
108 // create a new file (with the default value of bOverwrite, it will fail if
109 // the file already exists, otherwise it will overwrite it and succeed)
fcea31d5 110 bool Create(const wxString& fileName, bool bOverwrite = false,
246037e2 111 int access = wxS_DEFAULT);
fcea31d5 112 bool Open(const wxString& fileName, OpenMode mode = read,
246037e2 113 int access = wxS_DEFAULT);
a1530845 114 bool Close(); // Close is a NOP if not opened
61b02744
VZ
115
116 // assign an existing file descriptor and get it back from wxFile object
222702b1 117 void Attach(int lfd) { Close(); m_fd = lfd; m_error = false; }
61b02744
VZ
118 void Detach() { m_fd = fd_invalid; }
119 int fd() const { return m_fd; }
c801d85f
KB
120
121 // read/write (unbuffered)
f8a586e0
RL
122 // returns number of bytes read or wxInvalidOffset on error
123 ssize_t Read(void *pBuf, size_t nCount);
a1b82138 124 // returns the number of bytes written
30984dea 125 size_t Write(const void *pBuf, size_t nCount);
c801d85f 126 // returns true on success
5487ff0f 127 bool Write(const wxString& s, const wxMBConv& conv = wxMBConvUTF8());
c801d85f
KB
128 // flush data not yet written
129 bool Flush();
130
30984dea
VZ
131 // file pointer operations (return wxInvalidOffset on failure)
132 // move ptr ofs bytes related to start/current offset/end of file
133 wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
c801d85f 134 // move ptr to ofs bytes before the end
30984dea
VZ
135 wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
136 // get current offset
137 wxFileOffset Tell() const;
c801d85f 138 // get current file length
30984dea 139 wxFileOffset Length() const;
c801d85f
KB
140
141 // simple accessors
142 // is file opened?
143 bool IsOpened() const { return m_fd != fd_invalid; }
144 // is end of file reached?
145 bool Eof() const;
3103e8a9 146 // has an error occurred?
79c3e0e1 147 bool Error() const { return m_error; }
3c70014d 148 // type such as disk or pipe
0912690b 149 wxFileKind GetKind() const { return wxGetFileKind(m_fd); }
246037e2 150
c801d85f 151 // dtor closes the file if opened
a1b82138 152 ~wxFile() { Close(); }
c801d85f
KB
153
154private:
155 // copy ctor and assignment operator are private because
156 // it doesn't make sense to copy files this way:
157 // attempt to do it will provoke a compile-time error.
158 wxFile(const wxFile&);
159 wxFile& operator=(const wxFile&);
160
161 int m_fd; // file descriptor or INVALID_FD if not opened
79c3e0e1 162 bool m_error; // error memory
c801d85f
KB
163};
164
165// ----------------------------------------------------------------------------
166// class wxTempFile: if you want to replace another file, create an instance
167// of wxTempFile passing the name of the file to be replaced to the ctor. Then
168// you can write to wxTempFile and call Commit() function to replace the old
169// file (and close this one) or call Discard() to cancel the modification. If
170// you call neither of them, dtor will call Discard().
171// ----------------------------------------------------------------------------
886dd7d2 172
bddd7a8d 173class WXDLLIMPEXP_BASE wxTempFile
c801d85f
KB
174{
175public:
176 // ctors
177 // default
178 wxTempFile() { }
179 // associates the temp file with the file to be replaced and opens it
180 wxTempFile(const wxString& strName);
181
182 // open the temp file (strName is the name of file to be replaced)
183 bool Open(const wxString& strName);
184
185 // is the file opened?
186 bool IsOpened() const { return m_file.IsOpened(); }
e1265174
MW
187 // get current file length
188 wxFileOffset Length() const { return m_file.Length(); }
189 // move ptr ofs bytes related to start/current offset/end of file
190 wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
191 { return m_file.Seek(ofs, mode); }
192 // get current offset
193 wxFileOffset Tell() const { return m_file.Tell(); }
c801d85f
KB
194
195 // I/O (both functions return true on success, false on failure)
30984dea 196 bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
5487ff0f 197 bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
d3c0ce34 198 { return m_file.Write(str, conv); }
c801d85f 199
f1a73c6a
VZ
200 // flush data: can be called before closing file to ensure that data was
201 // correctly written out
202 bool Flush() { return m_file.Flush(); }
203
c801d85f
KB
204 // different ways to close the file
205 // validate changes and delete the old file of name m_strName
206 bool Commit();
207 // discard changes
208 void Discard();
209
210 // dtor calls Discard() if file is still opened
211 ~wxTempFile();
212
213private:
fd3f686c
VZ
214 // no copy ctor/assignment operator
215 wxTempFile(const wxTempFile&);
216 wxTempFile& operator=(const wxTempFile&);
217
c801d85f
KB
218 wxString m_strName, // name of the file to replace in Commit()
219 m_strTemp; // temporary file name
220 wxFile m_file; // the temporary file
221};
222
a1b82138 223#endif // wxUSE_FILE
ce4169a4 224
a1b82138 225#endif // _WX_FILEH__