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