]>
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
25 #include <wx/wfstream.h>
26 #include <wx/module.h>
27 #include <wx/filesys.h>
31 //--------------------------------------------------------------------------------
32 // wxFileSystemHandler
33 //--------------------------------------------------------------------------------
35 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
37 wxMimeTypesManager
wxFileSystemHandler::m_MimeMng
;
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
--) {
51 if (c
== '#') l2
= i
+ 1;
52 if (c
== '.') {ext
= loc
.Right(l2
-i
-1); break;}
53 if ((c
== '/') || (c
== '\\') || (c
== ':')) {return wxEmptyString
;}
55 ft
= m_MimeMng
.GetFileTypeFromExtension(ext
);
56 if (ft
&& (ft
-> GetMimeType(&mime
))) return mime
;
57 else return wxEmptyString
;
62 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
64 wxString s
= wxEmptyString
;
65 int i
, l
= location
.Length();
69 for (i
= l
-1; (i
>= 0) && ((location
[i
] != '#') || (!fnd
)); i
--) {
70 if ((location
[i
] == ':') && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
72 if (!fnd
) return "file";
73 for (++i
; (i
< l
) && (location
[i
] != ':'); i
++) s
<< location
[i
];
79 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
85 for (i
= location
.Length()-1; i
>= 0; i
--) {
86 if ((location
[i
] == ':') && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
87 else if (fnd
&& (location
[i
] == '#')) return location
.Left(i
);
94 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
96 int i
, l
= location
.Length();
98 for (i
= l
-1; (i
>= 0) && ((location
[i
] != ':') || (i
== 1) || (location
[i
-2] == ':')); i
--) {if (location
[i
] == '#') l2
= i
+ 1;}
99 if (i
== 0) return wxEmptyString
;
100 else return location
.Mid(i
+ 1, l2
- i
- 2);
105 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
108 int l
= location
.Length();
110 for (int i
= l
-1; i
>= 0; i
--) {
112 if (c
== '#') return location
.Right(l
-i
-1);
113 else if ((c
== '.') || (c
== '/') || (c
== '\\') || (c
== ':')) return wxEmptyString
;
115 return wxEmptyString
;
122 //--------------------------------------------------------------------------------
124 //--------------------------------------------------------------------------------
126 class wxLocalFSHandler
: public wxFileSystemHandler
129 virtual bool CanOpen(const wxString
& location
);
130 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
135 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
137 return GetProtocol(location
) == "file";
142 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
144 wxString right
= GetRightLocation(location
);
145 if (wxFileExists(right
))
146 return new wxFSFile(new wxFileInputStream(right
),
148 GetMimeTypeFromExt(location
),
149 GetAnchor(location
));
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
165 wxList
wxFileSystem::m_Handlers
;
169 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
174 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
175 if (m_Path
[i
] == '\\') m_Path
.GetWritableChar(i
) = '/'; // wanna be windows-safe
177 if (is_dir
== FALSE
) {
178 for (i
= m_Path
.Length()-1; i
>= 0; i
--) {
179 if (m_Path
[i
] == '/') {
180 if ((i
> 1) && (m_Path
[i
-1] == '/') && (m_Path
[i
-2] == ':')) {
189 else if (m_Path
[i
] == ':') {
195 for (i
= 0; i
< (int) m_Path
.Length(); i
++) {
196 if (m_Path
[i
] == ':') {
202 if (i
== (int) m_Path
.Length()) m_Path
= wxEmptyString
;
205 if (m_Path
[m_Path
.Length()-1] != '/') m_Path
<< '/';
206 m_Path
.Remove(pathpos
+1);
213 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
215 wxString loc
= location
;
223 for (i
= 0; i
< ln
; i
++) {
224 if (loc
[i
] == '\\') loc
.GetWritableChar(i
) = '/'; // wanna be windows-safe
225 if (!meta
) switch (loc
[i
]) {
226 case '/' : case ':' : case '#' : meta
= loc
[i
];
229 m_LastName
= wxEmptyString
;
231 // try relative paths first :
233 node
= m_Handlers
.GetFirst();
235 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
236 if (h
-> CanOpen(m_Path
+ location
)) {
237 s
= h
-> OpenFile(*this, m_Path
+ location
);
238 if (s
) {m_LastName
= m_Path
+ location
; break;}
240 node
= node
-> GetNext();
244 // if failed, try absolute paths :
246 node
= m_Handlers
.GetFirst();
248 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
249 if (h
-> CanOpen(location
)) {
250 s
= h
-> OpenFile(*this, location
);
251 if (s
) {m_LastName
= location
; break; }
253 node
= node
-> GetNext();
261 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
263 m_Handlers
.Append(handler
);
278 class wxFileSystemModule
: public wxModule
280 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
283 virtual bool OnInit()
285 wxFileSystem::AddHandler(new wxLocalFSHandler
);
288 virtual void OnExit() {}
291 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
295 #endif // wxUSE_FS_INET || wxUSE_FS_ZIP