1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: class for opening files - virtual file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
20 #error You cannot compile virtual file systems without wxUSE_STREAMS
23 #if wxUSE_HTML && !wxUSE_FILESYSTEM
24 #error You cannot compile wxHTML without virtual file systems
29 #include "wx/stream.h"
31 #include "wx/datetime.h"
34 class wxFileSystemHandler
;
37 //--------------------------------------------------------------------------------
39 // This class is a file opened using wxFileSystem. It consists of
40 // input stream, location, mime type & optional anchor
41 // (in 'index.htm#chapter2', 'chapter2' is anchor)
42 //--------------------------------------------------------------------------------
44 class WXDLLEXPORT wxFSFile
: public wxObject
47 wxFSFile(wxInputStream
*stream
, const wxString
& loc
,
48 const wxString
& mimetype
, const wxString
& anchor
,
53 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
57 virtual ~wxFSFile() { if (m_Stream
) delete m_Stream
; }
59 // returns stream. This doesn't _create_ stream, it only returns
61 wxInputStream
*GetStream() const {return m_Stream
;}
63 // returns file's mime type
64 const wxString
& GetMimeType() const {return m_MimeType
;}
66 // returns the original location (aka filename) of the file
67 const wxString
& GetLocation() const {return m_Location
;}
69 const wxString
& GetAnchor() const {return m_Anchor
;}
71 wxDateTime
GetModificationTime() const {return m_Modif
;}
74 wxInputStream
*m_Stream
;
85 //--------------------------------------------------------------------------------
86 // wxFileSystemHandler
87 // This class is FS handler for wxFileSystem. It provides
88 // interface to access certain
89 // kinds of files (HTPP, FTP, local, tar.gz etc..)
90 //--------------------------------------------------------------------------------
92 class WXDLLEXPORT wxFileSystemHandler
: public wxObject
95 wxFileSystemHandler() : wxObject() {}
97 // returns TRUE if this handler is able to open given location
98 virtual bool CanOpen(const wxString
& location
) = 0;
100 // opens given file and returns pointer to input stream.
101 // Returns NULL if opening failed.
102 // The location is always absolute path.
103 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
105 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
106 // the query to directories or wxFILE for files only or 0 for either.
107 // Returns filename or empty string if no more matching file exists
108 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
109 virtual wxString
FindNext();
112 // returns protocol ("file", "http", "tar" etc.) The last (most right)
114 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
115 wxString
GetProtocol(const wxString
& location
) const;
117 // returns left part of address:
118 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
119 wxString
GetLeftLocation(const wxString
& location
) const;
121 // returns anchor part of address:
122 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
123 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
124 wxString
GetAnchor(const wxString
& location
) const;
126 // returns right part of address:
127 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
128 wxString
GetRightLocation(const wxString
& location
) const;
130 // Returns MIME type of the file - w/o need to open it
131 // (default behaviour is that it returns type based on extension)
132 wxString
GetMimeTypeFromExt(const wxString
& location
);
134 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
140 //--------------------------------------------------------------------------------
142 // This class provides simple interface for opening various
143 // kinds of files (HTPP, FTP, local, tar.gz etc..)
144 //--------------------------------------------------------------------------------
146 class WXDLLEXPORT wxFileSystem
: public wxObject
149 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
); m_FindFileHandler
= NULL
;}
151 // sets the current location. Every call to OpenFile is
152 // relative to this location.
154 // unless is_dir = TRUE 'location' is *not* the directory but
155 // file contained in this directory
156 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
157 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
159 wxString
GetPath() const {return m_Path
;}
161 // opens given file and returns pointer to input stream.
162 // Returns NULL if opening failed.
163 // It first tries to open the file in relative scope
164 // (based on ChangePathTo()'s value) and then as an absolute
166 wxFSFile
* OpenFile(const wxString
& location
);
168 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
169 // the query to directories or wxFILE for files only or 0 for either.
170 // Returns filename or empty string if no more matching file exists
171 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
175 // In fact, this class is only front-end to the FS hanlers :-)
176 static void AddHandler(wxFileSystemHandler
*handler
);
178 // remove all items from the m_Handlers list
179 static void CleanUpHandlers();
183 // the path (location) we are currently in
184 // this is path, not file!
185 // (so if you opened test/demo.htm, it is
186 // "test/", not "test/demo.htm")
188 // name of last opened file (full path)
189 static wxList m_Handlers
;
190 // list of FS handlers
191 wxFileSystemHandler
*m_FindFileHandler
;
192 // handler that succeed in FindFirst query
194 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
202 To determine FS type, we're using standard KDE notation:
203 file:/absolute/path/file.htm
204 file:relative_path/xxxxx.html
205 /some/path/x.file ('file:' is default)
207 file:subdir/archive.tar.gz#tar:/README.txt
210 ':' - FS identificator is before this char
211 '#' - separator. It can be either HTML anchor ("index.html#news")
212 (in case there is no ':' in the string to the right from it)
214 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
215 this would access tgz archive stored on web)
216 '/' - directory (path) separator. It is used to determine upper-level path.
217 HEY! Don't use \ even if you're on Windows!
222 class wxLocalFSHandler
: public wxFileSystemHandler
225 virtual bool CanOpen(const wxString
& location
);
226 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
227 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
228 virtual wxString
FindNext();
230 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
231 // files on disk. This effectively makes 'root' the top-level directory
232 // and prevents access to files outside this directory.
233 // (This is similar to Unix command 'chroot'.)
234 static void Chroot(const wxString
& root
) { ms_root
= root
; }
237 static wxString ms_root
;