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"
25 #include "wx/wfstream.h"
26 #include "wx/module.h"
27 #include "wx/mimetype.h"
28 #include "wx/filename.h"
31 //--------------------------------------------------------------------------------
32 // wxFileSystemHandler
33 //--------------------------------------------------------------------------------
35 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
38 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
41 wxString loc
= GetRightLocation(location
);
43 int l
= loc
.length(), l2
;
46 for (int i
= l
-1; i
>= 0; i
--)
48 c
= loc
[(unsigned int) i
];
53 ext
= loc
.Right(l2
-i
-1);
56 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
61 static bool s_MinimalMimeEnsured
= false;
62 if (!s_MinimalMimeEnsured
)
64 static const wxFileTypeInfo fallbacks
[] =
66 wxFileTypeInfo(_T("image/jpeg"),
69 _T("JPEG image (from fallback)"),
70 _T("jpg"), _T("jpeg"), _T("JPG"), _T("JPEG"), NULL
),
71 wxFileTypeInfo(_T("image/gif"),
74 _T("GIF image (from fallback)"),
75 _T("gif"), _T("GIF"), NULL
),
76 wxFileTypeInfo(_T("image/png"),
79 _T("PNG image (from fallback)"),
80 _T("png"), _T("PNG"), NULL
),
81 wxFileTypeInfo(_T("image/bmp"),
84 _T("windows bitmap image (from fallback)"),
85 _T("bmp"), _T("BMP"), NULL
),
86 wxFileTypeInfo(_T("text/html"),
89 _T("HTML document (from fallback)"),
90 _T("htm"), _T("html"), _T("HTM"), _T("HTML"), NULL
),
91 // must terminate the table with this!
94 wxTheMimeTypesManager
->AddFallbacks(fallbacks
);
95 s_MinimalMimeEnsured
= true;
98 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
99 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
101 mime
= wxEmptyString
;
108 if ( ext
.IsSameAs(wxT("htm"), false) || ext
.IsSameAs(_T("html"), false) )
109 return wxT("text/html");
110 if ( ext
.IsSameAs(wxT("jpg"), false) || ext
.IsSameAs(_T("jpeg"), false) )
111 return wxT("image/jpeg");
112 if ( ext
.IsSameAs(wxT("gif"), false) )
113 return wxT("image/gif");
114 if ( ext
.IsSameAs(wxT("png"), false) )
115 return wxT("image/png");
116 if ( ext
.IsSameAs(wxT("bmp"), false) )
117 return wxT("image/bmp");
118 return wxEmptyString
;
124 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
126 wxString s
= wxEmptyString
;
127 int i
, l
= location
.length();
130 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
131 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
133 if (!fnd
) return wxT("file");
134 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
139 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
144 for (i
= location
.length()-1; i
>= 0; i
--) {
145 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
146 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
148 return wxEmptyString
;
151 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
153 int i
, l
= location
.length();
158 ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':')));
161 if (location
[i
] == wxT('#')) l2
= i
+ 1;
163 if (i
== 0) return wxEmptyString
;
164 else return location
.Mid(i
+ 1, l2
- i
- 2);
167 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
170 int l
= location
.length();
172 for (int i
= l
-1; i
>= 0; i
--) {
174 if (c
== wxT('#')) return location
.Right(l
-i
-1);
175 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
177 return wxEmptyString
;
181 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
184 return wxEmptyString
;
187 wxString
wxFileSystemHandler::FindNext()
189 return wxEmptyString
;
192 //--------------------------------------------------------------------------------
194 //--------------------------------------------------------------------------------
197 wxString
wxLocalFSHandler::ms_root
;
199 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
201 return GetProtocol(location
) == wxT("file");
204 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
206 // location has Unix path separators
207 wxString right
= GetRightLocation(location
);
208 wxFileName fn
= wxFileSystem::URLToFileName(right
);
209 wxString fullpath
= ms_root
+ fn
.GetFullPath();
211 if (!wxFileExists(fullpath
))
212 return (wxFSFile
*) NULL
;
214 // we need to check whether we can really read from this file, otherwise
215 // wxFSFile is not going to work
216 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
220 return (wxFSFile
*) NULL
;
223 return new wxFSFile(is
,
225 GetMimeTypeFromExt(location
),
228 ,wxDateTime(wxFileModificationTime(fullpath
))
229 #endif // wxUSE_DATETIME
233 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
235 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
236 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
239 wxString
wxLocalFSHandler::FindNext()
241 return wxFindNextFile();
246 //-----------------------------------------------------------------------------
248 //-----------------------------------------------------------------------------
250 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
251 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
254 wxList
wxFileSystem::m_Handlers
;
257 static wxString
MakeCorrectPath(const wxString
& path
)
264 for (i
= 0; i
< cnt
; i
++)
265 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
267 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
269 if (cnt
< 3) return p
;
271 r
<< p
.GetChar(0) << p
.GetChar(1);
273 // skip trailing ../.., if any
274 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
276 // remove back references: translate dir1/../dir2 to dir2
280 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
282 for (j
= r
.length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
283 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
285 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
291 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
297 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
301 m_Path
= MakeCorrectPath(location
);
305 if (m_Path
.length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
311 for (i
= m_Path
.length()-1; i
>= 0; i
--)
313 if (m_Path
[(unsigned int) i
] == wxT('/'))
315 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
326 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
333 for (i
= 0; i
< (int) m_Path
.length(); i
++)
335 if (m_Path
[(unsigned int) i
] == wxT(':'))
341 if (i
== (int) m_Path
.length())
342 m_Path
= wxEmptyString
;
346 m_Path
.Remove(pathpos
+1);
353 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
355 wxString loc
= MakeCorrectPath(location
);
359 wxList::compatibility_iterator node
;
363 for (i
= 0; i
< ln
; i
++)
367 case wxT('/') : case wxT(':') : case wxT('#') :
371 if (meta
!= 0) break;
373 m_LastName
= wxEmptyString
;
375 // try relative paths first :
376 if (meta
!= wxT(':'))
378 node
= m_Handlers
.GetFirst();
381 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
382 if (h
->CanOpen(m_Path
+ loc
))
384 s
= h
->OpenFile(*this, m_Path
+ loc
);
385 if (s
) { m_LastName
= m_Path
+ loc
; break; }
387 node
= node
->GetNext();
391 // if failed, try absolute paths :
394 node
= m_Handlers
.GetFirst();
397 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
400 s
= h
->OpenFile(*this, loc
);
401 if (s
) { m_LastName
= loc
; break; }
403 node
= node
->GetNext();
411 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
413 wxList::compatibility_iterator node
;
414 wxString
spec2(spec
);
416 m_FindFileHandler
= NULL
;
418 for (int i
= spec2
.length()-1; i
>= 0; i
--)
419 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
421 node
= m_Handlers
.GetFirst();
424 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
425 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
426 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
427 node
= node
->GetNext();
430 node
= m_Handlers
.GetFirst();
433 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
434 if (m_FindFileHandler
-> CanOpen(spec2
))
435 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
436 node
= node
->GetNext();
439 return wxEmptyString
;
444 wxString
wxFileSystem::FindNext()
446 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
447 else return m_FindFileHandler
-> FindNext();
452 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
454 m_Handlers
.Append(handler
);
458 void wxFileSystem::CleanUpHandlers()
460 WX_CLEAR_LIST(wxList
, m_Handlers
);
463 static const wxString
g_unixPathString(wxT("/"));
464 static const wxString
g_nativePathString(wxFILE_SEP_PATH
);
466 // Returns the native path for a file URL
467 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
471 if ( path
.Find(wxT("file://")) == 0 )
475 else if ( path
.Find(wxT("file:")) == 0 )
479 // Remove preceding double slash on Mac Classic
480 #if defined(__WXMAC__) && !defined(__UNIX__)
481 else if ( path
.Find(wxT("//")) == 0 )
485 path
.Replace(wxT("%25"), wxT("%"));
486 path
.Replace(wxT("%3A"), wxT(":"));
489 // file urls either start with a forward slash (local harddisk),
490 // otherwise they have a servername/sharename notation,
491 // which only exists on msw and corresponds to a unc
492 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
496 else if ( (url
.Find(wxT("file://")) == 0) &&
497 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
498 (path
.length() > 1) && (path
[1u] != wxT(':')) )
500 path
= wxT("//") + path
;
504 path
.Replace(g_unixPathString
, g_nativePathString
);
506 return wxFileName(path
, wxPATH_NATIVE
);
509 // Returns the file URL for a native path
510 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
512 wxFileName fn
= filename
;
513 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
514 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
517 // unc notation, wxMSW
518 if ( url
.Find(wxT("\\\\")) == 0 )
520 url
= wxT("//") + url
.Mid(2);
524 url
= wxT("/") + url
;
526 url
= wxT("/") + url
;
532 url
.Replace(g_nativePathString
, g_unixPathString
);
533 url
.Replace(wxT("%"), wxT("%25"));
534 url
.Replace(wxT(":"), wxT("%3A"));
535 url
= wxT("file:") + url
;
542 class wxFileSystemModule
: public wxModule
544 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
547 virtual bool OnInit()
549 wxFileSystem::AddHandler(new wxLocalFSHandler
);
552 virtual void OnExit()
554 wxFileSystem::CleanUpHandlers();
558 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)