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
)
39 static wxFileTypeInfo
*gs_FSMimeFallbacks
= NULL
;
42 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
45 wxString loc
= GetRightLocation(location
);
47 int l
= loc
.Length(), l2
;
50 for (int i
= l
-1; i
>= 0; i
--)
52 c
= loc
[(unsigned int) i
];
57 ext
= loc
.Right(l2
-i
-1);
60 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
65 static bool s_MinimalMimeEnsured
= FALSE
;
66 if (!s_MinimalMimeEnsured
) {
67 wxTheMimeTypesManager
->AddFallbacks(gs_FSMimeFallbacks
);
68 s_MinimalMimeEnsured
= TRUE
;
71 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
72 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
81 if ( ext
.IsSameAs(wxT("htm"), FALSE
) || ext
.IsSameAs(_T("html"), FALSE
) )
82 return wxT("text/html");
83 if ( ext
.IsSameAs(wxT("jpg"), FALSE
) || ext
.IsSameAs(_T("jpeg"), FALSE
) )
84 return wxT("image/jpeg");
85 if ( ext
.IsSameAs(wxT("gif"), FALSE
) )
86 return wxT("image/gif");
87 if ( ext
.IsSameAs(wxT("png"), FALSE
) )
88 return wxT("image/png");
89 if ( ext
.IsSameAs(wxT("bmp"), FALSE
) )
90 return wxT("image/bmp");
97 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
99 wxString s
= wxEmptyString
;
100 int i
, l
= location
.Length();
104 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
105 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
107 if (!fnd
) return wxT("file");
108 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
113 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
119 for (i
= location
.Length()-1; i
>= 0; i
--) {
120 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
121 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
123 return wxEmptyString
;
126 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
128 int i
, l
= location
.Length();
133 ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':')));
136 if (location
[i
] == wxT('#')) l2
= i
+ 1;
138 if (i
== 0) return wxEmptyString
;
139 else return location
.Mid(i
+ 1, l2
- i
- 2);
142 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
145 int l
= location
.Length();
147 for (int i
= l
-1; i
>= 0; i
--) {
149 if (c
== wxT('#')) return location
.Right(l
-i
-1);
150 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
152 return wxEmptyString
;
156 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
159 return wxEmptyString
;
162 wxString
wxFileSystemHandler::FindNext()
164 return wxEmptyString
;
167 //--------------------------------------------------------------------------------
169 //--------------------------------------------------------------------------------
172 wxString
wxLocalFSHandler::ms_root
;
174 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
176 return GetProtocol(location
) == wxT("file");
179 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
181 // location has Unix path separators
182 wxString right
= GetRightLocation(location
);
183 wxFileName fn
= wxFileSystem::URLToFileName(right
);
184 wxString fullpath
= ms_root
+ fn
.GetFullPath();
186 if (!wxFileExists(fullpath
))
187 return (wxFSFile
*) NULL
;
189 // we need to check whether we can really read from this file, otherwise
190 // wxFSFile is not going to work
191 wxFFileInputStream
*is
= new wxFFileInputStream(fullpath
);
195 return (wxFSFile
*) NULL
;
198 return new wxFSFile(is
,
200 GetMimeTypeFromExt(location
),
203 ,wxDateTime(wxFileModificationTime(fullpath
))
204 #endif // wxUSE_DATETIME
208 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
210 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
211 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
214 wxString
wxLocalFSHandler::FindNext()
216 return wxFindNextFile();
221 //-----------------------------------------------------------------------------
223 //-----------------------------------------------------------------------------
225 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
226 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
229 wxList
wxFileSystem::m_Handlers
;
232 static wxString
MakeCorrectPath(const wxString
& path
)
239 for (i
= 0; i
< cnt
; i
++)
240 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
242 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
244 if (cnt
< 3) return p
;
246 r
<< p
.GetChar(0) << p
.GetChar(1);
248 // skip trailing ../.., if any
249 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
251 // remove back references: translate dir1/../dir2 to dir2
255 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
257 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
258 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
260 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
266 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
272 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
276 m_Path
= MakeCorrectPath(location
);
280 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
286 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
288 if (m_Path
[(unsigned int) i
] == wxT('/'))
290 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
301 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
308 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
310 if (m_Path
[(unsigned int) i
] == wxT(':'))
316 if (i
== (int) m_Path
.Length())
317 m_Path
= wxEmptyString
;
321 m_Path
.Remove(pathpos
+1);
328 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
330 wxString loc
= MakeCorrectPath(location
);
334 wxList::compatibility_iterator node
;
338 for (i
= 0; i
< ln
; i
++)
342 case wxT('/') : case wxT(':') : case wxT('#') :
346 if (meta
!= 0) break;
348 m_LastName
= wxEmptyString
;
350 // try relative paths first :
351 if (meta
!= wxT(':'))
353 node
= m_Handlers
.GetFirst();
356 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
357 if (h
->CanOpen(m_Path
+ loc
))
359 s
= h
->OpenFile(*this, m_Path
+ loc
);
360 if (s
) { m_LastName
= m_Path
+ loc
; break; }
362 node
= node
->GetNext();
366 // if failed, try absolute paths :
369 node
= m_Handlers
.GetFirst();
372 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
375 s
= h
->OpenFile(*this, loc
);
376 if (s
) { m_LastName
= loc
; break; }
378 node
= node
->GetNext();
386 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
388 wxList::compatibility_iterator node
;
389 wxString
spec2(spec
);
391 m_FindFileHandler
= NULL
;
393 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
394 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
396 node
= m_Handlers
.GetFirst();
399 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
400 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
401 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
402 node
= node
->GetNext();
405 node
= m_Handlers
.GetFirst();
408 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
409 if (m_FindFileHandler
-> CanOpen(spec2
))
410 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
411 node
= node
->GetNext();
414 return wxEmptyString
;
419 wxString
wxFileSystem::FindNext()
421 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
422 else return m_FindFileHandler
-> FindNext();
427 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
429 m_Handlers
.Append(handler
);
433 void wxFileSystem::CleanUpHandlers()
435 WX_CLEAR_LIST(wxList
, m_Handlers
);
438 const static wxString
g_unixPathString(wxT("/"));
439 const static wxString
g_nativePathString(wxFILE_SEP_PATH
);
441 // Returns the native path for a file URL
442 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
446 if ( path
.Find(wxT("file://")) == 0 )
450 else if ( path
.Find(wxT("file:")) == 0 )
454 // Remove preceding double slash on Mac Classic
455 #if defined(__WXMAC__) && !defined(__UNIX__)
456 else if ( path
.Find(wxT("//")) == 0 )
460 path
.Replace(wxT("%25"), wxT("%"));
461 path
.Replace(wxT("%3A"), wxT(":"));
464 // file urls either start with a forward slash (local harddisk),
465 // otherwise they have a servername/sharename notation,
466 // which only exists on msw and corresponds to a unc
467 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
471 else if ( (url
.Find(wxT("file://")) == 0) &&
472 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
473 (path
.Length() > 1) && (path
[1u] != wxT(':')) )
475 path
= wxT("//") + path
;
479 path
.Replace(g_unixPathString
, g_nativePathString
);
481 return wxFileName(path
, wxPATH_NATIVE
);
484 // Returns the file URL for a native path
485 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
487 wxFileName fn
= filename
;
488 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
489 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
492 // unc notation, wxMSW
493 if ( url
.Find(wxT("\\\\")) == 0 )
499 url
= wxT("/") + url
;
501 url
= wxT("/") + url
;
507 url
.Replace(g_nativePathString
, g_unixPathString
);
508 url
.Replace(wxT("%"), wxT("%25"));
509 url
.Replace(wxT(":"), wxT("%3A"));
510 url
= wxT("file:") + url
;
517 class wxFileSystemModule
: public wxModule
519 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
522 virtual bool OnInit()
524 wxFileSystem::AddHandler(new wxLocalFSHandler
);
527 gs_FSMimeFallbacks
= new wxFileTypeInfo
[6];
528 gs_FSMimeFallbacks
[0] =
529 wxFileTypeInfo(_T("image/jpeg"),
532 _T("JPEG image (from fallback)"),
533 _T("jpg"), _T("jpeg"), NULL
);
534 gs_FSMimeFallbacks
[1] =
535 wxFileTypeInfo(_T("image/gif"),
538 _T("GIF image (from fallback)"),
540 gs_FSMimeFallbacks
[2] =
541 wxFileTypeInfo(_T("image/png"),
544 _T("PNG image (from fallback)"),
546 gs_FSMimeFallbacks
[3] =
547 wxFileTypeInfo(_T("image/bmp"),
550 _T("windows bitmap image (from fallback)"),
552 gs_FSMimeFallbacks
[4] =
553 wxFileTypeInfo(_T("text/html"),
556 _T("HTML document (from fallback)"),
557 _T("htm"), _T("html"), NULL
);
558 gs_FSMimeFallbacks
[5] =
559 // must terminate the table with this!
564 virtual void OnExit()
567 delete [] gs_FSMimeFallbacks
;
569 wxFileSystem::CleanUpHandlers();
573 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)