]>
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
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 static wxFileTypeInfo
*gs_FSMimeFallbacks
= NULL
;
40 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
42 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
43 wxString loc
= GetRightLocation(location
);
45 int l
= loc
.Length(), l2
;
49 for (int i
= l
-1; i
>= 0; i
--) {
50 c
= loc
[(unsigned int) i
];
51 if (c
== wxT('#')) l2
= i
+ 1;
52 if (c
== wxT('.')) {ext
= loc
.Right(l2
-i
-1); break;}
53 if ((c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) {return wxEmptyString
;}
56 static bool s_MinimalMimeEnsured
= FALSE
;
57 if (!s_MinimalMimeEnsured
) {
58 wxTheMimeTypesManager
-> AddFallbacks(gs_FSMimeFallbacks
);
59 s_MinimalMimeEnsured
= TRUE
;
62 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension(ext
);
63 if ( !ft
|| !ft
-> GetMimeType(&mime
) ) {
74 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
76 wxString s
= wxEmptyString
;
77 int i
, l
= location
.Length();
81 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
82 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
84 if (!fnd
) return wxT("file");
85 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
90 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
96 for (i
= location
.Length()-1; i
>= 0; i
--) {
97 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
98 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
100 return wxEmptyString
;
103 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
105 int i
, l
= location
.Length();
107 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
108 if (i
== 0) return wxEmptyString
;
109 else return location
.Mid(i
+ 1, l2
- i
- 2);
112 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
115 int l
= location
.Length();
117 for (int i
= l
-1; i
>= 0; i
--) {
119 if (c
== wxT('#')) return location
.Right(l
-i
-1);
120 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
122 return wxEmptyString
;
126 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
129 return wxEmptyString
;
132 wxString
wxFileSystemHandler::FindNext()
134 return wxEmptyString
;
137 //--------------------------------------------------------------------------------
139 //--------------------------------------------------------------------------------
141 class wxLocalFSHandler
: public wxFileSystemHandler
144 virtual bool CanOpen(const wxString
& location
);
145 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
146 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
147 virtual wxString
FindNext();
151 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
153 return GetProtocol(location
) == wxT("file");
156 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
158 // location has Unix path separators
159 wxString right
= GetRightLocation(location
);
160 wxFileName
fn( right
, wxPATH_UNIX
);
162 if (!wxFileExists( fn
.GetFullPath() ))
163 return (wxFSFile
*) NULL
;
165 return new wxFSFile(new wxFileInputStream( fn
.GetFullPath() ),
167 GetMimeTypeFromExt(location
),
169 wxDateTime(wxFileModificationTime( fn
.GetFullPath() )));
173 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
175 wxString right
= GetRightLocation(spec
);
176 return wxFindFirstFile(right
, flags
);
179 wxString
wxLocalFSHandler::FindNext()
181 return wxFindNextFile();
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
193 wxList
wxFileSystem::m_Handlers
;
196 static wxString
MakeCorrectPath(const wxString
& path
)
203 for (i
= 0; i
< cnt
; i
++)
204 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
206 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
208 if (cnt
< 3) return p
;
210 r
<< p
.GetChar(0) << p
.GetChar(1);
212 // skip trailing ../.., if any
213 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
215 // remove back references: translate dir1/../dir2 to dir2
219 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
221 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
222 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
224 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
230 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
236 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
240 m_Path
= MakeCorrectPath(location
);
244 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
250 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
252 if (m_Path
[(unsigned int) i
] == wxT('/'))
254 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
265 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
272 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
274 if (m_Path
[(unsigned int) i
] == wxT(':'))
280 if (i
== (int) m_Path
.Length())
281 m_Path
= wxEmptyString
;
285 m_Path
.Remove(pathpos
+1);
292 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
294 wxString loc
= MakeCorrectPath(location
);
302 for (i
= 0; i
< ln
; i
++)
306 case wxT('/') : case wxT(':') : case wxT('#') :
310 if (meta
!= 0) break;
312 m_LastName
= wxEmptyString
;
314 // try relative paths first :
315 if (meta
!= wxT(':'))
317 node
= m_Handlers
.GetFirst();
320 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
321 if (h
->CanOpen(m_Path
+ loc
))
323 s
= h
->OpenFile(*this, m_Path
+ loc
);
324 if (s
) { m_LastName
= m_Path
+ loc
; break; }
326 node
= node
->GetNext();
330 // if failed, try absolute paths :
333 node
= m_Handlers
.GetFirst();
336 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
339 s
= h
->OpenFile(*this, loc
);
340 if (s
) { m_LastName
= loc
; break; }
342 node
= node
->GetNext();
350 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
353 wxString
spec2(spec
);
355 m_FindFileHandler
= NULL
;
357 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
358 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
360 node
= m_Handlers
.GetFirst();
363 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
364 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
365 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
366 node
= node
->GetNext();
369 node
= m_Handlers
.GetFirst();
372 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
373 if (m_FindFileHandler
-> CanOpen(spec2
))
374 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
375 node
= node
->GetNext();
378 return wxEmptyString
;
383 wxString
wxFileSystem::FindNext()
385 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
386 else return m_FindFileHandler
-> FindNext();
391 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
393 m_Handlers
.Append(handler
);
397 void wxFileSystem::CleanUpHandlers()
399 m_Handlers
.DeleteContents(TRUE
);
408 class wxFileSystemModule
: public wxModule
410 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
413 virtual bool OnInit()
415 wxFileSystem::AddHandler(new wxLocalFSHandler
);
417 gs_FSMimeFallbacks
= new wxFileTypeInfo
[6];
418 gs_FSMimeFallbacks
[0] =
419 wxFileTypeInfo("image/jpeg",
422 "JPEG image (from fallback)",
423 "jpg", "jpeg", NULL
);
424 gs_FSMimeFallbacks
[1] =
425 wxFileTypeInfo("image/gif",
428 "GIF image (from fallback)",
430 gs_FSMimeFallbacks
[2] =
431 wxFileTypeInfo("image/png",
434 "PNG image (from fallback)",
436 gs_FSMimeFallbacks
[3] =
437 wxFileTypeInfo("image/bmp",
440 "windows bitmap image (from fallback)",
442 gs_FSMimeFallbacks
[4] =
443 wxFileTypeInfo("text/html",
446 "HTML document (from fallback)",
447 "htm", "html", NULL
);
448 gs_FSMimeFallbacks
[5] =
449 // must terminate the table with this!
454 virtual void OnExit()
456 delete [] gs_FSMimeFallbacks
;
457 wxFileSystem::CleanUpHandlers();
461 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)