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() { delete m_Stream
; }
61 // returns stream. This doesn't give away ownership of the stream object.
62 wxInputStream
*GetStream() const { return m_Stream
; }
64 // gives away the ownership of the current stream.
65 wxInputStream
*DetachStream()
67 wxInputStream
*stream
= m_Stream
;
72 // deletes the current stream and takes ownership of another.
73 void SetStream(wxInputStream
*stream
)
79 // returns file's mime type
80 const wxString
& GetMimeType() const { return m_MimeType
; }
82 // returns the original location (aka filename) of the file
83 const wxString
& GetLocation() const { return m_Location
; }
85 const wxString
& GetAnchor() const { return m_Anchor
; }
88 wxDateTime
GetModificationTime() const { return m_Modif
; }
89 #endif // wxUSE_DATETIME
92 wxInputStream
*m_Stream
;
98 #endif // wxUSE_DATETIME
100 DECLARE_ABSTRACT_CLASS(wxFSFile
)
101 DECLARE_NO_COPY_CLASS(wxFSFile
)
108 //--------------------------------------------------------------------------------
109 // wxFileSystemHandler
110 // This class is FS handler for wxFileSystem. It provides
111 // interface to access certain
112 // kinds of files (HTPP, FTP, local, tar.gz etc..)
113 //--------------------------------------------------------------------------------
115 class WXDLLIMPEXP_BASE wxFileSystemHandler
: public wxObject
118 wxFileSystemHandler() : wxObject() {}
120 // returns true if this handler is able to open given location
121 virtual bool CanOpen(const wxString
& location
) = 0;
123 // opens given file and returns pointer to input stream.
124 // Returns NULL if opening failed.
125 // The location is always absolute path.
126 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
128 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
129 // the query to directories or wxFILE for files only or 0 for either.
130 // Returns filename or empty string if no more matching file exists
131 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
132 virtual wxString
FindNext();
135 // returns protocol ("file", "http", "tar" etc.) The last (most right)
137 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
138 wxString
GetProtocol(const wxString
& location
) const;
140 // returns left part of address:
141 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
142 wxString
GetLeftLocation(const wxString
& location
) const;
144 // returns anchor part of address:
145 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
146 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
147 wxString
GetAnchor(const wxString
& location
) const;
149 // returns right part of address:
150 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
151 wxString
GetRightLocation(const wxString
& location
) const;
153 // Returns MIME type of the file - w/o need to open it
154 // (default behaviour is that it returns type based on extension)
155 wxString
GetMimeTypeFromExt(const wxString
& location
);
157 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
163 //--------------------------------------------------------------------------------
165 // This class provides simple interface for opening various
166 // kinds of files (HTPP, FTP, local, tar.gz etc..)
167 //--------------------------------------------------------------------------------
171 wxFS_READ
= 1, // Open for reading
172 wxFS_WRITE
= 2, // Open for writing
173 wxFS_SEEKABLE
= 4 // Returned stream will be seekable
176 class WXDLLIMPEXP_BASE wxFileSystem
: public wxObject
179 wxFileSystem() : wxObject() { m_FindFileHandler
= NULL
;}
180 virtual ~wxFileSystem() { }
182 // sets the current location. Every call to OpenFile is
183 // relative to this location.
185 // unless is_dir = true 'location' is *not* the directory but
186 // file contained in this directory
187 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
188 void ChangePathTo(const wxString
& location
, bool is_dir
= false);
190 wxString
GetPath() const {return m_Path
;}
192 // opens given file and returns pointer to input stream.
193 // Returns NULL if opening failed.
194 // It first tries to open the file in relative scope
195 // (based on ChangePathTo()'s value) and then as an absolute
197 wxFSFile
* OpenFile(const wxString
& location
, int flags
= wxFS_READ
);
199 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
200 // the query to directories or wxFILE for files only or 0 for either.
201 // Returns filename or empty string if no more matching file exists
202 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
205 // find a file in a list of directories, returns false if not found
206 bool FindFileInPath(wxString
*pStr
, const wxChar
*path
, const wxChar
*file
);
209 // In fact, this class is only front-end to the FS handlers :-)
210 static void AddHandler(wxFileSystemHandler
*handler
);
212 // Removes FS handler
213 static wxFileSystemHandler
* RemoveHandler(wxFileSystemHandler
*handler
);
215 // Returns true if there is a handler which can open the given location.
216 static bool HasHandlerForPath(const wxString
& location
);
218 // remove all items from the m_Handlers list
219 static void CleanUpHandlers();
221 // Returns the native path for a file URL
222 static wxFileName
URLToFileName(const wxString
& url
);
224 // Returns the file URL for a native path
225 static wxString
FileNameToURL(const wxFileName
& filename
);
230 // the path (location) we are currently in
231 // this is path, not file!
232 // (so if you opened test/demo.htm, it is
233 // "test/", not "test/demo.htm")
235 // name of last opened file (full path)
236 static wxList m_Handlers
;
237 // list of FS handlers
238 wxFileSystemHandler
*m_FindFileHandler
;
239 // handler that succeed in FindFirst query
241 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
242 DECLARE_NO_COPY_CLASS(wxFileSystem
)
250 To determine FS type, we're using standard KDE notation:
251 file:/absolute/path/file.htm
252 file:relative_path/xxxxx.html
253 /some/path/x.file ('file:' is default)
255 file:subdir/archive.tar.gz#tar:/README.txt
258 ':' - FS identificator is before this char
259 '#' - separator. It can be either HTML anchor ("index.html#news")
260 (in case there is no ':' in the string to the right from it)
262 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
263 this would access tgz archive stored on web)
264 '/' - directory (path) separator. It is used to determine upper-level path.
265 HEY! Don't use \ even if you're on Windows!
270 class WXDLLIMPEXP_BASE wxLocalFSHandler
: public wxFileSystemHandler
273 virtual bool CanOpen(const wxString
& location
);
274 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
275 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
276 virtual wxString
FindNext();
278 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
279 // files on disk. This effectively makes 'root' the top-level directory
280 // and prevents access to files outside this directory.
281 // (This is similar to Unix command 'chroot'.)
282 static void Chroot(const wxString
& root
) { ms_root
= root
; }
285 static wxString ms_root
;