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