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"
24 #include "wx/datetime.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
;
47 wxFSFile(wxInputStream
*stream
, const wxString
& loc
,
48 const wxString
& mimetype
, const wxString
& anchor
,
53 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
59 if (m_Stream
) delete m_Stream
;
62 wxInputStream
*GetStream() const {return m_Stream
;}
63 // returns stream. This doesn't _create_ stream, it only returns
66 const wxString
& GetMimeType() const {return m_MimeType
;}
67 // returns file's mime type
69 const wxString
& GetLocation() const {return m_Location
;}
70 // returns the original location (aka filename) of the file
72 const wxString
& GetAnchor() const {return m_Anchor
;}
74 wxDateTime
GetModificationTime() const {return m_Modif
;}
81 //--------------------------------------------------------------------------------
82 // wxFileSystemHandler
83 // This class is FS handler for wxFileSystem. It provides
84 // interface to access certain
85 // kinds of files (HTPP, FTP, local, tar.gz etc..)
86 //--------------------------------------------------------------------------------
88 class WXDLLEXPORT wxFileSystemHandler
: public wxObject
90 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
93 wxFileSystemHandler() : wxObject() {}
95 virtual bool CanOpen(const wxString
& location
) = 0;
96 // returns TRUE if this handler is able to open given location
98 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
99 // opens given file and returns pointer to input stream.
100 // Returns NULL if opening failed.
101 // The location is always absolute path.
103 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
104 virtual wxString
FindNext();
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
110 wxString
GetProtocol(const wxString
& location
) const;
111 // returns protocol ("file", "http", "tar" etc.) The last (most right)
113 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
115 wxString
GetLeftLocation(const wxString
& location
) const;
116 // returns left part of address:
117 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
119 wxString
GetAnchor(const wxString
& location
) const;
120 // returns anchor part of address:
121 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
122 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
124 wxString
GetRightLocation(const wxString
& location
) const;
125 // returns right part of address:
126 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
128 wxString
GetMimeTypeFromExt(const wxString
& location
);
129 // Returns MIME type of the file - w/o need to open it
130 // (default behaviour is that it returns type based on extension)
133 static void CleanUpStatics();
134 // deletes static members (m_MimeMng). It can be called
135 // as many times as you wish because m_MimeMng is created
139 static wxMimeTypesManager
*m_MimeMng
;
141 // (it's static and thus shared by all instances and derived classes)
147 //--------------------------------------------------------------------------------
149 // This class provides simple interface for opening various
150 // kinds of files (HTPP, FTP, local, tar.gz etc..)
151 //--------------------------------------------------------------------------------
153 class WXDLLEXPORT wxFileSystem
: public wxObject
155 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
158 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
); m_FindFileHandler
= NULL
;}
160 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
161 // sets the current location. Every call to OpenFile is
162 // relative to this location.
164 // unless is_dir = TRUE 'location' is *not* the directory but
165 // file contained in this directory
166 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
168 wxString
GetPath() const {return m_Path
;}
170 wxFSFile
* OpenFile(const wxString
& location
);
171 // opens given file and returns pointer to input stream.
172 // Returns NULL if opening failed.
173 // It first tries to open the file in relative scope
174 // (based on ChangePathTo()'s value) and then as an absolute
177 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
179 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
180 // the query to directories or wxFILE for files only or 0 for either.
181 // Returns filename or empty string if no more matching file exists
184 static void AddHandler(wxFileSystemHandler
*handler
);
186 // In fact, this class is only front-end to the FS hanlers :-)
188 static void CleanUpHandlers();
189 // remove all items from the m_Handlers list
193 // the path (location) we are currently in
194 // this is path, not file!
195 // (so if you opened test/demo.htm, it is
196 // "test/", not "test/demo.htm")
198 // name of last opened file (full path)
199 static wxList m_Handlers
;
200 // list of FS handlers
201 wxFileSystemHandler
*m_FindFileHandler
;
202 // handler that succeed in FindFirst query
210 To determine FS type, we're using standard KDE notation:
211 file:/absolute/path/file.htm
212 file:relative_path/xxxxx.html
213 /some/path/x.file ('file:' is default)
215 file:subdir/archive.tar.gz#tar:/README.txt
218 ':' - FS identificator is before this char
219 '#' - separator. It can be either HTML anchor ("index.html#news")
220 (in case there is no ':' in the string to the right from it)
222 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
223 this would access tgz archive stored on web)
224 '/' - directory (path) separator. It is used to determine upper-level path.
225 HEY! Don't use \ even if you're on Windows!
230 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS