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