]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: filesys.cpp | |
3 | // Purpose: wxFileSystem class - interface for opening files | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
6 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | #ifdef __GNUG__ | |
10 | #pragma implementation | |
11 | #endif | |
12 | ||
d30e0edd | 13 | #include "wx/wxprec.h" |
5526e819 VS |
14 | |
15 | #ifdef __BORDLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
31528cd3 | 19 | #if !wxUSE_SOCKETS |
31528cd3 | 20 | #undef wxUSE_FS_INET |
31528cd3 VZ |
21 | #define wxUSE_FS_INET 0 |
22 | #endif | |
23 | ||
d30e0edd | 24 | #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS |
5526e819 | 25 | |
d30e0edd RR |
26 | #include "wx/wfstream.h" |
27 | #include "wx/module.h" | |
28 | #include "wx/filesys.h" | |
5526e819 VS |
29 | |
30 | //-------------------------------------------------------------------------------- | |
31 | // wxFileSystemHandler | |
32 | //-------------------------------------------------------------------------------- | |
33 | ||
34 | IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject) | |
35 | ||
a76015e6 VS |
36 | wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL; |
37 | ||
38 | void wxFileSystemHandler::CleanUpStatics() | |
39 | { | |
40 | if (m_MimeMng) delete m_MimeMng; | |
41 | m_MimeMng = NULL; | |
42 | } | |
5526e819 VS |
43 | |
44 | ||
45 | wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location) | |
46 | { | |
47 | wxString ext = wxEmptyString, mime = wxEmptyString; | |
48 | wxString loc = GetRightLocation(location); | |
49 | char c; | |
50 | int l = loc.Length(), l2; | |
51 | wxFileType *ft; | |
52 | ||
53 | l2 = l; | |
54 | for (int i = l-1; i >= 0; i--) { | |
ea4f5235 | 55 | c = loc[(unsigned int) i]; |
223d09f6 KB |
56 | if (c == wxT('#')) l2 = i + 1; |
57 | if (c == wxT('.')) {ext = loc.Right(l2-i-1); break;} | |
58 | if ((c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) {return wxEmptyString;} | |
5526e819 | 59 | } |
956418ab VS |
60 | |
61 | if (m_MimeMng == NULL) { | |
62 | m_MimeMng = new wxMimeTypesManager; | |
63 | ||
64 | static const wxFileTypeInfo fallbacks[] = | |
65 | { | |
66 | wxFileTypeInfo("image/jpeg", | |
67 | "", | |
68 | "", | |
69 | "JPEG image (from fallback)", | |
70 | "jpg", "jpeg", NULL), | |
71 | wxFileTypeInfo("image/gif", | |
72 | "", | |
73 | "", | |
74 | "GIF image (from fallback)", | |
75 | "gif", NULL), | |
76 | wxFileTypeInfo("image/png", | |
77 | "", | |
78 | "", | |
79 | "PNG image (from fallback)", | |
80 | "png", NULL), | |
81 | wxFileTypeInfo("image/bmp", | |
82 | "", | |
83 | "", | |
84 | "windows bitmap image (from fallback)", | |
85 | "bmp", NULL), | |
86 | wxFileTypeInfo("text/html", | |
87 | "", | |
88 | "", | |
89 | "HTML document (from fallback)", | |
90 | "htm", "html", NULL), | |
91 | ||
92 | // must terminate the table with this! | |
93 | wxFileTypeInfo() | |
94 | }; | |
95 | ||
96 | m_MimeMng -> AddFallbacks(fallbacks); | |
97 | } | |
98 | ||
a76015e6 | 99 | ft = m_MimeMng -> GetFileTypeFromExtension(ext); |
5526e819 VS |
100 | if (ft && (ft -> GetMimeType(&mime))) return mime; |
101 | else return wxEmptyString; | |
102 | } | |
103 | ||
104 | ||
105 | ||
106 | wxString wxFileSystemHandler::GetProtocol(const wxString& location) const | |
107 | { | |
108 | wxString s = wxEmptyString; | |
109 | int i, l = location.Length(); | |
110 | bool fnd; | |
111 | ||
112 | fnd = FALSE; | |
223d09f6 KB |
113 | for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) { |
114 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; | |
5526e819 | 115 | } |
223d09f6 KB |
116 | if (!fnd) return wxT("file"); |
117 | for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i]; | |
5526e819 VS |
118 | return s; |
119 | } | |
120 | ||
121 | ||
5526e819 VS |
122 | wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const |
123 | { | |
124 | int i; | |
125 | bool fnd; | |
126 | ||
127 | fnd = FALSE; | |
128 | for (i = location.Length()-1; i >= 0; i--) { | |
223d09f6 KB |
129 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; |
130 | else if (fnd && (location[i] == wxT('#'))) return location.Left(i); | |
5526e819 VS |
131 | } |
132 | return wxEmptyString; | |
133 | } | |
134 | ||
5526e819 VS |
135 | wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const |
136 | { | |
137 | int i, l = location.Length(); | |
138 | int l2 = l + 1; | |
223d09f6 | 139 | for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;} |
5526e819 VS |
140 | if (i == 0) return wxEmptyString; |
141 | else return location.Mid(i + 1, l2 - i - 2); | |
142 | } | |
143 | ||
5526e819 VS |
144 | wxString wxFileSystemHandler::GetAnchor(const wxString& location) const |
145 | { | |
146 | char c; | |
147 | int l = location.Length(); | |
148 | ||
149 | for (int i = l-1; i >= 0; i--) { | |
150 | c = location[i]; | |
223d09f6 KB |
151 | if (c == wxT('#')) return location.Right(l-i-1); |
152 | else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString; | |
5526e819 VS |
153 | } |
154 | return wxEmptyString; | |
155 | } | |
156 | ||
5526e819 VS |
157 | //-------------------------------------------------------------------------------- |
158 | // wxLocalFSHandler | |
159 | //-------------------------------------------------------------------------------- | |
160 | ||
161 | class wxLocalFSHandler : public wxFileSystemHandler | |
162 | { | |
163 | public: | |
164 | virtual bool CanOpen(const wxString& location); | |
165 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
166 | }; | |
167 | ||
168 | ||
5526e819 VS |
169 | bool wxLocalFSHandler::CanOpen(const wxString& location) |
170 | { | |
223d09f6 | 171 | return GetProtocol(location) == wxT("file"); |
5526e819 VS |
172 | } |
173 | ||
5526e819 VS |
174 | wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
175 | { | |
176 | wxString right = GetRightLocation(location); | |
177 | if (wxFileExists(right)) | |
178 | return new wxFSFile(new wxFileInputStream(right), | |
179 | right, | |
180 | GetMimeTypeFromExt(location), | |
181 | GetAnchor(location)); | |
d30e0edd | 182 | else return (wxFSFile*) NULL; |
5526e819 VS |
183 | } |
184 | ||
5526e819 VS |
185 | //----------------------------------------------------------------------------- |
186 | // wxFileSystem | |
187 | //----------------------------------------------------------------------------- | |
188 | ||
189 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject) | |
190 | ||
191 | ||
192 | wxList wxFileSystem::m_Handlers; | |
193 | ||
194 | ||
195 | ||
196 | void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir) | |
197 | { | |
198 | int i, pathpos = -1; | |
199 | m_Path = location; | |
200 | ||
201 | for (i = m_Path.Length()-1; i >= 0; i--) | |
223d09f6 | 202 | if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe |
d30e0edd | 203 | |
269e8200 | 204 | if (is_dir == FALSE) |
d30e0edd | 205 | { |
269e8200 | 206 | for (i = m_Path.Length()-1; i >= 0; i--) |
d30e0edd | 207 | { |
223d09f6 | 208 | if (m_Path[(unsigned int) i] == wxT('/')) |
d30e0edd | 209 | { |
223d09f6 | 210 | if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':'))) |
d30e0edd | 211 | { |
5526e819 VS |
212 | i -= 2; |
213 | continue; | |
214 | } | |
269e8200 | 215 | else |
d30e0edd | 216 | { |
269e8200 | 217 | pathpos = i; |
5526e819 VS |
218 | break; |
219 | } | |
220 | } | |
223d09f6 | 221 | else if (m_Path[(unsigned int) i] == wxT(':')) { |
5526e819 VS |
222 | pathpos = i; |
223 | break; | |
224 | } | |
225 | } | |
269e8200 | 226 | if (pathpos == -1) |
d30e0edd | 227 | { |
269e8200 | 228 | for (i = 0; i < (int) m_Path.Length(); i++) |
d30e0edd | 229 | { |
223d09f6 | 230 | if (m_Path[(unsigned int) i] == wxT(':')) |
d30e0edd | 231 | { |
223d09f6 | 232 | //m_Path << wxT('/'); |
5526e819 VS |
233 | m_Path.Remove(i+1); |
234 | break; | |
235 | } | |
236 | } | |
269e8200 | 237 | if (i == (int) m_Path.Length()) |
d30e0edd | 238 | m_Path = wxEmptyString; |
5526e819 | 239 | } |
269e8200 | 240 | else |
d30e0edd | 241 | { |
223d09f6 KB |
242 | if (m_Path[m_Path.Length()-1] != wxT('/')) |
243 | m_Path << wxT('/'); | |
5526e819 VS |
244 | m_Path.Remove(pathpos+1); |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | ||
250 | ||
251 | wxFSFile* wxFileSystem::OpenFile(const wxString& location) | |
252 | { | |
253 | wxString loc = location; | |
254 | int i, ln; | |
255 | char meta; | |
256 | wxFSFile *s = NULL; | |
257 | wxNode *node; | |
258 | ||
259 | ln = loc.Length(); | |
260 | meta = 0; | |
269e8200 | 261 | for (i = 0; i < ln; i++) |
d30e0edd | 262 | { |
223d09f6 | 263 | if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe |
ea4f5235 | 264 | if (!meta) switch (loc[(unsigned int) i]) |
d30e0edd | 265 | { |
223d09f6 | 266 | case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i]; |
5526e819 VS |
267 | } |
268 | } | |
269 | m_LastName = wxEmptyString; | |
270 | ||
271 | // try relative paths first : | |
223d09f6 | 272 | if (meta != wxT(':')) |
d30e0edd | 273 | { |
5526e819 | 274 | node = m_Handlers.GetFirst(); |
d30e0edd RR |
275 | while (node) |
276 | { | |
5526e819 | 277 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); |
269e8200 | 278 | if (h->CanOpen(m_Path + location)) |
d30e0edd RR |
279 | { |
280 | s = h->OpenFile(*this, m_Path + location); | |
281 | if (s) { m_LastName = m_Path + location; break; } | |
5526e819 | 282 | } |
d30e0edd | 283 | node = node->GetNext(); |
5526e819 VS |
284 | } |
285 | } | |
286 | ||
287 | // if failed, try absolute paths : | |
269e8200 | 288 | if (s == NULL) |
d30e0edd | 289 | { |
5526e819 | 290 | node = m_Handlers.GetFirst(); |
d30e0edd RR |
291 | while (node) |
292 | { | |
293 | wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData(); | |
269e8200 | 294 | if (h->CanOpen(location)) |
d30e0edd RR |
295 | { |
296 | s = h->OpenFile(*this, location); | |
297 | if (s) { m_LastName = location; break; } | |
5526e819 | 298 | } |
d30e0edd | 299 | node = node->GetNext(); |
5526e819 VS |
300 | } |
301 | } | |
302 | return (s); | |
303 | } | |
304 | ||
305 | ||
5526e819 VS |
306 | void wxFileSystem::AddHandler(wxFileSystemHandler *handler) |
307 | { | |
308 | m_Handlers.Append(handler); | |
309 | } | |
310 | ||
311 | ||
269e8200 RD |
312 | void wxFileSystem::CleanUpHandlers() |
313 | { | |
314 | m_Handlers.DeleteContents(TRUE); | |
315 | m_Handlers.Clear(); | |
316 | } | |
317 | ||
5526e819 | 318 | |
5526e819 VS |
319 | ///// Module: |
320 | ||
321 | class wxFileSystemModule : public wxModule | |
322 | { | |
323 | DECLARE_DYNAMIC_CLASS(wxFileSystemModule) | |
324 | ||
325 | public: | |
326 | virtual bool OnInit() | |
327 | { | |
328 | wxFileSystem::AddHandler(new wxLocalFSHandler); | |
329 | return TRUE; | |
330 | } | |
269e8200 | 331 | virtual void OnExit() |
a76015e6 VS |
332 | { |
333 | wxFileSystemHandler::CleanUpStatics(); | |
269e8200 | 334 | wxFileSystem::CleanUpHandlers(); |
a76015e6 | 335 | } |
5526e819 VS |
336 | }; |
337 | ||
338 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule) | |
339 | ||
269e8200 | 340 | #endif |
d30e0edd | 341 | // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS |
5526e819 | 342 | |
a76015e6 VS |
343 | |
344 |