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