]>
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
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation
13 #include "wx/wxprec.h"
19 #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
21 #include "wx/wfstream.h"
22 #include "wx/module.h"
23 #include "wx/filesys.h"
25 //--------------------------------------------------------------------------------
26 // wxFileSystemHandler
27 //--------------------------------------------------------------------------------
29 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
31 wxMimeTypesManager
wxFileSystemHandler::m_MimeMng
;
34 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
36 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
37 wxString loc
= GetRightLocation(location
);
39 int l
= loc
.Length(), l2
;
43 for (int i
= l
-1; i
>= 0; i
--) {
45 if (c
== _T('#')) l2
= i
+ 1;
46 if (c
== _T('.')) {ext
= loc
.Right(l2
-i
-1); break;}
47 if ((c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) {return wxEmptyString
;}
49 ft
= m_MimeMng
.GetFileTypeFromExtension(ext
);
50 if (ft
&& (ft
-> GetMimeType(&mime
))) return mime
;
51 else return wxEmptyString
;
56 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
58 wxString s
= wxEmptyString
;
59 int i
, l
= location
.Length();
63 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T('#')) || (!fnd
)); i
--) {
64 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
66 if (!fnd
) return _T("file");
67 for (++i
; (i
< l
) && (location
[i
] != _T(':')); i
++) s
<< location
[i
];
72 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
78 for (i
= location
.Length()-1; i
>= 0; i
--) {
79 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
80 else if (fnd
&& (location
[i
] == _T('#'))) return location
.Left(i
);
85 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
87 int i
, l
= location
.Length();
89 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T(':')) || (i
== 1) || (location
[i
-2] == _T(':'))); i
--) {if (location
[i
] == _T('#')) l2
= i
+ 1;}
90 if (i
== 0) return wxEmptyString
;
91 else return location
.Mid(i
+ 1, l2
- i
- 2);
94 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
97 int l
= location
.Length();
99 for (int i
= l
-1; i
>= 0; i
--) {
101 if (c
== _T('#')) return location
.Right(l
-i
-1);
102 else if ((c
== _T('.')) || (c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) return wxEmptyString
;
104 return wxEmptyString
;
107 //--------------------------------------------------------------------------------
109 //--------------------------------------------------------------------------------
111 class wxLocalFSHandler
: public wxFileSystemHandler
114 virtual bool CanOpen(const wxString
& location
);
115 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
119 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
121 return GetProtocol(location
) == _T("file");
124 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
126 wxString right
= GetRightLocation(location
);
127 if (wxFileExists(right
))
128 return new wxFSFile(new wxFileInputStream(right
),
130 GetMimeTypeFromExt(location
),
131 GetAnchor(location
));
132 else return (wxFSFile
*) NULL
;
135 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
139 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
142 wxList
wxFileSystem::m_Handlers
;
146 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
151 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
152 if (m_Path
[i
] == _T('\\')) m_Path
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
156 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
158 if (m_Path
[i
] == _T('/'))
160 if ((i
> 1) && (m_Path
[i
-1] == _T('/')) && (m_Path
[i
-2] == _T(':')))
171 else if (m_Path
[i
] == _T(':')) {
178 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
180 if (m_Path
[i
] == _T(':'))
187 if (i
== (int) m_Path
.Length())
188 m_Path
= wxEmptyString
;
192 if (m_Path
[m_Path
.Length()-1] != _T('/'))
194 m_Path
.Remove(pathpos
+1);
201 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
203 wxString loc
= location
;
211 for (i
= 0; i
< ln
; i
++)
213 if (loc
[i
] == _T('\\')) loc
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
214 if (!meta
) switch (loc
[i
])
216 case _T('/') : case _T(':') : case _T('#') : meta
= loc
[i
];
219 m_LastName
= wxEmptyString
;
221 // try relative paths first :
224 node
= m_Handlers
.GetFirst();
227 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
228 if (h
->CanOpen(m_Path
+ location
))
230 s
= h
->OpenFile(*this, m_Path
+ location
);
231 if (s
) { m_LastName
= m_Path
+ location
; break; }
233 node
= node
->GetNext();
237 // if failed, try absolute paths :
240 node
= m_Handlers
.GetFirst();
243 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
244 if (h
->CanOpen(location
))
246 s
= h
->OpenFile(*this, location
);
247 if (s
) { m_LastName
= location
; break; }
249 node
= node
->GetNext();
256 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
258 m_Handlers
.Append(handler
);
265 class wxFileSystemModule
: public wxModule
267 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
270 virtual bool OnInit()
272 wxFileSystem::AddHandler(new wxLocalFSHandler
);
275 virtual void OnExit() {}
278 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
281 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS