]>
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>
23 #include <wx/wfstream.h>
25 #include <wx/module.h>
26 #include <wx/filesys.h>
30 //--------------------------------------------------------------------------------
31 // wxFileSystemHandler
32 //--------------------------------------------------------------------------------
34 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
36 wxMimeTypesManager
wxFileSystemHandler::m_MimeMng
;
39 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
41 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
42 wxString loc
= GetRightLocation(location
);
44 int l
= loc
.Length(), l2
;
48 for (int i
= l
-1; i
>= 0; i
--) {
50 if (c
== '#') l2
= i
+ 1;
51 if (c
== '.') {ext
= loc
.Right(l2
-i
-1); break;}
52 if ((c
== '/') || (c
== '\\') || (c
== ':')) {return wxEmptyString
;}
54 ft
= m_MimeMng
.GetFileTypeFromExtension(ext
);
55 if (ft
&& (ft
-> GetMimeType(&mime
))) return mime
;
56 else return wxEmptyString
;
61 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
63 wxString s
= wxEmptyString
;
64 int i
, l
= location
.Length();
68 for (i
= l
-1; (i
>= 0) && ((location
[i
] != '#') || (!fnd
)); i
--) {
69 if ((location
[i
] == ':') && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
71 if (!fnd
) return "file";
72 for (++i
; (i
< l
) && (location
[i
] != ':'); i
++) s
<< location
[i
];
78 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
84 for (i
= location
.Length()-1; i
>= 0; i
--) {
85 if ((location
[i
] == ':') && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
86 else if (fnd
&& (location
[i
] == '#')) return location
.Left(i
);
93 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
95 int i
, l
= location
.Length();
97 for (i
= l
-1; (i
>= 0) && ((location
[i
] != ':') || (i
== 1) || (location
[i
-2] == ':')); i
--) {if (location
[i
] == '#') l2
= i
+ 1;}
98 if (i
== 0) return wxEmptyString
;
99 else return location
.Mid(i
+ 1, l2
- i
- 2);
104 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
107 int l
= location
.Length();
109 for (int i
= l
-1; i
>= 0; i
--) {
111 if (c
== '#') return location
.Right(l
-i
-1);
112 else if ((c
== '.') || (c
== '/') || (c
== '\\') || (c
== ':')) return wxEmptyString
;
114 return wxEmptyString
;
121 //--------------------------------------------------------------------------------
123 //--------------------------------------------------------------------------------
125 class wxLocalFSHandler
: public wxFileSystemHandler
128 virtual bool CanOpen(const wxString
& location
);
129 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
134 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
136 return GetProtocol(location
) == "file";
141 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
143 wxString right
= GetRightLocation(location
);
144 if (wxFileExists(right
))
145 return new wxFSFile(new wxFileInputStream(right
),
147 GetMimeTypeFromExt(location
),
148 GetAnchor(location
));
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
161 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
164 wxList
wxFileSystem::m_Handlers
;
168 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
173 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
174 if (m_Path
[i
] == '\\') m_Path
.GetWritableChar(i
) = '/'; // wanna be windows-safe
176 if (is_dir
== FALSE
) {
177 for (i
= m_Path
.Length()-1; i
>= 0; i
--) {
178 if (m_Path
[i
] == '/') {
179 if ((i
> 1) && (m_Path
[i
-1] == '/') && (m_Path
[i
-2] == ':')) {
188 else if (m_Path
[i
] == ':') {
194 for (i
= 0; i
< (int) m_Path
.Length(); i
++) {
195 if (m_Path
[i
] == ':') {
201 if (i
== (int) m_Path
.Length()) m_Path
= wxEmptyString
;
204 if (m_Path
[m_Path
.Length()-1] != '/') m_Path
<< '/';
205 m_Path
.Remove(pathpos
+1);
212 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
214 wxString loc
= location
;
222 for (i
= 0; i
< ln
; i
++) {
223 if (loc
[i
] == '\\') loc
.GetWritableChar(i
) = '/'; // wanna be windows-safe
224 if (!meta
) switch (loc
[i
]) {
225 case '/' : case ':' : case '#' : meta
= loc
[i
];
228 m_LastName
= wxEmptyString
;
230 // try relative paths first :
232 node
= m_Handlers
.GetFirst();
234 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
235 if (h
-> CanOpen(m_Path
+ location
)) {
236 s
= h
-> OpenFile(*this, m_Path
+ location
);
237 if (s
) {m_LastName
= m_Path
+ location
; break;}
239 node
= node
-> GetNext();
243 // if failed, try absolute paths :
245 node
= m_Handlers
.GetFirst();
247 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
248 if (h
-> CanOpen(location
)) {
249 s
= h
-> OpenFile(*this, location
);
250 if (s
) {m_LastName
= location
; break; }
252 node
= node
-> GetNext();
260 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
262 m_Handlers
.Append(handler
);
277 class wxFileSystemModule
: public wxModule
279 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
282 virtual bool OnInit()
284 wxFileSystem::AddHandler(new wxLocalFSHandler
);
287 virtual void OnExit() {}
290 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)