]>
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
& spec
, int flags
) { return wxEmptyString
; }
166 wxString
wxFileSystemHandler::FindNext() { return wxEmptyString
; }
170 //--------------------------------------------------------------------------------
172 //--------------------------------------------------------------------------------
174 class wxLocalFSHandler
: public wxFileSystemHandler
177 virtual bool CanOpen(const wxString
& location
);
178 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
179 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
180 virtual wxString
FindNext();
184 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
186 return GetProtocol(location
) == wxT("file");
189 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
191 wxString right
= GetRightLocation(location
);
192 if (wxFileExists(right
))
193 return new wxFSFile(new wxFileInputStream(right
),
195 GetMimeTypeFromExt(location
),
196 GetAnchor(location
));
197 else return (wxFSFile
*) NULL
;
200 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
202 wxString right
= GetRightLocation(spec
);
203 return wxFindFirstFile(right
, flags
);
206 wxString
wxLocalFSHandler::FindNext()
208 return wxFindNextFile();
213 //-----------------------------------------------------------------------------
215 //-----------------------------------------------------------------------------
217 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
220 wxList
wxFileSystem::m_Handlers
;
224 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
229 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
230 if (m_Path
[(unsigned int) i
] == wxT('\\')) m_Path
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
234 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
240 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
242 if (m_Path
[(unsigned int) i
] == wxT('/'))
244 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
255 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
262 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
264 if (m_Path
[(unsigned int) i
] == wxT(':'))
270 if (i
== (int) m_Path
.Length())
271 m_Path
= wxEmptyString
;
275 m_Path
.Remove(pathpos
+1);
282 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
284 wxString loc
= location
;
292 for (i
= 0; i
< ln
; i
++)
294 if (loc
[(unsigned int) i
] == wxT('\\')) loc
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
296 switch (loc
[(unsigned int) i
])
298 case wxT('/') : case wxT(':') : case wxT('#') : meta
= loc
[(unsigned int) i
];
301 m_LastName
= wxEmptyString
;
303 // try relative paths first :
304 if (meta
!= wxT(':'))
306 node
= m_Handlers
.GetFirst();
309 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
310 if (h
->CanOpen(m_Path
+ location
))
312 s
= h
->OpenFile(*this, m_Path
+ location
);
313 if (s
) { m_LastName
= m_Path
+ location
; break; }
315 node
= node
->GetNext();
319 // if failed, try absolute paths :
322 node
= m_Handlers
.GetFirst();
325 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
326 if (h
->CanOpen(location
))
328 s
= h
->OpenFile(*this, location
);
329 if (s
) { m_LastName
= location
; break; }
331 node
= node
->GetNext();
339 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
342 wxString
spec2(spec
);
344 m_FindFileHandler
= NULL
;
346 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
347 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
349 node
= m_Handlers
.GetFirst();
352 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
353 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
354 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
355 node
= node
->GetNext();
358 node
= m_Handlers
.GetFirst();
361 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
362 if (m_FindFileHandler
-> CanOpen(spec2
))
363 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
364 node
= node
->GetNext();
367 return wxEmptyString
;
372 wxString
wxFileSystem::FindNext()
374 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
375 else return m_FindFileHandler
-> FindNext();
380 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
382 m_Handlers
.Append(handler
);
386 void wxFileSystem::CleanUpHandlers()
388 m_Handlers
.DeleteContents(TRUE
);
397 class wxFileSystemModule
: public wxModule
399 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
402 virtual bool OnInit()
404 wxFileSystem::AddHandler(new wxLocalFSHandler
);
407 virtual void OnExit()
409 wxFileSystemHandler::CleanUpStatics();
410 wxFileSystem::CleanUpHandlers();
414 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
417 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS