]>
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"
21 #define wxUSE_FS_INET 0
24 #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
26 #include "wx/wfstream.h"
27 #include "wx/module.h"
28 #include "wx/filesys.h"
30 //--------------------------------------------------------------------------------
31 // wxFileSystemHandler
32 //--------------------------------------------------------------------------------
34 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
36 wxMimeTypesManager
*wxFileSystemHandler::m_MimeMng
= NULL
;
38 void wxFileSystemHandler::CleanUpStatics()
40 if (m_MimeMng
) delete m_MimeMng
;
45 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
47 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
48 wxString loc
= GetRightLocation(location
);
50 int l
= loc
.Length(), l2
;
54 for (int i
= l
-1; i
>= 0; i
--) {
56 if (c
== _T('#')) l2
= i
+ 1;
57 if (c
== _T('.')) {ext
= loc
.Right(l2
-i
-1); break;}
58 if ((c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) {return wxEmptyString
;}
60 if (m_MimeMng
== NULL
) m_MimeMng
= new wxMimeTypesManager
;
61 ft
= m_MimeMng
-> GetFileTypeFromExtension(ext
);
62 if (ft
&& (ft
-> GetMimeType(&mime
))) return mime
;
63 else return wxEmptyString
;
68 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
70 wxString s
= wxEmptyString
;
71 int i
, l
= location
.Length();
75 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T('#')) || (!fnd
)); i
--) {
76 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
78 if (!fnd
) return _T("file");
79 for (++i
; (i
< l
) && (location
[i
] != _T(':')); i
++) s
<< location
[i
];
84 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
90 for (i
= location
.Length()-1; i
>= 0; i
--) {
91 if ((location
[i
] == _T(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
92 else if (fnd
&& (location
[i
] == _T('#'))) return location
.Left(i
);
97 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
99 int i
, l
= location
.Length();
101 for (i
= l
-1; (i
>= 0) && ((location
[i
] != _T(':')) || (i
== 1) || (location
[i
-2] == _T(':'))); i
--) {if (location
[i
] == _T('#')) l2
= i
+ 1;}
102 if (i
== 0) return wxEmptyString
;
103 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
== _T('#')) return location
.Right(l
-i
-1);
114 else if ((c
== _T('.')) || (c
== _T('/')) || (c
== _T('\\')) || (c
== _T(':'))) return wxEmptyString
;
116 return wxEmptyString
;
119 //--------------------------------------------------------------------------------
121 //--------------------------------------------------------------------------------
123 class wxLocalFSHandler
: public wxFileSystemHandler
126 virtual bool CanOpen(const wxString
& location
);
127 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
131 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
133 return GetProtocol(location
) == _T("file");
136 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
138 wxString right
= GetRightLocation(location
);
139 if (wxFileExists(right
))
140 return new wxFSFile(new wxFileInputStream(right
),
142 GetMimeTypeFromExt(location
),
143 GetAnchor(location
));
144 else return (wxFSFile
*) NULL
;
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
154 wxList
wxFileSystem::m_Handlers
;
158 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
163 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
164 if (m_Path
[i
] == _T('\\')) m_Path
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
168 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
170 if (m_Path
[i
] == _T('/'))
172 if ((i
> 1) && (m_Path
[i
-1] == _T('/')) && (m_Path
[i
-2] == _T(':')))
183 else if (m_Path
[i
] == _T(':')) {
190 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
192 if (m_Path
[i
] == _T(':'))
199 if (i
== (int) m_Path
.Length())
200 m_Path
= wxEmptyString
;
204 if (m_Path
[m_Path
.Length()-1] != _T('/'))
206 m_Path
.Remove(pathpos
+1);
213 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
215 wxString loc
= location
;
223 for (i
= 0; i
< ln
; i
++)
225 if (loc
[i
] == _T('\\')) loc
.GetWritableChar(i
) = _T('/'); // wanna be windows-safe
226 if (!meta
) switch (loc
[i
])
228 case _T('/') : case _T(':') : case _T('#') : meta
= loc
[i
];
231 m_LastName
= wxEmptyString
;
233 // try relative paths first :
236 node
= m_Handlers
.GetFirst();
239 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
240 if (h
->CanOpen(m_Path
+ location
))
242 s
= h
->OpenFile(*this, m_Path
+ location
);
243 if (s
) { m_LastName
= m_Path
+ location
; break; }
245 node
= node
->GetNext();
249 // if failed, try absolute paths :
252 node
= m_Handlers
.GetFirst();
255 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
256 if (h
->CanOpen(location
))
258 s
= h
->OpenFile(*this, location
);
259 if (s
) { m_LastName
= location
; break; }
261 node
= node
->GetNext();
268 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
270 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()
289 wxFileSystemHandler::CleanUpStatics();
293 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
296 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS