]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filesys.h
Detabified
[wxWidgets.git] / include / wx / filesys.h
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 #include "wx/defs.h"
14
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
24
25 #include "wx/stream.h"
26 #include "wx/datetime.h"
27 #include "wx/filename.h"
28
29 class WXDLLIMPEXP_BASE wxFSFile;
30 class WXDLLIMPEXP_BASE wxFileSystemHandler;
31 class WXDLLIMPEXP_BASE wxFileSystem;
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
40 class WXDLLIMPEXP_BASE wxFSFile : public wxObject
41 {
42 public:
43 wxFSFile(wxInputStream *stream, const wxString& loc,
44 const wxString& mimetype, const wxString& anchor
45 #if wxUSE_DATETIME
46 , wxDateTime modif
47 #endif // wxUSE_DATETIME
48 )
49 {
50 m_Stream = stream;
51 m_Location = loc;
52 m_MimeType = mimetype; m_MimeType.MakeLower();
53 m_Anchor = anchor;
54 #if wxUSE_DATETIME
55 m_Modif = modif;
56 #endif // wxUSE_DATETIME
57 }
58
59 virtual ~wxFSFile() { delete m_Stream; }
60
61 // returns stream. This doesn't give away ownership of the stream object.
62 wxInputStream *GetStream() const { return m_Stream; }
63
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 }
78
79 // returns file's mime type
80 const wxString& GetMimeType() const { return m_MimeType; }
81
82 // returns the original location (aka filename) of the file
83 const wxString& GetLocation() const { return m_Location; }
84
85 const wxString& GetAnchor() const { return m_Anchor; }
86
87 #if wxUSE_DATETIME
88 wxDateTime GetModificationTime() const { return m_Modif; }
89 #endif // wxUSE_DATETIME
90
91 private:
92 wxInputStream *m_Stream;
93 wxString m_Location;
94 wxString m_MimeType;
95 wxString m_Anchor;
96 #if wxUSE_DATETIME
97 wxDateTime m_Modif;
98 #endif // wxUSE_DATETIME
99
100 DECLARE_ABSTRACT_CLASS(wxFSFile)
101 DECLARE_NO_COPY_CLASS(wxFSFile)
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
115 class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject
116 {
117 public:
118 wxFileSystemHandler() : wxObject() {}
119
120 // returns true if this handler is able to open given location
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);
156
157 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler)
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
169 class WXDLLIMPEXP_BASE wxFileSystem : public wxObject
170 {
171 public:
172 wxFileSystem() : wxObject() { m_FindFileHandler = NULL;}
173 virtual ~wxFileSystem() { }
174
175 // sets the current location. Every call to OpenFile is
176 // relative to this location.
177 // NOTE !!
178 // unless is_dir = true 'location' is *not* the directory but
179 // file contained in this directory
180 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
181 void ChangePathTo(const wxString& location, bool is_dir = false);
182
183 wxString GetPath() const {return m_Path;}
184
185 // opens given file and returns pointer to input stream.
186 // Returns NULL if opening failed.
187 // It first tries to open the file in relative scope
188 // (based on ChangePathTo()'s value) and then as an absolute
189 // path.
190 wxFSFile* OpenFile(const wxString& location);
191
192 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
193 // the query to directories or wxFILE for files only or 0 for either.
194 // Returns filename or empty string if no more matching file exists
195 wxString FindFirst(const wxString& spec, int flags = 0);
196 wxString FindNext();
197
198 // find a file in a list of directories, returns false if not found
199 bool FindFileInPath(wxString *pStr, const wxChar *path, const wxChar *file);
200
201 // Adds FS handler.
202 // In fact, this class is only front-end to the FS handlers :-)
203 static void AddHandler(wxFileSystemHandler *handler);
204
205 // Removes FS handler
206 static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
207
208
209 // Returns true if there is a handler which can open the given location.
210 static bool HasHandlerForPath(const wxString& location);
211
212 // remove all items from the m_Handlers list
213 static void CleanUpHandlers();
214
215 // Returns the native path for a file URL
216 static wxFileName URLToFileName(const wxString& url);
217
218 // Returns the file URL for a native path
219 static wxString FileNameToURL(const wxFileName& filename);
220
221
222 protected:
223 wxString m_Path;
224 // the path (location) we are currently in
225 // this is path, not file!
226 // (so if you opened test/demo.htm, it is
227 // "test/", not "test/demo.htm")
228 wxString m_LastName;
229 // name of last opened file (full path)
230 static wxList m_Handlers;
231 // list of FS handlers
232 wxFileSystemHandler *m_FindFileHandler;
233 // handler that succeed in FindFirst query
234
235 DECLARE_DYNAMIC_CLASS(wxFileSystem)
236 DECLARE_NO_COPY_CLASS(wxFileSystem)
237 };
238
239
240 /*
241
242 'location' syntax:
243
244 To determine FS type, we're using standard KDE notation:
245 file:/absolute/path/file.htm
246 file:relative_path/xxxxx.html
247 /some/path/x.file ('file:' is default)
248 http://www.gnome.org
249 file:subdir/archive.tar.gz#tar:/README.txt
250
251 special characters :
252 ':' - FS identificator is before this char
253 '#' - separator. It can be either HTML anchor ("index.html#news")
254 (in case there is no ':' in the string to the right from it)
255 or FS separator
256 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
257 this would access tgz archive stored on web)
258 '/' - directory (path) separator. It is used to determine upper-level path.
259 HEY! Don't use \ even if you're on Windows!
260
261 */
262
263
264 class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler
265 {
266 public:
267 virtual bool CanOpen(const wxString& location);
268 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
269 virtual wxString FindFirst(const wxString& spec, int flags = 0);
270 virtual wxString FindNext();
271
272 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
273 // files on disk. This effectively makes 'root' the top-level directory
274 // and prevents access to files outside this directory.
275 // (This is similar to Unix command 'chroot'.)
276 static void Chroot(const wxString& root) { ms_root = root; }
277
278 protected:
279 static wxString ms_root;
280 };
281
282
283
284 #endif
285 // wxUSE_FILESYSTEM
286
287 #endif
288 // __FILESYS_H__