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