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