]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
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 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | #ifndef __FILESYS_H__ | |
10 | #define __FILESYS_H__ | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma interface | |
14 | #endif | |
15 | ||
d30e0edd RR |
16 | #include "wx/setup.h" |
17 | ||
18 | #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS | |
19 | ||
20 | #include "wx/stream.h" | |
21 | #include "wx/mimetype.h" | |
22 | #include "wx/url.h" | |
5526e819 VS |
23 | |
24 | ||
25 | class wxFSFile; | |
26 | class wxFileSystemHandler; | |
27 | class wxFileSystem; | |
28 | ||
29 | //-------------------------------------------------------------------------------- | |
30 | // wxFSFile | |
31 | // This class is a file opened using wxFileSystem. It consists of | |
32 | // input stream, location, mime type & optional anchor | |
33 | // (in 'index.htm#chapter2', 'chapter2' is anchor) | |
34 | //-------------------------------------------------------------------------------- | |
35 | ||
36 | class WXDLLEXPORT wxFSFile : public wxObject | |
37 | { | |
38 | private: | |
39 | wxInputStream *m_Stream; | |
40 | wxString m_Location; | |
41 | wxString m_MimeType; | |
42 | wxString m_Anchor; | |
43 | ||
44 | public: | |
45 | wxFSFile(wxInputStream *stream, const wxString& loc, const wxString& mimetype, const wxString& anchor) | |
46 | { | |
47 | m_Stream = stream; | |
48 | m_Location = loc; | |
49 | m_MimeType = mimetype; m_MimeType.MakeLower(); | |
50 | m_Anchor = anchor; | |
51 | } | |
52 | virtual ~wxFSFile() | |
53 | { | |
54 | if (m_Stream) delete m_Stream; | |
55 | } | |
56 | ||
57 | wxInputStream *GetStream() const {return m_Stream;} | |
58 | // returns stream. This doesn't _create_ stream, it only returns | |
59 | // pointer to it!! | |
60 | ||
61 | const wxString& GetMimeType() const {return m_MimeType;} | |
62 | // returns file's mime type | |
63 | ||
64 | const wxString& GetLocation() const {return m_Location;} | |
65 | // returns the original location (aka filename) of the file | |
66 | ||
67 | const wxString& GetAnchor() const {return m_Anchor;} | |
68 | }; | |
69 | ||
70 | ||
71 | ||
72 | ||
73 | ||
74 | //-------------------------------------------------------------------------------- | |
75 | // wxFileSystemHandler | |
76 | // This class is FS handler for wxFileSystem. It provides | |
77 | // interface to access certain | |
78 | // kinds of files (HTPP, FTP, local, tar.gz etc..) | |
79 | //-------------------------------------------------------------------------------- | |
80 | ||
81 | class WXDLLEXPORT wxFileSystemHandler : public wxObject | |
82 | { | |
83 | DECLARE_ABSTRACT_CLASS(wxFileSystemHandler) | |
84 | ||
85 | public: | |
86 | wxFileSystemHandler() : wxObject() {} | |
87 | ||
88 | virtual bool CanOpen(const wxString& location) = 0; | |
89 | // returns TRUE if this handler is able to open given location | |
90 | ||
91 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0; | |
92 | // opens given file and returns pointer to input stream. | |
93 | // Returns NULL if opening failed. | |
94 | // The location is always absolute path. | |
95 | ||
96 | protected: | |
97 | wxString GetProtocol(const wxString& location) const; | |
98 | // returns protocol ("file", "http", "tar" etc.) The last (most right) | |
99 | // protocol is used: | |
100 | // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
101 | ||
102 | wxString GetLeftLocation(const wxString& location) const; | |
103 | // returns left part of address: | |
104 | // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
105 | ||
106 | wxString GetAnchor(const wxString& location) const; | |
107 | // returns anchor part of address: | |
108 | // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"} | |
109 | // NOTE: anchor is NOT a part of GetLeftLocation()'s return value | |
110 | ||
111 | wxString GetRightLocation(const wxString& location) const; | |
112 | // returns right part of address: | |
113 | // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"} | |
114 | ||
115 | wxString GetMimeTypeFromExt(const wxString& location); | |
116 | // Returns MIME type of the file - w/o need to open it | |
117 | // (default behaviour is that it returns type based on extension) | |
118 | ||
119 | private: | |
120 | static wxMimeTypesManager m_MimeMng; | |
121 | // MIME manager | |
122 | // (it's static and thus shared by all instances and derived classes) | |
123 | }; | |
124 | ||
125 | ||
126 | ||
127 | ||
128 | //-------------------------------------------------------------------------------- | |
129 | // wxFileSystem | |
130 | // This class provides simple interface for opening various | |
131 | // kinds of files (HTPP, FTP, local, tar.gz etc..) | |
132 | //-------------------------------------------------------------------------------- | |
133 | ||
134 | class WXDLLEXPORT wxFileSystem : public wxObject | |
135 | { | |
136 | DECLARE_DYNAMIC_CLASS(wxFileSystem) | |
137 | ||
138 | private: | |
139 | wxString m_Path; | |
140 | // the path (location) we are currently in | |
141 | // this is path, not file! | |
142 | // (so if you opened test/demo.htm, it is | |
143 | // "test/", not "test/demo.htm") | |
144 | wxString m_LastName; | |
145 | // name of last opened file (full path) | |
146 | static wxList m_Handlers; | |
147 | // list of FS handlers | |
148 | ||
149 | public: | |
150 | wxFileSystem() : wxObject() {m_Path = m_LastName = wxEmptyString; m_Handlers.DeleteContents(TRUE);} | |
151 | ||
152 | void ChangePathTo(const wxString& location, bool is_dir = FALSE); | |
153 | // sets the current location. Every call to OpenFile is | |
154 | // relative to this location. | |
155 | // NOTE !! | |
156 | // unless is_dir = TRUE 'location' is *not* the directory but | |
157 | // file contained in this directory | |
158 | // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/") | |
159 | ||
160 | wxString GetPath() const {return m_Path;} | |
161 | ||
162 | wxFSFile* OpenFile(const wxString& location); | |
163 | // opens given file and returns pointer to input stream. | |
164 | // Returns NULL if opening failed. | |
165 | // It first tries to open the file in relative scope | |
166 | // (based on ChangePathTo()'s value) and then as an absolute | |
167 | // path. | |
168 | ||
169 | static void AddHandler(wxFileSystemHandler *handler); | |
170 | // Adds FS handler. | |
171 | // In fact, this class is only front-end to the FS hanlers :-) | |
172 | }; | |
173 | ||
174 | ||
175 | /* | |
176 | ||
177 | 'location' syntax: | |
178 | ||
179 | To determine FS type, we're using standard KDE notation: | |
180 | file:/absolute/path/file.htm | |
181 | file:relative_path/xxxxx.html | |
182 | /some/path/x.file ('file:' is default) | |
183 | http://www.gnome.org | |
184 | file:subdir/archive.tar.gz#tar:/README.txt | |
185 | ||
186 | special characters : | |
187 | ':' - FS identificator is before this char | |
188 | '#' - separator. It can be either HTML anchor ("index.html#news") | |
189 | (in case there is no ':' in the string to the right from it) | |
190 | or FS separator | |
191 | (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h" | |
192 | this would access tgz archive stored on web) | |
193 | '/' - directory (path) separator. It is used to determine upper-level path. | |
194 | HEY! Don't use \ even if you're on Windows! | |
195 | ||
196 | */ | |
197 | ||
d30e0edd RR |
198 | #endif |
199 | // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS | |
200 | ||
201 | #endif | |
202 | // __FILESYS_H__ |