]> git.saurik.com Git - wxWidgets.git/blame - include/wx/filesys.h
Better name for wxXmlResource::GetDirection() argument.
[wxWidgets.git] / include / wx / filesys.h
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
ce7208d4 2// Name: wx/filesys.h
5526e819
VS
3// Purpose: class for opening files - virtual file system
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
65571936 6// Licence: wxWindows licence
5526e819
VS
7/////////////////////////////////////////////////////////////////////////////
8
9#ifndef __FILESYS_H__
10#define __FILESYS_H__
11
2ecf902b 12#include "wx/defs.h"
d30e0edd 13
08e827d8
VZ
14#if wxUSE_FILESYSTEM
15
24528b0c
VS
16#if !wxUSE_STREAMS
17#error You cannot compile virtual file systems without wxUSE_STREAMS
18#endif
19
20#if wxUSE_HTML && !wxUSE_FILESYSTEM
21#error You cannot compile wxHTML without virtual file systems
22#endif
23
d30e0edd 24#include "wx/stream.h"
6ee654e6 25#include "wx/datetime.h"
9548c49a 26#include "wx/filename.h"
52ad298e 27#include "wx/hashmap.h"
5526e819 28
b5dbe15d
VS
29class WXDLLIMPEXP_FWD_BASE wxFSFile;
30class WXDLLIMPEXP_FWD_BASE wxFileSystemHandler;
31class WXDLLIMPEXP_FWD_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 40class WXDLLIMPEXP_BASE wxFSFile : public wxObject
5526e819 41{
19008b7b 42public:
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;
69cce151 52 m_MimeType = mimetype.Lower();
19008b7b 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
69cce151 80 const wxString& GetMimeType() const;
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
91private:
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)
c0c133e1 101 wxDECLARE_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 115class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject
5526e819 116{
19008b7b
VS
117public:
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
69cce151
VS
134 // Returns MIME type of the file - w/o need to open it
135 // (default behaviour is that it returns type based on extension)
136 static wxString GetMimeTypeFromExt(const wxString& location);
137
19008b7b
VS
138protected:
139 // returns protocol ("file", "http", "tar" etc.) The last (most right)
140 // protocol is used:
141 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
69cce151 142 static wxString GetProtocol(const wxString& location);
19008b7b
VS
143
144 // returns left part of address:
145 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
69cce151 146 static wxString GetLeftLocation(const wxString& location);
19008b7b
VS
147
148 // returns anchor part of address:
149 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
150 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
69cce151 151 static wxString GetAnchor(const wxString& location);
19008b7b
VS
152
153 // returns right part of address:
154 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
69cce151 155 static wxString GetRightLocation(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 169// Open Bit Flags
7e721c7a
FM
170enum wxFileSystemOpenFlags
171{
8c3dbc46 172 wxFS_READ = 1, // Open for reading
8c3dbc46
MW
173 wxFS_SEEKABLE = 4 // Returned stream will be seekable
174};
175
01c3ebb8 176WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL(wxFileSystemHandler*, wxFSHandlerHash, class WXDLLIMPEXP_BASE);
52ad298e 177
bddd7a8d 178class WXDLLIMPEXP_BASE wxFileSystem : public wxObject
5526e819 179{
19008b7b 180public:
bf07249c 181 wxFileSystem() : wxObject() { m_FindFileHandler = NULL;}
52ad298e 182 virtual ~wxFileSystem();
19008b7b
VS
183
184 // sets the current location. Every call to OpenFile is
185 // relative to this location.
186 // NOTE !!
a62848fd 187 // unless is_dir = true 'location' is *not* the directory but
19008b7b
VS
188 // file contained in this directory
189 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
a62848fd 190 void ChangePathTo(const wxString& location, bool is_dir = false);
19008b7b
VS
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.
8c3dbc46 199 wxFSFile* OpenFile(const wxString& location, int flags = wxFS_READ);
19008b7b
VS
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
3ab6fcee 207 // find a file in a list of directories, returns false if not found
593177c5
VS
208 bool FindFileInPath(wxString *pStr,
209 const wxString& path, const wxString& file);
3ab6fcee 210
19008b7b 211 // Adds FS handler.
43e8916f 212 // In fact, this class is only front-end to the FS handlers :-)
19008b7b
VS
213 static void AddHandler(wxFileSystemHandler *handler);
214
5949d307
RR
215 // Removes FS handler
216 static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
217
b8b37ced
VZ
218 // Returns true if there is a handler which can open the given location.
219 static bool HasHandlerForPath(const wxString& location);
220
19008b7b
VS
221 // remove all items from the m_Handlers list
222 static void CleanUpHandlers();
223
2b5f62a0 224 // Returns the native path for a file URL
9548c49a 225 static wxFileName URLToFileName(const wxString& url);
2b5f62a0
VZ
226
227 // Returns the file URL for a native path
9548c49a 228 static wxString FileNameToURL(const wxFileName& filename);
2b5f62a0
VZ
229
230
19008b7b 231protected:
52ad298e
MW
232 wxFileSystemHandler *MakeLocal(wxFileSystemHandler *h);
233
19008b7b
VS
234 wxString m_Path;
235 // the path (location) we are currently in
236 // this is path, not file!
237 // (so if you opened test/demo.htm, it is
238 // "test/", not "test/demo.htm")
239 wxString m_LastName;
240 // name of last opened file (full path)
241 static wxList m_Handlers;
242 // list of FS handlers
243 wxFileSystemHandler *m_FindFileHandler;
244 // handler that succeed in FindFirst query
52ad298e
MW
245 wxFSHandlerHash m_LocalHandlers;
246 // Handlers local to this instance
5526e819 247
19008b7b 248 DECLARE_DYNAMIC_CLASS(wxFileSystem)
c0c133e1 249 wxDECLARE_NO_COPY_CLASS(wxFileSystem);
5526e819
VS
250};
251
252
253/*
254
255'location' syntax:
256
257To determine FS type, we're using standard KDE notation:
258file:/absolute/path/file.htm
259file:relative_path/xxxxx.html
260/some/path/x.file ('file:' is default)
261http://www.gnome.org
262file:subdir/archive.tar.gz#tar:/README.txt
263
264special characters :
265 ':' - FS identificator is before this char
266 '#' - separator. It can be either HTML anchor ("index.html#news")
267 (in case there is no ':' in the string to the right from it)
268 or FS separator
269 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
270 this would access tgz archive stored on web)
271 '/' - directory (path) separator. It is used to determine upper-level path.
272 HEY! Don't use \ even if you're on Windows!
273
274*/
275
19008b7b 276
bddd7a8d 277class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler
19008b7b
VS
278{
279public:
280 virtual bool CanOpen(const wxString& location);
281 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
282 virtual wxString FindFirst(const wxString& spec, int flags = 0);
283 virtual wxString FindNext();
46837272 284
19008b7b
VS
285 // wxLocalFSHandler will prefix all filenames with 'root' before accessing
286 // files on disk. This effectively makes 'root' the top-level directory
46837272 287 // and prevents access to files outside this directory.
19008b7b
VS
288 // (This is similar to Unix command 'chroot'.)
289 static void Chroot(const wxString& root) { ms_root = root; }
46837272 290
19008b7b
VS
291protected:
292 static wxString ms_root;
293};
294
b7775a52
VZ
295// Stream reading data from wxFSFile: this allows to use virtual files with any
296// wx functions accepting streams.
297class WXDLLIMPEXP_BASE wxFSInputStream : public wxWrapperInputStream
298{
299public:
300 // Notice that wxFS_READ is implied in flags.
301 wxFSInputStream(const wxString& filename, int flags = 0);
302 virtual ~wxFSInputStream();
19008b7b 303
b7775a52
VZ
304private:
305 wxFSFile* m_file;
306
307 wxDECLARE_NO_COPY_CLASS(wxFSInputStream);
308};
19008b7b 309
d30e0edd 310#endif
24528b0c 311 // wxUSE_FILESYSTEM
d30e0edd 312
269e8200 313#endif
d30e0edd 314 // __FILESYS_H__