]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
6adf75e2df0fff527fd589bec893433e0ef304af
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
25 #include <wx/wfstream.h>
27 #include <wx/module.h>
28 #include <wx/filesys.h>
32 //--------------------------------------------------------------------------------
33 // wxFileSystemHandler
34 //--------------------------------------------------------------------------------
36 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
38 wxMimeTypesManager
wxFileSystemHandler::m_MimeMng
;
41 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
43 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
44 wxString loc
= GetRightLocation(location
);
46 int l
= loc
.Length(), l2
;
50 for (int i
= l
-1; i
>= 0; i
--) {
52 if (c
== '#') l2
= i
+ 1;
53 if (c
== '.') {ext
= loc
.Right(l2
-i
-1); break;}
54 if ((c
== '/') || (c
== '\\') || (c
== ':')) {return wxEmptyString
;}
56 ft
= m_MimeMng
.GetFileTypeFromExtension(ext
);
57 if (ft
&& (ft
-> GetMimeType(&mime
))) return mime
;
58 else return wxEmptyString
;
63 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
65 wxString s
= wxEmptyString
;
66 int i
, l
= location
.Length();
70 for (i
= l
-1; (i
>= 0) && ((location
[i
] != '#') || (!fnd
)); i
--) {
71 if ((location
[i
] == ':') && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
73 if (!fnd
) return "file";
74 for (++i
; (i
< l
) && (location
[i
] != ':'); i
++) s
<< location
[i
];
80 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
86 for (i
= location
.Length()-1; i
>= 0; i
--) {
87 if ((location
[i
] == ':') && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
88 else if (fnd
&& (location
[i
] == '#')) return location
.Left(i
);
95 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
97 int i
, l
= location
.Length();
99 for (i
= l
-1; (i
>= 0) && ((location
[i
] != ':') || (i
== 1) || (location
[i
-2] == ':')); i
--) {if (location
[i
] == '#') l2
= i
+ 1;}
100 if (i
== 0) return wxEmptyString
;
101 else return location
.Mid(i
+ 1, l2
- i
- 2);
106 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
109 int l
= location
.Length();
111 for (int i
= l
-1; i
>= 0; i
--) {
113 if (c
== '#') return location
.Right(l
-i
-1);
114 else if ((c
== '.') || (c
== '/') || (c
== '\\') || (c
== ':')) return wxEmptyString
;
116 return wxEmptyString
;
123 //--------------------------------------------------------------------------------
125 //--------------------------------------------------------------------------------
127 class wxLocalFSHandler
: public wxFileSystemHandler
130 virtual bool CanOpen(const wxString
& location
);
131 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
136 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
138 return GetProtocol(location
) == "file";
143 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
145 wxString right
= GetRightLocation(location
);
146 if (wxFileExists(right
))
147 return new wxFSFile(new wxFileInputStream(right
),
149 GetMimeTypeFromExt(location
),
150 GetAnchor(location
));
159 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
163 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
166 wxList
wxFileSystem::m_Handlers
;
170 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
175 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
176 if (m_Path
[i
] == '\\') m_Path
.GetWritableChar(i
) = '/'; // wanna be windows-safe
178 if (is_dir
== FALSE
) {
179 for (i
= m_Path
.Length()-1; i
>= 0; i
--) {
180 if (m_Path
[i
] == '/') {
181 if ((i
> 1) && (m_Path
[i
-1] == '/') && (m_Path
[i
-2] == ':')) {
190 else if (m_Path
[i
] == ':') {
196 for (i
= 0; i
< (int) m_Path
.Length(); i
++) {
197 if (m_Path
[i
] == ':') {
203 if (i
== (int) m_Path
.Length()) m_Path
= wxEmptyString
;
206 if (m_Path
[m_Path
.Length()-1] != '/') m_Path
<< '/';
207 m_Path
.Remove(pathpos
+1);
214 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
216 wxString loc
= location
;
224 for (i
= 0; i
< ln
; i
++) {
225 if (loc
[i
] == '\\') loc
.GetWritableChar(i
) = '/'; // wanna be windows-safe
226 if (!meta
) switch (loc
[i
]) {
227 case '/' : case ':' : case '#' : meta
= loc
[i
];
230 m_LastName
= wxEmptyString
;
232 // try relative paths first :
234 node
= m_Handlers
.GetFirst();
236 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
237 if (h
-> CanOpen(m_Path
+ location
)) {
238 s
= h
-> OpenFile(*this, m_Path
+ location
);
239 if (s
) {m_LastName
= m_Path
+ location
; break;}
241 node
= node
-> GetNext();
245 // if failed, try absolute paths :
247 node
= m_Handlers
.GetFirst();
249 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
250 if (h
-> CanOpen(location
)) {
251 s
= h
-> OpenFile(*this, location
);
252 if (s
) {m_LastName
= location
; break; }
254 node
= node
-> GetNext();
262 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
264 m_Handlers
.Append(handler
);
279 class wxFileSystemModule
: public wxModule
281 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
284 virtual bool OnInit()
286 wxFileSystem::AddHandler(new wxLocalFSHandler
);
289 virtual void OnExit() {}
292 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
296 #endif // wxUSE_FS_INET || wxUSE_FS_ZIP