]>
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$ |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifndef __FILESYS_H__ | |
11 | #define __FILESYS_H__ | |
12 | ||
2ecf902b | 13 | #include "wx/defs.h" |
d30e0edd | 14 | |
24528b0c VS |
15 | #if !wxUSE_STREAMS |
16 | #error You cannot compile virtual file systems without wxUSE_STREAMS | |
17 | #endif | |
18 | ||
19 | #if wxUSE_HTML && !wxUSE_FILESYSTEM | |
20 | #error You cannot compile wxHTML without virtual file systems | |
21 | #endif | |
22 | ||
23 | #if wxUSE_FILESYSTEM | |
d30e0edd RR |
24 | |
25 | #include "wx/stream.h" | |
6ee654e6 | 26 | #include "wx/datetime.h" |
9548c49a | 27 | #include "wx/filename.h" |
5526e819 | 28 | |
446e5259 VS |
29 | class WXDLLIMPEXP_BASE wxFSFile; |
30 | class WXDLLIMPEXP_BASE wxFileSystemHandler; | |
31 | class WXDLLIMPEXP_BASE wxFileSystem; | |
5526e819 VS |
32 | |
33 | //-------------------------------------------------------------------------------- | |
34 | // wxFSFile | |
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 | //-------------------------------------------------------------------------------- | |
39 | ||
bddd7a8d | 40 | class WXDLLIMPEXP_BASE wxFSFile : public wxObject |
5526e819 | 41 | { |
19008b7b | 42 | public: |
46837272 | 43 | wxFSFile(wxInputStream *stream, const wxString& loc, |
e2b87f38 VZ |
44 | const wxString& mimetype, const wxString& anchor |
45 | #if wxUSE_DATETIME | |
46 | , wxDateTime modif | |
47 | #endif // wxUSE_DATETIME | |
48 | ) | |
19008b7b VS |
49 | { |
50 | m_Stream = stream; | |
51 | m_Location = loc; | |
52 | m_MimeType = mimetype; m_MimeType.MakeLower(); | |
53 | m_Anchor = anchor; | |
e2b87f38 | 54 | #if wxUSE_DATETIME |
19008b7b | 55 | m_Modif = modif; |
e2b87f38 | 56 | #endif // wxUSE_DATETIME |
19008b7b | 57 | } |
e2b87f38 | 58 | |
19008b7b VS |
59 | virtual ~wxFSFile() { if (m_Stream) delete m_Stream; } |
60 | ||
61 | // returns stream. This doesn't _create_ stream, it only returns | |
ae500232 | 62 | // pointer to it. |
19008b7b VS |
63 | wxInputStream *GetStream() const {return m_Stream;} |
64 | ||
65 | // returns file's mime type | |
66 | const wxString& GetMimeType() const {return m_MimeType;} | |
67 | ||
68 | // returns the original location (aka filename) of the file | |
69 | const wxString& GetLocation() const {return m_Location;} | |
70 | ||
71 | const wxString& GetAnchor() const {return m_Anchor;} | |
72 | ||
e2b87f38 | 73 | #if wxUSE_DATETIME |
19008b7b | 74 | wxDateTime GetModificationTime() const {return m_Modif;} |
e2b87f38 | 75 | #endif // wxUSE_DATETIME |
19008b7b VS |
76 | |
77 | private: | |
78 | wxInputStream *m_Stream; | |
79 | wxString m_Location; | |
80 | wxString m_MimeType; | |
81 | wxString m_Anchor; | |
e2b87f38 | 82 | #if wxUSE_DATETIME |
19008b7b | 83 | wxDateTime m_Modif; |
e2b87f38 | 84 | #endif // wxUSE_DATETIME |
46837272 RD |
85 | |
86 | DECLARE_ABSTRACT_CLASS(wxFSFile) | |
22f3361e | 87 | DECLARE_NO_COPY_CLASS(wxFSFile) |
5526e819 VS |
88 | }; |
89 | ||
90 | ||
91 | ||
92 | ||
93 | ||
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 | //-------------------------------------------------------------------------------- | |
100 | ||
bddd7a8d | 101 | class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject |
5526e819 | 102 | { |
19008b7b VS |
103 | public: |
104 | wxFileSystemHandler() : wxObject() {} | |
105 | ||
a62848fd | 106 | // returns true if this handler is able to open given location |
19008b7b VS |
107 | virtual bool CanOpen(const wxString& location) = 0; |
108 | ||
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; | |
113 | ||
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(); | |
119 | ||
120 | protected: | |
121 | // returns protocol ("file", "http", "tar" etc.) The last (most right) | |
122 | // protocol is used: | |
123 | // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
124 | wxString GetProtocol(const wxString& location) const; | |
125 | ||
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; | |
129 | ||
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; | |
134 | ||
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; | |
138 | ||
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); | |
5526e819 | 142 | |
19008b7b | 143 | DECLARE_ABSTRACT_CLASS(wxFileSystemHandler) |
5526e819 VS |
144 | }; |
145 | ||
146 | ||
147 | ||
148 | ||
149 | //-------------------------------------------------------------------------------- | |
150 | // wxFileSystem | |
151 | // This class provides simple interface for opening various | |
152 | // kinds of files (HTPP, FTP, local, tar.gz etc..) | |
153 | //-------------------------------------------------------------------------------- | |
154 | ||
bddd7a8d | 155 | class WXDLLIMPEXP_BASE wxFileSystem : public wxObject |
5526e819 | 156 | { |
19008b7b | 157 | public: |
bf07249c VZ |
158 | wxFileSystem() : wxObject() { m_FindFileHandler = NULL;} |
159 | virtual ~wxFileSystem() { } | |
19008b7b VS |
160 | |
161 | // sets the current location. Every call to OpenFile is | |
162 | // relative to this location. | |
163 | // NOTE !! | |
a62848fd | 164 | // unless is_dir = true 'location' is *not* the directory but |
19008b7b VS |
165 | // file contained in this directory |
166 | // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/") | |
a62848fd | 167 | void ChangePathTo(const wxString& location, bool is_dir = false); |
19008b7b VS |
168 | |
169 | wxString GetPath() const {return m_Path;} | |
170 | ||
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 | |
175 | // path. | |
176 | wxFSFile* OpenFile(const wxString& location); | |
177 | ||
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); | |
182 | wxString FindNext(); | |
183 | ||
3ab6fcee VZ |
184 | // find a file in a list of directories, returns false if not found |
185 | bool FindFileInPath(wxString *pStr, const wxChar *path, const wxChar *file); | |
186 | ||
19008b7b | 187 | // Adds FS handler. |
43e8916f | 188 | // In fact, this class is only front-end to the FS handlers :-) |
19008b7b VS |
189 | static void AddHandler(wxFileSystemHandler *handler); |
190 | ||
b8b37ced VZ |
191 | // Returns true if there is a handler which can open the given location. |
192 | static bool HasHandlerForPath(const wxString& location); | |
193 | ||
19008b7b VS |
194 | // remove all items from the m_Handlers list |
195 | static void CleanUpHandlers(); | |
196 | ||
2b5f62a0 | 197 | // Returns the native path for a file URL |
9548c49a | 198 | static wxFileName URLToFileName(const wxString& url); |
2b5f62a0 VZ |
199 | |
200 | // Returns the file URL for a native path | |
9548c49a | 201 | static wxString FileNameToURL(const wxFileName& filename); |
2b5f62a0 VZ |
202 | |
203 | ||
19008b7b VS |
204 | protected: |
205 | wxString m_Path; | |
206 | // the path (location) we are currently in | |
207 | // this is path, not file! | |
208 | // (so if you opened test/demo.htm, it is | |
209 | // "test/", not "test/demo.htm") | |
210 | wxString m_LastName; | |
211 | // name of last opened file (full path) | |
212 | static wxList m_Handlers; | |
213 | // list of FS handlers | |
214 | wxFileSystemHandler *m_FindFileHandler; | |
215 | // handler that succeed in FindFirst query | |
5526e819 | 216 | |
19008b7b | 217 | DECLARE_DYNAMIC_CLASS(wxFileSystem) |
22f3361e | 218 | DECLARE_NO_COPY_CLASS(wxFileSystem) |
5526e819 VS |
219 | }; |
220 | ||
221 | ||
222 | /* | |
223 | ||
224 | 'location' syntax: | |
225 | ||
226 | To determine FS type, we're using standard KDE notation: | |
227 | file:/absolute/path/file.htm | |
228 | file:relative_path/xxxxx.html | |
229 | /some/path/x.file ('file:' is default) | |
230 | http://www.gnome.org | |
231 | file:subdir/archive.tar.gz#tar:/README.txt | |
232 | ||
233 | special characters : | |
234 | ':' - FS identificator is before this char | |
235 | '#' - separator. It can be either HTML anchor ("index.html#news") | |
236 | (in case there is no ':' in the string to the right from it) | |
237 | or FS separator | |
238 | (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h" | |
239 | this would access tgz archive stored on web) | |
240 | '/' - directory (path) separator. It is used to determine upper-level path. | |
241 | HEY! Don't use \ even if you're on Windows! | |
242 | ||
243 | */ | |
244 | ||
19008b7b | 245 | |
bddd7a8d | 246 | class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler |
19008b7b VS |
247 | { |
248 | public: | |
249 | virtual bool CanOpen(const wxString& location); | |
250 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
251 | virtual wxString FindFirst(const wxString& spec, int flags = 0); | |
252 | virtual wxString FindNext(); | |
46837272 | 253 | |
19008b7b VS |
254 | // wxLocalFSHandler will prefix all filenames with 'root' before accessing |
255 | // files on disk. This effectively makes 'root' the top-level directory | |
46837272 | 256 | // and prevents access to files outside this directory. |
19008b7b VS |
257 | // (This is similar to Unix command 'chroot'.) |
258 | static void Chroot(const wxString& root) { ms_root = root; } | |
46837272 | 259 | |
19008b7b VS |
260 | protected: |
261 | static wxString ms_root; | |
262 | }; | |
263 | ||
264 | ||
265 | ||
d30e0edd | 266 | #endif |
24528b0c | 267 | // wxUSE_FILESYSTEM |
d30e0edd | 268 | |
269e8200 | 269 | #endif |
d30e0edd | 270 | // __FILESYS_H__ |