]> git.saurik.com Git - wxWidgets.git/blob - include/wx/file.h
set correct properties for the new files
[wxWidgets.git] / include / wx / file.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: file.h
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>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_FILEH__
14 #define _WX_FILEH__
15
16 #include "wx/defs.h"
17
18 #if wxUSE_FILE
19
20 #include "wx/string.h"
21 #include "wx/filefn.h"
22 #include "wx/strconv.h"
23
24 // ----------------------------------------------------------------------------
25 // constants
26 // ----------------------------------------------------------------------------
27
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
30 enum 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 };
72
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 // ----------------------------------------------------------------------------
79
80 class WXDLLIMPEXP_BASE wxFile
81 {
82 public:
83 // more file constants
84 // -------------------
85 // opening mode
86 enum OpenMode { read, write, read_write, write_append, write_excl };
87 // standard values for file descriptor
88 enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
89
90 // static functions
91 // ----------------
92 // check whether a regular file by this name exists
93 static bool Exists(const wxString& name);
94 // check whether we can access the given file in given mode
95 // (only read and write make sense here)
96 static bool Access(const wxString& name, OpenMode mode);
97
98 // ctors
99 // -----
100 // def ctor
101 wxFile() { m_fd = fd_invalid; m_error = false; }
102 // open specified file (may fail, use IsOpened())
103 wxFile(const wxString& fileName, OpenMode mode = read);
104 // attach to (already opened) file
105 wxFile(int lfd) { m_fd = lfd; m_error = false; }
106
107 // open/close
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)
110 bool Create(const wxString& fileName, bool bOverwrite = false,
111 int access = wxS_DEFAULT);
112 bool Open(const wxString& fileName, OpenMode mode = read,
113 int access = wxS_DEFAULT);
114 bool Close(); // Close is a NOP if not opened
115
116 // assign an existing file descriptor and get it back from wxFile object
117 void Attach(int lfd) { Close(); m_fd = lfd; m_error = false; }
118 void Detach() { m_fd = fd_invalid; }
119 int fd() const { return m_fd; }
120
121 // read/write (unbuffered)
122 // returns number of bytes read or wxInvalidOffset on error
123 ssize_t Read(void *pBuf, size_t nCount);
124 // returns the number of bytes written
125 size_t Write(const void *pBuf, size_t nCount);
126 // returns true on success
127 bool Write(const wxString& s, const wxMBConv& conv = wxMBConvUTF8());
128 // flush data not yet written
129 bool Flush();
130
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);
134 // move ptr to ofs bytes before the end
135 wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
136 // get current offset
137 wxFileOffset Tell() const;
138 // get current file length
139 wxFileOffset Length() const;
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;
146 // has an error occurred?
147 bool Error() const { return m_error; }
148 // type such as disk or pipe
149 wxFileKind GetKind() const { return wxGetFileKind(m_fd); }
150
151 // dtor closes the file if opened
152 ~wxFile() { Close(); }
153
154 private:
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
162 bool m_error; // error memory
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 // ----------------------------------------------------------------------------
172
173 class WXDLLIMPEXP_BASE wxTempFile
174 {
175 public:
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(); }
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(); }
194
195 // I/O (both functions return true on success, false on failure)
196 bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
197 bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
198 { return m_file.Write(str, conv); }
199
200 // different ways to close the file
201 // validate changes and delete the old file of name m_strName
202 bool Commit();
203 // discard changes
204 void Discard();
205
206 // dtor calls Discard() if file is still opened
207 ~wxTempFile();
208
209 private:
210 // no copy ctor/assignment operator
211 wxTempFile(const wxTempFile&);
212 wxTempFile& operator=(const wxTempFile&);
213
214 wxString m_strName, // name of the file to replace in Commit()
215 m_strTemp; // temporary file name
216 wxFile m_file; // the temporary file
217 };
218
219 #endif // wxUSE_FILE
220
221 #endif // _WX_FILEH__