]>
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"
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
= NULL
;
40 void wxFileSystemHandler::CleanUpStatics()
42 if (m_MimeMng
) delete m_MimeMng
;
47 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
49 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
50 wxString loc
= GetRightLocation(location
);
52 int l
= loc
.Length(), l2
;
56 for (int i
= l
-1; i
>= 0; i
--) {
58 if (c
== _T('#')) l2
= i
+ 1;
59 if (c
== _T('.')) {ext
= loc
.Right(l2
-i
-1); break;}
60 if ((c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) {return wxEmptyString
;}
62 if (m_MimeMng
== NULL
) m_MimeMng
= new wxMimeTypesManager
;
63 ft
= m_MimeMng
-> GetFileTypeFromExtension(ext
);
64 if (ft
&& (ft
-> GetMimeType(&mime
))) return mime
;
65 else return wxEmptyString
;
70 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
72 wxString s
= wxEmptyString
;
73 int i
, l
= location
.Length();
77 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T('#')) || (!fnd
)); i
--) {
78 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
80 if (!fnd
) return _T("file");
81 for (++i
; (i
< l
) && (location
[i
] != _T(':')); i
++) s
<< location
[i
];
86 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
92 for (i
= location
.Length()-1; i
>= 0; i
--) {
93 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
94 else if (fnd
&& (location
[i
] == _T('#'))) return location
.Left(i
);
99 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
101 int i
, l
= location
.Length();
103 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T(':')) || (i
== 1) || (location
[i
-2] == _T(':'))); i
--) {if (location
[i
] == _T('#')) l2
= i
+ 1;}
104 if (i
== 0) return wxEmptyString
;
105 else return location
.Mid(i
+ 1, l2
- i
- 2);
108 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
111 int l
= location
.Length();
113 for (int i
= l
-1; i
>= 0; i
--) {
115 if (c
== _T('#')) return location
.Right(l
-i
-1);
116 else if ((c
== _T('.')) || (c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) return wxEmptyString
;
118 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
);
133 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
135 return GetProtocol(location
) == _T("file");
138 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
140 wxString right
= GetRightLocation(location
);
141 if (wxFileExists(right
))
142 return new wxFSFile(new wxFileInputStream(right
),
144 GetMimeTypeFromExt(location
),
145 GetAnchor(location
));
146 else return (wxFSFile
*) NULL
;
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
156 wxList
wxFileSystem::m_Handlers
;
160 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
165 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
166 if (m_Path
[i
] == _T('\\')) m_Path
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
170 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
172 if (m_Path
[i
] == _T('/'))
174 if ((i
> 1) && (m_Path
[i
-1] == _T('/')) && (m_Path
[i
-2] == _T(':')))
185 else if (m_Path
[i
] == _T(':')) {
192 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
194 if (m_Path
[i
] == _T(':'))
201 if (i
== (int) m_Path
.Length())
202 m_Path
= wxEmptyString
;
206 if (m_Path
[m_Path
.Length()-1] != _T('/'))
208 m_Path
.Remove(pathpos
+1);
215 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
217 wxString loc
= location
;
225 for (i
= 0; i
< ln
; i
++)
227 if (loc
[i
] == _T('\\')) loc
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
228 if (!meta
) switch (loc
[i
])
230 case _T('/') : case _T(':') : case _T('#') : meta
= loc
[i
];
233 m_LastName
= wxEmptyString
;
235 // try relative paths first :
238 node
= m_Handlers
.GetFirst();
241 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
242 if (h
->CanOpen(m_Path
+ location
))
244 s
= h
->OpenFile(*this, m_Path
+ location
);
245 if (s
) { m_LastName
= m_Path
+ location
; break; }
247 node
= node
->GetNext();
251 // if failed, try absolute paths :
254 node
= m_Handlers
.GetFirst();
257 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
258 if (h
->CanOpen(location
))
260 s
= h
->OpenFile(*this, location
);
261 if (s
) { m_LastName
= location
; break; }
263 node
= node
->GetNext();
270 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
272 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()
291 wxFileSystemHandler::CleanUpStatics();
295 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
298 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS