]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/ffile.h
5671e2e53109a23042ce950347e0f56ff71fe7b6
[wxWidgets.git] / interface / wx / ffile.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ffile.h
3 // Purpose: interface of wxFFile
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
7
8
9 /**
10 @class wxFFile
11
12 wxFFile implements buffered file I/O.
13
14 This is a very small class designed to minimize the overhead of using it - in fact,
15 there is hardly any overhead at all, but using it brings you automatic error checking
16 and hides differences between platforms and compilers.
17
18 It wraps inside it a @c FILE * handle used by standard C IO library (also known as @c stdio).
19
20 @library{wxbase}
21 @category{file}
22
23 @see wxFFile::IsOpened
24 */
25 class wxFFile
26 {
27 public:
28 wxFFile();
29
30 /**
31 Opens a file with the given file pointer, which has already been opened.
32
33 @param fp
34 An existing file descriptor, such as stderr.
35 */
36 wxFFile(FILE* fp);
37
38 /**
39 Opens a file with the given mode.
40 As there is no way to return whether the operation was successful or not from
41 the constructor you should test the return value of IsOpened() to check that it
42 didn't fail.
43
44 @param filename
45 The filename.
46 @param mode
47 The mode in which to open the file using standard C strings.
48 Note that you should use "b" flag if you use binary files under Windows
49 or the results might be unexpected due to automatic newline conversion done
50 for the text files.
51 */
52 wxFFile(const wxString& filename, const wxString& mode = "r");
53
54
55 /**
56 Destructor will close the file.
57
58 @note it is not virtual so you should @e not derive from wxFFile!
59 */
60 ~wxFFile();
61
62 /**
63 Attaches an existing file pointer to the wxFFile object.
64
65 The descriptor should be already opened and it will be closed by wxFFile object.
66 */
67 void Attach(FILE* fp, const wxString& name = wxEmptyString);
68
69 /**
70 Closes the file and returns @true on success.
71 */
72 bool Close();
73
74 /**
75 Get back a file pointer from wxFFile object -- the caller is responsible for
76 closing the file if this descriptor is opened.
77
78 IsOpened() will return @false after call to Detach().
79 */
80 void Detach();
81
82 /**
83 Returns @true if an attempt has been made to read @e past
84 the end of the file.
85
86 Note that the behaviour of the file descriptor based class wxFile is different as
87 wxFile::Eof() will return @true here as soon as the last byte of the file has been read.
88
89 Also note that this method may only be called for opened files and may crash if
90 the file is not opened.
91
92 @todo THIS METHOD MAY CRASH? DOESN'T SOUND GOOD
93
94 @see IsOpened()
95 */
96 bool Eof() const;
97
98 /**
99 Returns @true if an error has occurred on this file, similar to the standard
100 @c ferror() function.
101
102 Please note that this method may only be called for opened files and may crash
103 if the file is not opened.
104
105 @todo THIS METHOD MAY CRASH? DOESN'T SOUND GOOD
106
107 @see IsOpened()
108 */
109 bool Error() const;
110
111 /**
112 Flushes the file and returns @true on success.
113 */
114 bool Flush();
115
116 /**
117 Returns the type of the file.
118
119 @see wxFileKind
120 */
121 wxFileKind GetKind() const;
122
123 /**
124 Returns the file name.
125
126 This is the name that was specified when the object was constructed or
127 to the last call to Open(). Notice that it may be empty if Attach() was
128 called without specifying the name.
129 */
130 const wxString& GetName() const;
131
132 /**
133 Returns @true if the file is opened.
134
135 Most of the methods of this class may only be used for an opened file.
136 */
137 bool IsOpened() const;
138
139 /**
140 Returns the length of the file.
141 */
142 wxFileOffset Length() const;
143
144 /**
145 Opens the file, returning @true if successful.
146
147 @param filename
148 The filename.
149 @param mode
150 The mode in which to open the file.
151 */
152 bool Open(const wxString& filename, const wxString& mode = "r");
153
154 /**
155 Reads the specified number of bytes into a buffer, returning the actual number read.
156
157 @param buffer
158 A buffer to receive the data.
159 @param count
160 The number of bytes to read.
161
162 @return The number of bytes read.
163 */
164 size_t Read(void* buffer, size_t count);
165
166 /**
167 Reads the entire contents of the file into a string.
168
169 @param str
170 String to read data into.
171 @param conv
172 Conversion object to use in Unicode build; by default supposes
173 that file contents is encoded in UTF-8.
174
175 @return @true if file was read successfully, @false otherwise.
176 */
177 bool ReadAll(wxString* str, const wxMBConv& conv = wxConvAuto());
178
179 /**
180 Seeks to the specified position and returns @true on success.
181
182 @param ofs
183 Offset to seek to.
184 @param mode
185 One of wxFromStart, wxFromEnd, wxFromCurrent.
186 */
187 bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
188
189 /**
190 Moves the file pointer to the specified number of bytes before the end of the
191 file and returns @true on success.
192
193 @param ofs
194 Number of bytes before the end of the file.
195 */
196 bool SeekEnd(wxFileOffset ofs = 0);
197
198 /**
199 Returns the current position.
200 */
201 wxFileOffset Tell() const;
202
203 /**
204 Writes the contents of the string to the file, returns @true on success.
205
206 The second argument is only meaningful in Unicode build of wxWidgets when
207 @a conv is used to convert @a str to multibyte representation.
208 */
209 bool Write(const wxString& str, const wxMBConv& conv = wxConvAuto());
210
211 /**
212 Writes the specified number of bytes from a buffer.
213
214 @param buffer
215 A buffer containing the data.
216 @param count
217 The number of bytes to write.
218
219 @return The number of bytes written.
220 */
221 size_t Write(const void* buffer, size_t count);
222
223 /**
224 Returns the file pointer associated with the file.
225 */
226 FILE* fp() const;
227 };
228