]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filesys.h
class/struct mismatch
[wxWidgets.git] / include / wx / filesys.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 #include "wx/hashmap.h"
29
30 class WXDLLIMPEXP_BASE wxFSFile;
31 class WXDLLIMPEXP_BASE wxFileSystemHandler;
32 class WXDLLIMPEXP_BASE wxFileSystem;
33
34 //--------------------------------------------------------------------------------
35 // wxFSFile
36 // This class is a file opened using wxFileSystem. It consists of
37 // input stream, location, mime type & optional anchor
38 // (in 'index.htm#chapter2', 'chapter2' is anchor)
39 //--------------------------------------------------------------------------------
40
41 class WXDLLIMPEXP_BASE wxFSFile : public wxObject
42 {
43 public:
44 wxFSFile(wxInputStream *stream, const wxString& loc,
45 const wxString& mimetype, const wxString& anchor
46 #if wxUSE_DATETIME
47 , wxDateTime modif
48 #endif // wxUSE_DATETIME
49 )
50 {
51 m_Stream = stream;
52 m_Location = loc;
53 m_MimeType = mimetype; m_MimeType.MakeLower();
54 m_Anchor = anchor;
55 #if wxUSE_DATETIME
56 m_Modif = modif;
57 #endif // wxUSE_DATETIME
58 }
59
60 virtual ~wxFSFile() { delete m_Stream; }
61
62 // returns stream. This doesn't give away ownership of the stream object.
63 wxInputStream *GetStream() const { return m_Stream; }
64
65 // gives away the ownership of the current stream.
66 wxInputStream *DetachStream()
67 {
68 wxInputStream *stream = m_Stream;
69 m_Stream = NULL;
70 return stream;
71 }
72
73 // deletes the current stream and takes ownership of another.
74 void SetStream(wxInputStream *stream)
75 {
76 delete m_Stream;
77 m_Stream = stream;
78 }
79
80 // returns file's mime type
81 const wxString& GetMimeType() const { return m_MimeType; }
82
83 // returns the original location (aka filename) of the file
84 const wxString& GetLocation() const { return m_Location; }
85
86 const wxString& GetAnchor() const { return m_Anchor; }
87
88 #if wxUSE_DATETIME
89 wxDateTime GetModificationTime() const { return m_Modif; }
90 #endif // wxUSE_DATETIME
91
92 private:
93 wxInputStream *m_Stream;
94 wxString m_Location;
95 wxString m_MimeType;
96 wxString m_Anchor;
97 #if wxUSE_DATETIME
98 wxDateTime m_Modif;
99 #endif // wxUSE_DATETIME
100
101 DECLARE_ABSTRACT_CLASS(wxFSFile)
102 DECLARE_NO_COPY_CLASS(wxFSFile)
103 };
104
105
106
107
108
109 //--------------------------------------------------------------------------------
110 // wxFileSystemHandler
111 // This class is FS handler for wxFileSystem. It provides
112 // interface to access certain
113 // kinds of files (HTPP, FTP, local, tar.gz etc..)
114 //--------------------------------------------------------------------------------
115
116 class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject
117 {
118 public:
119 wxFileSystemHandler() : wxObject() {}
120
121 // returns true if this handler is able to open given location
122 virtual bool CanOpen(const wxString& location) = 0;
123
124 // opens given file and returns pointer to input stream.
125 // Returns NULL if opening failed.
126 // The location is always absolute path.
127 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0;
128
129 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
130 // the query to directories or wxFILE for files only or 0 for either.
131 // Returns filename or empty string if no more matching file exists
132 virtual wxString FindFirst(const wxString& spec, int flags = 0);
133 virtual wxString FindNext();
134
135 protected:
136 // returns protocol ("file", "http", "tar" etc.) The last (most right)
137 // protocol is used:
138 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
139 wxString GetProtocol(const wxString& location) const;
140
141 // returns left part of address:
142 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
143 wxString GetLeftLocation(const wxString& location) const;
144
145 // returns anchor part of address:
146 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
147 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
148 wxString GetAnchor(const wxString& location) const;
149
150 // returns right part of address:
151 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
152 wxString GetRightLocation(const wxString& location) const;
153
154 // Returns MIME type of the file - w/o need to open it
155 // (default behaviour is that it returns type based on extension)
156 wxString GetMimeTypeFromExt(const wxString& location);
157
158 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler)
159 };
160
161
162
163
164 //--------------------------------------------------------------------------------
165 // wxFileSystem
166 // This class provides simple interface for opening various
167 // kinds of files (HTPP, FTP, local, tar.gz etc..)
168 //--------------------------------------------------------------------------------
169
170 // Open Bit Flags
171 enum {
172 wxFS_READ = 1, // Open for reading
173 wxFS_SEEKABLE = 4 // Returned stream will be seekable
174 };
175
176 WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL(wxFileSystemHandler*, wxFSHandlerHash, class WXDLLIMPEXP_BASE);
177
178 class WXDLLIMPEXP_BASE wxFileSystem : public wxObject
179 {
180 public:
181 wxFileSystem() : wxObject() { m_FindFileHandler = NULL;}
182 virtual ~wxFileSystem();
183
184 // sets the current location. Every call to OpenFile is
185 // relative to this location.
186 // NOTE !!
187 // unless is_dir = true 'location' is *not* the directory but
188 // file contained in this directory
189 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
190 void ChangePathTo(const wxString& location, bool is_dir = false);
191
192 wxString GetPath() const {return m_Path;}
193
194 // opens given file and returns pointer to input stream.
195 // Returns NULL if opening failed.
196 // It first tries to open the file in relative scope
197 // (based on ChangePathTo()'s value) and then as an absolute
198 // path.
199 wxFSFile* OpenFile(const wxString& location, int flags = wxFS_READ);
200
201 // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
202 // the query to directories or wxFILE for files only or 0 for either.
203 // Returns filename or empty string if no more matching file exists
204 wxString FindFirst(const wxString& spec, int flags = 0);
205 wxString FindNext();
206
207 // find a file in a list of directories, returns false if not found
208 bool FindFileInPath(wxString *pStr, const wxChar *path, const wxChar *file);
209
210 // Adds FS handler.
211 // In fact, this class is only front-end to the FS handlers :-)
212 static void AddHandler(wxFileSystemHandler *handler);
213
214 // Removes FS handler
215 static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
216
217 // Returns true if there is a handler which can open the given location.
218 static bool HasHandlerForPath(const wxString& location);
219
220 // remove all items from the m_Handlers list
221 static void CleanUpHandlers();
222
223 // Returns the native path for a file URL
224 static wxFileName URLToFileName(const wxString& url);
225
226 // Returns the file URL for a native path
227 static wxString FileNameToURL(const wxFileName& filename);
228
229
230 protected:
231 wxFileSystemHandler *MakeLocal(wxFileSystemHandler *h);
232
233 wxString m_Path;
234 // the path (location) we are currently in
235 // this is path, not file!
236 // (so if you opened test/demo.htm, it is
237 // "test/", not "test/demo.htm")
238 wxString m_LastName;
239 // name of last opened file (full path)
240 static wxList m_Handlers;
241 // list of FS handlers
242 wxFileSystemHandler *m_FindFileHandler;
243 // handler that succeed in FindFirst query
244 wxFSHandlerHash m_LocalHandlers;
245 // Handlers local to this instance
246
247 DECLARE_DYNAMIC_CLASS(wxFileSystem)
248 DECLARE_NO_COPY_CLASS(wxFileSystem)
249 };
250
251
252 /*
253
254 'location' syntax:
255
256 To determine FS type, we're using standard KDE notation:
257 file:/absolute/path/file.htm
258 file:relative_path/xxxxx.html
259 /some/path/x.file ('file:' is default)
260 http://www.gnome.org
261 file:subdir/archive.tar.gz#tar:/README.txt
262
263 special characters :
264 ':' - FS identificator is before this char
265 '#' - separator. It can be either HTML anchor ("index.html#news")
266 (in case there is no ':' in the string to the right from it)
267 or FS separator
268 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
269 this would access tgz archive stored on web)
270 '/' - directory (path) separator. It is used to determine upper-level path.
271 HEY! Don't use \ even if you're on Windows!
272
273 */
274
275
276 class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler
277 {
278 public:
279 virtual bool CanOpen(const wxString& location);
280 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
281 virtual wxString FindFirst(const wxString& spec, int flags = 0);
282 virtual wxString FindNext();
283
284 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
285 // files on disk. This effectively makes 'root' the top-level directory
286 // and prevents access to files outside this directory.
287 // (This is similar to Unix command 'chroot'.)
288 static void Chroot(const wxString& root) { ms_root = root; }
289
290 protected:
291 static wxString ms_root;
292 };
293
294
295
296 #endif
297 // wxUSE_FILESYSTEM
298
299 #endif
300 // __FILESYS_H__