]>
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; | |
84d1cd43 | 51 | m_destroy = true; |
19008b7b VS |
52 | m_Location = loc; |
53 | m_MimeType = mimetype; m_MimeType.MakeLower(); | |
54 | m_Anchor = anchor; | |
e2b87f38 | 55 | #if wxUSE_DATETIME |
19008b7b | 56 | m_Modif = modif; |
e2b87f38 | 57 | #endif // wxUSE_DATETIME |
19008b7b | 58 | } |
e2b87f38 | 59 | |
84d1cd43 | 60 | virtual ~wxFSFile() { if (m_Stream && m_destroy) delete m_Stream; } |
19008b7b VS |
61 | |
62 | // returns stream. This doesn't _create_ stream, it only returns | |
ae500232 | 63 | // pointer to it. |
19008b7b VS |
64 | wxInputStream *GetStream() const {return m_Stream;} |
65 | ||
84d1cd43 RR |
66 | // gives away the ownership of stream |
67 | void DetachStream() { m_destroy = false; } | |
68 | ||
19008b7b VS |
69 | // returns file's mime type |
70 | const wxString& GetMimeType() const {return m_MimeType;} | |
71 | ||
72 | // returns the original location (aka filename) of the file | |
73 | const wxString& GetLocation() const {return m_Location;} | |
74 | ||
75 | const wxString& GetAnchor() const {return m_Anchor;} | |
76 | ||
e2b87f38 | 77 | #if wxUSE_DATETIME |
19008b7b | 78 | wxDateTime GetModificationTime() const {return m_Modif;} |
e2b87f38 | 79 | #endif // wxUSE_DATETIME |
19008b7b VS |
80 | |
81 | private: | |
82 | wxInputStream *m_Stream; | |
84d1cd43 | 83 | bool m_destroy; |
19008b7b VS |
84 | wxString m_Location; |
85 | wxString m_MimeType; | |
86 | wxString m_Anchor; | |
e2b87f38 | 87 | #if wxUSE_DATETIME |
19008b7b | 88 | wxDateTime m_Modif; |
e2b87f38 | 89 | #endif // wxUSE_DATETIME |
46837272 RD |
90 | |
91 | DECLARE_ABSTRACT_CLASS(wxFSFile) | |
22f3361e | 92 | DECLARE_NO_COPY_CLASS(wxFSFile) |
5526e819 VS |
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 | ||
bddd7a8d | 106 | class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject |
5526e819 | 107 | { |
19008b7b VS |
108 | public: |
109 | wxFileSystemHandler() : wxObject() {} | |
110 | ||
a62848fd | 111 | // returns true if this handler is able to open given location |
19008b7b VS |
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); | |
5526e819 | 147 | |
19008b7b | 148 | DECLARE_ABSTRACT_CLASS(wxFileSystemHandler) |
5526e819 VS |
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 | ||
bddd7a8d | 160 | class WXDLLIMPEXP_BASE wxFileSystem : public wxObject |
5526e819 | 161 | { |
19008b7b | 162 | public: |
bf07249c VZ |
163 | wxFileSystem() : wxObject() { m_FindFileHandler = NULL;} |
164 | virtual ~wxFileSystem() { } | |
19008b7b VS |
165 | |
166 | // sets the current location. Every call to OpenFile is | |
167 | // relative to this location. | |
168 | // NOTE !! | |
a62848fd | 169 | // unless is_dir = true 'location' is *not* the directory but |
19008b7b VS |
170 | // file contained in this directory |
171 | // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/") | |
a62848fd | 172 | void ChangePathTo(const wxString& location, bool is_dir = false); |
19008b7b VS |
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 | ||
3ab6fcee VZ |
189 | // find a file in a list of directories, returns false if not found |
190 | bool FindFileInPath(wxString *pStr, const wxChar *path, const wxChar *file); | |
191 | ||
19008b7b | 192 | // Adds FS handler. |
43e8916f | 193 | // In fact, this class is only front-end to the FS handlers :-) |
19008b7b VS |
194 | static void AddHandler(wxFileSystemHandler *handler); |
195 | ||
5949d307 RR |
196 | // Removes FS handler |
197 | static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler); | |
198 | ||
199 | ||
b8b37ced VZ |
200 | // Returns true if there is a handler which can open the given location. |
201 | static bool HasHandlerForPath(const wxString& location); | |
202 | ||
19008b7b VS |
203 | // remove all items from the m_Handlers list |
204 | static void CleanUpHandlers(); | |
205 | ||
2b5f62a0 | 206 | // Returns the native path for a file URL |
9548c49a | 207 | static wxFileName URLToFileName(const wxString& url); |
2b5f62a0 VZ |
208 | |
209 | // Returns the file URL for a native path | |
9548c49a | 210 | static wxString FileNameToURL(const wxFileName& filename); |
2b5f62a0 VZ |
211 | |
212 | ||
19008b7b VS |
213 | protected: |
214 | wxString m_Path; | |
215 | // the path (location) we are currently in | |
216 | // this is path, not file! | |
217 | // (so if you opened test/demo.htm, it is | |
218 | // "test/", not "test/demo.htm") | |
219 | wxString m_LastName; | |
220 | // name of last opened file (full path) | |
221 | static wxList m_Handlers; | |
222 | // list of FS handlers | |
223 | wxFileSystemHandler *m_FindFileHandler; | |
224 | // handler that succeed in FindFirst query | |
5526e819 | 225 | |
19008b7b | 226 | DECLARE_DYNAMIC_CLASS(wxFileSystem) |
22f3361e | 227 | DECLARE_NO_COPY_CLASS(wxFileSystem) |
5526e819 VS |
228 | }; |
229 | ||
230 | ||
231 | /* | |
232 | ||
233 | 'location' syntax: | |
234 | ||
235 | To determine FS type, we're using standard KDE notation: | |
236 | file:/absolute/path/file.htm | |
237 | file:relative_path/xxxxx.html | |
238 | /some/path/x.file ('file:' is default) | |
239 | http://www.gnome.org | |
240 | file:subdir/archive.tar.gz#tar:/README.txt | |
241 | ||
242 | special characters : | |
243 | ':' - FS identificator is before this char | |
244 | '#' - separator. It can be either HTML anchor ("index.html#news") | |
245 | (in case there is no ':' in the string to the right from it) | |
246 | or FS separator | |
247 | (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h" | |
248 | this would access tgz archive stored on web) | |
249 | '/' - directory (path) separator. It is used to determine upper-level path. | |
250 | HEY! Don't use \ even if you're on Windows! | |
251 | ||
252 | */ | |
253 | ||
19008b7b | 254 | |
bddd7a8d | 255 | class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler |
19008b7b VS |
256 | { |
257 | public: | |
258 | virtual bool CanOpen(const wxString& location); | |
259 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
260 | virtual wxString FindFirst(const wxString& spec, int flags = 0); | |
261 | virtual wxString FindNext(); | |
46837272 | 262 | |
19008b7b VS |
263 | // wxLocalFSHandler will prefix all filenames with 'root' before accessing |
264 | // files on disk. This effectively makes 'root' the top-level directory | |
46837272 | 265 | // and prevents access to files outside this directory. |
19008b7b VS |
266 | // (This is similar to Unix command 'chroot'.) |
267 | static void Chroot(const wxString& root) { ms_root = root; } | |
46837272 | 268 | |
19008b7b VS |
269 | protected: |
270 | static wxString ms_root; | |
271 | }; | |
272 | ||
273 | ||
274 | ||
d30e0edd | 275 | #endif |
24528b0c | 276 | // wxUSE_FILESYSTEM |
d30e0edd | 277 | |
269e8200 | 278 | #endif |
d30e0edd | 279 | // __FILESYS_H__ |