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