1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filesys.cpp
3 // Purpose: wxFileSystem class - interface for opening files
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
19 #include "wx/filesys.h"
23 #include "wx/module.h"
26 #include "wx/wfstream.h"
27 #include "wx/mimetype.h"
28 #include "wx/filename.h"
29 #include "wx/tokenzr.h"
30 #include "wx/private/fileback.h"
33 //--------------------------------------------------------------------------------
34 // wxFileSystemHandler
35 //--------------------------------------------------------------------------------
37 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
40 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
43 wxString loc
= GetRightLocation(location
);
45 int l
= loc
.length(), l2
;
48 for (int i
= l
-1; i
>= 0; i
--)
50 c
= loc
[(unsigned int) i
];
55 ext
= loc
.Right(l2
-i
-1);
58 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
63 static bool s_MinimalMimeEnsured
= false;
64 if (!s_MinimalMimeEnsured
)
66 static const wxFileTypeInfo fallbacks
[] =
68 wxFileTypeInfo(_T("image/jpeg"),
71 _T("JPEG image (from fallback)"),
72 _T("jpg"), _T("jpeg"), _T("JPG"), _T("JPEG"), NULL
),
73 wxFileTypeInfo(_T("image/gif"),
76 _T("GIF image (from fallback)"),
77 _T("gif"), _T("GIF"), NULL
),
78 wxFileTypeInfo(_T("image/png"),
81 _T("PNG image (from fallback)"),
82 _T("png"), _T("PNG"), NULL
),
83 wxFileTypeInfo(_T("image/bmp"),
86 _T("windows bitmap image (from fallback)"),
87 _T("bmp"), _T("BMP"), NULL
),
88 wxFileTypeInfo(_T("text/html"),
91 _T("HTML document (from fallback)"),
92 _T("htm"), _T("html"), _T("HTM"), _T("HTML"), NULL
),
93 // must terminate the table with this!
96 wxTheMimeTypesManager
->AddFallbacks(fallbacks
);
97 s_MinimalMimeEnsured
= true;
100 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
101 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
103 mime
= wxEmptyString
;
110 if ( ext
.IsSameAs(wxT("htm"), false) || ext
.IsSameAs(_T("html"), false) )
111 return wxT("text/html");
112 if ( ext
.IsSameAs(wxT("jpg"), false) || ext
.IsSameAs(_T("jpeg"), false) )
113 return wxT("image/jpeg");
114 if ( ext
.IsSameAs(wxT("gif"), false) )
115 return wxT("image/gif");
116 if ( ext
.IsSameAs(wxT("png"), false) )
117 return wxT("image/png");
118 if ( ext
.IsSameAs(wxT("bmp"), false) )
119 return wxT("image/bmp");
120 return wxEmptyString
;
126 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
128 wxString s
= wxEmptyString
;
129 int i
, l
= location
.length();
132 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
133 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
135 if (!fnd
) return wxT("file");
136 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
141 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
146 for (i
= location
.length()-1; i
>= 0; i
--) {
147 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
148 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
150 return wxEmptyString
;
153 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
155 int i
, l
= location
.length();
160 ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':')));
163 if (location
[i
] == wxT('#')) l2
= i
+ 1;
165 if (i
== 0) return wxEmptyString
;
166 else return location
.Mid(i
+ 1, l2
- i
- 2);
169 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
172 int l
= location
.length();
174 for (int i
= l
-1; i
>= 0; i
--) {
176 if (c
== wxT('#')) return location
.Right(l
-i
-1);
177 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
179 return wxEmptyString
;
183 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
186 return wxEmptyString
;
189 wxString
wxFileSystemHandler::FindNext()
191 return wxEmptyString
;
194 //--------------------------------------------------------------------------------
196 //--------------------------------------------------------------------------------
199 wxString
wxLocalFSHandler::ms_root
;
201 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
203 return GetProtocol(location
) == wxT("file");
206 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
208 // location has Unix path separators
209 wxString right
= GetRightLocation(location
);
210 wxFileName fn
= wxFileSystem::URLToFileName(right
);
211 wxString fullpath
= ms_root
+ fn
.GetFullPath();
213 if (!wxFileExists(fullpath
))
214 return (wxFSFile
*) NULL
;
216 // we need to check whether we can really read from this file, otherwise
217 // wxFSFile is not going to work
219 wxFileInputStream
*is
= new wxFileInputStream(fullpath
);
221 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
223 #error One of wxUSE_FILE or wxUSE_FFILE must be set to 1 for wxFSHandler to work
228 return (wxFSFile
*) NULL
;
231 return new wxFSFile(is
,
233 GetMimeTypeFromExt(location
),
236 ,wxDateTime(wxFileModificationTime(fullpath
))
237 #endif // wxUSE_DATETIME
241 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
243 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
244 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
247 wxString
wxLocalFSHandler::FindNext()
249 return wxFindNextFile();
254 //-----------------------------------------------------------------------------
256 //-----------------------------------------------------------------------------
258 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
259 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
262 wxList
wxFileSystem::m_Handlers
;
265 wxFileSystem::~wxFileSystem()
267 WX_CLEAR_HASH_MAP(wxFSHandlerHash
, m_LocalHandlers
)
271 static wxString
MakeCorrectPath(const wxString
& path
)
278 for (i
= 0; i
< cnt
; i
++)
279 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
281 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
283 if (cnt
< 3) return p
;
285 r
<< p
.GetChar(0) << p
.GetChar(1);
287 // skip trailing ../.., if any
288 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
290 // remove back references: translate dir1/../dir2 to dir2
294 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
296 for (j
= r
.length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
297 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
299 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
305 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
311 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
315 m_Path
= MakeCorrectPath(location
);
319 if (m_Path
.length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
325 for (i
= m_Path
.length()-1; i
>= 0; i
--)
327 if (m_Path
[(unsigned int) i
] == wxT('/'))
329 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
340 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
347 for (i
= 0; i
< (int) m_Path
.length(); i
++)
349 if (m_Path
[(unsigned int) i
] == wxT(':'))
355 if (i
== (int) m_Path
.length())
356 m_Path
= wxEmptyString
;
360 m_Path
.Remove(pathpos
+1);
367 wxFileSystemHandler
*wxFileSystem::MakeLocal(wxFileSystemHandler
*h
)
369 wxClassInfo
*classinfo
= h
->GetClassInfo();
371 if (classinfo
->IsDynamic())
373 wxFileSystemHandler
*& local
= m_LocalHandlers
[classinfo
];
375 local
= (wxFileSystemHandler
*)classinfo
->CreateObject();
386 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
, int flags
)
388 if ((flags
& wxFS_READ
) == 0)
391 wxString loc
= MakeCorrectPath(location
);
395 wxList::compatibility_iterator node
;
399 for (i
= 0; i
< ln
; i
++)
403 case wxT('/') : case wxT(':') : case wxT('#') :
407 if (meta
!= 0) break;
409 m_LastName
= wxEmptyString
;
411 // try relative paths first :
412 if (meta
!= wxT(':'))
414 node
= m_Handlers
.GetFirst();
417 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
418 if (h
->CanOpen(m_Path
+ loc
))
420 s
= MakeLocal(h
)->OpenFile(*this, m_Path
+ loc
);
421 if (s
) { m_LastName
= m_Path
+ loc
; break; }
423 node
= node
->GetNext();
427 // if failed, try absolute paths :
430 node
= m_Handlers
.GetFirst();
433 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
436 s
= MakeLocal(h
)->OpenFile(*this, loc
);
437 if (s
) { m_LastName
= loc
; break; }
439 node
= node
->GetNext();
443 if (s
&& (flags
& wxFS_SEEKABLE
) != 0 && !s
->GetStream()->IsSeekable())
445 wxBackedInputStream
*stream
;
446 stream
= new wxBackedInputStream(s
->DetachStream());
447 stream
->FindLength();
448 s
->SetStream(stream
);
456 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
458 wxList::compatibility_iterator node
;
459 wxString
spec2(spec
);
461 m_FindFileHandler
= NULL
;
463 for (int i
= spec2
.length()-1; i
>= 0; i
--)
464 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
466 node
= m_Handlers
.GetFirst();
469 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
470 if (h
-> CanOpen(m_Path
+ spec2
))
472 m_FindFileHandler
= MakeLocal(h
);
473 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
475 node
= node
->GetNext();
478 node
= m_Handlers
.GetFirst();
481 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
482 if (h
-> CanOpen(spec2
))
484 m_FindFileHandler
= MakeLocal(h
);
485 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
487 node
= node
->GetNext();
490 return wxEmptyString
;
495 wxString
wxFileSystem::FindNext()
497 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
498 else return m_FindFileHandler
-> FindNext();
501 bool wxFileSystem::FindFileInPath(wxString
*pStr
,
503 const wxChar
*basename
)
505 // we assume that it's not empty
506 wxCHECK_MSG( !wxIsEmpty(basename
), false,
507 _T("empty file name in wxFileSystem::FindFileInPath"));
509 // skip path separator in the beginning of the file name if present
510 if ( wxIsPathSeparator(*basename
) )
513 wxStringTokenizer
tokenizer(path
, wxPATH_SEP
);
514 while ( tokenizer
.HasMoreTokens() )
516 wxString strFile
= tokenizer
.GetNextToken();
517 if ( !wxEndsWithPathSeparator(strFile
) )
518 strFile
+= wxFILE_SEP_PATH
;
521 wxFSFile
*file
= OpenFile(strFile
);
533 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
535 // prepend the handler to the beginning of the list because handlers added
536 // last should have the highest priority to allow overriding them
537 m_Handlers
.Insert((size_t)0, handler
);
540 wxFileSystemHandler
* wxFileSystem::RemoveHandler(wxFileSystemHandler
*handler
)
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
))
554 bool wxFileSystem::HasHandlerForPath(const wxString
&location
)
556 for ( wxList::compatibility_iterator node
= m_Handlers
.GetFirst();
557 node
; node
= node
->GetNext() )
559 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
560 if (h
->CanOpen(location
))
567 void wxFileSystem::CleanUpHandlers()
569 WX_CLEAR_LIST(wxList
, m_Handlers
);
572 static const wxString
g_unixPathString(wxT("/"));
573 static const wxString
g_nativePathString(wxFILE_SEP_PATH
);
575 // Returns the native path for a file URL
576 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
580 if ( path
.Find(wxT("file://")) == 0 )
584 else if ( path
.Find(wxT("file:")) == 0 )
588 // Remove preceding double slash on Mac Classic
589 #if defined(__WXMAC__) && !defined(__UNIX__)
590 else if ( path
.Find(wxT("//")) == 0 )
594 path
.Replace(wxT("%25"), wxT("%"));
595 path
.Replace(wxT("%3A"), wxT(":"));
598 // file urls either start with a forward slash (local harddisk),
599 // otherwise they have a servername/sharename notation,
600 // which only exists on msw and corresponds to a unc
601 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
605 else if ( (url
.Find(wxT("file://")) == 0) &&
606 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
607 (path
.length() > 1) && (path
[1u] != wxT(':')) )
609 path
= wxT("//") + path
;
613 path
.Replace(g_unixPathString
, g_nativePathString
);
615 return wxFileName(path
, wxPATH_NATIVE
);
618 // Returns the file URL for a native path
619 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
621 wxFileName fn
= filename
;
622 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
623 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
626 // unc notation, wxMSW
627 if ( url
.Find(wxT("\\\\")) == 0 )
629 url
= wxT("//") + url
.Mid(2);
633 url
= wxT("/") + url
;
635 url
= wxT("/") + url
;
641 url
.Replace(g_nativePathString
, g_unixPathString
);
642 url
.Replace(wxT("%"), wxT("%25"));
643 url
.Replace(wxT(":"), wxT("%3A"));
644 url
= wxT("file:") + url
;
651 class wxFileSystemModule
: public wxModule
653 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
656 wxFileSystemModule() :
662 virtual bool OnInit()
664 m_handler
= new wxLocalFSHandler
;
665 wxFileSystem::AddHandler(m_handler
);
668 virtual void OnExit()
670 delete wxFileSystem::RemoveHandler(m_handler
);
672 wxFileSystem::CleanUpHandlers();
676 wxFileSystemHandler
* m_handler
;
680 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)