Test for encoding conversion success in wxFile::Write to avoid crash
[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 #define wxS_IRUSR 00400
31 #define wxS_IWUSR 00200
32 #define wxS_IXUSR 00100
33
34 #define wxS_IRGRP 00040
35 #define wxS_IWGRP 00020
36 #define wxS_IXGRP 00010
37
38 #define wxS_IROTH 00004
39 #define wxS_IWOTH 00002
40 #define wxS_IXOTH 00001
41
42 // default mode for the new files: corresponds to umask 022
43 #define wxS_DEFAULT (wxS_IRUSR | wxS_IWUSR | wxS_IRGRP | wxS_IWGRP |\
44 wxS_IROTH | wxS_IWOTH)
45
46 // ----------------------------------------------------------------------------
47 // class wxFile: raw file IO
48 //
49 // NB: for space efficiency this class has no virtual functions, including
50 // dtor which is _not_ virtual, so it shouldn't be used as a base class.
51 // ----------------------------------------------------------------------------
52
53 class WXDLLIMPEXP_BASE wxFile
54 {
55 public:
56 // more file constants
57 // -------------------
58 // opening mode
59 enum OpenMode { read, write, read_write, write_append, write_excl };
60 // standard values for file descriptor
61 enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
62
63 // static functions
64 // ----------------
65 // check whether a regular file by this name exists
66 static bool Exists(const wxChar *name);
67 // check whether we can access the given file in given mode
68 // (only read and write make sense here)
69 static bool Access(const wxChar *name, OpenMode mode);
70
71 // ctors
72 // -----
73 // def ctor
74 wxFile() { m_fd = fd_invalid; m_error = false; }
75 // open specified file (may fail, use IsOpened())
76 wxFile(const wxChar *szFileName, OpenMode mode = read);
77 // attach to (already opened) file
78 wxFile(int lfd) { m_fd = lfd; m_error = false; }
79
80 // open/close
81 // create a new file (with the default value of bOverwrite, it will fail if
82 // the file already exists, otherwise it will overwrite it and succeed)
83 bool Create(const wxChar *szFileName, bool bOverwrite = false,
84 int access = wxS_DEFAULT);
85 bool Open(const wxChar *szFileName, OpenMode mode = read,
86 int access = wxS_DEFAULT);
87 bool Close(); // Close is a NOP if not opened
88
89 // assign an existing file descriptor and get it back from wxFile object
90 void Attach(int lfd) { Close(); m_fd = lfd; m_error = false; }
91 void Detach() { m_fd = fd_invalid; }
92 int fd() const { return m_fd; }
93
94 // read/write (unbuffered)
95 // returns number of bytes read or wxInvalidOffset on error
96 ssize_t Read(void *pBuf, size_t nCount);
97 // returns the number of bytes written
98 size_t Write(const void *pBuf, size_t nCount);
99 // returns true on success
100 bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8)
101 {
102 const wxWX2MBbuf buf = s.mb_str(conv);
103 if (buf)
104 {
105 size_t size = strlen(buf);
106 return Write((const char *) buf, size) == size;
107 }
108 else
109 return false;
110 }
111 // flush data not yet written
112 bool Flush();
113
114 // file pointer operations (return wxInvalidOffset on failure)
115 // move ptr ofs bytes related to start/current offset/end of file
116 wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
117 // move ptr to ofs bytes before the end
118 wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
119 // get current offset
120 wxFileOffset Tell() const;
121 // get current file length
122 wxFileOffset Length() const;
123
124 // simple accessors
125 // is file opened?
126 bool IsOpened() const { return m_fd != fd_invalid; }
127 // is end of file reached?
128 bool Eof() const;
129 // has an error occurred?
130 bool Error() const { return m_error; }
131 // type such as disk or pipe
132 wxFileKind GetKind() const { return wxGetFileKind(m_fd); }
133
134 // dtor closes the file if opened
135 ~wxFile() { Close(); }
136
137 private:
138 // copy ctor and assignment operator are private because
139 // it doesn't make sense to copy files this way:
140 // attempt to do it will provoke a compile-time error.
141 wxFile(const wxFile&);
142 wxFile& operator=(const wxFile&);
143
144 int m_fd; // file descriptor or INVALID_FD if not opened
145 bool m_error; // error memory
146 };
147
148 // ----------------------------------------------------------------------------
149 // class wxTempFile: if you want to replace another file, create an instance
150 // of wxTempFile passing the name of the file to be replaced to the ctor. Then
151 // you can write to wxTempFile and call Commit() function to replace the old
152 // file (and close this one) or call Discard() to cancel the modification. If
153 // you call neither of them, dtor will call Discard().
154 // ----------------------------------------------------------------------------
155
156 class WXDLLIMPEXP_BASE wxTempFile
157 {
158 public:
159 // ctors
160 // default
161 wxTempFile() { }
162 // associates the temp file with the file to be replaced and opens it
163 wxTempFile(const wxString& strName);
164
165 // open the temp file (strName is the name of file to be replaced)
166 bool Open(const wxString& strName);
167
168 // is the file opened?
169 bool IsOpened() const { return m_file.IsOpened(); }
170 // get current file length
171 wxFileOffset Length() const { return m_file.Length(); }
172 // move ptr ofs bytes related to start/current offset/end of file
173 wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
174 { return m_file.Seek(ofs, mode); }
175 // get current offset
176 wxFileOffset Tell() const { return m_file.Tell(); }
177
178 // I/O (both functions return true on success, false on failure)
179 bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
180 bool Write(const wxString& str, const wxMBConv& conv = wxConvUTF8)
181 { return m_file.Write(str, conv); }
182
183 // different ways to close the file
184 // validate changes and delete the old file of name m_strName
185 bool Commit();
186 // discard changes
187 void Discard();
188
189 // dtor calls Discard() if file is still opened
190 ~wxTempFile();
191
192 private:
193 // no copy ctor/assignment operator
194 wxTempFile(const wxTempFile&);
195 wxTempFile& operator=(const wxTempFile&);
196
197 wxString m_strName, // name of the file to replace in Commit()
198 m_strTemp; // temporary file name
199 wxFile m_file; // the temporary file
200 };
201
202 #endif // wxUSE_FILE
203
204 #endif // _WX_FILEH__