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