1 /////////////////////////////////////////////////////////////////////////////
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/wfstream.h"
20 #include "wx/module.h"
21 #include "wx/filesys.h"
22 #include "wx/mimetype.h"
23 #include "wx/filename.h"
27 //--------------------------------------------------------------------------------
28 // wxFileSystemHandler
29 //--------------------------------------------------------------------------------
31 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
34 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
37 wxString loc
= GetRightLocation(location
);
39 int l
= loc
.Length(), l2
;
42 for (int i
= l
-1; i
>= 0; i
--)
44 c
= loc
[(unsigned int) i
];
49 ext
= loc
.Right(l2
-i
-1);
52 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
57 static bool s_MinimalMimeEnsured
= false;
58 if (!s_MinimalMimeEnsured
)
60 static const wxFileTypeInfo fallbacks
[] =
62 wxFileTypeInfo(_T("image/jpeg"),
65 _T("JPEG image (from fallback)"),
66 _T("jpg"), _T("jpeg"), _T("JPG"), _T("JPEG"), NULL
),
67 wxFileTypeInfo(_T("image/gif"),
70 _T("GIF image (from fallback)"),
71 _T("gif"), _T("GIF"), NULL
),
72 wxFileTypeInfo(_T("image/png"),
75 _T("PNG image (from fallback)"),
76 _T("png"), _T("PNG"), NULL
),
77 wxFileTypeInfo(_T("image/bmp"),
80 _T("windows bitmap image (from fallback)"),
81 _T("bmp"), _T("BMP"), NULL
),
82 wxFileTypeInfo(_T("text/html"),
85 _T("HTML document (from fallback)"),
86 _T("htm"), _T("html"), _T("HTM"), _T("HTML"), NULL
),
87 // must terminate the table with this!
90 wxTheMimeTypesManager
->AddFallbacks(fallbacks
);
91 s_MinimalMimeEnsured
= true;
94 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
95 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
104 if ( ext
.IsSameAs(wxT("htm"), false) || ext
.IsSameAs(_T("html"), false) )
105 return wxT("text/html");
106 if ( ext
.IsSameAs(wxT("jpg"), false) || ext
.IsSameAs(_T("jpeg"), false) )
107 return wxT("image/jpeg");
108 if ( ext
.IsSameAs(wxT("gif"), false) )
109 return wxT("image/gif");
110 if ( ext
.IsSameAs(wxT("png"), false) )
111 return wxT("image/png");
112 if ( ext
.IsSameAs(wxT("bmp"), false) )
113 return wxT("image/bmp");
114 return wxEmptyString
;
120 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
122 wxString s
= wxEmptyString
;
123 int i
, l
= location
.Length();
126 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
127 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
129 if (!fnd
) return wxT("file");
130 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
135 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
140 for (i
= location
.Length()-1; i
>= 0; i
--) {
141 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= true;
142 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
144 return wxEmptyString
;
147 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
149 int i
, l
= location
.Length();
154 ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':')));
157 if (location
[i
] == wxT('#')) l2
= i
+ 1;
159 if (i
== 0) return wxEmptyString
;
160 else return location
.Mid(i
+ 1, l2
- i
- 2);
163 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
166 int l
= location
.Length();
168 for (int i
= l
-1; i
>= 0; i
--) {
170 if (c
== wxT('#')) return location
.Right(l
-i
-1);
171 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
173 return wxEmptyString
;
177 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
180 return wxEmptyString
;
183 wxString
wxFileSystemHandler::FindNext()
185 return wxEmptyString
;
188 //--------------------------------------------------------------------------------
190 //--------------------------------------------------------------------------------
193 wxString
wxLocalFSHandler::ms_root
;
195 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
197 return GetProtocol(location
) == wxT("file");
200 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
202 // location has Unix path separators
203 wxString right
= GetRightLocation(location
);
204 wxFileName fn
= wxFileSystem::URLToFileName(right
);
205 wxString fullpath
= ms_root
+ fn
.GetFullPath();
207 if (!wxFileExists(fullpath
))
208 return (wxFSFile
*) NULL
;
210 // we need to check whether we can really read from this file, otherwise
211 // wxFSFile is not going to work
212 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
216 return (wxFSFile
*) NULL
;
219 return new wxFSFile(is
,
221 GetMimeTypeFromExt(location
),
224 ,wxDateTime(wxFileModificationTime(fullpath
))
225 #endif // wxUSE_DATETIME
229 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
231 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
232 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
235 wxString
wxLocalFSHandler::FindNext()
237 return wxFindNextFile();
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
246 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
247 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
250 wxList
wxFileSystem::m_Handlers
;
253 static wxString
MakeCorrectPath(const wxString
& path
)
260 for (i
= 0; i
< cnt
; i
++)
261 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
263 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
265 if (cnt
< 3) return p
;
267 r
<< p
.GetChar(0) << p
.GetChar(1);
269 // skip trailing ../.., if any
270 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
272 // remove back references: translate dir1/../dir2 to dir2
276 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
278 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
279 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
281 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
287 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
293 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
297 m_Path
= MakeCorrectPath(location
);
301 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
307 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
309 if (m_Path
[(unsigned int) i
] == wxT('/'))
311 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
322 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
329 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
331 if (m_Path
[(unsigned int) i
] == wxT(':'))
337 if (i
== (int) m_Path
.Length())
338 m_Path
= wxEmptyString
;
342 m_Path
.Remove(pathpos
+1);
349 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
351 wxString loc
= MakeCorrectPath(location
);
355 wxList::compatibility_iterator node
;
359 for (i
= 0; i
< ln
; i
++)
363 case wxT('/') : case wxT(':') : case wxT('#') :
367 if (meta
!= 0) break;
369 m_LastName
= wxEmptyString
;
371 // try relative paths first :
372 if (meta
!= wxT(':'))
374 node
= m_Handlers
.GetFirst();
377 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
378 if (h
->CanOpen(m_Path
+ loc
))
380 s
= h
->OpenFile(*this, m_Path
+ loc
);
381 if (s
) { m_LastName
= m_Path
+ loc
; break; }
383 node
= node
->GetNext();
387 // if failed, try absolute paths :
390 node
= m_Handlers
.GetFirst();
393 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
396 s
= h
->OpenFile(*this, loc
);
397 if (s
) { m_LastName
= loc
; break; }
399 node
= node
->GetNext();
407 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
409 wxList::compatibility_iterator node
;
410 wxString
spec2(spec
);
412 m_FindFileHandler
= NULL
;
414 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
415 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
417 node
= m_Handlers
.GetFirst();
420 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
421 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
422 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
423 node
= node
->GetNext();
426 node
= m_Handlers
.GetFirst();
429 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
430 if (m_FindFileHandler
-> CanOpen(spec2
))
431 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
432 node
= node
->GetNext();
435 return wxEmptyString
;
440 wxString
wxFileSystem::FindNext()
442 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
443 else return m_FindFileHandler
-> FindNext();
448 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
450 m_Handlers
.Append(handler
);
454 void wxFileSystem::CleanUpHandlers()
456 WX_CLEAR_LIST(wxList
, m_Handlers
);
459 static const wxString
g_unixPathString(wxT("/"));
460 static const wxString
g_nativePathString(wxFILE_SEP_PATH
);
462 // Returns the native path for a file URL
463 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
467 if ( path
.Find(wxT("file://")) == 0 )
471 else if ( path
.Find(wxT("file:")) == 0 )
475 // Remove preceding double slash on Mac Classic
476 #if defined(__WXMAC__) && !defined(__UNIX__)
477 else if ( path
.Find(wxT("//")) == 0 )
481 path
.Replace(wxT("%25"), wxT("%"));
482 path
.Replace(wxT("%3A"), wxT(":"));
485 // file urls either start with a forward slash (local harddisk),
486 // otherwise they have a servername/sharename notation,
487 // which only exists on msw and corresponds to a unc
488 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
492 else if ( (url
.Find(wxT("file://")) == 0) &&
493 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
494 (path
.Length() > 1) && (path
[1u] != wxT(':')) )
496 path
= wxT("//") + path
;
500 path
.Replace(g_unixPathString
, g_nativePathString
);
502 return wxFileName(path
, wxPATH_NATIVE
);
505 // Returns the file URL for a native path
506 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
508 wxFileName fn
= filename
;
509 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
510 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
513 // unc notation, wxMSW
514 if ( url
.Find(wxT("\\\\")) == 0 )
516 url
= wxT("//") + url
.Mid(2);
520 url
= wxT("/") + url
;
522 url
= wxT("/") + url
;
528 url
.Replace(g_nativePathString
, g_unixPathString
);
529 url
.Replace(wxT("%"), wxT("%25"));
530 url
.Replace(wxT(":"), wxT("%3A"));
531 url
= wxT("file:") + url
;
538 class wxFileSystemModule
: public wxModule
540 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
543 virtual bool OnInit()
545 wxFileSystem::AddHandler(new wxLocalFSHandler
);
548 virtual void OnExit()
550 wxFileSystem::CleanUpHandlers();
554 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)