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"
23 #include "wx/datetime.h"
26 class wxFileSystemHandler
;
29 //--------------------------------------------------------------------------------
31 // This class is a file opened using wxFileSystem. It consists of
32 // input stream, location, mime type & optional anchor
33 // (in 'index.htm#chapter2', 'chapter2' is anchor)
34 //--------------------------------------------------------------------------------
36 class WXDLLEXPORT wxFSFile
: public wxObject
39 wxInputStream
*m_Stream
;
46 wxFSFile(wxInputStream
*stream
, const wxString
& loc
,
47 const wxString
& mimetype
, const wxString
& anchor
,
52 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
58 if (m_Stream
) delete m_Stream
;
61 wxInputStream
*GetStream() const {return m_Stream
;}
62 // returns stream. This doesn't _create_ stream, it only returns
65 const wxString
& GetMimeType() const {return m_MimeType
;}
66 // returns file's mime type
68 const wxString
& GetLocation() const {return m_Location
;}
69 // returns the original location (aka filename) of the file
71 const wxString
& GetAnchor() const {return m_Anchor
;}
73 wxDateTime
GetModificationTime() const {return m_Modif
;}
80 //--------------------------------------------------------------------------------
81 // wxFileSystemHandler
82 // This class is FS handler for wxFileSystem. It provides
83 // interface to access certain
84 // kinds of files (HTPP, FTP, local, tar.gz etc..)
85 //--------------------------------------------------------------------------------
87 class WXDLLEXPORT wxFileSystemHandler
: public wxObject
89 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
92 wxFileSystemHandler() : wxObject() {}
94 virtual bool CanOpen(const wxString
& location
) = 0;
95 // returns TRUE if this handler is able to open given location
97 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
98 // opens given file and returns pointer to input stream.
99 // Returns NULL if opening failed.
100 // The location is always absolute path.
102 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
103 virtual wxString
FindNext();
104 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
105 // the query to directories or wxFILE for files only or 0 for either.
106 // Returns filename or empty string if no more matching file exists
109 wxString
GetProtocol(const wxString
& location
) const;
110 // returns protocol ("file", "http", "tar" etc.) The last (most right)
112 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
114 wxString
GetLeftLocation(const wxString
& location
) const;
115 // returns left part of address:
116 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
118 wxString
GetAnchor(const wxString
& location
) const;
119 // returns anchor part of address:
120 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
121 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
123 wxString
GetRightLocation(const wxString
& location
) const;
124 // returns right part of address:
125 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
127 wxString
GetMimeTypeFromExt(const wxString
& location
);
128 // Returns MIME type of the file - w/o need to open it
129 // (default behaviour is that it returns type based on extension)
135 //--------------------------------------------------------------------------------
137 // This class provides simple interface for opening various
138 // kinds of files (HTPP, FTP, local, tar.gz etc..)
139 //--------------------------------------------------------------------------------
141 class WXDLLEXPORT wxFileSystem
: public wxObject
143 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
146 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
); m_FindFileHandler
= NULL
;}
148 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
149 // sets the current location. Every call to OpenFile is
150 // relative to this location.
152 // unless is_dir = TRUE 'location' is *not* the directory but
153 // file contained in this directory
154 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
156 wxString
GetPath() const {return m_Path
;}
158 wxFSFile
* OpenFile(const wxString
& location
);
159 // opens given file and returns pointer to input stream.
160 // Returns NULL if opening failed.
161 // It first tries to open the file in relative scope
162 // (based on ChangePathTo()'s value) and then as an absolute
165 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
167 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
168 // the query to directories or wxFILE for files only or 0 for either.
169 // Returns filename or empty string if no more matching file exists
172 static void AddHandler(wxFileSystemHandler
*handler
);
174 // In fact, this class is only front-end to the FS hanlers :-)
176 static void CleanUpHandlers();
177 // remove all items from the m_Handlers list
181 // the path (location) we are currently in
182 // this is path, not file!
183 // (so if you opened test/demo.htm, it is
184 // "test/", not "test/demo.htm")
186 // name of last opened file (full path)
187 static wxList m_Handlers
;
188 // list of FS handlers
189 wxFileSystemHandler
*m_FindFileHandler
;
190 // handler that succeed in FindFirst query
198 To determine FS type, we're using standard KDE notation:
199 file:/absolute/path/file.htm
200 file:relative_path/xxxxx.html
201 /some/path/x.file ('file:' is default)
203 file:subdir/archive.tar.gz#tar:/README.txt
206 ':' - FS identificator is before this char
207 '#' - separator. It can be either HTML anchor ("index.html#news")
208 (in case there is no ':' in the string to the right from it)
210 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
211 this would access tgz archive stored on web)
212 '/' - directory (path) separator. It is used to determine upper-level path.
213 HEY! Don't use \ even if you're on Windows!
218 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS