]> git.saurik.com Git - wxWidgets.git/blob - include/wx/file.h
add support for GtkFontChooserDialog, new in GTK 3.2
[wxWidgets.git] / include / wx / file.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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/convauto.h"
23
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 // ----------------------------------------------------------------------------
30
31 class WXDLLIMPEXP_BASE wxFile
32 {
33 public:
34 // more file constants
35 // -------------------
36 // opening mode
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 };
40
41 // static functions
42 // ----------------
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);
48
49 // ctors
50 // -----
51 // def ctor
52 wxFile() { m_fd = fd_invalid; m_lasterror = 0; }
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_lasterror = 0; }
57
58 // open/close
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
66
67 // assign an existing file descriptor and get it back from wxFile object
68 void Attach(int lfd) { Close(); m_fd = lfd; m_lasterror = 0; }
69 void Detach() { m_fd = fd_invalid; }
70 int fd() const { return m_fd; }
71
72 // read/write (unbuffered)
73 // read all data from the file into a string (useful for text files)
74 bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
75 // returns number of bytes read or wxInvalidOffset on error
76 ssize_t Read(void *pBuf, size_t nCount);
77 // returns the number of bytes written
78 size_t Write(const void *pBuf, size_t nCount);
79 // returns true on success
80 bool Write(const wxString& s, const wxMBConv& conv = wxMBConvUTF8());
81 // flush data not yet written
82 bool Flush();
83
84 // file pointer operations (return wxInvalidOffset on failure)
85 // move ptr ofs bytes related to start/current offset/end of file
86 wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
87 // move ptr to ofs bytes before the end
88 wxFileOffset SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
89 // get current offset
90 wxFileOffset Tell() const;
91 // get current file length
92 wxFileOffset Length() const;
93
94 // simple accessors
95 // is file opened?
96 bool IsOpened() const { return m_fd != fd_invalid; }
97 // is end of file reached?
98 bool Eof() const;
99 // has an error occurred?
100 bool Error() const { return m_lasterror != 0; }
101 // get last errno
102 int GetLastError() const { return m_lasterror; }
103 // reset error state
104 void ClearLastError() { m_lasterror = 0; }
105 // type such as disk or pipe
106 wxFileKind GetKind() const { return wxGetFileKind(m_fd); }
107
108
109 // dtor closes the file if opened
110 ~wxFile() { Close(); }
111
112 private:
113 // copy ctor and assignment operator are private because
114 // it doesn't make sense to copy files this way:
115 // attempt to do it will provoke a compile-time error.
116 wxFile(const wxFile&);
117 wxFile& operator=(const wxFile&);
118
119 // Copy the value of errno into m_lasterror if rc == -1 and return true in
120 // this case (indicating that we've got an error). Otherwise return false.
121 //
122 // Notice that we use the possibly 64 bit wxFileOffset instead of int here so
123 // that it works for checking the result of functions such as tell() too.
124 bool CheckForError(wxFileOffset rc) const;
125
126
127 int m_fd; // file descriptor or INVALID_FD if not opened
128 int m_lasterror; // errno value of last error
129 };
130
131 // ----------------------------------------------------------------------------
132 // class wxTempFile: if you want to replace another file, create an instance
133 // of wxTempFile passing the name of the file to be replaced to the ctor. Then
134 // you can write to wxTempFile and call Commit() function to replace the old
135 // file (and close this one) or call Discard() to cancel the modification. If
136 // you call neither of them, dtor will call Discard().
137 // ----------------------------------------------------------------------------
138
139 class WXDLLIMPEXP_BASE wxTempFile
140 {
141 public:
142 // ctors
143 // default
144 wxTempFile() { }
145 // associates the temp file with the file to be replaced and opens it
146 wxTempFile(const wxString& strName);
147
148 // open the temp file (strName is the name of file to be replaced)
149 bool Open(const wxString& strName);
150
151 // is the file opened?
152 bool IsOpened() const { return m_file.IsOpened(); }
153 // get current file length
154 wxFileOffset Length() const { return m_file.Length(); }
155 // move ptr ofs bytes related to start/current offset/end of file
156 wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
157 { return m_file.Seek(ofs, mode); }
158 // get current offset
159 wxFileOffset Tell() const { return m_file.Tell(); }
160
161 // I/O (both functions return true on success, false on failure)
162 bool Write(const void *p, size_t n) { return m_file.Write(p, n) == n; }
163 bool Write(const wxString& str, const wxMBConv& conv = wxMBConvUTF8())
164 { return m_file.Write(str, conv); }
165
166 // flush data: can be called before closing file to ensure that data was
167 // correctly written out
168 bool Flush() { return m_file.Flush(); }
169
170 // different ways to close the file
171 // validate changes and delete the old file of name m_strName
172 bool Commit();
173 // discard changes
174 void Discard();
175
176 // dtor calls Discard() if file is still opened
177 ~wxTempFile();
178
179 private:
180 // no copy ctor/assignment operator
181 wxTempFile(const wxTempFile&);
182 wxTempFile& operator=(const wxTempFile&);
183
184 wxString m_strName, // name of the file to replace in Commit()
185 m_strTemp; // temporary file name
186 wxFile m_file; // the temporary file
187 };
188
189 #endif // wxUSE_FILE
190
191 #endif // _WX_FILEH__