]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
e4db172a | 2 | // Name: src/common/filesys.cpp |
5526e819 VS |
3 | // Purpose: wxFileSystem class - interface for opening files |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 1999 Vaclav Slavik | |
5be0cf65 | 6 | // CVS-ID: $Id$ |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
d30e0edd | 10 | #include "wx/wxprec.h" |
5526e819 | 11 | |
2b5f62a0 | 12 | #ifdef __BORLANDC__ |
e4db172a | 13 | #pragma hdrstop |
5526e819 VS |
14 | #endif |
15 | ||
31528cd3 | 16 | |
24528b0c | 17 | #if wxUSE_FILESYSTEM |
5526e819 | 18 | |
e4db172a WS |
19 | #include "wx/filesys.h" |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/log.h" | |
02761f6c | 23 | #include "wx/module.h" |
e4db172a WS |
24 | #endif |
25 | ||
c895624b | 26 | #include "wx/sysopt.h" |
d30e0edd | 27 | #include "wx/wfstream.h" |
73725567 | 28 | #include "wx/mimetype.h" |
6464f4cb | 29 | #include "wx/filename.h" |
3ab6fcee | 30 | #include "wx/tokenzr.h" |
d81f6eac | 31 | #include "wx/uri.h" |
916af76f | 32 | #include "wx/private/fileback.h" |
73725567 | 33 | |
69cce151 VS |
34 | // ---------------------------------------------------------------------------- |
35 | // wxFSFile | |
36 | // ---------------------------------------------------------------------------- | |
73725567 | 37 | |
69cce151 VS |
38 | const wxString& wxFSFile::GetMimeType() const |
39 | { | |
40 | if ( m_MimeType.empty() && !m_Location.empty() ) | |
41 | { | |
42 | wxConstCast(this, wxFSFile)->m_MimeType = | |
43 | wxFileSystemHandler::GetMimeTypeFromExt(m_Location); | |
44 | } | |
45 | ||
46 | return m_MimeType; | |
47 | } | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
5526e819 | 50 | // wxFileSystemHandler |
69cce151 | 51 | // ---------------------------------------------------------------------------- |
5526e819 VS |
52 | |
53 | IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject) | |
54 | ||
5526e819 | 55 | |
69cce151 | 56 | /* static */ |
5526e819 VS |
57 | wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location) |
58 | { | |
45681cd7 | 59 | wxString ext, mime; |
5526e819 | 60 | wxString loc = GetRightLocation(location); |
53b99810 | 61 | wxChar c; |
e4db172a | 62 | int l = loc.length(), l2; |
5526e819 VS |
63 | |
64 | l2 = l; | |
46837272 | 65 | for (int i = l-1; i >= 0; i--) |
45681cd7 | 66 | { |
ea4f5235 | 67 | c = loc[(unsigned int) i]; |
45681cd7 VS |
68 | if ( c == wxT('#') ) |
69 | l2 = i + 1; | |
70 | if ( c == wxT('.') ) | |
71 | { | |
46837272 | 72 | ext = loc.Right(l2-i-1); |
45681cd7 VS |
73 | break; |
74 | } | |
75 | if ( (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':')) ) | |
76 | return wxEmptyString; | |
5526e819 | 77 | } |
956418ab | 78 | |
45681cd7 | 79 | #if wxUSE_MIMETYPE |
121680bf | 80 | static bool s_MinimalMimeEnsured = false; |
c895624b JS |
81 | |
82 | // Don't use mime types manager if the application doesn't need it and it would be | |
83 | // cause an unacceptable delay, especially on startup. | |
c895624b | 84 | #if wxUSE_SYSTEM_OPTIONS |
107b52ed | 85 | if ( !wxSystemOptions::GetOptionInt(wxT("filesys.no-mimetypesmanager")) ) |
c895624b | 86 | #endif |
121680bf | 87 | { |
c895624b | 88 | if (!s_MinimalMimeEnsured) |
121680bf | 89 | { |
c895624b JS |
90 | static const wxFileTypeInfo fallbacks[] = |
91 | { | |
9a83f860 | 92 | wxFileTypeInfo(wxT("image/jpeg"), |
c895624b JS |
93 | wxEmptyString, |
94 | wxEmptyString, | |
9a83f860 VZ |
95 | wxT("JPEG image (from fallback)"), |
96 | wxT("jpg"), wxT("jpeg"), wxT("JPG"), wxT("JPEG"), wxNullPtr), | |
97 | wxFileTypeInfo(wxT("image/gif"), | |
c895624b JS |
98 | wxEmptyString, |
99 | wxEmptyString, | |
9a83f860 VZ |
100 | wxT("GIF image (from fallback)"), |
101 | wxT("gif"), wxT("GIF"), wxNullPtr), | |
102 | wxFileTypeInfo(wxT("image/png"), | |
c895624b JS |
103 | wxEmptyString, |
104 | wxEmptyString, | |
9a83f860 VZ |
105 | wxT("PNG image (from fallback)"), |
106 | wxT("png"), wxT("PNG"), wxNullPtr), | |
107 | wxFileTypeInfo(wxT("image/bmp"), | |
c895624b JS |
108 | wxEmptyString, |
109 | wxEmptyString, | |
9a83f860 VZ |
110 | wxT("windows bitmap image (from fallback)"), |
111 | wxT("bmp"), wxT("BMP"), wxNullPtr), | |
112 | wxFileTypeInfo(wxT("text/html"), | |
c895624b JS |
113 | wxEmptyString, |
114 | wxEmptyString, | |
9a83f860 VZ |
115 | wxT("HTML document (from fallback)"), |
116 | wxT("htm"), wxT("html"), wxT("HTM"), wxT("HTML"), wxNullPtr), | |
44d0f703 VS |
117 | // must terminate the table with this! |
118 | wxFileTypeInfo() | |
c895624b JS |
119 | }; |
120 | wxTheMimeTypesManager->AddFallbacks(fallbacks); | |
121 | s_MinimalMimeEnsured = true; | |
122 | } | |
03647350 | 123 | |
c895624b JS |
124 | wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext); |
125 | if ( !ft || !ft -> GetMimeType(&mime) ) | |
126 | { | |
127 | mime = wxEmptyString; | |
128 | } | |
03647350 | 129 | |
c895624b | 130 | delete ft; |
03647350 | 131 | |
c895624b | 132 | return mime; |
956418ab | 133 | } |
c895624b JS |
134 | else |
135 | #endif | |
45681cd7 | 136 | { |
9a83f860 | 137 | if ( ext.IsSameAs(wxT("htm"), false) || ext.IsSameAs(wxT("html"), false) ) |
c895624b | 138 | return wxT("text/html"); |
9a83f860 | 139 | if ( ext.IsSameAs(wxT("jpg"), false) || ext.IsSameAs(wxT("jpeg"), false) ) |
c895624b JS |
140 | return wxT("image/jpeg"); |
141 | if ( ext.IsSameAs(wxT("gif"), false) ) | |
142 | return wxT("image/gif"); | |
143 | if ( ext.IsSameAs(wxT("png"), false) ) | |
144 | return wxT("image/png"); | |
145 | if ( ext.IsSameAs(wxT("bmp"), false) ) | |
146 | return wxT("image/bmp"); | |
147 | return wxEmptyString; | |
51f79d5c | 148 | } |
5526e819 VS |
149 | } |
150 | ||
151 | ||
152 | ||
69cce151 VS |
153 | /* static */ |
154 | wxString wxFileSystemHandler::GetProtocol(const wxString& location) | |
5526e819 VS |
155 | { |
156 | wxString s = wxEmptyString; | |
e4db172a | 157 | int i, l = location.length(); |
a62848fd | 158 | bool fnd = false; |
5526e819 | 159 | |
223d09f6 | 160 | for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) { |
a62848fd | 161 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = true; |
5526e819 | 162 | } |
223d09f6 KB |
163 | if (!fnd) return wxT("file"); |
164 | for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i]; | |
5526e819 VS |
165 | return s; |
166 | } | |
167 | ||
168 | ||
69cce151 VS |
169 | /* static */ |
170 | wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) | |
5526e819 VS |
171 | { |
172 | int i; | |
a62848fd | 173 | bool fnd = false; |
5526e819 | 174 | |
e4db172a | 175 | for (i = location.length()-1; i >= 0; i--) { |
a62848fd | 176 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = true; |
223d09f6 | 177 | else if (fnd && (location[i] == wxT('#'))) return location.Left(i); |
5526e819 VS |
178 | } |
179 | return wxEmptyString; | |
180 | } | |
181 | ||
69cce151 VS |
182 | /* static */ |
183 | wxString wxFileSystemHandler::GetRightLocation(const wxString& location) | |
5526e819 | 184 | { |
e4db172a | 185 | int i, l = location.length(); |
5526e819 | 186 | int l2 = l + 1; |
008a56c9 | 187 | |
a62848fd WS |
188 | for (i = l-1; |
189 | (i >= 0) && | |
f8ae31dc | 190 | ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); |
008a56c9 | 191 | i--) |
c8c29c49 JS |
192 | { |
193 | if (location[i] == wxT('#')) l2 = i + 1; | |
194 | } | |
5526e819 VS |
195 | if (i == 0) return wxEmptyString; |
196 | else return location.Mid(i + 1, l2 - i - 2); | |
197 | } | |
198 | ||
69cce151 VS |
199 | /* static */ |
200 | wxString wxFileSystemHandler::GetAnchor(const wxString& location) | |
5526e819 | 201 | { |
53b99810 | 202 | wxChar c; |
e4db172a | 203 | int l = location.length(); |
5526e819 VS |
204 | |
205 | for (int i = l-1; i >= 0; i--) { | |
206 | c = location[i]; | |
91915e1c VS |
207 | if (c == wxT('#')) |
208 | return location.Right(l-i-1); | |
209 | else if ((c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) | |
210 | return wxEmptyString; | |
5526e819 VS |
211 | } |
212 | return wxEmptyString; | |
213 | } | |
214 | ||
aaa66113 | 215 | |
f76dbc4d VZ |
216 | wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec), |
217 | int WXUNUSED(flags)) | |
218 | { | |
219 | return wxEmptyString; | |
220 | } | |
aaa66113 | 221 | |
f76dbc4d VZ |
222 | wxString wxFileSystemHandler::FindNext() |
223 | { | |
224 | return wxEmptyString; | |
225 | } | |
aaa66113 | 226 | |
5526e819 VS |
227 | //-------------------------------------------------------------------------------- |
228 | // wxLocalFSHandler | |
229 | //-------------------------------------------------------------------------------- | |
230 | ||
5526e819 | 231 | |
19008b7b | 232 | wxString wxLocalFSHandler::ms_root; |
5526e819 | 233 | |
5526e819 VS |
234 | bool wxLocalFSHandler::CanOpen(const wxString& location) |
235 | { | |
223d09f6 | 236 | return GetProtocol(location) == wxT("file"); |
5526e819 VS |
237 | } |
238 | ||
5526e819 VS |
239 | wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
240 | { | |
6464f4cb | 241 | // location has Unix path separators |
008a56c9 | 242 | wxString right = GetRightLocation(location); |
9548c49a | 243 | wxFileName fn = wxFileSystem::URLToFileName(right); |
008a56c9 | 244 | wxString fullpath = ms_root + fn.GetFullPath(); |
46837272 | 245 | |
008a56c9 | 246 | if (!wxFileExists(fullpath)) |
d3b9f782 | 247 | return NULL; |
46837272 | 248 | |
3e5175b7 VZ |
249 | // we need to check whether we can really read from this file, otherwise |
250 | // wxFSFile is not going to work | |
8d6c5e4f | 251 | #if wxUSE_FFILE |
3e5175b7 | 252 | wxFFileInputStream *is = new wxFFileInputStream(fullpath); |
8d6c5e4f VS |
253 | #elif wxUSE_FILE |
254 | wxFileInputStream *is = new wxFileInputStream(fullpath); | |
d9b0ee1e VZ |
255 | #else |
256 | #error One of wxUSE_FILE or wxUSE_FFILE must be set to 1 for wxFSHandler to work | |
257 | #endif | |
3e5175b7 VZ |
258 | if ( !is->Ok() ) |
259 | { | |
260 | delete is; | |
d3b9f782 | 261 | return NULL; |
3e5175b7 VZ |
262 | } |
263 | ||
264 | return new wxFSFile(is, | |
f6bcfd97 | 265 | right, |
69cce151 | 266 | wxEmptyString, |
e2b87f38 VZ |
267 | GetAnchor(location) |
268 | #if wxUSE_DATETIME | |
269 | ,wxDateTime(wxFileModificationTime(fullpath)) | |
270 | #endif // wxUSE_DATETIME | |
271 | ); | |
5526e819 VS |
272 | } |
273 | ||
aaa66113 VS |
274 | wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags) |
275 | { | |
008a56c9 | 276 | wxFileName fn = wxFileSystem::URLToFileName(GetRightLocation(spec)); |
8e63b7b8 VS |
277 | const wxString found = wxFindFirstFile(ms_root + fn.GetFullPath(), flags); |
278 | if ( found.empty() ) | |
279 | return found; | |
280 | return wxFileSystem::FileNameToURL(found); | |
aaa66113 VS |
281 | } |
282 | ||
283 | wxString wxLocalFSHandler::FindNext() | |
284 | { | |
8e63b7b8 VS |
285 | const wxString found = wxFindNextFile(); |
286 | if ( found.empty() ) | |
287 | return found; | |
288 | return wxFileSystem::FileNameToURL(found); | |
aaa66113 VS |
289 | } |
290 | ||
291 | ||
292 | ||
5526e819 VS |
293 | //----------------------------------------------------------------------------- |
294 | // wxFileSystem | |
295 | //----------------------------------------------------------------------------- | |
296 | ||
297 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject) | |
46837272 | 298 | IMPLEMENT_ABSTRACT_CLASS(wxFSFile, wxObject) |
5526e819 VS |
299 | |
300 | ||
301 | wxList wxFileSystem::m_Handlers; | |
302 | ||
303 | ||
52ad298e MW |
304 | wxFileSystem::~wxFileSystem() |
305 | { | |
306 | WX_CLEAR_HASH_MAP(wxFSHandlerHash, m_LocalHandlers) | |
307 | } | |
308 | ||
309 | ||
5be0cf65 VS |
310 | static wxString MakeCorrectPath(const wxString& path) |
311 | { | |
312 | wxString p(path); | |
313 | wxString r; | |
314 | int i, j, cnt; | |
86b3203f | 315 | |
e4db172a | 316 | cnt = p.length(); |
5be0cf65 | 317 | for (i = 0; i < cnt; i++) |
f6081a04 | 318 | if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe |
86b3203f | 319 | |
5be0cf65 | 320 | if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; } |
86b3203f | 321 | |
5be0cf65 | 322 | if (cnt < 3) return p; |
86b3203f | 323 | |
b2f60e03 | 324 | r << p.GetChar(0) << p.GetChar(1); |
86b3203f | 325 | |
5be0cf65 | 326 | // skip trailing ../.., if any |
b2f60e03 | 327 | for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i); |
86b3203f | 328 | |
5be0cf65 VS |
329 | // remove back references: translate dir1/../dir2 to dir2 |
330 | for (; i < cnt; i++) | |
331 | { | |
b2f60e03 BJ |
332 | r << p.GetChar(i); |
333 | if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.')) | |
5be0cf65 | 334 | { |
e4db172a | 335 | for (j = r.length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {} |
b2f60e03 | 336 | if (j >= 0 && r.GetChar(j) != wxT(':')) |
5be0cf65 | 337 | { |
b2f60e03 | 338 | for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {} |
5be0cf65 VS |
339 | r.Remove(j + 1); |
340 | } | |
341 | } | |
342 | } | |
86b3203f | 343 | |
b2f60e03 | 344 | for (; i < cnt; i++) r << p.GetChar(i); |
86b3203f | 345 | |
5be0cf65 VS |
346 | return r; |
347 | } | |
348 | ||
5526e819 VS |
349 | |
350 | void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir) | |
351 | { | |
352 | int i, pathpos = -1; | |
5526e819 | 353 | |
5be0cf65 | 354 | m_Path = MakeCorrectPath(location); |
d30e0edd | 355 | |
aaa66113 VS |
356 | if (is_dir) |
357 | { | |
e4db172a | 358 | if (m_Path.length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':')) |
d81152f4 | 359 | m_Path << wxT('/'); |
aaa66113 | 360 | } |
86b3203f | 361 | |
aaa66113 | 362 | else |
d30e0edd | 363 | { |
e4db172a | 364 | for (i = m_Path.length()-1; i >= 0; i--) |
d81152f4 | 365 | { |
223d09f6 | 366 | if (m_Path[(unsigned int) i] == wxT('/')) |
d81152f4 | 367 | { |
223d09f6 | 368 | if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':'))) |
d81152f4 | 369 | { |
5526e819 VS |
370 | i -= 2; |
371 | continue; | |
372 | } | |
269e8200 | 373 | else |
d81152f4 | 374 | { |
269e8200 | 375 | pathpos = i; |
5526e819 VS |
376 | break; |
377 | } | |
378 | } | |
aaa66113 VS |
379 | else if (m_Path[(unsigned int) i] == wxT(':')) { |
380 | pathpos = i; | |
381 | break; | |
382 | } | |
5526e819 | 383 | } |
269e8200 | 384 | if (pathpos == -1) |
d81152f4 | 385 | { |
e4db172a | 386 | for (i = 0; i < (int) m_Path.length(); i++) |
d81152f4 | 387 | { |
223d09f6 | 388 | if (m_Path[(unsigned int) i] == wxT(':')) |
d81152f4 | 389 | { |
5526e819 VS |
390 | m_Path.Remove(i+1); |
391 | break; | |
392 | } | |
393 | } | |
e4db172a | 394 | if (i == (int) m_Path.length()) |
d81152f4 | 395 | m_Path = wxEmptyString; |
5526e819 | 396 | } |
269e8200 | 397 | else |
d81152f4 | 398 | { |
5526e819 VS |
399 | m_Path.Remove(pathpos+1); |
400 | } | |
401 | } | |
402 | } | |
403 | ||
404 | ||
405 | ||
52ad298e MW |
406 | wxFileSystemHandler *wxFileSystem::MakeLocal(wxFileSystemHandler *h) |
407 | { | |
408 | wxClassInfo *classinfo = h->GetClassInfo(); | |
409 | ||
410 | if (classinfo->IsDynamic()) | |
411 | { | |
412 | wxFileSystemHandler*& local = m_LocalHandlers[classinfo]; | |
413 | if (!local) | |
414 | local = (wxFileSystemHandler*)classinfo->CreateObject(); | |
415 | return local; | |
416 | } | |
417 | else | |
418 | { | |
419 | return h; | |
420 | } | |
421 | } | |
422 | ||
423 | ||
424 | ||
8c3dbc46 | 425 | wxFSFile* wxFileSystem::OpenFile(const wxString& location, int flags) |
5526e819 | 426 | { |
0ce98614 MW |
427 | if ((flags & wxFS_READ) == 0) |
428 | return NULL; | |
429 | ||
5be0cf65 VS |
430 | wxString loc = MakeCorrectPath(location); |
431 | unsigned i, ln; | |
53b99810 | 432 | wxChar meta; |
5526e819 | 433 | wxFSFile *s = NULL; |
df5168c4 | 434 | wxList::compatibility_iterator node; |
5526e819 | 435 | |
e4db172a | 436 | ln = loc.length(); |
5526e819 | 437 | meta = 0; |
269e8200 | 438 | for (i = 0; i < ln; i++) |
d30e0edd | 439 | { |
c9f78968 | 440 | switch ( loc[i].GetValue() ) |
2148cce2 | 441 | { |
86b3203f | 442 | case wxT('/') : case wxT(':') : case wxT('#') : |
2148cce2 VS |
443 | meta = loc[i]; |
444 | break; | |
445 | } | |
446 | if (meta != 0) break; | |
5526e819 VS |
447 | } |
448 | m_LastName = wxEmptyString; | |
449 | ||
450 | // try relative paths first : | |
223d09f6 | 451 | if (meta != wxT(':')) |
d30e0edd | 452 | { |
5526e819 | 453 | node = m_Handlers.GetFirst(); |
d30e0edd | 454 | while (node) |
d81152f4 | 455 | { |
5526e819 | 456 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); |
7dee4b2b | 457 | if (h->CanOpen(m_Path + loc)) |
d81152f4 | 458 | { |
52ad298e | 459 | s = MakeLocal(h)->OpenFile(*this, m_Path + loc); |
7dee4b2b | 460 | if (s) { m_LastName = m_Path + loc; break; } |
5526e819 | 461 | } |
d30e0edd | 462 | node = node->GetNext(); |
5526e819 VS |
463 | } |
464 | } | |
465 | ||
466 | // if failed, try absolute paths : | |
269e8200 | 467 | if (s == NULL) |
d30e0edd | 468 | { |
5526e819 | 469 | node = m_Handlers.GetFirst(); |
d30e0edd | 470 | while (node) |
d81152f4 | 471 | { |
d30e0edd | 472 | wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData(); |
7dee4b2b | 473 | if (h->CanOpen(loc)) |
d81152f4 | 474 | { |
52ad298e | 475 | s = MakeLocal(h)->OpenFile(*this, loc); |
7dee4b2b | 476 | if (s) { m_LastName = loc; break; } |
5526e819 | 477 | } |
d30e0edd | 478 | node = node->GetNext(); |
5526e819 VS |
479 | } |
480 | } | |
8c3dbc46 MW |
481 | |
482 | if (s && (flags & wxFS_SEEKABLE) != 0 && !s->GetStream()->IsSeekable()) | |
483 | { | |
484 | wxBackedInputStream *stream; | |
485 | stream = new wxBackedInputStream(s->DetachStream()); | |
486 | stream->FindLength(); | |
487 | s->SetStream(stream); | |
488 | } | |
489 | ||
5526e819 VS |
490 | return (s); |
491 | } | |
492 | ||
493 | ||
aaa66113 VS |
494 | |
495 | wxString wxFileSystem::FindFirst(const wxString& spec, int flags) | |
496 | { | |
df5168c4 | 497 | wxList::compatibility_iterator node; |
aaa66113 | 498 | wxString spec2(spec); |
86b3203f | 499 | |
aaa66113 VS |
500 | m_FindFileHandler = NULL; |
501 | ||
e4db172a | 502 | for (int i = spec2.length()-1; i >= 0; i--) |
f6081a04 | 503 | if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe |
aaa66113 VS |
504 | |
505 | node = m_Handlers.GetFirst(); | |
506 | while (node) | |
507 | { | |
52ad298e MW |
508 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); |
509 | if (h -> CanOpen(m_Path + spec2)) | |
510 | { | |
511 | m_FindFileHandler = MakeLocal(h); | |
aaa66113 | 512 | return m_FindFileHandler -> FindFirst(m_Path + spec2, flags); |
52ad298e | 513 | } |
aaa66113 | 514 | node = node->GetNext(); |
86b3203f | 515 | } |
aaa66113 VS |
516 | |
517 | node = m_Handlers.GetFirst(); | |
518 | while (node) | |
519 | { | |
52ad298e MW |
520 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); |
521 | if (h -> CanOpen(spec2)) | |
522 | { | |
523 | m_FindFileHandler = MakeLocal(h); | |
aaa66113 | 524 | return m_FindFileHandler -> FindFirst(spec2, flags); |
52ad298e | 525 | } |
aaa66113 | 526 | node = node->GetNext(); |
86b3203f DW |
527 | } |
528 | ||
529 | return wxEmptyString; | |
aaa66113 VS |
530 | } |
531 | ||
532 | ||
533 | ||
534 | wxString wxFileSystem::FindNext() | |
535 | { | |
536 | if (m_FindFileHandler == NULL) return wxEmptyString; | |
537 | else return m_FindFileHandler -> FindNext(); | |
538 | } | |
539 | ||
3ab6fcee | 540 | bool wxFileSystem::FindFileInPath(wxString *pStr, |
593177c5 VS |
541 | const wxString& path, |
542 | const wxString& basename) | |
3ab6fcee VZ |
543 | { |
544 | // we assume that it's not empty | |
593177c5 | 545 | wxCHECK_MSG( !basename.empty(), false, |
9a83f860 | 546 | wxT("empty file name in wxFileSystem::FindFileInPath")); |
3ab6fcee | 547 | |
593177c5 | 548 | wxString name; |
3ab6fcee | 549 | // skip path separator in the beginning of the file name if present |
593177c5 VS |
550 | if ( wxIsPathSeparator(basename[0u]) ) |
551 | name = basename.substr(1); | |
552 | else | |
553 | name = basename; | |
aaa66113 | 554 | |
3ab6fcee VZ |
555 | wxStringTokenizer tokenizer(path, wxPATH_SEP); |
556 | while ( tokenizer.HasMoreTokens() ) | |
557 | { | |
558 | wxString strFile = tokenizer.GetNextToken(); | |
559 | if ( !wxEndsWithPathSeparator(strFile) ) | |
560 | strFile += wxFILE_SEP_PATH; | |
593177c5 | 561 | strFile += name; |
3ab6fcee VZ |
562 | |
563 | wxFSFile *file = OpenFile(strFile); | |
564 | if ( file ) | |
565 | { | |
566 | delete file; | |
567 | *pStr = strFile; | |
568 | return true; | |
569 | } | |
570 | } | |
571 | ||
572 | return false; | |
573 | } | |
aaa66113 | 574 | |
5526e819 VS |
575 | void wxFileSystem::AddHandler(wxFileSystemHandler *handler) |
576 | { | |
4948ebf3 VZ |
577 | // prepend the handler to the beginning of the list because handlers added |
578 | // last should have the highest priority to allow overriding them | |
374b4f1c | 579 | m_Handlers.Insert((size_t)0, handler); |
5526e819 VS |
580 | } |
581 | ||
5949d307 RR |
582 | wxFileSystemHandler* wxFileSystem::RemoveHandler(wxFileSystemHandler *handler) |
583 | { | |
584 | // if handler has already been removed (or deleted) | |
585 | // we return NULL. This is by design in case | |
586 | // CleanUpHandlers() is called before RemoveHandler | |
587 | // is called, as we cannot control the order | |
588 | // which modules are unloaded | |
589 | if (!m_Handlers.DeleteObject(handler)) | |
590 | return NULL; | |
591 | ||
592 | return handler; | |
593 | } | |
594 | ||
595 | ||
b8b37ced VZ |
596 | bool wxFileSystem::HasHandlerForPath(const wxString &location) |
597 | { | |
598 | for ( wxList::compatibility_iterator node = m_Handlers.GetFirst(); | |
599 | node; node = node->GetNext() ) | |
600 | { | |
601 | wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData(); | |
602 | if (h->CanOpen(location)) | |
603 | return true; | |
604 | } | |
605 | ||
606 | return false; | |
607 | } | |
5526e819 | 608 | |
269e8200 RD |
609 | void wxFileSystem::CleanUpHandlers() |
610 | { | |
df5168c4 | 611 | WX_CLEAR_LIST(wxList, m_Handlers); |
269e8200 RD |
612 | } |
613 | ||
60431236 WS |
614 | static const wxString g_unixPathString(wxT("/")); |
615 | static const wxString g_nativePathString(wxFILE_SEP_PATH); | |
5526e819 | 616 | |
2b5f62a0 | 617 | // Returns the native path for a file URL |
9548c49a | 618 | wxFileName wxFileSystem::URLToFileName(const wxString& url) |
2b5f62a0 | 619 | { |
a62848fd | 620 | wxString path = url; |
2b5f62a0 | 621 | |
a62848fd WS |
622 | if ( path.Find(wxT("file://")) == 0 ) |
623 | { | |
624 | path = path.Mid(7); | |
625 | } | |
008a56c9 | 626 | else if ( path.Find(wxT("file:")) == 0 ) |
a62848fd WS |
627 | { |
628 | path = path.Mid(5); | |
629 | } | |
630 | // Remove preceding double slash on Mac Classic | |
d0200d9e JS |
631 | #if defined(__WXMAC__) && !defined(__UNIX__) |
632 | else if ( path.Find(wxT("//")) == 0 ) | |
633 | path = path.Mid(2); | |
634 | #endif | |
a62848fd | 635 | |
d81f6eac | 636 | path = wxURI::Unescape(path); |
2b5f62a0 | 637 | |
8ad944dc | 638 | #ifdef __WXMSW__ |
a62848fd | 639 | // file urls either start with a forward slash (local harddisk), |
2b5f62a0 VZ |
640 | // otherwise they have a servername/sharename notation, |
641 | // which only exists on msw and corresponds to a unc | |
a62848fd WS |
642 | if ( path[0u] == wxT('/') && path [1u] != wxT('/')) |
643 | { | |
644 | path = path.Mid(1); | |
645 | } | |
646 | else if ( (url.Find(wxT("file://")) == 0) && | |
2b5f62a0 | 647 | (path.Find(wxT('/')) != wxNOT_FOUND) && |
e4db172a | 648 | (path.length() > 1) && (path[1u] != wxT(':')) ) |
a62848fd WS |
649 | { |
650 | path = wxT("//") + path; | |
651 | } | |
2b5f62a0 | 652 | #endif |
9b798c66 | 653 | |
a62848fd | 654 | path.Replace(g_unixPathString, g_nativePathString); |
2b5f62a0 | 655 | |
a62848fd | 656 | return wxFileName(path, wxPATH_NATIVE); |
2b5f62a0 VZ |
657 | } |
658 | ||
659 | // Returns the file URL for a native path | |
9548c49a | 660 | wxString wxFileSystem::FileNameToURL(const wxFileName& filename) |
2b5f62a0 | 661 | { |
9548c49a VS |
662 | wxFileName fn = filename; |
663 | fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE); | |
664 | wxString url = fn.GetFullPath(wxPATH_NATIVE); | |
9b798c66 | 665 | |
2a5d3f57 JS |
666 | #ifndef __UNIX__ |
667 | // unc notation, wxMSW | |
a62848fd | 668 | if ( url.Find(wxT("\\\\")) == 0 ) |
9548c49a | 669 | { |
60c315ca | 670 | url = wxT("//") + url.Mid(2); |
9548c49a VS |
671 | } |
672 | else | |
673 | { | |
674 | url = wxT("/") + url; | |
d0200d9e JS |
675 | #ifdef __WXMAC__ |
676 | url = wxT("/") + url; | |
677 | #endif | |
678 | ||
9548c49a | 679 | } |
2b5f62a0 | 680 | #endif |
9b798c66 | 681 | |
9548c49a | 682 | url.Replace(g_nativePathString, g_unixPathString); |
68ce1d34 | 683 | url.Replace(wxT("%"), wxT("%25")); // '%'s must be replaced first! |
d81f6eac | 684 | url.Replace(wxT("#"), wxT("%23")); |
8fda05d7 VZ |
685 | |
686 | // notice that all colons *must* be encoded in the paths used by | |
687 | // wxFileSystem even though this makes URLs produced by this method | |
688 | // unusable with IE under Windows as it requires "file:///c:/foo.bar" and | |
689 | // doesn't accept "file:///c%3a/foo.bar" -- but then we never made any | |
690 | // guarantees about general suitability of the strings returned by this | |
691 | // method, they must work with wxFileSystem only and not encoding the colon | |
692 | // breaks handling of "http://wherever/whatever.zip#zip:filename.ext" URLs | |
693 | // so we really can't do this without heavy changes to the parsing code | |
694 | // here, in particular in GetRightLocation() | |
008a56c9 VS |
695 | url.Replace(wxT(":"), wxT("%3A")); |
696 | url = wxT("file:") + url; | |
55129143 VS |
697 | |
698 | // Do wxURI- and common practice-compatible escaping: encode the string | |
699 | // into UTF-8, then escape anything non-ASCII: | |
700 | return wxURI(url).BuildURI(); | |
2b5f62a0 | 701 | } |
aaa66113 VS |
702 | |
703 | ||
5526e819 VS |
704 | ///// Module: |
705 | ||
706 | class wxFileSystemModule : public wxModule | |
707 | { | |
708 | DECLARE_DYNAMIC_CLASS(wxFileSystemModule) | |
709 | ||
710 | public: | |
5949d307 RR |
711 | wxFileSystemModule() : |
712 | wxModule(), | |
713 | m_handler(NULL) | |
714 | { | |
715 | } | |
716 | ||
5526e819 VS |
717 | virtual bool OnInit() |
718 | { | |
5949d307 RR |
719 | m_handler = new wxLocalFSHandler; |
720 | wxFileSystem::AddHandler(m_handler); | |
121680bf | 721 | return true; |
5526e819 | 722 | } |
269e8200 | 723 | virtual void OnExit() |
d81152f4 | 724 | { |
5949d307 RR |
725 | delete wxFileSystem::RemoveHandler(m_handler); |
726 | ||
269e8200 | 727 | wxFileSystem::CleanUpHandlers(); |
d81152f4 | 728 | } |
5949d307 RR |
729 | |
730 | private: | |
731 | wxFileSystemHandler* m_handler; | |
732 | ||
5526e819 VS |
733 | }; |
734 | ||
735 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule) | |
736 | ||
269e8200 | 737 | #endif |
24528b0c | 738 | // wxUSE_FILESYSTEM |