]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: filesys.h | |
ea6a2ccb | 3 | // Purpose: interface of wxFileSystem, wxFileSystemHandler, wxFSFile |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
ea6a2ccb FM |
9 | |
10 | /** | |
11 | Open Bit Flags | |
12 | */ | |
13 | enum wxFileSystemOpenFlags | |
14 | { | |
15 | wxFS_READ = 1, /**< Open for reading */ | |
16 | wxFS_SEEKABLE = 4 /**< Returned stream will be seekable */ | |
17 | }; | |
18 | ||
19 | ||
23324ae1 FM |
20 | /** |
21 | @class wxFileSystem | |
7c913512 | 22 | |
ea6a2ccb FM |
23 | This class provides an interface for opening files on different file systems. |
24 | It can handle absolute and/or local filenames. | |
25 | ||
26 | It uses a system of handlers (see wxFileSystemHandler) to provide access to | |
27 | user-defined virtual file systems. | |
7c913512 | 28 | |
23324ae1 FM |
29 | @library{wxbase} |
30 | @category{vfs} | |
7c913512 | 31 | |
ea6a2ccb | 32 | @see wxFileSystemHandler, wxFSFile, @ref overview_fs |
23324ae1 FM |
33 | */ |
34 | class wxFileSystem : public wxObject | |
35 | { | |
36 | public: | |
37 | /** | |
38 | Constructor. | |
39 | */ | |
40 | wxFileSystem(); | |
41 | ||
42 | /** | |
ea6a2ccb FM |
43 | This static function adds new handler into the list of handlers |
44 | (see wxFileSystemHandler) which provide access to virtual FS. | |
45 | ||
46 | Note that if two handlers for the same protocol are added, the last | |
47 | added one takes precedence. | |
48 | ||
49 | @note You can call: | |
50 | @code | |
51 | wxFileSystem::AddHandler(new My_FS_Handler); | |
52 | @endcode | |
53 | This is because (a) AddHandler is a static method, and (b) the | |
54 | handlers are deleted in wxFileSystem's destructor so that you | |
55 | don't have to care about it. | |
23324ae1 FM |
56 | */ |
57 | static void AddHandler(wxFileSystemHandler handler); | |
58 | ||
59 | /** | |
ea6a2ccb FM |
60 | Sets the current location. @a location parameter passed to OpenFile() is |
61 | relative to this path. | |
62 | ||
63 | @remarks Unless @a is_dir is @true the @a location parameter is not the | |
64 | directory name but the name of the file in this directory. | |
65 | ||
66 | All these commands change the path to "dir/subdir/": | |
67 | ||
68 | @code | |
69 | ChangePathTo("dir/subdir/xh.htm"); | |
70 | ChangePathTo("dir/subdir", true); | |
71 | ChangePathTo("dir/subdir/", true); | |
72 | @endcode | |
73 | ||
74 | Example: | |
75 | @code | |
76 | f = fs->OpenFile("hello.htm"); // opens file 'hello.htm' | |
77 | fs->ChangePathTo("subdir/folder", true); | |
78 | f = fs->OpenFile("hello.htm"); // opens file 'subdir/folder/hello.htm' !! | |
79 | @endcode | |
3c4f71cc | 80 | |
7c913512 | 81 | @param location |
4cc4bfaf | 82 | the new location. Its meaning depends on the value of is_dir |
7c913512 | 83 | @param is_dir |
ea6a2ccb FM |
84 | if @true location is new directory. |
85 | If @false (the default) location is file in the new directory. | |
23324ae1 | 86 | */ |
4cc4bfaf | 87 | void ChangePathTo(const wxString& location, bool is_dir = false); |
23324ae1 FM |
88 | |
89 | /** | |
ea6a2ccb | 90 | Converts a wxFileName into an URL. |
3c4f71cc | 91 | |
4cc4bfaf | 92 | @see URLToFileName(), wxFileName |
23324ae1 | 93 | */ |
ea6a2ccb | 94 | static wxString FileNameToURL(const wxFileName& filename); |
23324ae1 FM |
95 | |
96 | /** | |
4cc4bfaf | 97 | Looks for the file with the given name @a file in a colon or semi-colon |
ea6a2ccb FM |
98 | (depending on the current platform) separated list of directories in @a path. |
99 | ||
100 | If the file is found in any directory, returns @true and the full path | |
101 | of the file in @a str, otherwise returns @false and doesn't modify @a str. | |
3c4f71cc | 102 | |
f21dd16b | 103 | @param pStr |
4cc4bfaf | 104 | Receives the full path of the file, must not be @NULL |
7c913512 | 105 | @param path |
4cc4bfaf | 106 | wxPATH_SEP-separated list of directories |
7c913512 | 107 | @param file |
4cc4bfaf | 108 | the name of the file to look for |
23324ae1 | 109 | */ |
43c48e1e | 110 | bool FindFileInPath(wxString* pStr, const wxString& path, |
23324ae1 FM |
111 | const wxString& file); |
112 | ||
113 | /** | |
ea6a2ccb FM |
114 | Works like ::wxFindFirstFile(). |
115 | ||
116 | Returns the name of the first filename (within filesystem's current path) | |
117 | that matches @a wildcard. | |
118 | ||
119 | @param wildcard | |
120 | The wildcard that the filename must match | |
121 | @param flags | |
122 | One of wxFILE (only files), wxDIR (only directories) or 0 (both). | |
23324ae1 FM |
123 | */ |
124 | wxString FindFirst(const wxString& wildcard, int flags = 0); | |
125 | ||
126 | /** | |
ea6a2ccb | 127 | Returns the next filename that matches the parameters passed to FindFirst(). |
23324ae1 FM |
128 | */ |
129 | wxString FindNext(); | |
130 | ||
131 | /** | |
ea6a2ccb | 132 | Returns the actual path (set by wxFileSystem::ChangePathTo). |
23324ae1 | 133 | */ |
adaaa686 | 134 | wxString GetPath() const; |
23324ae1 FM |
135 | |
136 | /** | |
137 | This static function returns @true if there is a registered handler which can | |
ea6a2ccb | 138 | open the given location. |
23324ae1 | 139 | */ |
4cc4bfaf | 140 | static bool HasHandlerForPath(const wxString& location); |
23324ae1 FM |
141 | |
142 | /** | |
ea6a2ccb FM |
143 | Opens the file and returns a pointer to a wxFSFile object or @NULL if failed. |
144 | ||
145 | It first tries to open the file in relative scope (based on value passed to | |
146 | ChangePathTo() method) and then as an absolute path. | |
147 | ||
148 | Note that the user is responsible for deleting the returned wxFSFile. | |
149 | @a flags can be one or more of the ::wxFileSystemOpenFlags values | |
150 | combined together. | |
3c4f71cc | 151 | |
23324ae1 FM |
152 | A stream opened with just the default @e wxFS_READ flag may |
153 | or may not be seekable depending on the underlying source. | |
ea6a2ccb FM |
154 | |
155 | Passing @e "wxFS_READ | wxFS_SEEKABLE" for @a flags will back | |
156 | a stream that is not natively seekable with memory or a file | |
23324ae1 FM |
157 | and return a stream that is always seekable. |
158 | */ | |
159 | wxFSFile* OpenFile(const wxString& location, | |
160 | int flags = wxFS_READ); | |
161 | ||
162 | /** | |
ea6a2ccb FM |
163 | Converts URL into a well-formed filename. |
164 | The URL must use the @c file protocol. | |
23324ae1 FM |
165 | */ |
166 | static wxFileName URLToFileName(const wxString& url); | |
167 | }; | |
168 | ||
169 | ||
e54c96f1 | 170 | |
23324ae1 FM |
171 | /** |
172 | @class wxFSFile | |
7c913512 | 173 | |
23324ae1 | 174 | This class represents a single file opened by wxFileSystem. |
7c913512 | 175 | It provides more information than wxWindow's input stream |
23324ae1 | 176 | (stream, filename, mime type, anchor). |
7c913512 | 177 | |
ea6a2ccb FM |
178 | @note Any pointer returned by a method of wxFSFile is valid only as long as |
179 | the wxFSFile object exists. For example a call to GetStream() | |
180 | doesn't @e create the stream but only returns the pointer to it. | |
181 | In other words after 10 calls to GetStream() you will have obtained | |
182 | ten identical pointers. | |
7c913512 | 183 | |
23324ae1 FM |
184 | @library{wxbase} |
185 | @category{vfs} | |
7c913512 | 186 | |
ea6a2ccb | 187 | @see wxFileSystemHandler, wxFileSystem, @ref overview_fs |
23324ae1 FM |
188 | */ |
189 | class wxFSFile : public wxObject | |
190 | { | |
191 | public: | |
192 | /** | |
ea6a2ccb FM |
193 | Constructor. You probably won't use it. See the Note for details. |
194 | ||
195 | It is seldom used by the application programmer but you will need it if | |
196 | you are writing your own virtual FS. For example you may need something | |
197 | similar to wxMemoryInputStream, but because wxMemoryInputStream doesn't | |
198 | free the memory when destroyed and thus passing a memory stream pointer | |
199 | into wxFSFile constructor would lead to memory leaks, you can write your | |
200 | own class derived from wxFSFile: | |
201 | ||
202 | @code | |
203 | class wxMyFSFile : public wxFSFile | |
204 | { | |
205 | private: | |
206 | void *m_Mem; | |
207 | public: | |
208 | wxMyFSFile(.....) | |
209 | ~wxMyFSFile() {free(m_Mem);} | |
210 | // of course dtor is virtual ;-) | |
211 | }; | |
212 | @endcode | |
213 | ||
214 | If you are not sure of the meaning of these params, see the description | |
215 | of the GetXXXX() functions. | |
3c4f71cc | 216 | |
7c913512 | 217 | @param stream |
4cc4bfaf | 218 | The input stream that will be used to access data |
7c913512 | 219 | @param location |
4cc4bfaf | 220 | The full location (aka filename) of the file |
7c913512 | 221 | @param mimetype |
4cc4bfaf FM |
222 | MIME type of this file. It may be left empty, in which |
223 | case the type will be determined from file's extension (location must | |
224 | not be empty in this case). | |
7c913512 | 225 | @param anchor |
4cc4bfaf | 226 | Anchor. See GetAnchor() for details. |
76e9224e FM |
227 | @param modif |
228 | Modification date and time for this file. | |
23324ae1 | 229 | */ |
8067ee11 FM |
230 | wxFSFile(wxInputStream* stream, const wxString& location, |
231 | const wxString& mimetype, const wxString& anchor, | |
76e9224e | 232 | wxDateTime modif); |
23324ae1 FM |
233 | |
234 | /** | |
235 | Detaches the stream from the wxFSFile object. That is, the | |
ea6a2ccb FM |
236 | stream obtained with GetStream() will continue its existance |
237 | after the wxFSFile object is deleted. | |
238 | ||
239 | You will have to delete the stream yourself. | |
23324ae1 | 240 | */ |
43c48e1e | 241 | wxInputStream* DetachStream(); |
23324ae1 FM |
242 | |
243 | /** | |
244 | Returns anchor (if present). The term of @b anchor can be easily | |
245 | explained using few examples: | |
3c4f71cc | 246 | |
ea6a2ccb FM |
247 | @verbatim |
248 | index.htm#anchor // 'anchor' is anchor | |
249 | index/wx001.htm // NO anchor here! | |
250 | archive/main.zip#zip:index.htm#global // 'global' | |
251 | archive/main.zip#zip:index.htm // NO anchor here! | |
252 | @endverbatim | |
253 | ||
23324ae1 | 254 | Usually an anchor is presented only if the MIME type is 'text/html'. |
ea6a2ccb FM |
255 | But it may have some meaning with other files; for example myanim.avi#200 |
256 | may refer to position in animation or reality.wrl#MyView may refer | |
257 | to a predefined view in VRML. | |
23324ae1 | 258 | */ |
ea6a2ccb | 259 | const wxString& GetAnchor() const; |
23324ae1 FM |
260 | |
261 | /** | |
7c913512 | 262 | Returns full location of the file, including path and protocol. |
ea6a2ccb FM |
263 | |
264 | Examples: | |
265 | @verbatim | |
266 | http://www.wxwidgets.org | |
267 | http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/archive.zip#zip:info.txt | |
268 | file:/home/vasek/index.htm | |
269 | relative-file.htm | |
270 | @endverbatim | |
23324ae1 | 271 | */ |
ea6a2ccb | 272 | const wxString& GetLocation() const; |
23324ae1 FM |
273 | |
274 | /** | |
ea6a2ccb FM |
275 | Returns the MIME type of the content of this file. |
276 | ||
277 | It is either extension-based (see wxMimeTypesManager) or extracted from | |
23324ae1 FM |
278 | HTTP protocol Content-Type header. |
279 | */ | |
ea6a2ccb | 280 | const wxString& GetMimeType() const; |
23324ae1 FM |
281 | |
282 | /** | |
283 | Returns time when this file was modified. | |
284 | */ | |
328f5751 | 285 | wxDateTime GetModificationTime() const; |
23324ae1 FM |
286 | |
287 | /** | |
ea6a2ccb FM |
288 | Returns pointer to the stream. |
289 | ||
290 | You can use the returned stream to directly access data. | |
291 | You may suppose that the stream provide Seek and GetSize functionality | |
23324ae1 FM |
292 | (even in the case of the HTTP protocol which doesn't provide |
293 | this by default. wxHtml uses local cache to work around | |
294 | this and to speed up the connection). | |
295 | */ | |
328f5751 | 296 | wxInputStream* GetStream() const; |
23324ae1 FM |
297 | }; |
298 | ||
299 | ||
e54c96f1 | 300 | |
23324ae1 FM |
301 | /** |
302 | @class wxFileSystemHandler | |
7c913512 | 303 | |
ea6a2ccb FM |
304 | Classes derived from wxFileSystemHandler are used to access virtual file systems. |
305 | ||
306 | Its public interface consists of two methods: wxFileSystemHandler::CanOpen | |
7c913512 | 307 | and wxFileSystemHandler::OpenFile. |
ea6a2ccb | 308 | |
23324ae1 | 309 | It provides additional protected methods to simplify the process |
ea6a2ccb FM |
310 | of opening the file: GetProtocol(), GetLeftLocation(), GetRightLocation(), |
311 | GetAnchor(), GetMimeTypeFromExt(). | |
7c913512 | 312 | |
ea6a2ccb | 313 | Please have a look at overview (see wxFileSystem) if you don't know how locations |
23324ae1 | 314 | are constructed. |
7c913512 | 315 | |
ea6a2ccb FM |
316 | Also consult the @ref overview_fs_wxhtmlfs "list of available handlers". |
317 | ||
318 | Note that the handlers are shared by all instances of wxFileSystem. | |
319 | ||
320 | @remarks | |
321 | wxHTML library provides handlers for local files and HTTP or FTP protocol. | |
7c913512 | 322 | |
ea6a2ccb FM |
323 | @note |
324 | The location parameter passed to OpenFile() or CanOpen() methods is always an | |
325 | absolute path. You don't need to check the FS's current path. | |
326 | ||
327 | @beginWxPerlOnly | |
328 | In wxPerl, you need to derive your file system handler class | |
23324ae1 | 329 | from Wx::PlFileSystemHandler. |
ea6a2ccb | 330 | @endWxPerlOnly |
7c913512 | 331 | |
23324ae1 FM |
332 | @library{wxbase} |
333 | @category{vfs} | |
7c913512 | 334 | |
ea6a2ccb | 335 | @see wxFileSystem, wxFSFile, @ref overview_fs |
23324ae1 FM |
336 | */ |
337 | class wxFileSystemHandler : public wxObject | |
338 | { | |
339 | public: | |
340 | /** | |
341 | Constructor. | |
342 | */ | |
343 | wxFileSystemHandler(); | |
344 | ||
345 | /** | |
346 | Returns @true if the handler is able to open this file. This function doesn't | |
347 | check whether the file exists or not, it only checks if it knows the protocol. | |
348 | Example: | |
3c4f71cc | 349 | |
ea6a2ccb FM |
350 | @code |
351 | bool MyHand::CanOpen(const wxString& location) | |
352 | { | |
353 | return (GetProtocol(location) == "http"); | |
354 | } | |
355 | @endcode | |
356 | ||
23324ae1 FM |
357 | Must be overridden in derived handlers. |
358 | */ | |
da1ed74c | 359 | virtual bool CanOpen(const wxString& location) = 0; |
23324ae1 FM |
360 | |
361 | /** | |
ea6a2ccb FM |
362 | Works like ::wxFindFirstFile(). |
363 | ||
364 | Returns the name of the first filename (within filesystem's current path) | |
365 | that matches @e wildcard. @a flags may be one of wxFILE (only files), | |
366 | wxDIR (only directories) or 0 (both). | |
367 | ||
23324ae1 FM |
368 | This method is only called if CanOpen() returns @true. |
369 | */ | |
370 | virtual wxString FindFirst(const wxString& wildcard, | |
371 | int flags = 0); | |
372 | ||
373 | /** | |
374 | Returns next filename that matches parameters passed to wxFileSystem::FindFirst. | |
ea6a2ccb FM |
375 | |
376 | This method is only called if CanOpen() returns @true and FindFirst() | |
23324ae1 FM |
377 | returned a non-empty string. |
378 | */ | |
379 | virtual wxString FindNext(); | |
380 | ||
381 | /** | |
382 | Returns the anchor if present in the location. | |
ea6a2ccb FM |
383 | See wxFSFile::GetAnchor for details. |
384 | ||
385 | Example: | |
386 | @code | |
387 | GetAnchor("index.htm#chapter2") == "chapter2" | |
388 | @endcode | |
389 | ||
cdbcf4c2 | 390 | @note the anchor is NOT part of the left location. |
23324ae1 | 391 | */ |
0004982c | 392 | static wxString GetAnchor(const wxString& location); |
23324ae1 FM |
393 | |
394 | /** | |
7c913512 | 395 | Returns the left location string extracted from @e location. |
ea6a2ccb FM |
396 | |
397 | Example: | |
398 | @code | |
399 | GetLeftLocation("file:myzipfile.zip#zip:index.htm") == "file:myzipfile.zip" | |
400 | @endcode | |
23324ae1 | 401 | */ |
0004982c | 402 | static wxString GetLeftLocation(const wxString& location); |
23324ae1 FM |
403 | |
404 | /** | |
ea6a2ccb FM |
405 | Returns the MIME type based on @b extension of @a location. |
406 | (While wxFSFile::GetMimeType() returns real MIME type - either | |
407 | extension-based or queried from HTTP.) | |
408 | ||
409 | Example: | |
410 | @code | |
411 | GetMimeTypeFromExt("index.htm") == "text/html" | |
412 | @endcode | |
23324ae1 | 413 | */ |
adaaa686 | 414 | static wxString GetMimeTypeFromExt(const wxString& location); |
23324ae1 FM |
415 | |
416 | /** | |
ea6a2ccb FM |
417 | Returns the protocol string extracted from @a location. |
418 | ||
419 | Example: | |
420 | @code | |
421 | GetProtocol("file:myzipfile.zip#zip:index.htm") == "zip" | |
422 | @endcode | |
23324ae1 | 423 | */ |
0004982c | 424 | static wxString GetProtocol(const wxString& location); |
23324ae1 FM |
425 | |
426 | /** | |
ea6a2ccb FM |
427 | Returns the right location string extracted from @a location. |
428 | ||
429 | Example: | |
430 | @code | |
431 | GetRightLocation("file:myzipfile.zip#zip:index.htm") == "index.htm" | |
432 | @endcode | |
23324ae1 | 433 | */ |
0004982c | 434 | static wxString GetRightLocation(const wxString& location); |
23324ae1 FM |
435 | |
436 | /** | |
437 | Opens the file and returns wxFSFile pointer or @NULL if failed. | |
23324ae1 | 438 | Must be overridden in derived handlers. |
3c4f71cc | 439 | |
7c913512 | 440 | @param fs |
ea6a2ccb FM |
441 | Parent FS (the FS from that OpenFile was called). |
442 | See the ZIP handler for details of how to use it. | |
7c913512 | 443 | @param location |
4cc4bfaf | 444 | The absolute location of file. |
23324ae1 | 445 | */ |
fadc2df6 | 446 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0; |
23324ae1 | 447 | }; |
e54c96f1 | 448 |