]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: ffile.h | |
e54c96f1 | 3 | // Purpose: interface of wxFFile |
23324ae1 | 4 | // Author: wxWidgets team |
526954c5 | 5 | // Licence: wxWindows licence |
23324ae1 FM |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
1add55ae | 8 | |
23324ae1 FM |
9 | /** |
10 | @class wxFFile | |
7c913512 | 11 | |
1add55ae FM |
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). | |
7c913512 | 19 | |
23324ae1 FM |
20 | @library{wxbase} |
21 | @category{file} | |
7c913512 | 22 | |
e54c96f1 | 23 | @see wxFFile::IsOpened |
23324ae1 | 24 | */ |
7c913512 | 25 | class wxFFile |
23324ae1 FM |
26 | { |
27 | public: | |
1add55ae FM |
28 | wxFFile(); |
29 | ||
23324ae1 FM |
30 | /** |
31 | Opens a file with the given file pointer, which has already been opened. | |
3c4f71cc | 32 | |
1add55ae FM |
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 | ||
7c913512 | 44 | @param filename |
4cc4bfaf | 45 | The filename. |
7c913512 | 46 | @param mode |
4cc4bfaf FM |
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. | |
23324ae1 | 51 | */ |
7c913512 | 52 | wxFFile(const wxString& filename, const wxString& mode = "r"); |
1add55ae | 53 | |
23324ae1 FM |
54 | |
55 | /** | |
56 | Destructor will close the file. | |
1add55ae FM |
57 | |
58 | @note it is not virtual so you should @e not derive from wxFFile! | |
23324ae1 FM |
59 | */ |
60 | ~wxFFile(); | |
61 | ||
62 | /** | |
63 | Attaches an existing file pointer to the wxFFile object. | |
1add55ae FM |
64 | |
65 | The descriptor should be already opened and it will be closed by wxFFile object. | |
23324ae1 | 66 | */ |
43c48e1e | 67 | void Attach(FILE* fp, const wxString& name = wxEmptyString); |
23324ae1 FM |
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 | |
1add55ae FM |
76 | closing the file if this descriptor is opened. |
77 | ||
78 | IsOpened() will return @false after call to Detach(). | |
37961915 VZ |
79 | |
80 | @return The FILE pointer (this is new since wxWidgets 3.0.0, in the | |
81 | previous versions this method didn't return anything). | |
23324ae1 | 82 | */ |
37961915 | 83 | FILE* Detach(); |
23324ae1 FM |
84 | |
85 | /** | |
d13b34d3 | 86 | Returns @true if an attempt has been made to read @e past |
7c913512 | 87 | the end of the file. |
1add55ae FM |
88 | |
89 | Note that the behaviour of the file descriptor based class wxFile is different as | |
90 | wxFile::Eof() will return @true here as soon as the last byte of the file has been read. | |
91 | ||
23324ae1 FM |
92 | Also note that this method may only be called for opened files and may crash if |
93 | the file is not opened. | |
3c4f71cc | 94 | |
1add55ae FM |
95 | @todo THIS METHOD MAY CRASH? DOESN'T SOUND GOOD |
96 | ||
4cc4bfaf | 97 | @see IsOpened() |
23324ae1 | 98 | */ |
328f5751 | 99 | bool Eof() const; |
23324ae1 FM |
100 | |
101 | /** | |
102 | Returns @true if an error has occurred on this file, similar to the standard | |
103 | @c ferror() function. | |
1add55ae | 104 | |
23324ae1 FM |
105 | Please note that this method may only be called for opened files and may crash |
106 | if the file is not opened. | |
3c4f71cc | 107 | |
1add55ae FM |
108 | @todo THIS METHOD MAY CRASH? DOESN'T SOUND GOOD |
109 | ||
4cc4bfaf | 110 | @see IsOpened() |
23324ae1 | 111 | */ |
1add55ae | 112 | bool Error() const; |
23324ae1 FM |
113 | |
114 | /** | |
115 | Flushes the file and returns @true on success. | |
116 | */ | |
117 | bool Flush(); | |
118 | ||
119 | /** | |
1add55ae FM |
120 | Returns the type of the file. |
121 | ||
122 | @see wxFileKind | |
23324ae1 | 123 | */ |
328f5751 | 124 | wxFileKind GetKind() const; |
23324ae1 | 125 | |
6ea6639c VZ |
126 | /** |
127 | Returns the file name. | |
128 | ||
129 | This is the name that was specified when the object was constructed or | |
130 | to the last call to Open(). Notice that it may be empty if Attach() was | |
131 | called without specifying the name. | |
132 | */ | |
133 | const wxString& GetName() const; | |
134 | ||
23324ae1 | 135 | /** |
1add55ae FM |
136 | Returns @true if the file is opened. |
137 | ||
138 | Most of the methods of this class may only be used for an opened file. | |
23324ae1 | 139 | */ |
328f5751 | 140 | bool IsOpened() const; |
23324ae1 FM |
141 | |
142 | /** | |
143 | Returns the length of the file. | |
144 | */ | |
328f5751 | 145 | wxFileOffset Length() const; |
23324ae1 FM |
146 | |
147 | /** | |
148 | Opens the file, returning @true if successful. | |
3c4f71cc | 149 | |
7c913512 | 150 | @param filename |
4cc4bfaf | 151 | The filename. |
7c913512 | 152 | @param mode |
4cc4bfaf | 153 | The mode in which to open the file. |
23324ae1 FM |
154 | */ |
155 | bool Open(const wxString& filename, const wxString& mode = "r"); | |
156 | ||
157 | /** | |
1add55ae | 158 | Reads the specified number of bytes into a buffer, returning the actual number read. |
3c4f71cc | 159 | |
7c913512 | 160 | @param buffer |
4cc4bfaf | 161 | A buffer to receive the data. |
7c913512 | 162 | @param count |
4cc4bfaf | 163 | The number of bytes to read. |
3c4f71cc | 164 | |
d29a9a8a | 165 | @return The number of bytes read. |
23324ae1 FM |
166 | */ |
167 | size_t Read(void* buffer, size_t count); | |
168 | ||
169 | /** | |
23324ae1 | 170 | Reads the entire contents of the file into a string. |
3c4f71cc | 171 | |
7c913512 | 172 | @param str |
4cc4bfaf | 173 | String to read data into. |
7c913512 | 174 | @param conv |
4cc4bfaf FM |
175 | Conversion object to use in Unicode build; by default supposes |
176 | that file contents is encoded in UTF-8. | |
3c4f71cc | 177 | |
d29a9a8a | 178 | @return @true if file was read successfully, @false otherwise. |
23324ae1 | 179 | */ |
1add55ae | 180 | bool ReadAll(wxString* str, const wxMBConv& conv = wxConvAuto()); |
23324ae1 FM |
181 | |
182 | /** | |
183 | Seeks to the specified position and returns @true on success. | |
3c4f71cc | 184 | |
7c913512 | 185 | @param ofs |
4cc4bfaf | 186 | Offset to seek to. |
7c913512 | 187 | @param mode |
4cc4bfaf | 188 | One of wxFromStart, wxFromEnd, wxFromCurrent. |
23324ae1 FM |
189 | */ |
190 | bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart); | |
191 | ||
192 | /** | |
193 | Moves the file pointer to the specified number of bytes before the end of the | |
1add55ae | 194 | file and returns @true on success. |
3c4f71cc | 195 | |
7c913512 | 196 | @param ofs |
4cc4bfaf | 197 | Number of bytes before the end of the file. |
23324ae1 FM |
198 | */ |
199 | bool SeekEnd(wxFileOffset ofs = 0); | |
200 | ||
201 | /** | |
202 | Returns the current position. | |
203 | */ | |
328f5751 | 204 | wxFileOffset Tell() const; |
23324ae1 FM |
205 | |
206 | /** | |
23324ae1 | 207 | Writes the contents of the string to the file, returns @true on success. |
1add55ae | 208 | |
23324ae1 | 209 | The second argument is only meaningful in Unicode build of wxWidgets when |
1add55ae FM |
210 | @a conv is used to convert @a str to multibyte representation. |
211 | */ | |
212 | bool Write(const wxString& str, const wxMBConv& conv = wxConvAuto()); | |
213 | ||
214 | /** | |
215 | Writes the specified number of bytes from a buffer. | |
216 | ||
217 | @param buffer | |
218 | A buffer containing the data. | |
219 | @param count | |
220 | The number of bytes to write. | |
221 | ||
d29a9a8a | 222 | @return The number of bytes written. |
23324ae1 | 223 | */ |
1add55ae | 224 | size_t Write(const void* buffer, size_t count); |
23324ae1 FM |
225 | |
226 | /** | |
227 | Returns the file pointer associated with the file. | |
228 | */ | |
328f5751 | 229 | FILE* fp() const; |
23324ae1 | 230 | }; |
e54c96f1 | 231 |