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"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 const wxString
& wxFSFile::GetMimeType() const
38 if ( m_MimeType
.empty() && !m_Location
.empty() )
40 wxConstCast(this, wxFSFile
)->m_MimeType
=
41 wxFileSystemHandler::GetMimeTypeFromExt(m_Location
);
47 // ----------------------------------------------------------------------------
48 // wxFileSystemHandler
49 // ----------------------------------------------------------------------------
51 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
55 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
58 wxString loc
= GetRightLocation(location
);
60 int l
= loc
.length(), l2
;
63 for (int i
= l
-1; i
>= 0; i
--)
65 c
= loc
[(unsigned int) i
];
70 ext
= loc
.Right(l2
-i
-1);
73 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
78 static bool s_MinimalMimeEnsured
= false;
79 if (!s_MinimalMimeEnsured
)
81 static const wxFileTypeInfo fallbacks
[] =
83 wxFileTypeInfo(_T("image/jpeg"),
86 _T("JPEG image (from fallback)"),
87 _T("jpg"), _T("jpeg"), _T("JPG"), _T("JPEG"), wxNullPtr
),
88 wxFileTypeInfo(_T("image/gif"),
91 _T("GIF image (from fallback)"),
92 _T("gif"), _T("GIF"), wxNullPtr
),
93 wxFileTypeInfo(_T("image/png"),
96 _T("PNG image (from fallback)"),
97 _T("png"), _T("PNG"), wxNullPtr
),
98 wxFileTypeInfo(_T("image/bmp"),
101 _T("windows bitmap image (from fallback)"),
102 _T("bmp"), _T("BMP"), wxNullPtr
),
103 wxFileTypeInfo(_T("text/html"),
106 _T("HTML document (from fallback)"),
107 _T("htm"), _T("html"), _T("HTM"), _T("HTML"), wxNullPtr
),
108 // must terminate the table with this!
111 wxTheMimeTypesManager
->AddFallbacks(fallbacks
);
112 s_MinimalMimeEnsured
= true;
115 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
116 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
118 mime
= wxEmptyString
;
125 if ( ext
.IsSameAs(wxT("htm"), false) || ext
.IsSameAs(_T("html"), false) )
126 return wxT("text/html");
127 if ( ext
.IsSameAs(wxT("jpg"), false) || ext
.IsSameAs(_T("jpeg"), false) )
128 return wxT("image/jpeg");
129 if ( ext
.IsSameAs(wxT("gif"), false) )
130 return wxT("image/gif");
131 if ( ext
.IsSameAs(wxT("png"), false) )
132 return wxT("image/png");
133 if ( ext
.IsSameAs(wxT("bmp"), false) )
134 return wxT("image/bmp");
135 return wxEmptyString
;
142 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
)
144 wxString s
= wxEmptyString
;
145 int i
, l
= location
.length();
148 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
149 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
151 if (!fnd
) return wxT("file");
152 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
158 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
)
163 for (i
= location
.length()-1; i
>= 0; i
--) {
164 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
165 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
167 return wxEmptyString
;
171 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
)
173 int i
, l
= location
.length();
178 ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':')));
181 if (location
[i
] == wxT('#')) l2
= i
+ 1;
183 if (i
== 0) return wxEmptyString
;
184 else return location
.Mid(i
+ 1, l2
- i
- 2);
188 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
)
191 int l
= location
.length();
193 for (int i
= l
-1; i
>= 0; i
--) {
195 if (c
== wxT('#')) return location
.Right(l
-i
-1);
196 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
198 return wxEmptyString
;
202 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
205 return wxEmptyString
;
208 wxString
wxFileSystemHandler::FindNext()
210 return wxEmptyString
;
213 //--------------------------------------------------------------------------------
215 //--------------------------------------------------------------------------------
218 wxString
wxLocalFSHandler::ms_root
;
220 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
222 return GetProtocol(location
) == wxT("file");
225 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
227 // location has Unix path separators
228 wxString right
= GetRightLocation(location
);
229 wxFileName fn
= wxFileSystem::URLToFileName(right
);
230 wxString fullpath
= ms_root
+ fn
.GetFullPath();
232 if (!wxFileExists(fullpath
))
233 return (wxFSFile
*) NULL
;
235 // we need to check whether we can really read from this file, otherwise
236 // wxFSFile is not going to work
238 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
240 wxFileInputStream
*is
= new wxFileInputStream(fullpath
);
242 #error One of wxUSE_FILE or wxUSE_FFILE must be set to 1 for wxFSHandler to work
247 return (wxFSFile
*) NULL
;
250 return new wxFSFile(is
,
255 ,wxDateTime(wxFileModificationTime(fullpath
))
256 #endif // wxUSE_DATETIME
260 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
262 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
263 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
266 wxString
wxLocalFSHandler::FindNext()
268 return wxFindNextFile();
273 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
278 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
281 wxList
wxFileSystem::m_Handlers
;
284 wxFileSystem::~wxFileSystem()
286 WX_CLEAR_HASH_MAP(wxFSHandlerHash
, m_LocalHandlers
)
290 static wxString
MakeCorrectPath(const wxString
& path
)
297 for (i
= 0; i
< cnt
; i
++)
298 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
300 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
302 if (cnt
< 3) return p
;
304 r
<< p
.GetChar(0) << p
.GetChar(1);
306 // skip trailing ../.., if any
307 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
309 // remove back references: translate dir1/../dir2 to dir2
313 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
315 for (j
= r
.length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
316 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
318 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
324 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
330 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
334 m_Path
= MakeCorrectPath(location
);
338 if (m_Path
.length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
344 for (i
= m_Path
.length()-1; i
>= 0; i
--)
346 if (m_Path
[(unsigned int) i
] == wxT('/'))
348 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
359 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
366 for (i
= 0; i
< (int) m_Path
.length(); i
++)
368 if (m_Path
[(unsigned int) i
] == wxT(':'))
374 if (i
== (int) m_Path
.length())
375 m_Path
= wxEmptyString
;
379 m_Path
.Remove(pathpos
+1);
386 wxFileSystemHandler
*wxFileSystem::MakeLocal(wxFileSystemHandler
*h
)
388 wxClassInfo
*classinfo
= h
->GetClassInfo();
390 if (classinfo
->IsDynamic())
392 wxFileSystemHandler
*& local
= m_LocalHandlers
[classinfo
];
394 local
= (wxFileSystemHandler
*)classinfo
->CreateObject();
405 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
, int flags
)
407 if ((flags
& wxFS_READ
) == 0)
410 wxString loc
= MakeCorrectPath(location
);
414 wxList::compatibility_iterator node
;
418 for (i
= 0; i
< ln
; i
++)
420 switch ( loc
[i
].GetValue() )
422 case wxT('/') : case wxT(':') : case wxT('#') :
426 if (meta
!= 0) break;
428 m_LastName
= wxEmptyString
;
430 // try relative paths first :
431 if (meta
!= wxT(':'))
433 node
= m_Handlers
.GetFirst();
436 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
437 if (h
->CanOpen(m_Path
+ loc
))
439 s
= MakeLocal(h
)->OpenFile(*this, m_Path
+ loc
);
440 if (s
) { m_LastName
= m_Path
+ loc
; break; }
442 node
= node
->GetNext();
446 // if failed, try absolute paths :
449 node
= m_Handlers
.GetFirst();
452 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
455 s
= MakeLocal(h
)->OpenFile(*this, loc
);
456 if (s
) { m_LastName
= loc
; break; }
458 node
= node
->GetNext();
462 if (s
&& (flags
& wxFS_SEEKABLE
) != 0 && !s
->GetStream()->IsSeekable())
464 wxBackedInputStream
*stream
;
465 stream
= new wxBackedInputStream(s
->DetachStream());
466 stream
->FindLength();
467 s
->SetStream(stream
);
475 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
477 wxList::compatibility_iterator node
;
478 wxString
spec2(spec
);
480 m_FindFileHandler
= NULL
;
482 for (int i
= spec2
.length()-1; i
>= 0; i
--)
483 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
485 node
= m_Handlers
.GetFirst();
488 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
489 if (h
-> CanOpen(m_Path
+ spec2
))
491 m_FindFileHandler
= MakeLocal(h
);
492 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
494 node
= node
->GetNext();
497 node
= m_Handlers
.GetFirst();
500 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
501 if (h
-> CanOpen(spec2
))
503 m_FindFileHandler
= MakeLocal(h
);
504 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
506 node
= node
->GetNext();
509 return wxEmptyString
;
514 wxString
wxFileSystem::FindNext()
516 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
517 else return m_FindFileHandler
-> FindNext();
520 bool wxFileSystem::FindFileInPath(wxString
*pStr
,
522 const wxChar
*basename
)
524 // we assume that it's not empty
525 wxCHECK_MSG( !wxIsEmpty(basename
), false,
526 _T("empty file name in wxFileSystem::FindFileInPath"));
528 // skip path separator in the beginning of the file name if present
529 if ( wxIsPathSeparator(*basename
) )
532 wxStringTokenizer
tokenizer(path
, wxPATH_SEP
);
533 while ( tokenizer
.HasMoreTokens() )
535 wxString strFile
= tokenizer
.GetNextToken();
536 if ( !wxEndsWithPathSeparator(strFile
) )
537 strFile
+= wxFILE_SEP_PATH
;
540 wxFSFile
*file
= OpenFile(strFile
);
552 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
554 // prepend the handler to the beginning of the list because handlers added
555 // last should have the highest priority to allow overriding them
556 m_Handlers
.Insert((size_t)0, handler
);
559 wxFileSystemHandler
* wxFileSystem::RemoveHandler(wxFileSystemHandler
*handler
)
561 // if handler has already been removed (or deleted)
562 // we return NULL. This is by design in case
563 // CleanUpHandlers() is called before RemoveHandler
564 // is called, as we cannot control the order
565 // which modules are unloaded
566 if (!m_Handlers
.DeleteObject(handler
))
573 bool wxFileSystem::HasHandlerForPath(const wxString
&location
)
575 for ( wxList::compatibility_iterator node
= m_Handlers
.GetFirst();
576 node
; node
= node
->GetNext() )
578 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
579 if (h
->CanOpen(location
))
586 void wxFileSystem::CleanUpHandlers()
588 WX_CLEAR_LIST(wxList
, m_Handlers
);
591 static const wxString
g_unixPathString(wxT("/"));
592 static const wxString
g_nativePathString(wxFILE_SEP_PATH
);
594 // Returns the native path for a file URL
595 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
599 if ( path
.Find(wxT("file://")) == 0 )
603 else if ( path
.Find(wxT("file:")) == 0 )
607 // Remove preceding double slash on Mac Classic
608 #if defined(__WXMAC__) && !defined(__UNIX__)
609 else if ( path
.Find(wxT("//")) == 0 )
613 path
.Replace(wxT("%25"), wxT("%"));
614 path
.Replace(wxT("%3A"), wxT(":"));
617 // file urls either start with a forward slash (local harddisk),
618 // otherwise they have a servername/sharename notation,
619 // which only exists on msw and corresponds to a unc
620 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
624 else if ( (url
.Find(wxT("file://")) == 0) &&
625 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
626 (path
.length() > 1) && (path
[1u] != wxT(':')) )
628 path
= wxT("//") + path
;
632 path
.Replace(g_unixPathString
, g_nativePathString
);
634 return wxFileName(path
, wxPATH_NATIVE
);
637 // Returns the file URL for a native path
638 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
640 wxFileName fn
= filename
;
641 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
642 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
645 // unc notation, wxMSW
646 if ( url
.Find(wxT("\\\\")) == 0 )
648 url
= wxT("//") + url
.Mid(2);
652 url
= wxT("/") + url
;
654 url
= wxT("/") + url
;
660 url
.Replace(g_nativePathString
, g_unixPathString
);
661 url
.Replace(wxT("%"), wxT("%25"));
662 url
.Replace(wxT(":"), wxT("%3A"));
663 url
= wxT("file:") + url
;
670 class wxFileSystemModule
: public wxModule
672 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
675 wxFileSystemModule() :
681 virtual bool OnInit()
683 m_handler
= new wxLocalFSHandler
;
684 wxFileSystem::AddHandler(m_handler
);
687 virtual void OnExit()
689 delete wxFileSystem::RemoveHandler(m_handler
);
691 wxFileSystem::CleanUpHandlers();
695 wxFileSystemHandler
* m_handler
;
699 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)