]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileSystem class - interface for opening files
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
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"
32 //--------------------------------------------------------------------------------
33 // wxFileSystemHandler
34 //--------------------------------------------------------------------------------
36 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
40 static wxFileTypeInfo
*gs_FSMimeFallbacks
= NULL
;
43 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
46 wxString loc
= GetRightLocation(location
);
48 int l
= loc
.Length(), l2
;
51 for (int i
= l
-1; i
>= 0; i
--)
53 c
= loc
[(unsigned int) i
];
58 ext
= loc
.Right(l2
-i
-1);
61 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
66 static bool s_MinimalMimeEnsured
= FALSE
;
67 if (!s_MinimalMimeEnsured
) {
68 wxTheMimeTypesManager
->AddFallbacks(gs_FSMimeFallbacks
);
69 s_MinimalMimeEnsured
= TRUE
;
72 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
73 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
82 if ( ext
.IsSameAs(wxT("htm"), FALSE
) || ext
.IsSameAs(_T("html"), FALSE
) )
83 return wxT("text/html");
84 if ( ext
.IsSameAs(wxT("jpg"), FALSE
) || ext
.IsSameAs(_T("jpeg"), FALSE
) )
85 return wxT("image/jpeg");
86 if ( ext
.IsSameAs(wxT("gif"), FALSE
) )
87 return wxT("image/gif");
88 if ( ext
.IsSameAs(wxT("png"), FALSE
) )
89 return wxT("image/png");
90 if ( ext
.IsSameAs(wxT("bmp"), FALSE
) )
91 return wxT("image/bmp");
98 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
100 wxString s
= wxEmptyString
;
101 int i
, l
= location
.Length();
105 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
106 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
108 if (!fnd
) return wxT("file");
109 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
114 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
120 for (i
= location
.Length()-1; i
>= 0; i
--) {
121 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
122 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
124 return wxEmptyString
;
127 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
129 int i
, l
= location
.Length();
134 ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':')));
137 if (location
[i
] == wxT('#')) l2
= i
+ 1;
139 if (i
== 0) return wxEmptyString
;
140 else return location
.Mid(i
+ 1, l2
- i
- 2);
143 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
146 int l
= location
.Length();
148 for (int i
= l
-1; i
>= 0; i
--) {
150 if (c
== wxT('#')) return location
.Right(l
-i
-1);
151 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
153 return wxEmptyString
;
157 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
160 return wxEmptyString
;
163 wxString
wxFileSystemHandler::FindNext()
165 return wxEmptyString
;
168 //--------------------------------------------------------------------------------
170 //--------------------------------------------------------------------------------
173 wxString
wxLocalFSHandler::ms_root
;
175 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
177 return GetProtocol(location
) == wxT("file");
180 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
182 // location has Unix path separators
183 wxString right
= GetRightLocation(location
);
184 wxFileName fn
= wxFileSystem::URLToFileName(right
);
185 wxString fullpath
= ms_root
+ fn
.GetFullPath();
187 if (!wxFileExists(fullpath
))
188 return (wxFSFile
*) NULL
;
190 return new wxFSFile(new wxFFileInputStream(fullpath
),
192 GetMimeTypeFromExt(location
),
194 wxDateTime(wxFileModificationTime(fullpath
)));
197 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
199 wxFileName fn
= wxFileSystem::URLToFileName(GetRightLocation(spec
));
200 return wxFindFirstFile(ms_root
+ fn
.GetFullPath(), flags
);
203 wxString
wxLocalFSHandler::FindNext()
205 return wxFindNextFile();
210 //-----------------------------------------------------------------------------
212 //-----------------------------------------------------------------------------
214 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
215 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
218 wxList
wxFileSystem::m_Handlers
;
221 static wxString
MakeCorrectPath(const wxString
& path
)
228 for (i
= 0; i
< cnt
; i
++)
229 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
231 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
233 if (cnt
< 3) return p
;
235 r
<< p
.GetChar(0) << p
.GetChar(1);
237 // skip trailing ../.., if any
238 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
240 // remove back references: translate dir1/../dir2 to dir2
244 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
246 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
247 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
249 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
255 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
261 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
265 m_Path
= MakeCorrectPath(location
);
269 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
275 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
277 if (m_Path
[(unsigned int) i
] == wxT('/'))
279 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
290 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
297 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
299 if (m_Path
[(unsigned int) i
] == wxT(':'))
305 if (i
== (int) m_Path
.Length())
306 m_Path
= wxEmptyString
;
310 m_Path
.Remove(pathpos
+1);
317 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
319 wxString loc
= MakeCorrectPath(location
);
327 for (i
= 0; i
< ln
; i
++)
331 case wxT('/') : case wxT(':') : case wxT('#') :
335 if (meta
!= 0) break;
337 m_LastName
= wxEmptyString
;
339 // try relative paths first :
340 if (meta
!= wxT(':'))
342 node
= m_Handlers
.GetFirst();
345 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
346 if (h
->CanOpen(m_Path
+ loc
))
348 s
= h
->OpenFile(*this, m_Path
+ loc
);
349 if (s
) { m_LastName
= m_Path
+ loc
; break; }
351 node
= node
->GetNext();
355 // if failed, try absolute paths :
358 node
= m_Handlers
.GetFirst();
361 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
364 s
= h
->OpenFile(*this, loc
);
365 if (s
) { m_LastName
= loc
; break; }
367 node
= node
->GetNext();
375 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
378 wxString
spec2(spec
);
380 m_FindFileHandler
= NULL
;
382 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
383 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
385 node
= m_Handlers
.GetFirst();
388 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
389 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
390 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
391 node
= node
->GetNext();
394 node
= m_Handlers
.GetFirst();
397 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
398 if (m_FindFileHandler
-> CanOpen(spec2
))
399 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
400 node
= node
->GetNext();
403 return wxEmptyString
;
408 wxString
wxFileSystem::FindNext()
410 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
411 else return m_FindFileHandler
-> FindNext();
416 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
418 m_Handlers
.Append(handler
);
422 void wxFileSystem::CleanUpHandlers()
424 m_Handlers
.DeleteContents(TRUE
);
428 const static wxString
g_unixPathString(wxT("/"));
429 const static wxString
g_nativePathString(wxFILE_SEP_PATH
);
431 // Returns the native path for a file URL
432 wxFileName
wxFileSystem::URLToFileName(const wxString
& url
)
436 if ( path
.Find(wxT("file://")) == 0 )
440 else if ( path
.Find(wxT("file:")) == 0 )
444 // Remove preceding double slash on Mac Classic
445 #if defined(__WXMAC__) && !defined(__UNIX__)
446 else if ( path
.Find(wxT("//")) == 0 )
450 path
.Replace(wxT("%25"), wxT("%"));
451 path
.Replace(wxT("%3A"), wxT(":"));
454 // file urls either start with a forward slash (local harddisk),
455 // otherwise they have a servername/sharename notation,
456 // which only exists on msw and corresponds to a unc
457 if ( path
[0u] == wxT('/') && path
[1u] != wxT('/'))
461 else if ( (url
.Find(wxT("file://")) == 0) &&
462 (path
.Find(wxT('/')) != wxNOT_FOUND
) &&
463 (path
.Length() > 1) && (path
[1u] != wxT(':')) )
465 path
= wxT("//") + path
;
469 path
.Replace(g_unixPathString
, g_nativePathString
);
471 return wxFileName(path
, wxPATH_NATIVE
);
474 // Returns the file URL for a native path
475 wxString
wxFileSystem::FileNameToURL(const wxFileName
& filename
)
477 wxFileName fn
= filename
;
478 fn
.Normalize(wxPATH_NORM_DOTS
| wxPATH_NORM_TILDE
| wxPATH_NORM_ABSOLUTE
);
479 wxString url
= fn
.GetFullPath(wxPATH_NATIVE
);
482 // unc notation, wxMSW
483 if ( url
.Find(wxT("\\\\")) == 0 )
489 url
= wxT("/") + url
;
491 url
= wxT("/") + url
;
497 url
.Replace(g_nativePathString
, g_unixPathString
);
498 url
.Replace(wxT("%"), wxT("%25"));
499 url
.Replace(wxT(":"), wxT("%3A"));
500 url
= wxT("file:") + url
;
507 class wxFileSystemModule
: public wxModule
509 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
512 virtual bool OnInit()
514 wxFileSystem::AddHandler(new wxLocalFSHandler
);
517 gs_FSMimeFallbacks
= new wxFileTypeInfo
[6];
518 gs_FSMimeFallbacks
[0] =
519 wxFileTypeInfo(_T("image/jpeg"),
522 _T("JPEG image (from fallback)"),
523 _T("jpg"), _T("jpeg"), NULL
);
524 gs_FSMimeFallbacks
[1] =
525 wxFileTypeInfo(_T("image/gif"),
528 _T("GIF image (from fallback)"),
530 gs_FSMimeFallbacks
[2] =
531 wxFileTypeInfo(_T("image/png"),
534 _T("PNG image (from fallback)"),
536 gs_FSMimeFallbacks
[3] =
537 wxFileTypeInfo(_T("image/bmp"),
540 _T("windows bitmap image (from fallback)"),
542 gs_FSMimeFallbacks
[4] =
543 wxFileTypeInfo(_T("text/html"),
546 _T("HTML document (from fallback)"),
547 _T("htm"), _T("html"), NULL
);
548 gs_FSMimeFallbacks
[5] =
549 // must terminate the table with this!
554 virtual void OnExit()
557 delete [] gs_FSMimeFallbacks
;
559 wxFileSystem::CleanUpHandlers();
563 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)