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 wxMimeTypesManager m_MimeMng
;
122 // (it's static and thus shared by all instances and derived classes)
128 //--------------------------------------------------------------------------------
130 // This class provides simple interface for opening various
131 // kinds of files (HTPP, FTP, local, tar.gz etc..)
132 //--------------------------------------------------------------------------------
134 class WXDLLEXPORT wxFileSystem
: public wxObject
136 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
140 // the path (location) we are currently in
141 // this is path, not file!
142 // (so if you opened test/demo.htm, it is
143 // "test/", not "test/demo.htm")
145 // name of last opened file (full path)
146 static wxList m_Handlers
;
147 // list of FS handlers
150 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
);}
152 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
153 // sets the current location. Every call to OpenFile is
154 // relative to this location.
156 // unless is_dir = TRUE 'location' is *not* the directory but
157 // file contained in this directory
158 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
160 wxString
GetPath() const {return m_Path
;}
162 wxFSFile
* OpenFile(const wxString
& location
);
163 // opens given file and returns pointer to input stream.
164 // Returns NULL if opening failed.
165 // It first tries to open the file in relative scope
166 // (based on ChangePathTo()'s value) and then as an absolute
169 static void AddHandler(wxFileSystemHandler
*handler
);
171 // In fact, this class is only front-end to the FS hanlers :-)
179 To determine FS type, we're using standard KDE notation:
180 file:/absolute/path/file.htm
181 file:relative_path/xxxxx.html
182 /some/path/x.file ('file:' is default)
184 file:subdir/archive.tar.gz#tar:/README.txt
187 ':' - FS identificator is before this char
188 '#' - separator. It can be either HTML anchor ("index.html#news")
189 (in case there is no ':' in the string to the right from it)
191 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
192 this would access tgz archive stored on web)
193 '/' - directory (path) separator. It is used to determine upper-level path.
194 HEY! Don't use \ even if you're on Windows!
199 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS