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 /////////////////////////////////////////////////////////////////////////////
18 #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
20 #include "wx/stream.h"
21 #include "wx/mimetype.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
;
45 wxFSFile(wxInputStream
*stream
, const wxString
& loc
, const wxString
& mimetype
, const wxString
& anchor
)
49 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
54 if (m_Stream
) delete m_Stream
;
57 wxInputStream
*GetStream() const {return m_Stream
;}
58 // returns stream. This doesn't _create_ stream, it only returns
61 const wxString
& GetMimeType() const {return m_MimeType
;}
62 // returns file's mime type
64 const wxString
& GetLocation() const {return m_Location
;}
65 // returns the original location (aka filename) of the file
67 const wxString
& GetAnchor() const {return m_Anchor
;}
74 //--------------------------------------------------------------------------------
75 // wxFileSystemHandler
76 // This class is FS handler for wxFileSystem. It provides
77 // interface to access certain
78 // kinds of files (HTPP, FTP, local, tar.gz etc..)
79 //--------------------------------------------------------------------------------
81 class WXDLLEXPORT wxFileSystemHandler
: public wxObject
83 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
86 wxFileSystemHandler() : wxObject() {}
88 virtual bool CanOpen(const wxString
& location
) = 0;
89 // returns TRUE if this handler is able to open given location
91 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
92 // opens given file and returns pointer to input stream.
93 // Returns NULL if opening failed.
94 // The location is always absolute path.
97 wxString
GetProtocol(const wxString
& location
) const;
98 // returns protocol ("file", "http", "tar" etc.) The last (most right)
100 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
102 wxString
GetLeftLocation(const wxString
& location
) const;
103 // returns left part of address:
104 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
106 wxString
GetAnchor(const wxString
& location
) const;
107 // returns anchor part of address:
108 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
109 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
111 wxString
GetRightLocation(const wxString
& location
) const;
112 // returns right part of address:
113 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
115 wxString
GetMimeTypeFromExt(const wxString
& location
);
116 // Returns MIME type of the file - w/o need to open it
117 // (default behaviour is that it returns type based on extension)
120 static void CleanUpStatics();
121 // deletes static members (m_MimeMng). It can be called
122 // as many times as you wish because m_MimeMng is created
126 static wxMimeTypesManager
*m_MimeMng
;
128 // (it's static and thus shared by all instances and derived classes)
134 //--------------------------------------------------------------------------------
136 // This class provides simple interface for opening various
137 // kinds of files (HTPP, FTP, local, tar.gz etc..)
138 //--------------------------------------------------------------------------------
140 class WXDLLEXPORT wxFileSystem
: public wxObject
142 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
146 // the path (location) we are currently in
147 // this is path, not file!
148 // (so if you opened test/demo.htm, it is
149 // "test/", not "test/demo.htm")
151 // name of last opened file (full path)
152 static wxList m_Handlers
;
153 // list of FS handlers
156 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
);}
158 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
159 // sets the current location. Every call to OpenFile is
160 // relative to this location.
162 // unless is_dir = TRUE 'location' is *not* the directory but
163 // file contained in this directory
164 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
166 wxString
GetPath() const {return m_Path
;}
168 wxFSFile
* OpenFile(const wxString
& location
);
169 // opens given file and returns pointer to input stream.
170 // Returns NULL if opening failed.
171 // It first tries to open the file in relative scope
172 // (based on ChangePathTo()'s value) and then as an absolute
175 static void AddHandler(wxFileSystemHandler
*handler
);
177 // In fact, this class is only front-end to the FS hanlers :-)
179 static void CleanUpHandlers();
180 // remove all items from the m_Handlers list
188 To determine FS type, we're using standard KDE notation:
189 file:/absolute/path/file.htm
190 file:relative_path/xxxxx.html
191 /some/path/x.file ('file:' is default)
193 file:subdir/archive.tar.gz#tar:/README.txt
196 ':' - FS identificator is before this char
197 '#' - separator. It can be either HTML anchor ("index.html#news")
198 (in case there is no ':' in the string to the right from it)
200 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
201 this would access tgz archive stored on web)
202 '/' - directory (path) separator. It is used to determine upper-level path.
203 HEY! Don't use \ even if you're on Windows!
208 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS