]>
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
))) {
104 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
106 wxString s
= wxEmptyString
;
107 int i
, l
= location
.Length();
111 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
112 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
114 if (!fnd
) return wxT("file");
115 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
120 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
126 for (i
= location
.Length()-1; i
>= 0; i
--) {
127 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
128 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
130 return wxEmptyString
;
133 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
135 int i
, l
= location
.Length();
137 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
138 if (i
== 0) return wxEmptyString
;
139 else return location
.Mid(i
+ 1, l2
- i
- 2);
142 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
145 int l
= location
.Length();
147 for (int i
= l
-1; i
>= 0; i
--) {
149 if (c
== wxT('#')) return location
.Right(l
-i
-1);
150 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
152 return wxEmptyString
;
156 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
159 return wxEmptyString
;
162 wxString
wxFileSystemHandler::FindNext()
164 return wxEmptyString
;
167 //--------------------------------------------------------------------------------
169 //--------------------------------------------------------------------------------
171 class wxLocalFSHandler
: public wxFileSystemHandler
174 virtual bool CanOpen(const wxString
& location
);
175 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
176 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
177 virtual wxString
FindNext();
181 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
183 return GetProtocol(location
) == wxT("file");
186 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
188 wxString right
= GetRightLocation(location
);
189 if (wxFileExists(right
))
190 return new wxFSFile(new wxFileInputStream(right
),
192 GetMimeTypeFromExt(location
),
194 wxDateTime(wxFileModificationTime(right
)));
195 else return (wxFSFile
*) NULL
;
198 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
200 wxString right
= GetRightLocation(spec
);
201 return wxFindFirstFile(right
, flags
);
204 wxString
wxLocalFSHandler::FindNext()
206 return wxFindNextFile();
211 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
215 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
218 wxList
wxFileSystem::m_Handlers
;
221 static wxString
MakeCorrectPath(const wxString
& path
)
228 for (i
= 0; i
< cnt
; i
++)
229 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
231 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
233 if (cnt
< 3) return p
;
235 r
<< p
.GetChar(0) << p
.GetChar(1);
237 // skip trailing ../.., if any
238 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
240 // remove back references: translate dir1/../dir2 to dir2
244 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
246 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
247 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
249 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
255 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
261 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
265 m_Path
= MakeCorrectPath(location
);
269 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
275 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
277 if (m_Path
[(unsigned int) i
] == wxT('/'))
279 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
290 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
297 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
299 if (m_Path
[(unsigned int) i
] == wxT(':'))
305 if (i
== (int) m_Path
.Length())
306 m_Path
= wxEmptyString
;
310 m_Path
.Remove(pathpos
+1);
317 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
319 wxString loc
= MakeCorrectPath(location
);
327 for (i
= 0; i
< ln
; i
++)
332 case wxT('/') : case wxT(':') : case wxT('#') : meta
= loc
[i
];
335 m_LastName
= wxEmptyString
;
337 // try relative paths first :
338 if (meta
!= wxT(':'))
340 node
= m_Handlers
.GetFirst();
343 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
344 if (h
->CanOpen(m_Path
+ loc
))
346 s
= h
->OpenFile(*this, m_Path
+ loc
);
347 if (s
) { m_LastName
= m_Path
+ loc
; break; }
349 node
= node
->GetNext();
353 // if failed, try absolute paths :
356 node
= m_Handlers
.GetFirst();
359 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
362 s
= h
->OpenFile(*this, loc
);
363 if (s
) { m_LastName
= loc
; break; }
365 node
= node
->GetNext();
373 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
376 wxString
spec2(spec
);
378 m_FindFileHandler
= NULL
;
380 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
381 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
383 node
= m_Handlers
.GetFirst();
386 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
387 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
388 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
389 node
= node
->GetNext();
392 node
= m_Handlers
.GetFirst();
395 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
396 if (m_FindFileHandler
-> CanOpen(spec2
))
397 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
398 node
= node
->GetNext();
401 return wxEmptyString
;
406 wxString
wxFileSystem::FindNext()
408 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
409 else return m_FindFileHandler
-> FindNext();
414 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
416 m_Handlers
.Append(handler
);
420 void wxFileSystem::CleanUpHandlers()
422 m_Handlers
.DeleteContents(TRUE
);
431 class wxFileSystemModule
: public wxModule
433 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
436 virtual bool OnInit()
438 wxFileSystem::AddHandler(new wxLocalFSHandler
);
441 virtual void OnExit()
443 wxFileSystem::CleanUpHandlers();
447 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)