]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
38b29efa3c5020bdc3c2a0d0fab101356a1342ee
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"
22 #define wxUSE_FS_ZIP 0
23 #define wxUSE_FS_INET 0
26 #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
28 #include "wx/wfstream.h"
29 #include "wx/module.h"
30 #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
== _T('#')) l2
= i
+ 1;
53 if (c
== _T('.')) {ext
= loc
.Right(l2
-i
-1); break;}
54 if ((c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) {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
] != _T('#')) || (!fnd
)); i
--) {
71 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
73 if (!fnd
) return _T("file");
74 for (++i
; (i
< l
) && (location
[i
] != _T(':')); 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
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
87 else if (fnd
&& (location
[i
] == _T('#'))) return location
.Left(i
);
92 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
94 int i
, l
= location
.Length();
96 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T(':')) || (i
== 1) || (location
[i
-2] == _T(':'))); i
--) {if (location
[i
] == _T('#')) l2
= i
+ 1;}
97 if (i
== 0) return wxEmptyString
;
98 else return location
.Mid(i
+ 1, l2
- i
- 2);
101 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
104 int l
= location
.Length();
106 for (int i
= l
-1; i
>= 0; i
--) {
108 if (c
== _T('#')) return location
.Right(l
-i
-1);
109 else if ((c
== _T('.')) || (c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) return wxEmptyString
;
111 return wxEmptyString
;
114 //--------------------------------------------------------------------------------
116 //--------------------------------------------------------------------------------
118 class wxLocalFSHandler
: public wxFileSystemHandler
121 virtual bool CanOpen(const wxString
& location
);
122 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
126 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
128 return GetProtocol(location
) == _T("file");
131 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
133 wxString right
= GetRightLocation(location
);
134 if (wxFileExists(right
))
135 return new wxFSFile(new wxFileInputStream(right
),
137 GetMimeTypeFromExt(location
),
138 GetAnchor(location
));
139 else return (wxFSFile
*) NULL
;
142 //-----------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------
146 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
149 wxList
wxFileSystem::m_Handlers
;
153 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
158 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
159 if (m_Path
[i
] == _T('\\')) m_Path
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
163 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
165 if (m_Path
[i
] == _T('/'))
167 if ((i
> 1) && (m_Path
[i
-1] == _T('/')) && (m_Path
[i
-2] == _T(':')))
178 else if (m_Path
[i
] == _T(':')) {
185 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
187 if (m_Path
[i
] == _T(':'))
194 if (i
== (int) m_Path
.Length())
195 m_Path
= wxEmptyString
;
199 if (m_Path
[m_Path
.Length()-1] != _T('/'))
201 m_Path
.Remove(pathpos
+1);
208 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
210 wxString loc
= location
;
218 for (i
= 0; i
< ln
; i
++)
220 if (loc
[i
] == _T('\\')) loc
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
221 if (!meta
) switch (loc
[i
])
223 case _T('/') : case _T(':') : case _T('#') : meta
= loc
[i
];
226 m_LastName
= wxEmptyString
;
228 // try relative paths first :
231 node
= m_Handlers
.GetFirst();
234 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
235 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 :
247 node
= m_Handlers
.GetFirst();
250 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
251 if (h
->CanOpen(location
))
253 s
= h
->OpenFile(*this, location
);
254 if (s
) { m_LastName
= location
; break; }
256 node
= node
->GetNext();
263 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
265 m_Handlers
.Append(handler
);
272 class wxFileSystemModule
: public wxModule
274 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
277 virtual bool OnInit()
279 wxFileSystem::AddHandler(new wxLocalFSHandler
);
282 virtual void OnExit() {}
285 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
288 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS