1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: class for opening files - virtual file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
17 #error You cannot compile virtual file systems without wxUSE_STREAMS
20 #if wxUSE_HTML && !wxUSE_FILESYSTEM
21 #error You cannot compile wxHTML without virtual file systems
24 #include "wx/stream.h"
25 #include "wx/datetime.h"
26 #include "wx/filename.h"
27 #include "wx/hashmap.h"
29 class WXDLLIMPEXP_FWD_BASE wxFSFile
;
30 class WXDLLIMPEXP_FWD_BASE wxFileSystemHandler
;
31 class WXDLLIMPEXP_FWD_BASE wxFileSystem
;
33 //--------------------------------------------------------------------------------
35 // This class is a file opened using wxFileSystem. It consists of
36 // input stream, location, mime type & optional anchor
37 // (in 'index.htm#chapter2', 'chapter2' is anchor)
38 //--------------------------------------------------------------------------------
40 class WXDLLIMPEXP_BASE wxFSFile
: public wxObject
43 wxFSFile(wxInputStream
*stream
, const wxString
& loc
,
44 const wxString
& mimetype
, const wxString
& anchor
47 #endif // wxUSE_DATETIME
52 m_MimeType
= mimetype
.Lower();
56 #endif // wxUSE_DATETIME
59 virtual ~wxFSFile() { delete m_Stream
; }
61 // returns stream. This doesn't give away ownership of the stream object.
62 wxInputStream
*GetStream() const { return m_Stream
; }
64 // gives away the ownership of the current stream.
65 wxInputStream
*DetachStream()
67 wxInputStream
*stream
= m_Stream
;
72 // deletes the current stream and takes ownership of another.
73 void SetStream(wxInputStream
*stream
)
79 // returns file's mime type
80 const wxString
& GetMimeType() const;
82 // returns the original location (aka filename) of the file
83 const wxString
& GetLocation() const { return m_Location
; }
85 const wxString
& GetAnchor() const { return m_Anchor
; }
88 wxDateTime
GetModificationTime() const { return m_Modif
; }
89 #endif // wxUSE_DATETIME
92 wxInputStream
*m_Stream
;
98 #endif // wxUSE_DATETIME
100 DECLARE_ABSTRACT_CLASS(wxFSFile
)
101 wxDECLARE_NO_COPY_CLASS(wxFSFile
);
108 //--------------------------------------------------------------------------------
109 // wxFileSystemHandler
110 // This class is FS handler for wxFileSystem. It provides
111 // interface to access certain
112 // kinds of files (HTPP, FTP, local, tar.gz etc..)
113 //--------------------------------------------------------------------------------
115 class WXDLLIMPEXP_BASE wxFileSystemHandler
: public wxObject
118 wxFileSystemHandler() : wxObject() {}
120 // returns true if this handler is able to open given location
121 virtual bool CanOpen(const wxString
& location
) = 0;
123 // opens given file and returns pointer to input stream.
124 // Returns NULL if opening failed.
125 // The location is always absolute path.
126 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
128 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
129 // the query to directories or wxFILE for files only or 0 for either.
130 // Returns filename or empty string if no more matching file exists
131 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
132 virtual wxString
FindNext();
134 // Returns MIME type of the file - w/o need to open it
135 // (default behaviour is that it returns type based on extension)
136 static wxString
GetMimeTypeFromExt(const wxString
& location
);
139 // returns protocol ("file", "http", "tar" etc.) The last (most right)
141 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
142 static wxString
GetProtocol(const wxString
& location
);
144 // returns left part of address:
145 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
146 static wxString
GetLeftLocation(const wxString
& location
);
148 // returns anchor part of address:
149 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
150 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
151 static wxString
GetAnchor(const wxString
& location
);
153 // returns right part of address:
154 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
155 static wxString
GetRightLocation(const wxString
& location
);
157 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
163 //--------------------------------------------------------------------------------
165 // This class provides simple interface for opening various
166 // kinds of files (HTPP, FTP, local, tar.gz etc..)
167 //--------------------------------------------------------------------------------
170 enum wxFileSystemOpenFlags
172 wxFS_READ
= 1, // Open for reading
173 wxFS_SEEKABLE
= 4 // Returned stream will be seekable
176 WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL(wxFileSystemHandler
*, wxFSHandlerHash
, class WXDLLIMPEXP_BASE
);
178 class WXDLLIMPEXP_BASE wxFileSystem
: public wxObject
181 wxFileSystem() : wxObject() { m_FindFileHandler
= NULL
;}
182 virtual ~wxFileSystem();
184 // sets the current location. Every call to OpenFile is
185 // relative to this location.
187 // unless is_dir = true 'location' is *not* the directory but
188 // file contained in this directory
189 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
190 void ChangePathTo(const wxString
& location
, bool is_dir
= false);
192 wxString
GetPath() const {return m_Path
;}
194 // opens given file and returns pointer to input stream.
195 // Returns NULL if opening failed.
196 // It first tries to open the file in relative scope
197 // (based on ChangePathTo()'s value) and then as an absolute
199 wxFSFile
* OpenFile(const wxString
& location
, int flags
= wxFS_READ
);
201 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
202 // the query to directories or wxFILE for files only or 0 for either.
203 // Returns filename or empty string if no more matching file exists
204 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
207 // find a file in a list of directories, returns false if not found
208 bool FindFileInPath(wxString
*pStr
,
209 const wxString
& path
, const wxString
& file
);
212 // In fact, this class is only front-end to the FS handlers :-)
213 static void AddHandler(wxFileSystemHandler
*handler
);
215 // Removes FS handler
216 static wxFileSystemHandler
* RemoveHandler(wxFileSystemHandler
*handler
);
218 // Returns true if there is a handler which can open the given location.
219 static bool HasHandlerForPath(const wxString
& location
);
221 // remove all items from the m_Handlers list
222 static void CleanUpHandlers();
224 // Returns the native path for a file URL
225 static wxFileName
URLToFileName(const wxString
& url
);
227 // Returns the file URL for a native path
228 static wxString
FileNameToURL(const wxFileName
& filename
);
232 wxFileSystemHandler
*MakeLocal(wxFileSystemHandler
*h
);
235 // the path (location) we are currently in
236 // this is path, not file!
237 // (so if you opened test/demo.htm, it is
238 // "test/", not "test/demo.htm")
240 // name of last opened file (full path)
241 static wxList m_Handlers
;
242 // list of FS handlers
243 wxFileSystemHandler
*m_FindFileHandler
;
244 // handler that succeed in FindFirst query
245 wxFSHandlerHash m_LocalHandlers
;
246 // Handlers local to this instance
248 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
249 wxDECLARE_NO_COPY_CLASS(wxFileSystem
);
257 To determine FS type, we're using standard KDE notation:
258 file:/absolute/path/file.htm
259 file:relative_path/xxxxx.html
260 /some/path/x.file ('file:' is default)
262 file:subdir/archive.tar.gz#tar:/README.txt
265 ':' - FS identificator is before this char
266 '#' - separator. It can be either HTML anchor ("index.html#news")
267 (in case there is no ':' in the string to the right from it)
269 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
270 this would access tgz archive stored on web)
271 '/' - directory (path) separator. It is used to determine upper-level path.
272 HEY! Don't use \ even if you're on Windows!
277 class WXDLLIMPEXP_BASE wxLocalFSHandler
: public wxFileSystemHandler
280 virtual bool CanOpen(const wxString
& location
);
281 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
282 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
283 virtual wxString
FindNext();
285 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
286 // files on disk. This effectively makes 'root' the top-level directory
287 // and prevents access to files outside this directory.
288 // (This is similar to Unix command 'chroot'.)
289 static void Chroot(const wxString
& root
) { ms_root
= root
; }
292 static wxString ms_root
;
295 // Stream reading data from wxFSFile: this allows to use virtual files with any
296 // wx functions accepting streams.
297 class WXDLLIMPEXP_BASE wxFSInputStream
: public wxWrapperInputStream
300 // Notice that wxFS_READ is implied in flags.
301 wxFSInputStream(const wxString
& filename
, int flags
= 0);
302 virtual ~wxFSInputStream();
307 wxDECLARE_NO_COPY_CLASS(wxFSInputStream
);