]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filesys.h
Add wxFS_SEEKABLE open flag.
[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 // 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
176 class WXDLLIMPEXP_BASE wxFileSystem : public wxObject
177 {
178 public:
179 wxFileSystem() : wxObject() { m_FindFileHandler = NULL;}
180 virtual ~wxFileSystem() { }
181
182 // sets the current location. Every call to OpenFile is
183 // relative to this location.
184 // NOTE !!
185 // unless is_dir = true 'location' is *not* the directory but
186 // file contained in this directory
187 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
188 void ChangePathTo(const wxString& location, bool is_dir = false);
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.
197 wxFSFile* OpenFile(const wxString& location, int flags = wxFS_READ);
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
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
208 // Adds FS handler.
209 // In fact, this class is only front-end to the FS handlers :-)
210 static void AddHandler(wxFileSystemHandler *handler);
211
212 // Removes FS handler
213 static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
214
215 // Returns true if there is a handler which can open the given location.
216 static bool HasHandlerForPath(const wxString& location);
217
218 // remove all items from the m_Handlers list
219 static void CleanUpHandlers();
220
221 // Returns the native path for a file URL
222 static wxFileName URLToFileName(const wxString& url);
223
224 // Returns the file URL for a native path
225 static wxString FileNameToURL(const wxFileName& filename);
226
227
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
240
241 DECLARE_DYNAMIC_CLASS(wxFileSystem)
242 DECLARE_NO_COPY_CLASS(wxFileSystem)
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
269
270 class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler
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();
277
278 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
279 // files on disk. This effectively makes 'root' the top-level directory
280 // and prevents access to files outside this directory.
281 // (This is similar to Unix command 'chroot'.)
282 static void Chroot(const wxString& root) { ms_root = root; }
283
284 protected:
285 static wxString ms_root;
286 };
287
288
289
290 #endif
291 // wxUSE_FILESYSTEM
292
293 #endif
294 // __FILESYS_H__