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 /////////////////////////////////////////////////////////////////////////////
16 #include <wx/stream.h>
17 #include <wx/mimetype.h>
22 class wxFileSystemHandler
;
25 //--------------------------------------------------------------------------------
27 // This class is a file opened using wxFileSystem. It consists of
28 // input stream, location, mime type & optional anchor
29 // (in 'index.htm#chapter2', 'chapter2' is anchor)
30 //--------------------------------------------------------------------------------
32 class WXDLLEXPORT wxFSFile
: public wxObject
35 wxInputStream
*m_Stream
;
41 wxFSFile(wxInputStream
*stream
, const wxString
& loc
, const wxString
& mimetype
, const wxString
& anchor
)
45 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
50 if (m_Stream
) delete m_Stream
;
53 wxInputStream
*GetStream() const {return m_Stream
;}
54 // returns stream. This doesn't _create_ stream, it only returns
57 const wxString
& GetMimeType() const {return m_MimeType
;}
58 // returns file's mime type
60 const wxString
& GetLocation() const {return m_Location
;}
61 // returns the original location (aka filename) of the file
63 const wxString
& GetAnchor() const {return m_Anchor
;}
70 //--------------------------------------------------------------------------------
71 // wxFileSystemHandler
72 // This class is FS handler for wxFileSystem. It provides
73 // interface to access certain
74 // kinds of files (HTPP, FTP, local, tar.gz etc..)
75 //--------------------------------------------------------------------------------
77 class WXDLLEXPORT wxFileSystemHandler
: public wxObject
79 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
82 wxFileSystemHandler() : wxObject() {}
84 virtual bool CanOpen(const wxString
& location
) = 0;
85 // returns TRUE if this handler is able to open given location
87 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
88 // opens given file and returns pointer to input stream.
89 // Returns NULL if opening failed.
90 // The location is always absolute path.
93 wxString
GetProtocol(const wxString
& location
) const;
94 // returns protocol ("file", "http", "tar" etc.) The last (most right)
96 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
98 wxString
GetLeftLocation(const wxString
& location
) const;
99 // returns left part of address:
100 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
102 wxString
GetAnchor(const wxString
& location
) const;
103 // returns anchor part of address:
104 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
105 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
107 wxString
GetRightLocation(const wxString
& location
) const;
108 // returns right part of address:
109 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
111 wxString
GetMimeTypeFromExt(const wxString
& location
);
112 // Returns MIME type of the file - w/o need to open it
113 // (default behaviour is that it returns type based on extension)
116 static wxMimeTypesManager m_MimeMng
;
118 // (it's static and thus shared by all instances and derived classes)
124 //--------------------------------------------------------------------------------
126 // This class provides simple interface for opening various
127 // kinds of files (HTPP, FTP, local, tar.gz etc..)
128 //--------------------------------------------------------------------------------
130 class WXDLLEXPORT wxFileSystem
: public wxObject
132 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
136 // the path (location) we are currently in
137 // this is path, not file!
138 // (so if you opened test/demo.htm, it is
139 // "test/", not "test/demo.htm")
141 // name of last opened file (full path)
142 static wxList m_Handlers
;
143 // list of FS handlers
146 wxFileSystem() : wxObject() {m_Path
= m_LastName
= wxEmptyString
; m_Handlers
.DeleteContents(TRUE
);}
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 static void AddHandler(wxFileSystemHandler
*handler
);
167 // In fact, this class is only front-end to the FS hanlers :-)
175 To determine FS type, we're using standard KDE notation:
176 file:/absolute/path/file.htm
177 file:relative_path/xxxxx.html
178 /some/path/x.file ('file:' is default)
180 file:subdir/archive.tar.gz#tar:/README.txt
183 ':' - FS identificator is before this char
184 '#' - separator. It can be either HTML anchor ("index.html#news")
185 (in case there is no ':' in the string to the right from it)
187 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
188 this would access tgz archive stored on web)
189 '/' - directory (path) separator. It is used to determine upper-level path.
190 HEY! Don't use \ even if you're on Windows!
194 #endif // __FILESYS_H__