]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/filesys.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxFileSystem, wxFileSystemHandler, wxFSFile
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 enum wxFileSystemOpenFlags
15 wxFS_READ
= 1, /**< Open for reading */
16 wxFS_SEEKABLE
= 4 /**< Returned stream will be seekable */
23 This class provides an interface for opening files on different file systems.
24 It can handle absolute and/or local filenames.
26 It uses a system of handlers (see wxFileSystemHandler) to provide access to
27 user-defined virtual file systems.
32 @see wxFileSystemHandler, wxFSFile, @ref overview_fs
34 class wxFileSystem
: public wxObject
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).
48 This static function adds new handler into the list of handlers
49 (see wxFileSystemHandler) which provide access to virtual FS.
51 Note that if two handlers for the same protocol are added, the last
52 added one takes precedence.
56 wxFileSystem::AddHandler(new My_FS_Handler);
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.
62 static void AddHandler(wxFileSystemHandler
* handler
);
65 Sets the current location. @a location parameter passed to OpenFile() is
66 relative to this path.
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.
71 All these commands change the path to "dir/subdir/":
74 ChangePathTo("dir/subdir/xh.htm");
75 ChangePathTo("dir/subdir", true);
76 ChangePathTo("dir/subdir/", true);
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' !!
87 the new location. Its meaning depends on the value of is_dir
89 if @true location is new directory.
90 If @false (the default) location is file in the new directory.
92 void ChangePathTo(const wxString
& location
, bool is_dir
= false);
95 Converts a wxFileName into an URL.
97 @see URLToFileName(), wxFileName
99 static wxString
FileNameToURL(const wxFileName
& filename
);
102 Looks for the file with the given name @a file in a colon or semi-colon
103 (depending on the current platform) separated list of directories in @a path.
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.
109 Receives the full path of the file, must not be @NULL
111 wxPATH_SEP-separated list of directories
113 the name of the file to look for
115 bool FindFileInPath(wxString
* pStr
, const wxString
& path
,
116 const wxString
& file
);
119 Works like ::wxFindFirstFile().
121 Returns the name of the first filename (within filesystem's current path)
122 that matches @a wildcard.
125 The wildcard that the filename must match
127 One of wxFILE (only files), wxDIR (only directories) or 0 (both).
129 wxString
FindFirst(const wxString
& wildcard
, int flags
= 0);
132 Returns the next filename that matches the parameters passed to FindFirst().
137 Returns the actual path (set by wxFileSystem::ChangePathTo).
139 wxString
GetPath() const;
142 This static function returns @true if there is a registered handler which can
143 open the given location.
145 static bool HasHandlerForPath(const wxString
& location
);
148 Opens the file and returns a pointer to a wxFSFile object or @NULL if failed.
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.
153 Note that the user is responsible for deleting the returned wxFSFile.
154 @a flags can be one or more of the ::wxFileSystemOpenFlags values
157 A stream opened with just the default @e wxFS_READ flag may
158 or may not be seekable depending on the underlying source.
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
162 and return a stream that is always seekable.
165 The @a location argument is, despite this method's name @em not
166 a filename. It is a "location", aka wxFileSystem URL (see
169 wxFSFile
* OpenFile(const wxString
& location
,
170 int flags
= wxFS_READ
);
173 Converts URL into a well-formed filename.
174 The URL must use the @c file protocol.
176 static wxFileName
URLToFileName(const wxString
& url
);
184 This class represents a single file opened by wxFileSystem.
185 It provides more informations than wxWidgets' input streams
186 (stream, filename, mime type, anchor).
188 @note Any pointer returned by a method of wxFSFile is valid only as long as
189 the wxFSFile object exists. For example a call to GetStream()
190 doesn't @e create the stream but only returns the pointer to it.
191 In other words after 10 calls to GetStream() you will have obtained
192 ten identical pointers.
197 @see wxFileSystemHandler, wxFileSystem, @ref overview_fs
199 class wxFSFile
: public wxObject
203 Constructor. You probably won't use it. See the Note for details.
205 It is seldom used by the application programmer but you will need it if
206 you are writing your own virtual FS. For example you may need something
207 similar to wxMemoryInputStream, but because wxMemoryInputStream doesn't
208 free the memory when destroyed and thus passing a memory stream pointer
209 into wxFSFile constructor would lead to memory leaks, you can write your
210 own class derived from wxFSFile:
213 class wxMyFSFile : public wxFSFile
219 ~wxMyFSFile() {free(m_Mem);}
220 // of course dtor is virtual ;-)
224 If you are not sure of the meaning of these params, see the description
225 of the GetXXXX() functions.
228 The input stream that will be used to access data
230 The full location (aka filename) of the file
232 MIME type of this file. It may be left empty, in which
233 case the type will be determined from file's extension (location must
234 not be empty in this case).
236 Anchor. See GetAnchor() for details.
238 Modification date and time for this file.
240 wxFSFile(wxInputStream
* stream
, const wxString
& location
,
241 const wxString
& mimetype
, const wxString
& anchor
,
245 Detaches the stream from the wxFSFile object. That is, the
246 stream obtained with GetStream() will continue its existance
247 after the wxFSFile object is deleted.
249 You will have to delete the stream yourself.
251 wxInputStream
* DetachStream();
254 Returns anchor (if present). The term of @b anchor can be easily
255 explained using few examples:
258 index.htm#anchor // 'anchor' is anchor
259 index/wx001.htm // NO anchor here!
260 archive/main.zip#zip:index.htm#global // 'global'
261 archive/main.zip#zip:index.htm // NO anchor here!
264 Usually an anchor is presented only if the MIME type is 'text/html'.
265 But it may have some meaning with other files; for example myanim.avi#200
266 may refer to position in animation or reality.wrl#MyView may refer
267 to a predefined view in VRML.
269 const wxString
& GetAnchor() const;
272 Returns full location of the file, including path and protocol.
276 http://www.wxwidgets.org
277 http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/archive.zip#zip:info.txt
278 file:/home/vasek/index.htm
282 const wxString
& GetLocation() const;
285 Returns the MIME type of the content of this file.
287 It is either extension-based (see wxMimeTypesManager) or extracted from
288 HTTP protocol Content-Type header.
290 const wxString
& GetMimeType() const;
293 Returns time when this file was modified.
295 wxDateTime
GetModificationTime() const;
298 Returns pointer to the stream.
300 You can use the returned stream to directly access data.
301 You may suppose that the stream provide Seek and GetSize functionality
302 (even in the case of the HTTP protocol which doesn't provide
303 this by default. wxHtml uses local cache to work around
304 this and to speed up the connection).
306 wxInputStream
* GetStream() const;
312 @class wxFileSystemHandler
314 Classes derived from wxFileSystemHandler are used to access virtual file systems.
316 Its public interface consists of two methods: wxFileSystemHandler::CanOpen
317 and wxFileSystemHandler::OpenFile.
319 It provides additional protected methods to simplify the process
320 of opening the file: GetProtocol(), GetLeftLocation(), GetRightLocation(),
321 GetAnchor(), GetMimeTypeFromExt().
323 Please have a look at overview (see wxFileSystem) if you don't know how locations
326 Also consult the @ref overview_fs_wxhtmlfs "list of available handlers".
328 Note that the handlers are shared by all instances of wxFileSystem.
331 wxHTML library provides handlers for local files and HTTP or FTP protocol.
334 The location parameter passed to OpenFile() or CanOpen() methods is always an
335 absolute path. You don't need to check the FS's current path.
338 In wxPerl, you need to derive your file system handler class
339 from @c Wx::PlFileSystemHandler.
345 @see wxFileSystem, wxFSFile, @ref overview_fs
347 class wxFileSystemHandler
: public wxObject
353 wxFileSystemHandler();
356 Returns @true if the handler is able to open this file. This function doesn't
357 check whether the file exists or not, it only checks if it knows the protocol.
361 bool MyHand::CanOpen(const wxString& location)
363 return (GetProtocol(location) == "http");
367 Must be overridden in derived handlers.
369 virtual bool CanOpen(const wxString
& location
) = 0;
372 Works like ::wxFindFirstFile().
374 Returns the name of the first filename (within filesystem's current path)
375 that matches @e wildcard. @a flags may be one of wxFILE (only files),
376 wxDIR (only directories) or 0 (both).
378 This method is only called if CanOpen() returns @true.
380 virtual wxString
FindFirst(const wxString
& wildcard
,
384 Returns next filename that matches parameters passed to wxFileSystem::FindFirst.
386 This method is only called if CanOpen() returns @true and FindFirst()
387 returned a non-empty string.
389 virtual wxString
FindNext();
392 Returns the MIME type based on @b extension of @a location.
393 (While wxFSFile::GetMimeType() returns real MIME type - either
394 extension-based or queried from HTTP.)
398 GetMimeTypeFromExt("index.htm") == "text/html"
401 static wxString
GetMimeTypeFromExt(const wxString
& location
);
404 Opens the file and returns wxFSFile pointer or @NULL if failed.
405 Must be overridden in derived handlers.
408 Parent FS (the FS from that OpenFile was called).
409 See the ZIP handler for details of how to use it.
411 The absolute location of file.
413 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
418 Returns the anchor if present in the location.
419 See wxFSFile::GetAnchor for details.
423 GetAnchor("index.htm#chapter2") == "chapter2"
426 @note the anchor is NOT part of the left location.
428 static wxString
GetAnchor(const wxString
& location
);
431 Returns the left location string extracted from @e location.
435 GetLeftLocation("file:myzipfile.zip#zip:index.htm") == "file:myzipfile.zip"
438 static wxString
GetLeftLocation(const wxString
& location
);
441 Returns the protocol string extracted from @a location.
445 GetProtocol("file:myzipfile.zip#zip:index.htm") == "zip"
448 static wxString
GetProtocol(const wxString
& location
);
451 Returns the right location string extracted from @a location.
455 GetRightLocation("file:myzipfile.zip#zip:index.htm") == "index.htm"
458 static wxString
GetRightLocation(const wxString
& location
);