]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: filesys.h | |
3 | // Purpose: class for opening files - virtual file system | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // RCS-ID: $Id$ | |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifndef __FILESYS_H__ | |
11 | #define __FILESYS_H__ | |
12 | ||
13 | #if defined(__GNUG__) && !defined(__APPLE__) | |
14 | #pragma interface "filesys.h" | |
15 | #endif | |
16 | ||
17 | #include "wx/setup.h" | |
18 | ||
19 | #if !wxUSE_STREAMS | |
20 | #error You cannot compile virtual file systems without wxUSE_STREAMS | |
21 | #endif | |
22 | ||
23 | #if wxUSE_HTML && !wxUSE_FILESYSTEM | |
24 | #error You cannot compile wxHTML without virtual file systems | |
25 | #endif | |
26 | ||
27 | #if wxUSE_FILESYSTEM | |
28 | ||
29 | #include "wx/stream.h" | |
30 | #include "wx/url.h" | |
31 | #include "wx/datetime.h" | |
32 | #include "wx/filename.h" | |
33 | ||
34 | class wxFSFile; | |
35 | class wxFileSystemHandler; | |
36 | class wxFileSystem; | |
37 | ||
38 | //-------------------------------------------------------------------------------- | |
39 | // wxFSFile | |
40 | // This class is a file opened using wxFileSystem. It consists of | |
41 | // input stream, location, mime type & optional anchor | |
42 | // (in 'index.htm#chapter2', 'chapter2' is anchor) | |
43 | //-------------------------------------------------------------------------------- | |
44 | ||
45 | class WXDLLIMPEXP_BASE wxFSFile : public wxObject | |
46 | { | |
47 | public: | |
48 | wxFSFile(wxInputStream *stream, const wxString& loc, | |
49 | const wxString& mimetype, const wxString& anchor | |
50 | #if wxUSE_DATETIME | |
51 | , wxDateTime modif | |
52 | #endif // wxUSE_DATETIME | |
53 | ) | |
54 | { | |
55 | m_Stream = stream; | |
56 | m_Location = loc; | |
57 | m_MimeType = mimetype; m_MimeType.MakeLower(); | |
58 | m_Anchor = anchor; | |
59 | #if wxUSE_DATETIME | |
60 | m_Modif = modif; | |
61 | #endif // wxUSE_DATETIME | |
62 | } | |
63 | ||
64 | virtual ~wxFSFile() { if (m_Stream) delete m_Stream; } | |
65 | ||
66 | // returns stream. This doesn't _create_ stream, it only returns | |
67 | // pointer to it. | |
68 | wxInputStream *GetStream() const {return m_Stream;} | |
69 | ||
70 | // returns file's mime type | |
71 | const wxString& GetMimeType() const {return m_MimeType;} | |
72 | ||
73 | // returns the original location (aka filename) of the file | |
74 | const wxString& GetLocation() const {return m_Location;} | |
75 | ||
76 | const wxString& GetAnchor() const {return m_Anchor;} | |
77 | ||
78 | #if wxUSE_DATETIME | |
79 | wxDateTime GetModificationTime() const {return m_Modif;} | |
80 | #endif // wxUSE_DATETIME | |
81 | ||
82 | private: | |
83 | wxInputStream *m_Stream; | |
84 | wxString m_Location; | |
85 | wxString m_MimeType; | |
86 | wxString m_Anchor; | |
87 | #if wxUSE_DATETIME | |
88 | wxDateTime m_Modif; | |
89 | #endif // wxUSE_DATETIME | |
90 | ||
91 | DECLARE_ABSTRACT_CLASS(wxFSFile) | |
92 | DECLARE_NO_COPY_CLASS(wxFSFile) | |
93 | }; | |
94 | ||
95 | ||
96 | ||
97 | ||
98 | ||
99 | //-------------------------------------------------------------------------------- | |
100 | // wxFileSystemHandler | |
101 | // This class is FS handler for wxFileSystem. It provides | |
102 | // interface to access certain | |
103 | // kinds of files (HTPP, FTP, local, tar.gz etc..) | |
104 | //-------------------------------------------------------------------------------- | |
105 | ||
106 | class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject | |
107 | { | |
108 | public: | |
109 | wxFileSystemHandler() : wxObject() {} | |
110 | ||
111 | // returns TRUE if this handler is able to open given location | |
112 | virtual bool CanOpen(const wxString& location) = 0; | |
113 | ||
114 | // opens given file and returns pointer to input stream. | |
115 | // Returns NULL if opening failed. | |
116 | // The location is always absolute path. | |
117 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0; | |
118 | ||
119 | // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting | |
120 | // the query to directories or wxFILE for files only or 0 for either. | |
121 | // Returns filename or empty string if no more matching file exists | |
122 | virtual wxString FindFirst(const wxString& spec, int flags = 0); | |
123 | virtual wxString FindNext(); | |
124 | ||
125 | protected: | |
126 | // returns protocol ("file", "http", "tar" etc.) The last (most right) | |
127 | // protocol is used: | |
128 | // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
129 | wxString GetProtocol(const wxString& location) const; | |
130 | ||
131 | // returns left part of address: | |
132 | // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
133 | wxString GetLeftLocation(const wxString& location) const; | |
134 | ||
135 | // returns anchor part of address: | |
136 | // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"} | |
137 | // NOTE: anchor is NOT a part of GetLeftLocation()'s return value | |
138 | wxString GetAnchor(const wxString& location) const; | |
139 | ||
140 | // returns right part of address: | |
141 | // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
142 | wxString GetRightLocation(const wxString& location) const; | |
143 | ||
144 | // Returns MIME type of the file - w/o need to open it | |
145 | // (default behaviour is that it returns type based on extension) | |
146 | wxString GetMimeTypeFromExt(const wxString& location); | |
147 | ||
148 | DECLARE_ABSTRACT_CLASS(wxFileSystemHandler) | |
149 | }; | |
150 | ||
151 | ||
152 | ||
153 | ||
154 | //-------------------------------------------------------------------------------- | |
155 | // wxFileSystem | |
156 | // This class provides simple interface for opening various | |
157 | // kinds of files (HTPP, FTP, local, tar.gz etc..) | |
158 | //-------------------------------------------------------------------------------- | |
159 | ||
160 | class WXDLLIMPEXP_BASE wxFileSystem : public wxObject | |
161 | { | |
162 | public: | |
163 | wxFileSystem() : wxObject() {m_Path = m_LastName = wxEmptyString; m_FindFileHandler = NULL;} | |
164 | ~wxFileSystem() { WX_CLEAR_LIST(wxList, m_Handlers); } | |
165 | ||
166 | // sets the current location. Every call to OpenFile is | |
167 | // relative to this location. | |
168 | // NOTE !! | |
169 | // unless is_dir = TRUE 'location' is *not* the directory but | |
170 | // file contained in this directory | |
171 | // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/") | |
172 | void ChangePathTo(const wxString& location, bool is_dir = FALSE); | |
173 | ||
174 | wxString GetPath() const {return m_Path;} | |
175 | ||
176 | // opens given file and returns pointer to input stream. | |
177 | // Returns NULL if opening failed. | |
178 | // It first tries to open the file in relative scope | |
179 | // (based on ChangePathTo()'s value) and then as an absolute | |
180 | // path. | |
181 | wxFSFile* OpenFile(const wxString& location); | |
182 | ||
183 | // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting | |
184 | // the query to directories or wxFILE for files only or 0 for either. | |
185 | // Returns filename or empty string if no more matching file exists | |
186 | wxString FindFirst(const wxString& spec, int flags = 0); | |
187 | wxString FindNext(); | |
188 | ||
189 | // Adds FS handler. | |
190 | // In fact, this class is only front-end to the FS hanlers :-) | |
191 | static void AddHandler(wxFileSystemHandler *handler); | |
192 | ||
193 | // remove all items from the m_Handlers list | |
194 | static void CleanUpHandlers(); | |
195 | ||
196 | // Returns the native path for a file URL | |
197 | static wxFileName URLToFileName(const wxString& url); | |
198 | ||
199 | // Returns the file URL for a native path | |
200 | static wxString FileNameToURL(const wxFileName& filename); | |
201 | ||
202 | ||
203 | protected: | |
204 | wxString m_Path; | |
205 | // the path (location) we are currently in | |
206 | // this is path, not file! | |
207 | // (so if you opened test/demo.htm, it is | |
208 | // "test/", not "test/demo.htm") | |
209 | wxString m_LastName; | |
210 | // name of last opened file (full path) | |
211 | static wxList m_Handlers; | |
212 | // list of FS handlers | |
213 | wxFileSystemHandler *m_FindFileHandler; | |
214 | // handler that succeed in FindFirst query | |
215 | ||
216 | DECLARE_DYNAMIC_CLASS(wxFileSystem) | |
217 | DECLARE_NO_COPY_CLASS(wxFileSystem) | |
218 | }; | |
219 | ||
220 | ||
221 | /* | |
222 | ||
223 | 'location' syntax: | |
224 | ||
225 | To determine FS type, we're using standard KDE notation: | |
226 | file:/absolute/path/file.htm | |
227 | file:relative_path/xxxxx.html | |
228 | /some/path/x.file ('file:' is default) | |
229 | http://www.gnome.org | |
230 | file:subdir/archive.tar.gz#tar:/README.txt | |
231 | ||
232 | special characters : | |
233 | ':' - FS identificator is before this char | |
234 | '#' - separator. It can be either HTML anchor ("index.html#news") | |
235 | (in case there is no ':' in the string to the right from it) | |
236 | or FS separator | |
237 | (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h" | |
238 | this would access tgz archive stored on web) | |
239 | '/' - directory (path) separator. It is used to determine upper-level path. | |
240 | HEY! Don't use \ even if you're on Windows! | |
241 | ||
242 | */ | |
243 | ||
244 | ||
245 | class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler | |
246 | { | |
247 | public: | |
248 | virtual bool CanOpen(const wxString& location); | |
249 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
250 | virtual wxString FindFirst(const wxString& spec, int flags = 0); | |
251 | virtual wxString FindNext(); | |
252 | ||
253 | // wxLocalFSHandler will prefix all filenames with 'root' before accessing | |
254 | // files on disk. This effectively makes 'root' the top-level directory | |
255 | // and prevents access to files outside this directory. | |
256 | // (This is similar to Unix command 'chroot'.) | |
257 | static void Chroot(const wxString& root) { ms_root = root; } | |
258 | ||
259 | protected: | |
260 | static wxString ms_root; | |
261 | }; | |
262 | ||
263 | ||
264 | ||
265 | #endif | |
266 | // wxUSE_FILESYSTEM | |
267 | ||
268 | #endif | |
269 | // __FILESYS_H__ |