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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filesys.h"
14 #include "wx/wxprec.h"
23 #include "wx/wfstream.h"
24 #include "wx/module.h"
25 #include "wx/filesys.h"
26 #include "wx/mimetype.h"
27 #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();
131 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
132 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
134 if (!fnd
) return wxT("file");
135 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
140 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
218 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
222 return (wxFSFile
*) NULL
;
225 return new wxFSFile(is
,
227 GetMimeTypeFromExt(location
),
230 ,wxDateTime(wxFileModificationTime(fullpath
))
231 #endif // wxUSE_DATETIME
235 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
237 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
238 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
241 wxString
wxLocalFSHandler::FindNext()
243 return wxFindNextFile();
248 //-----------------------------------------------------------------------------
250 //-----------------------------------------------------------------------------
252 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
253 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
256 wxList
wxFileSystem::m_Handlers
;
259 static wxString
MakeCorrectPath(const wxString
& path
)
266 for (i
= 0; i
< cnt
; i
++)
267 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
269 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
271 if (cnt
< 3) return p
;
273 r
<< p
.GetChar(0) << p
.GetChar(1);
275 // skip trailing ../.., if any
276 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
278 // remove back references: translate dir1/../dir2 to dir2
282 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
284 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
285 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
287 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
293 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
299 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
303 m_Path
= MakeCorrectPath(location
);
307 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
313 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
315 if (m_Path
[(unsigned int) i
] == wxT('/'))
317 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
328 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
335 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
337 if (m_Path
[(unsigned int) i
] == wxT(':'))
343 if (i
== (int) m_Path
.Length())
344 m_Path
= wxEmptyString
;
348 m_Path
.Remove(pathpos
+1);
355 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
357 wxString loc
= MakeCorrectPath(location
);
361 wxList::compatibility_iterator node
;
365 for (i
= 0; i
< ln
; i
++)
369 case wxT('/') : case wxT(':') : case wxT('#') :
373 if (meta
!= 0) break;
375 m_LastName
= wxEmptyString
;
377 // try relative paths first :
378 if (meta
!= wxT(':'))
380 node
= m_Handlers
.GetFirst();
383 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
384 if (h
->CanOpen(m_Path
+ loc
))
386 s
= h
->OpenFile(*this, m_Path
+ loc
);
387 if (s
) { m_LastName
= m_Path
+ loc
; break; }
389 node
= node
->GetNext();
393 // if failed, try absolute paths :
396 node
= m_Handlers
.GetFirst();
399 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
402 s
= h
->OpenFile(*this, loc
);
403 if (s
) { m_LastName
= loc
; break; }
405 node
= node
->GetNext();
413 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
415 wxList::compatibility_iterator node
;
416 wxString
spec2(spec
);
418 m_FindFileHandler
= NULL
;
420 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
421 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
423 node
= m_Handlers
.GetFirst();
426 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
427 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
428 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
429 node
= node
->GetNext();
432 node
= m_Handlers
.GetFirst();
435 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
436 if (m_FindFileHandler
-> CanOpen(spec2
))
437 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
438 node
= node
->GetNext();
441 return wxEmptyString
;
446 wxString
wxFileSystem::FindNext()
448 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
449 else return m_FindFileHandler
-> FindNext();
454 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
456 m_Handlers
.Append(handler
);
460 void wxFileSystem::CleanUpHandlers()
462 WX_CLEAR_LIST(wxList
, m_Handlers
);
465 const static wxString
g_unixPathString(wxT("/"));
466 const static wxString
g_nativePathString(wxFILE_SEP_PATH
);
468 // Returns the native path for a file URL
469 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
473 if ( path
.Find(wxT("file://")) == 0 )
477 else if ( path
.Find(wxT("file:")) == 0 )
481 // Remove preceding double slash on Mac Classic
482 #if defined(__WXMAC__) && !defined(__UNIX__)
483 else if ( path
.Find(wxT("//")) == 0 )
487 path
.Replace(wxT("%25"), wxT("%"));
488 path
.Replace(wxT("%3A"), wxT(":"));
491 // file urls either start with a forward slash (local harddisk),
492 // otherwise they have a servername/sharename notation,
493 // which only exists on msw and corresponds to a unc
494 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
498 else if ( (url
.Find(wxT("file://")) == 0) &&
499 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
500 (path
.Length() > 1) && (path
[1u] != wxT(':')) )
502 path
= wxT("//") + path
;
506 path
.Replace(g_unixPathString
, g_nativePathString
);
508 return wxFileName(path
, wxPATH_NATIVE
);
511 // Returns the file URL for a native path
512 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
514 wxFileName fn
= filename
;
515 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
516 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
519 // unc notation, wxMSW
520 if ( url
.Find(wxT("\\\\")) == 0 )
526 url
= wxT("/") + url
;
528 url
= wxT("/") + url
;
534 url
.Replace(g_nativePathString
, g_unixPathString
);
535 url
.Replace(wxT("%"), wxT("%25"));
536 url
.Replace(wxT(":"), wxT("%3A"));
537 url
= wxT("file:") + url
;
544 class wxFileSystemModule
: public wxModule
546 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
549 virtual bool OnInit()
551 wxFileSystem::AddHandler(new wxLocalFSHandler
);
554 virtual void OnExit()
556 wxFileSystem::CleanUpHandlers();
560 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)