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 /////////////////////////////////////////////////////////////////////////////
19 #if (wxUSE_HTML || wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
21 #include "wx/stream.h"
22 #include "wx/mimetype.h"
27 class wxFileSystemHandler
;
30 //--------------------------------------------------------------------------------
32 // This class is a file opened using wxFileSystem. It consists of
33 // input stream, location, mime type & optional anchor
34 // (in 'index.htm#chapter2', 'chapter2' is anchor)
35 //--------------------------------------------------------------------------------
37 class WXDLLEXPORT wxFSFile
: public wxObject
40 wxInputStream
*m_Stream
;
46 wxFSFile(wxInputStream
*stream
, const wxString
& loc
, const wxString
& mimetype
, const wxString
& anchor
)
50 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
55 if (m_Stream
) delete m_Stream
;
58 wxInputStream
*GetStream() const {return m_Stream
;}
59 // returns stream. This doesn't _create_ stream, it only returns
62 const wxString
& GetMimeType() const {return m_MimeType
;}
63 // returns file's mime type
65 const wxString
& GetLocation() const {return m_Location
;}
66 // returns the original location (aka filename) of the file
68 const wxString
& GetAnchor() const {return m_Anchor
;}
75 //--------------------------------------------------------------------------------
76 // wxFileSystemHandler
77 // This class is FS handler for wxFileSystem. It provides
78 // interface to access certain
79 // kinds of files (HTPP, FTP, local, tar.gz etc..)
80 //--------------------------------------------------------------------------------
82 class WXDLLEXPORT wxFileSystemHandler
: public wxObject
84 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
87 wxFileSystemHandler() : wxObject() {}
89 virtual bool CanOpen(const wxString
& location
) = 0;
90 // returns TRUE if this handler is able to open given location
92 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
93 // opens given file and returns pointer to input stream.
94 // Returns NULL if opening failed.
95 // The location is always absolute path.
97 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
98 virtual wxString
FindNext();
99 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
100 // the query to directories or wxFILE for files only or 0 for either.
101 // Returns filename or empty string if no more matching file exists
104 wxString
GetProtocol(const wxString
& location
) const;
105 // returns protocol ("file", "http", "tar" etc.) The last (most right)
107 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
109 wxString
GetLeftLocation(const wxString
& location
) const;
110 // returns left part of address:
111 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
113 wxString
GetAnchor(const wxString
& location
) const;
114 // returns anchor part of address:
115 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
116 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
118 wxString
GetRightLocation(const wxString
& location
) const;
119 // returns right part of address:
120 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
122 wxString
GetMimeTypeFromExt(const wxString
& location
);
123 // Returns MIME type of the file - w/o need to open it
124 // (default behaviour is that it returns type based on extension)
127 static void CleanUpStatics();
128 // deletes static members (m_MimeMng). It can be called
129 // as many times as you wish because m_MimeMng is created
133 static wxMimeTypesManager
*m_MimeMng
;
135 // (it's static and thus shared by all instances and derived classes)
141 //--------------------------------------------------------------------------------
143 // This class provides simple interface for opening various
144 // kinds of files (HTPP, FTP, local, tar.gz etc..)
145 //--------------------------------------------------------------------------------
147 class WXDLLEXPORT wxFileSystem
: public wxObject
149 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
152 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
); m_FindFileHandler
= NULL
;}
154 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
155 // sets the current location. Every call to OpenFile is
156 // relative to this location.
158 // unless is_dir = TRUE 'location' is *not* the directory but
159 // file contained in this directory
160 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
162 wxString
GetPath() const {return m_Path
;}
164 wxFSFile
* OpenFile(const wxString
& location
);
165 // opens given file and returns pointer to input stream.
166 // Returns NULL if opening failed.
167 // It first tries to open the file in relative scope
168 // (based on ChangePathTo()'s value) and then as an absolute
171 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
173 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
174 // the query to directories or wxFILE for files only or 0 for either.
175 // Returns filename or empty string if no more matching file exists
178 static void AddHandler(wxFileSystemHandler
*handler
);
180 // In fact, this class is only front-end to the FS hanlers :-)
182 static void CleanUpHandlers();
183 // remove all items from the m_Handlers list
187 // the path (location) we are currently in
188 // this is path, not file!
189 // (so if you opened test/demo.htm, it is
190 // "test/", not "test/demo.htm")
192 // name of last opened file (full path)
193 static wxList m_Handlers
;
194 // list of FS handlers
195 wxFileSystemHandler
*m_FindFileHandler
;
196 // handler that succeed in FindFirst query
204 To determine FS type, we're using standard KDE notation:
205 file:/absolute/path/file.htm
206 file:relative_path/xxxxx.html
207 /some/path/x.file ('file:' is default)
209 file:subdir/archive.tar.gz#tar:/README.txt
212 ':' - FS identificator is before this char
213 '#' - separator. It can be either HTML anchor ("index.html#news")
214 (in case there is no ':' in the string to the right from it)
216 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
217 this would access tgz archive stored on web)
218 '/' - directory (path) separator. It is used to determine upper-level path.
219 HEY! Don't use \ even if you're on Windows!
224 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS