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