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 /////////////////////////////////////////////////////////////////////////////
16 #error You cannot compile virtual file systems without wxUSE_STREAMS
19 #if wxUSE_HTML && !wxUSE_FILESYSTEM
20 #error You cannot compile wxHTML without virtual file systems
25 #include "wx/stream.h"
26 #include "wx/datetime.h"
27 #include "wx/filename.h"
29 class WXDLLIMPEXP_BASE wxFSFile
;
30 class WXDLLIMPEXP_BASE wxFileSystemHandler
;
31 class WXDLLIMPEXP_BASE wxFileSystem
;
33 //--------------------------------------------------------------------------------
35 // This class is a file opened using wxFileSystem. It consists of
36 // input stream, location, mime type & optional anchor
37 // (in 'index.htm#chapter2', 'chapter2' is anchor)
38 //--------------------------------------------------------------------------------
40 class WXDLLIMPEXP_BASE wxFSFile
: public wxObject
43 wxFSFile(wxInputStream
*stream
, const wxString
& loc
,
44 const wxString
& mimetype
, const wxString
& anchor
47 #endif // wxUSE_DATETIME
52 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
56 #endif // wxUSE_DATETIME
59 virtual ~wxFSFile() { if (m_Stream
) delete m_Stream
; }
61 // returns stream. This doesn't _create_ stream, it only returns
63 wxInputStream
*GetStream() const {return m_Stream
;}
65 // returns file's mime type
66 const wxString
& GetMimeType() const {return m_MimeType
;}
68 // returns the original location (aka filename) of the file
69 const wxString
& GetLocation() const {return m_Location
;}
71 const wxString
& GetAnchor() const {return m_Anchor
;}
74 wxDateTime
GetModificationTime() const {return m_Modif
;}
75 #endif // wxUSE_DATETIME
78 wxInputStream
*m_Stream
;
84 #endif // wxUSE_DATETIME
86 DECLARE_ABSTRACT_CLASS(wxFSFile
)
87 DECLARE_NO_COPY_CLASS(wxFSFile
)
94 //--------------------------------------------------------------------------------
95 // wxFileSystemHandler
96 // This class is FS handler for wxFileSystem. It provides
97 // interface to access certain
98 // kinds of files (HTPP, FTP, local, tar.gz etc..)
99 //--------------------------------------------------------------------------------
101 class WXDLLIMPEXP_BASE wxFileSystemHandler
: public wxObject
104 wxFileSystemHandler() : wxObject() {}
106 // returns true if this handler is able to open given location
107 virtual bool CanOpen(const wxString
& location
) = 0;
109 // opens given file and returns pointer to input stream.
110 // Returns NULL if opening failed.
111 // The location is always absolute path.
112 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
114 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
115 // the query to directories or wxFILE for files only or 0 for either.
116 // Returns filename or empty string if no more matching file exists
117 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
118 virtual wxString
FindNext();
121 // returns protocol ("file", "http", "tar" etc.) The last (most right)
123 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
124 wxString
GetProtocol(const wxString
& location
) const;
126 // returns left part of address:
127 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
128 wxString
GetLeftLocation(const wxString
& location
) const;
130 // returns anchor part of address:
131 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
132 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
133 wxString
GetAnchor(const wxString
& location
) const;
135 // returns right part of address:
136 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
137 wxString
GetRightLocation(const wxString
& location
) const;
139 // Returns MIME type of the file - w/o need to open it
140 // (default behaviour is that it returns type based on extension)
141 wxString
GetMimeTypeFromExt(const wxString
& location
);
143 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
149 //--------------------------------------------------------------------------------
151 // This class provides simple interface for opening various
152 // kinds of files (HTPP, FTP, local, tar.gz etc..)
153 //--------------------------------------------------------------------------------
155 class WXDLLIMPEXP_BASE wxFileSystem
: public wxObject
158 wxFileSystem() : wxObject() { m_FindFileHandler
= NULL
;}
159 virtual ~wxFileSystem() { }
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/")
167 void ChangePathTo(const wxString
& location
, bool is_dir
= false);
169 wxString
GetPath() const {return m_Path
;}
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
176 wxFSFile
* OpenFile(const wxString
& location
);
178 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
179 // the query to directories or wxFILE for files only or 0 for either.
180 // Returns filename or empty string if no more matching file exists
181 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
185 // In fact, this class is only front-end to the FS handlers :-)
186 static void AddHandler(wxFileSystemHandler
*handler
);
188 // remove all items from the m_Handlers list
189 static void CleanUpHandlers();
191 // Returns the native path for a file URL
192 static wxFileName
URLToFileName(const wxString
& url
);
194 // Returns the file URL for a native path
195 static wxString
FileNameToURL(const wxFileName
& filename
);
200 // the path (location) we are currently in
201 // this is path, not file!
202 // (so if you opened test/demo.htm, it is
203 // "test/", not "test/demo.htm")
205 // name of last opened file (full path)
206 static wxList m_Handlers
;
207 // list of FS handlers
208 wxFileSystemHandler
*m_FindFileHandler
;
209 // handler that succeed in FindFirst query
211 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
212 DECLARE_NO_COPY_CLASS(wxFileSystem
)
220 To determine FS type, we're using standard KDE notation:
221 file:/absolute/path/file.htm
222 file:relative_path/xxxxx.html
223 /some/path/x.file ('file:' is default)
225 file:subdir/archive.tar.gz#tar:/README.txt
228 ':' - FS identificator is before this char
229 '#' - separator. It can be either HTML anchor ("index.html#news")
230 (in case there is no ':' in the string to the right from it)
232 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
233 this would access tgz archive stored on web)
234 '/' - directory (path) separator. It is used to determine upper-level path.
235 HEY! Don't use \ even if you're on Windows!
240 class WXDLLIMPEXP_BASE wxLocalFSHandler
: public wxFileSystemHandler
243 virtual bool CanOpen(const wxString
& location
);
244 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
245 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
246 virtual wxString
FindNext();
248 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
249 // files on disk. This effectively makes 'root' the top-level directory
250 // and prevents access to files outside this directory.
251 // (This is similar to Unix command 'chroot'.)
252 static void Chroot(const wxString
& root
) { ms_root
= root
; }
255 static wxString ms_root
;