]>
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
--) {
55 c
= loc
[(unsigned int) i
];
56 if (c
== wxT('#')) l2
= i
+ 1;
57 if (c
== wxT('.')) {ext
= loc
.Right(l2
-i
-1); break;}
58 if ((c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) {return wxEmptyString
;}
61 if (m_MimeMng
== NULL
) {
62 m_MimeMng
= new wxMimeTypesManager
;
64 static const wxFileTypeInfo fallbacks
[] =
66 wxFileTypeInfo("image/jpeg",
69 "JPEG image (from fallback)",
71 wxFileTypeInfo("image/gif",
74 "GIF image (from fallback)",
76 wxFileTypeInfo("image/png",
79 "PNG image (from fallback)",
81 wxFileTypeInfo("image/bmp",
84 "windows bitmap image (from fallback)",
86 wxFileTypeInfo("text/html",
89 "HTML document (from fallback)",
92 // must terminate the table with this!
96 m_MimeMng
-> AddFallbacks(fallbacks
);
99 ft
= m_MimeMng
-> GetFileTypeFromExtension(ext
);
100 if (ft
&& (ft
-> GetMimeType(&mime
))) {
106 return wxEmptyString
;
112 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
114 wxString s
= wxEmptyString
;
115 int i
, l
= location
.Length();
119 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
120 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
122 if (!fnd
) return wxT("file");
123 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
128 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
134 for (i
= location
.Length()-1; i
>= 0; i
--) {
135 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
136 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
138 return wxEmptyString
;
141 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
143 int i
, l
= location
.Length();
145 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
146 if (i
== 0) return wxEmptyString
;
147 else return location
.Mid(i
+ 1, l2
- i
- 2);
150 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
153 int l
= location
.Length();
155 for (int i
= l
-1; i
>= 0; i
--) {
157 if (c
== wxT('#')) return location
.Right(l
-i
-1);
158 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
160 return wxEmptyString
;
164 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
167 return wxEmptyString
;
170 wxString
wxFileSystemHandler::FindNext()
172 return wxEmptyString
;
175 //--------------------------------------------------------------------------------
177 //--------------------------------------------------------------------------------
179 class wxLocalFSHandler
: public wxFileSystemHandler
182 virtual bool CanOpen(const wxString
& location
);
183 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
184 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
185 virtual wxString
FindNext();
189 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
191 return GetProtocol(location
) == wxT("file");
194 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
196 wxString right
= GetRightLocation(location
);
197 if (wxFileExists(right
))
198 return new wxFSFile(new wxFileInputStream(right
),
200 GetMimeTypeFromExt(location
),
201 GetAnchor(location
));
202 else return (wxFSFile
*) NULL
;
205 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
207 wxString right
= GetRightLocation(spec
);
208 return wxFindFirstFile(right
, flags
);
211 wxString
wxLocalFSHandler::FindNext()
213 return wxFindNextFile();
218 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
222 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
225 wxList
wxFileSystem::m_Handlers
;
229 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
234 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
235 if (m_Path
[(unsigned int) i
] == wxT('\\')) m_Path
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
239 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
245 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
247 if (m_Path
[(unsigned int) i
] == wxT('/'))
249 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
260 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
267 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
269 if (m_Path
[(unsigned int) i
] == wxT(':'))
275 if (i
== (int) m_Path
.Length())
276 m_Path
= wxEmptyString
;
280 m_Path
.Remove(pathpos
+1);
287 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
289 wxString loc
= location
;
297 for (i
= 0; i
< ln
; i
++)
299 if (loc
[(unsigned int) i
] == wxT('\\')) loc
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
301 switch (loc
[(unsigned int) i
])
303 case wxT('/') : case wxT(':') : case wxT('#') : meta
= loc
[(unsigned int) i
];
306 m_LastName
= wxEmptyString
;
308 // try relative paths first :
309 if (meta
!= wxT(':'))
311 node
= m_Handlers
.GetFirst();
314 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
315 if (h
->CanOpen(m_Path
+ loc
))
317 s
= h
->OpenFile(*this, m_Path
+ loc
);
318 if (s
) { m_LastName
= m_Path
+ loc
; break; }
320 node
= node
->GetNext();
324 // if failed, try absolute paths :
327 node
= m_Handlers
.GetFirst();
330 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
333 s
= h
->OpenFile(*this, loc
);
334 if (s
) { m_LastName
= loc
; break; }
336 node
= node
->GetNext();
344 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
347 wxString
spec2(spec
);
349 m_FindFileHandler
= NULL
;
351 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
352 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
354 node
= m_Handlers
.GetFirst();
357 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
358 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
359 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
360 node
= node
->GetNext();
363 node
= m_Handlers
.GetFirst();
366 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
367 if (m_FindFileHandler
-> CanOpen(spec2
))
368 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
369 node
= node
->GetNext();
372 return wxEmptyString
;
377 wxString
wxFileSystem::FindNext()
379 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
380 else return m_FindFileHandler
-> FindNext();
385 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
387 m_Handlers
.Append(handler
);
391 void wxFileSystem::CleanUpHandlers()
393 m_Handlers
.DeleteContents(TRUE
);
402 class wxFileSystemModule
: public wxModule
404 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
407 virtual bool OnInit()
409 wxFileSystem::AddHandler(new wxLocalFSHandler
);
412 virtual void OnExit()
414 wxFileSystemHandler::CleanUpStatics();
415 wxFileSystem::CleanUpHandlers();
419 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
422 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS