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