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) ||
161 (location
[i
-2] == wxT(':'))
163 || ((i
> 3) && location
[i
-4] == wxT(':')) // E.g. file://C:/thing.jpg
168 if (location
[i
] == wxT('#')) l2
= i
+ 1;
171 // We may now have //c:/thing.jpg
172 if (((i
+ 4) < l
) && location
[i
+1] == wxT('/') && location
[i
+2] == wxT('/') &&
173 location
[i
+4] == wxT(':'))
178 if (i
== 0) return wxEmptyString
;
179 else return location
.Mid(i
+ 1, l2
- i
- 2);
182 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
185 int l
= location
.Length();
187 for (int i
= l
-1; i
>= 0; i
--) {
189 if (c
== wxT('#')) return location
.Right(l
-i
-1);
190 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
192 return wxEmptyString
;
196 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
199 return wxEmptyString
;
202 wxString
wxFileSystemHandler::FindNext()
204 return wxEmptyString
;
207 //--------------------------------------------------------------------------------
209 //--------------------------------------------------------------------------------
212 wxString
wxLocalFSHandler::ms_root
;
214 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
216 return GetProtocol(location
) == wxT("file");
219 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
221 // location has Unix path separators
222 wxString right
= GetRightLocation(location
);
223 wxFileName fn
= wxFileSystem::URLToFileName(right
);
224 wxString fullpath
= ms_root
+ fn
.GetFullPath();
226 if (!wxFileExists(fullpath
))
227 return (wxFSFile
*) NULL
;
229 // we need to check whether we can really read from this file, otherwise
230 // wxFSFile is not going to work
231 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
235 return (wxFSFile
*) NULL
;
238 return new wxFSFile(is
,
240 GetMimeTypeFromExt(location
),
243 ,wxDateTime(wxFileModificationTime(fullpath
))
244 #endif // wxUSE_DATETIME
248 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
250 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
251 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
254 wxString
wxLocalFSHandler::FindNext()
256 return wxFindNextFile();
261 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
266 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
269 wxList
wxFileSystem::m_Handlers
;
272 static wxString
MakeCorrectPath(const wxString
& path
)
279 for (i
= 0; i
< cnt
; i
++)
280 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
282 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
284 if (cnt
< 3) return p
;
286 r
<< p
.GetChar(0) << p
.GetChar(1);
288 // skip trailing ../.., if any
289 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
291 // remove back references: translate dir1/../dir2 to dir2
295 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
297 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
298 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
300 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
306 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
312 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
316 m_Path
= MakeCorrectPath(location
);
320 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
326 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
328 if (m_Path
[(unsigned int) i
] == wxT('/'))
330 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
341 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
348 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
350 if (m_Path
[(unsigned int) i
] == wxT(':'))
356 if (i
== (int) m_Path
.Length())
357 m_Path
= wxEmptyString
;
361 m_Path
.Remove(pathpos
+1);
368 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
370 wxString loc
= MakeCorrectPath(location
);
374 wxList::compatibility_iterator node
;
378 for (i
= 0; i
< ln
; i
++)
382 case wxT('/') : case wxT(':') : case wxT('#') :
386 if (meta
!= 0) break;
388 m_LastName
= wxEmptyString
;
390 // try relative paths first :
391 if (meta
!= wxT(':'))
393 node
= m_Handlers
.GetFirst();
396 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
397 if (h
->CanOpen(m_Path
+ loc
))
399 s
= h
->OpenFile(*this, m_Path
+ loc
);
400 if (s
) { m_LastName
= m_Path
+ loc
; break; }
402 node
= node
->GetNext();
406 // if failed, try absolute paths :
409 node
= m_Handlers
.GetFirst();
412 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
415 s
= h
->OpenFile(*this, loc
);
416 if (s
) { m_LastName
= loc
; break; }
418 node
= node
->GetNext();
426 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
428 wxList::compatibility_iterator node
;
429 wxString
spec2(spec
);
431 m_FindFileHandler
= NULL
;
433 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
434 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
436 node
= m_Handlers
.GetFirst();
439 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
440 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
441 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
442 node
= node
->GetNext();
445 node
= m_Handlers
.GetFirst();
448 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
449 if (m_FindFileHandler
-> CanOpen(spec2
))
450 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
451 node
= node
->GetNext();
454 return wxEmptyString
;
459 wxString
wxFileSystem::FindNext()
461 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
462 else return m_FindFileHandler
-> FindNext();
467 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
469 m_Handlers
.Append(handler
);
473 void wxFileSystem::CleanUpHandlers()
475 WX_CLEAR_LIST(wxList
, m_Handlers
);
478 const static wxString
g_unixPathString(wxT("/"));
479 const static wxString
g_nativePathString(wxFILE_SEP_PATH
);
481 // Returns the native path for a file URL
482 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
486 if ( path
.Find(wxT("file://")) == 0 )
490 else if ( path
.Find(wxT("file:")) == 0 )
494 // Remove preceding double slash on Mac Classic
495 #if defined(__WXMAC__) && !defined(__UNIX__)
496 else if ( path
.Find(wxT("//")) == 0 )
500 path
.Replace(wxT("%25"), wxT("%"));
501 path
.Replace(wxT("%3A"), wxT(":"));
504 // file urls either start with a forward slash (local harddisk),
505 // otherwise they have a servername/sharename notation,
506 // which only exists on msw and corresponds to a unc
507 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
511 else if ( (url
.Find(wxT("file://")) == 0) &&
512 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
513 (path
.Length() > 1) && (path
[1u] != wxT(':')) )
515 path
= wxT("//") + path
;
519 path
.Replace(g_unixPathString
, g_nativePathString
);
521 return wxFileName(path
, wxPATH_NATIVE
);
524 // Returns the file URL for a native path
525 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
527 wxFileName fn
= filename
;
528 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
529 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
532 // unc notation, wxMSW
533 if ( url
.Find(wxT("\\\\")) == 0 )
539 url
= wxT("/") + url
;
541 url
= wxT("/") + url
;
547 url
.Replace(g_nativePathString
, g_unixPathString
);
548 url
.Replace(wxT("%"), wxT("%25"));
549 url
.Replace(wxT(":"), wxT("%3A"));
550 url
= wxT("file:") + url
;
557 class wxFileSystemModule
: public wxModule
559 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
562 virtual bool OnInit()
564 wxFileSystem::AddHandler(new wxLocalFSHandler
);
567 virtual void OnExit()
569 wxFileSystem::CleanUpHandlers();
573 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)