]>
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 | |
03402e29 | 59 | virtual ~wxFSFile() { delete m_Stream; } |
19008b7b | 60 | |
03402e29 MW |
61 | // returns stream. This doesn't give away ownership of the stream object. |
62 | wxInputStream *GetStream() const { return m_Stream; } | |
19008b7b | 63 | |
03402e29 MW |
64 | // gives away the ownership of the current stream. |
65 | wxInputStream *DetachStream() | |
66 | { | |
67 | wxInputStream *stream = m_Stream; | |
68 | m_Stream = NULL; | |
69 | return stream; | |
70 | } | |
71 | ||
72 | // deletes the current stream and takes ownership of another. | |
73 | void SetStream(wxInputStream *stream) | |
74 | { | |
75 | delete m_Stream; | |
76 | m_Stream = stream; | |
77 | } | |
84d1cd43 | 78 | |
19008b7b | 79 | // returns file's mime type |
03402e29 | 80 | const wxString& GetMimeType() const { return m_MimeType; } |
19008b7b VS |
81 | |
82 | // returns the original location (aka filename) of the file | |
03402e29 | 83 | const wxString& GetLocation() const { return m_Location; } |
19008b7b | 84 | |
03402e29 | 85 | const wxString& GetAnchor() const { return m_Anchor; } |
19008b7b | 86 | |
e2b87f38 | 87 | #if wxUSE_DATETIME |
03402e29 | 88 | wxDateTime GetModificationTime() const { return m_Modif; } |
e2b87f38 | 89 | #endif // wxUSE_DATETIME |
19008b7b VS |
90 | |
91 | private: | |
92 | wxInputStream *m_Stream; | |
93 | wxString m_Location; | |
94 | wxString m_MimeType; | |
95 | wxString m_Anchor; | |
e2b87f38 | 96 | #if wxUSE_DATETIME |
19008b7b | 97 | wxDateTime m_Modif; |
e2b87f38 | 98 | #endif // wxUSE_DATETIME |
46837272 RD |
99 | |
100 | DECLARE_ABSTRACT_CLASS(wxFSFile) | |
22f3361e | 101 | DECLARE_NO_COPY_CLASS(wxFSFile) |
5526e819 VS |
102 | }; |
103 | ||
104 | ||
105 | ||
106 | ||
107 | ||
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 | //-------------------------------------------------------------------------------- | |
114 | ||
bddd7a8d | 115 | class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject |
5526e819 | 116 | { |
19008b7b VS |
117 | public: |
118 | wxFileSystemHandler() : wxObject() {} | |
119 | ||
a62848fd | 120 | // returns true if this handler is able to open given location |
19008b7b VS |
121 | virtual bool CanOpen(const wxString& location) = 0; |
122 | ||
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; | |
127 | ||
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(); | |
133 | ||
134 | protected: | |
135 | // returns protocol ("file", "http", "tar" etc.) The last (most right) | |
136 | // protocol is used: | |
137 | // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
138 | wxString GetProtocol(const wxString& location) const; | |
139 | ||
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; | |
143 | ||
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; | |
148 | ||
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; | |
152 | ||
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); | |
5526e819 | 156 | |
19008b7b | 157 | DECLARE_ABSTRACT_CLASS(wxFileSystemHandler) |
5526e819 VS |
158 | }; |
159 | ||
160 | ||
161 | ||
162 | ||
163 | //-------------------------------------------------------------------------------- | |
164 | // wxFileSystem | |
165 | // This class provides simple interface for opening various | |
166 | // kinds of files (HTPP, FTP, local, tar.gz etc..) | |
167 | //-------------------------------------------------------------------------------- | |
168 | ||
8c3dbc46 MW |
169 | // Open Bit Flags |
170 | enum { | |
171 | wxFS_READ = 1, // Open for reading | |
172 | wxFS_WRITE = 2, // Open for writing | |
173 | wxFS_SEEKABLE = 4 // Returned stream will be seekable | |
174 | }; | |
175 | ||
bddd7a8d | 176 | class WXDLLIMPEXP_BASE wxFileSystem : public wxObject |
5526e819 | 177 | { |
19008b7b | 178 | public: |
bf07249c VZ |
179 | wxFileSystem() : wxObject() { m_FindFileHandler = NULL;} |
180 | virtual ~wxFileSystem() { } | |
19008b7b VS |
181 | |
182 | // sets the current location. Every call to OpenFile is | |
183 | // relative to this location. | |
184 | // NOTE !! | |
a62848fd | 185 | // unless is_dir = true 'location' is *not* the directory but |
19008b7b VS |
186 | // file contained in this directory |
187 | // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/") | |
a62848fd | 188 | void ChangePathTo(const wxString& location, bool is_dir = false); |
19008b7b VS |
189 | |
190 | wxString GetPath() const {return m_Path;} | |
191 | ||
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 | |
196 | // path. | |
8c3dbc46 | 197 | wxFSFile* OpenFile(const wxString& location, int flags = wxFS_READ); |
19008b7b VS |
198 | |
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); | |
203 | wxString FindNext(); | |
204 | ||
3ab6fcee VZ |
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); | |
207 | ||
19008b7b | 208 | // Adds FS handler. |
43e8916f | 209 | // In fact, this class is only front-end to the FS handlers :-) |
19008b7b VS |
210 | static void AddHandler(wxFileSystemHandler *handler); |
211 | ||
5949d307 RR |
212 | // Removes FS handler |
213 | static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler); | |
214 | ||
b8b37ced VZ |
215 | // Returns true if there is a handler which can open the given location. |
216 | static bool HasHandlerForPath(const wxString& location); | |
217 | ||
19008b7b VS |
218 | // remove all items from the m_Handlers list |
219 | static void CleanUpHandlers(); | |
220 | ||
2b5f62a0 | 221 | // Returns the native path for a file URL |
9548c49a | 222 | static wxFileName URLToFileName(const wxString& url); |
2b5f62a0 VZ |
223 | |
224 | // Returns the file URL for a native path | |
9548c49a | 225 | static wxString FileNameToURL(const wxFileName& filename); |
2b5f62a0 VZ |
226 | |
227 | ||
19008b7b VS |
228 | protected: |
229 | wxString m_Path; | |
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") | |
234 | wxString m_LastName; | |
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 | |
5526e819 | 240 | |
19008b7b | 241 | DECLARE_DYNAMIC_CLASS(wxFileSystem) |
22f3361e | 242 | DECLARE_NO_COPY_CLASS(wxFileSystem) |
5526e819 VS |
243 | }; |
244 | ||
245 | ||
246 | /* | |
247 | ||
248 | 'location' syntax: | |
249 | ||
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) | |
254 | http://www.gnome.org | |
255 | file:subdir/archive.tar.gz#tar:/README.txt | |
256 | ||
257 | special characters : | |
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) | |
261 | or FS separator | |
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! | |
266 | ||
267 | */ | |
268 | ||
19008b7b | 269 | |
bddd7a8d | 270 | class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler |
19008b7b VS |
271 | { |
272 | public: | |
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(); | |
46837272 | 277 | |
19008b7b VS |
278 | // wxLocalFSHandler will prefix all filenames with 'root' before accessing |
279 | // files on disk. This effectively makes 'root' the top-level directory | |
46837272 | 280 | // and prevents access to files outside this directory. |
19008b7b VS |
281 | // (This is similar to Unix command 'chroot'.) |
282 | static void Chroot(const wxString& root) { ms_root = root; } | |
46837272 | 283 | |
19008b7b VS |
284 | protected: |
285 | static wxString ms_root; | |
286 | }; | |
287 | ||
288 | ||
289 | ||
d30e0edd | 290 | #endif |
24528b0c | 291 | // wxUSE_FILESYSTEM |
d30e0edd | 292 | |
269e8200 | 293 | #endif |
d30e0edd | 294 | // __FILESYS_H__ |