]>
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_HTML || wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
26 #include "wx/wfstream.h"
27 #include "wx/module.h"
28 #include "wx/filesys.h"
29 #include "wx/mimetype.h"
34 //--------------------------------------------------------------------------------
35 // wxFileSystemHandler
36 //--------------------------------------------------------------------------------
38 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
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
--) {
51 c
= loc
[(unsigned int) i
];
52 if (c
== wxT('#')) l2
= i
+ 1;
53 if (c
== wxT('.')) {ext
= loc
.Right(l2
-i
-1); break;}
54 if ((c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) {return wxEmptyString
;}
57 static bool s_MinimalMimeEnsured
= FALSE
;
58 if (!s_MinimalMimeEnsured
) {
59 static const wxFileTypeInfo fallbacks
[] =
61 wxFileTypeInfo("image/jpeg",
64 "JPEG image (from fallback)",
66 wxFileTypeInfo("image/gif",
69 "GIF image (from fallback)",
71 wxFileTypeInfo("image/png",
74 "PNG image (from fallback)",
76 wxFileTypeInfo("image/bmp",
79 "windows bitmap image (from fallback)",
81 wxFileTypeInfo("text/html",
84 "HTML document (from fallback)",
87 // must terminate the table with this!
91 wxTheMimeTypesManager
-> AddFallbacks(fallbacks
);
94 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension(ext
);
95 if (ft
&& (ft
-> GetMimeType(&mime
))) {
101 return wxEmptyString
;
107 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
109 wxString s
= wxEmptyString
;
110 int i
, l
= location
.Length();
114 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
115 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
117 if (!fnd
) return wxT("file");
118 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
123 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
129 for (i
= location
.Length()-1; i
>= 0; i
--) {
130 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
131 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
133 return wxEmptyString
;
136 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
138 int i
, l
= location
.Length();
140 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
141 if (i
== 0) return wxEmptyString
;
142 else return location
.Mid(i
+ 1, l2
- i
- 2);
145 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
148 int l
= location
.Length();
150 for (int i
= l
-1; i
>= 0; i
--) {
152 if (c
== wxT('#')) return location
.Right(l
-i
-1);
153 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
155 return wxEmptyString
;
159 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
162 return wxEmptyString
;
165 wxString
wxFileSystemHandler::FindNext()
167 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
),
197 wxDateTime(wxFileModificationTime(right
)));
198 else return (wxFSFile
*) NULL
;
201 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
203 wxString right
= GetRightLocation(spec
);
204 return wxFindFirstFile(right
, flags
);
207 wxString
wxLocalFSHandler::FindNext()
209 return wxFindNextFile();
214 //-----------------------------------------------------------------------------
216 //-----------------------------------------------------------------------------
218 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
221 wxList
wxFileSystem::m_Handlers
;
225 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
230 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
231 if (m_Path
[(unsigned int) i
] == wxT('\\')) m_Path
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
235 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
241 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
243 if (m_Path
[(unsigned int) i
] == wxT('/'))
245 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
256 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
263 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
265 if (m_Path
[(unsigned int) i
] == wxT(':'))
271 if (i
== (int) m_Path
.Length())
272 m_Path
= wxEmptyString
;
276 m_Path
.Remove(pathpos
+1);
283 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
285 wxString loc
= location
;
293 for (i
= 0; i
< ln
; i
++)
295 if (loc
[(unsigned int) i
] == wxT('\\')) loc
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
297 switch (loc
[(unsigned int) i
])
299 case wxT('/') : case wxT(':') : case wxT('#') : meta
= loc
[(unsigned int) i
];
302 m_LastName
= wxEmptyString
;
304 // try relative paths first :
305 if (meta
!= wxT(':'))
307 node
= m_Handlers
.GetFirst();
310 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
311 if (h
->CanOpen(m_Path
+ loc
))
313 s
= h
->OpenFile(*this, m_Path
+ loc
);
314 if (s
) { m_LastName
= m_Path
+ loc
; break; }
316 node
= node
->GetNext();
320 // if failed, try absolute paths :
323 node
= m_Handlers
.GetFirst();
326 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
329 s
= h
->OpenFile(*this, loc
);
330 if (s
) { m_LastName
= loc
; break; }
332 node
= node
->GetNext();
340 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
343 wxString
spec2(spec
);
345 m_FindFileHandler
= NULL
;
347 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
348 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
350 node
= m_Handlers
.GetFirst();
353 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
354 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
355 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
356 node
= node
->GetNext();
359 node
= m_Handlers
.GetFirst();
362 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
363 if (m_FindFileHandler
-> CanOpen(spec2
))
364 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
365 node
= node
->GetNext();
368 return wxEmptyString
;
373 wxString
wxFileSystem::FindNext()
375 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
376 else return m_FindFileHandler
-> FindNext();
381 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
383 m_Handlers
.Append(handler
);
387 void wxFileSystem::CleanUpHandlers()
389 m_Handlers
.DeleteContents(TRUE
);
398 class wxFileSystemModule
: public wxModule
400 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
403 virtual bool OnInit()
405 wxFileSystem::AddHandler(new wxLocalFSHandler
);
408 virtual void OnExit()
410 wxFileSystem::CleanUpHandlers();
414 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
417 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS