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