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