]>
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
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include "wx/wxprec.h"
23 #include "wx/wfstream.h"
24 #include "wx/module.h"
25 #include "wx/filesys.h"
26 #include "wx/mimetype.h"
31 //--------------------------------------------------------------------------------
32 // wxFileSystemHandler
33 //--------------------------------------------------------------------------------
35 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
38 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
40 wxString ext
= wxEmptyString
, mime
= wxEmptyString
;
41 wxString loc
= GetRightLocation(location
);
43 int l
= loc
.Length(), l2
;
47 for (int i
= l
-1; i
>= 0; i
--) {
48 c
= loc
[(unsigned int) i
];
49 if (c
== wxT('#')) l2
= i
+ 1;
50 if (c
== wxT('.')) {ext
= loc
.Right(l2
-i
-1); break;}
51 if ((c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) {return wxEmptyString
;}
54 static bool s_MinimalMimeEnsured
= FALSE
;
55 if (!s_MinimalMimeEnsured
) {
56 static const wxFileTypeInfo fallbacks
[] =
58 wxFileTypeInfo("image/jpeg",
61 "JPEG image (from fallback)",
63 wxFileTypeInfo("image/gif",
66 "GIF image (from fallback)",
68 wxFileTypeInfo("image/png",
71 "PNG image (from fallback)",
73 wxFileTypeInfo("image/bmp",
76 "windows bitmap image (from fallback)",
78 wxFileTypeInfo("text/html",
81 "HTML document (from fallback)",
84 // must terminate the table with this!
88 wxTheMimeTypesManager
-> AddFallbacks(fallbacks
);
91 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension(ext
);
92 if ( !ft
|| !ft
-> GetMimeType(&mime
) ) {
103 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
105 wxString s
= wxEmptyString
;
106 int i
, l
= location
.Length();
110 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
111 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
113 if (!fnd
) return wxT("file");
114 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
119 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
125 for (i
= location
.Length()-1; i
>= 0; i
--) {
126 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
127 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
129 return wxEmptyString
;
132 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
134 int i
, l
= location
.Length();
136 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
137 if (i
== 0) return wxEmptyString
;
138 else return location
.Mid(i
+ 1, l2
- i
- 2);
141 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
144 int l
= location
.Length();
146 for (int i
= l
-1; i
>= 0; i
--) {
148 if (c
== wxT('#')) return location
.Right(l
-i
-1);
149 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
151 return wxEmptyString
;
155 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
158 return wxEmptyString
;
161 wxString
wxFileSystemHandler::FindNext()
163 return wxEmptyString
;
166 //--------------------------------------------------------------------------------
168 //--------------------------------------------------------------------------------
170 class wxLocalFSHandler
: public wxFileSystemHandler
173 virtual bool CanOpen(const wxString
& location
);
174 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
175 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
176 virtual wxString
FindNext();
180 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
182 return GetProtocol(location
) == wxT("file");
185 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
187 wxString right
= GetRightLocation(location
);
188 if (!wxFileExists(right
))
189 return (wxFSFile
*) NULL
;
191 return new wxFSFile(new wxFileInputStream(right
),
193 GetMimeTypeFromExt(location
),
195 wxDateTime(wxFileModificationTime(right
)));
199 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
201 wxString right
= GetRightLocation(spec
);
202 return wxFindFirstFile(right
, flags
);
205 wxString
wxLocalFSHandler::FindNext()
207 return wxFindNextFile();
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
216 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
219 wxList
wxFileSystem::m_Handlers
;
222 static wxString
MakeCorrectPath(const wxString
& path
)
229 for (i
= 0; i
< cnt
; i
++)
230 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
232 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
234 if (cnt
< 3) return p
;
236 r
<< p
.GetChar(0) << p
.GetChar(1);
238 // skip trailing ../.., if any
239 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
241 // remove back references: translate dir1/../dir2 to dir2
245 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
247 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
248 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
250 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
256 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
262 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
266 m_Path
= MakeCorrectPath(location
);
270 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
276 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
278 if (m_Path
[(unsigned int) i
] == wxT('/'))
280 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
291 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
298 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
300 if (m_Path
[(unsigned int) i
] == wxT(':'))
306 if (i
== (int) m_Path
.Length())
307 m_Path
= wxEmptyString
;
311 m_Path
.Remove(pathpos
+1);
318 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
320 wxString loc
= MakeCorrectPath(location
);
328 for (i
= 0; i
< ln
; i
++)
332 case wxT('/') : case wxT(':') : case wxT('#') :
336 if (meta
!= 0) break;
338 m_LastName
= wxEmptyString
;
340 // try relative paths first :
341 if (meta
!= wxT(':'))
343 node
= m_Handlers
.GetFirst();
346 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
347 if (h
->CanOpen(m_Path
+ loc
))
349 s
= h
->OpenFile(*this, m_Path
+ loc
);
350 if (s
) { m_LastName
= m_Path
+ loc
; break; }
352 node
= node
->GetNext();
356 // if failed, try absolute paths :
359 node
= m_Handlers
.GetFirst();
362 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
365 s
= h
->OpenFile(*this, loc
);
366 if (s
) { m_LastName
= loc
; break; }
368 node
= node
->GetNext();
376 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
379 wxString
spec2(spec
);
381 m_FindFileHandler
= NULL
;
383 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
384 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
386 node
= m_Handlers
.GetFirst();
389 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
390 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
391 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
392 node
= node
->GetNext();
395 node
= m_Handlers
.GetFirst();
398 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
399 if (m_FindFileHandler
-> CanOpen(spec2
))
400 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
401 node
= node
->GetNext();
404 return wxEmptyString
;
409 wxString
wxFileSystem::FindNext()
411 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
412 else return m_FindFileHandler
-> FindNext();
417 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
419 m_Handlers
.Append(handler
);
423 void wxFileSystem::CleanUpHandlers()
425 m_Handlers
.DeleteContents(TRUE
);
434 class wxFileSystemModule
: public wxModule
436 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
439 virtual bool OnInit()
441 wxFileSystem::AddHandler(new wxLocalFSHandler
);
444 virtual void OnExit()
446 wxFileSystem::CleanUpHandlers();
450 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)