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 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(__APPLE__)
14 #pragma interface "filesys.h"
20 #error You cannot compile virtual file systems without wxUSE_STREAMS
23 #if wxUSE_HTML && !wxUSE_FILESYSTEM
24 #error You cannot compile wxHTML without virtual file systems
29 #include "wx/stream.h"
30 #include "wx/datetime.h"
31 #include "wx/filename.h"
34 class wxFileSystemHandler
;
37 //--------------------------------------------------------------------------------
39 // This class is a file opened using wxFileSystem. It consists of
40 // input stream, location, mime type & optional anchor
41 // (in 'index.htm#chapter2', 'chapter2' is anchor)
42 //--------------------------------------------------------------------------------
44 class WXDLLIMPEXP_BASE wxFSFile
: public wxObject
47 wxFSFile(wxInputStream
*stream
, const wxString
& loc
,
48 const wxString
& mimetype
, const wxString
& anchor
51 #endif // wxUSE_DATETIME
56 m_MimeType
= mimetype
; m_MimeType
.MakeLower();
60 #endif // wxUSE_DATETIME
63 virtual ~wxFSFile() { if (m_Stream
) delete m_Stream
; }
65 // returns stream. This doesn't _create_ stream, it only returns
67 wxInputStream
*GetStream() const {return m_Stream
;}
69 // returns file's mime type
70 const wxString
& GetMimeType() const {return m_MimeType
;}
72 // returns the original location (aka filename) of the file
73 const wxString
& GetLocation() const {return m_Location
;}
75 const wxString
& GetAnchor() const {return m_Anchor
;}
78 wxDateTime
GetModificationTime() const {return m_Modif
;}
79 #endif // wxUSE_DATETIME
82 wxInputStream
*m_Stream
;
88 #endif // wxUSE_DATETIME
90 DECLARE_ABSTRACT_CLASS(wxFSFile
)
91 DECLARE_NO_COPY_CLASS(wxFSFile
)
98 //--------------------------------------------------------------------------------
99 // wxFileSystemHandler
100 // This class is FS handler for wxFileSystem. It provides
101 // interface to access certain
102 // kinds of files (HTPP, FTP, local, tar.gz etc..)
103 //--------------------------------------------------------------------------------
105 class WXDLLIMPEXP_BASE wxFileSystemHandler
: public wxObject
108 wxFileSystemHandler() : wxObject() {}
110 // returns TRUE if this handler is able to open given location
111 virtual bool CanOpen(const wxString
& location
) = 0;
113 // opens given file and returns pointer to input stream.
114 // Returns NULL if opening failed.
115 // The location is always absolute path.
116 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
) = 0;
118 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
119 // the query to directories or wxFILE for files only or 0 for either.
120 // Returns filename or empty string if no more matching file exists
121 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
122 virtual wxString
FindNext();
125 // returns protocol ("file", "http", "tar" etc.) The last (most right)
127 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
128 wxString
GetProtocol(const wxString
& location
) const;
130 // returns left part of address:
131 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
132 wxString
GetLeftLocation(const wxString
& location
) const;
134 // returns anchor part of address:
135 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
136 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
137 wxString
GetAnchor(const wxString
& location
) const;
139 // returns right part of address:
140 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
141 wxString
GetRightLocation(const wxString
& location
) const;
143 // Returns MIME type of the file - w/o need to open it
144 // (default behaviour is that it returns type based on extension)
145 wxString
GetMimeTypeFromExt(const wxString
& location
);
147 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler
)
153 //--------------------------------------------------------------------------------
155 // This class provides simple interface for opening various
156 // kinds of files (HTPP, FTP, local, tar.gz etc..)
157 //--------------------------------------------------------------------------------
159 class WXDLLIMPEXP_BASE wxFileSystem
: public wxObject
162 wxFileSystem() : wxObject() { m_FindFileHandler
= NULL
;}
163 virtual ~wxFileSystem() { }
165 // sets the current location. Every call to OpenFile is
166 // relative to this location.
168 // unless is_dir = TRUE 'location' is *not* the directory but
169 // file contained in this directory
170 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
171 void ChangePathTo(const wxString
& location
, bool is_dir
= FALSE
);
173 wxString
GetPath() const {return m_Path
;}
175 // opens given file and returns pointer to input stream.
176 // Returns NULL if opening failed.
177 // It first tries to open the file in relative scope
178 // (based on ChangePathTo()'s value) and then as an absolute
180 wxFSFile
* OpenFile(const wxString
& location
);
182 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
183 // the query to directories or wxFILE for files only or 0 for either.
184 // Returns filename or empty string if no more matching file exists
185 wxString
FindFirst(const wxString
& spec
, int flags
= 0);
189 // In fact, this class is only front-end to the FS hanlers :-)
190 static void AddHandler(wxFileSystemHandler
*handler
);
192 // remove all items from the m_Handlers list
193 static void CleanUpHandlers();
195 // Returns the native path for a file URL
196 static wxFileName
URLToFileName(const wxString
& url
);
198 // Returns the file URL for a native path
199 static wxString
FileNameToURL(const wxFileName
& filename
);
204 // the path (location) we are currently in
205 // this is path, not file!
206 // (so if you opened test/demo.htm, it is
207 // "test/", not "test/demo.htm")
209 // name of last opened file (full path)
210 static wxList m_Handlers
;
211 // list of FS handlers
212 wxFileSystemHandler
*m_FindFileHandler
;
213 // handler that succeed in FindFirst query
215 DECLARE_DYNAMIC_CLASS(wxFileSystem
)
216 DECLARE_NO_COPY_CLASS(wxFileSystem
)
224 To determine FS type, we're using standard KDE notation:
225 file:/absolute/path/file.htm
226 file:relative_path/xxxxx.html
227 /some/path/x.file ('file:' is default)
229 file:subdir/archive.tar.gz#tar:/README.txt
232 ':' - FS identificator is before this char
233 '#' - separator. It can be either HTML anchor ("index.html#news")
234 (in case there is no ':' in the string to the right from it)
236 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
237 this would access tgz archive stored on web)
238 '/' - directory (path) separator. It is used to determine upper-level path.
239 HEY! Don't use \ even if you're on Windows!
244 class WXDLLIMPEXP_BASE wxLocalFSHandler
: public wxFileSystemHandler
247 virtual bool CanOpen(const wxString
& location
);
248 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
249 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
250 virtual wxString
FindNext();
252 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
253 // files on disk. This effectively makes 'root' the top-level directory
254 // and prevents access to files outside this directory.
255 // (This is similar to Unix command 'chroot'.)
256 static void Chroot(const wxString
& root
) { ms_root
= root
; }
259 static wxString ms_root
;