]>
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 "filesys.h"
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"
27 #include "wx/filename.h"
31 //--------------------------------------------------------------------------------
32 // wxFileSystemHandler
33 //--------------------------------------------------------------------------------
35 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
)
39 static wxFileTypeInfo
*gs_FSMimeFallbacks
= NULL
;
42 wxString
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
)
45 wxString loc
= GetRightLocation(location
);
47 int l
= loc
.Length(), l2
;
50 for (int i
= l
-1; i
>= 0; i
--)
52 c
= loc
[(unsigned int) i
];
57 ext
= loc
.Right(l2
-i
-1);
60 if ( (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':')) )
65 static bool s_MinimalMimeEnsured
= FALSE
;
66 if (!s_MinimalMimeEnsured
) {
67 wxTheMimeTypesManager
->AddFallbacks(gs_FSMimeFallbacks
);
68 s_MinimalMimeEnsured
= TRUE
;
71 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
72 if ( !ft
|| !ft
-> GetMimeType(&mime
) )
81 if ( ext
.IsSameAs(wxT("htm"), FALSE
) || ext
.IsSameAs(_T("html"), FALSE
) )
82 return wxT("text/html");
83 if ( ext
.IsSameAs(wxT("jpg"), FALSE
) || ext
.IsSameAs(_T("jpeg"), FALSE
) )
84 return wxT("image/jpeg");
85 if ( ext
.IsSameAs(wxT("gif"), FALSE
) )
86 return wxT("image/gif");
87 if ( ext
.IsSameAs(wxT("png"), FALSE
) )
88 return wxT("image/png");
89 if ( ext
.IsSameAs(wxT("bmp"), FALSE
) )
90 return wxT("image/bmp");
97 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
99 wxString s
= wxEmptyString
;
100 int i
, l
= location
.Length();
104 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
105 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
107 if (!fnd
) return wxT("file");
108 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
113 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
119 for (i
= location
.Length()-1; i
>= 0; i
--) {
120 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
121 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
123 return wxEmptyString
;
126 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
128 int i
, l
= location
.Length();
130 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
131 if (i
== 0) return wxEmptyString
;
132 else return location
.Mid(i
+ 1, l2
- i
- 2);
135 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
138 int l
= location
.Length();
140 for (int i
= l
-1; i
>= 0; i
--) {
142 if (c
== wxT('#')) return location
.Right(l
-i
-1);
143 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
145 return wxEmptyString
;
149 wxString
wxFileSystemHandler::FindFirst(const wxString
& WXUNUSED(spec
),
152 return wxEmptyString
;
155 wxString
wxFileSystemHandler::FindNext()
157 return wxEmptyString
;
160 //--------------------------------------------------------------------------------
162 //--------------------------------------------------------------------------------
165 wxString
wxLocalFSHandler::ms_root
;
167 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
169 return GetProtocol(location
) == wxT("file");
172 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
174 // location has Unix path separators
175 wxString right
= ms_root
+ GetRightLocation(location
);
176 wxFileName
fn(right
, wxPATH_UNIX
);
178 if (!wxFileExists(fn
.GetFullPath()))
179 return (wxFSFile
*) NULL
;
181 return new wxFSFile(new wxFFileInputStream(fn
.GetFullPath()),
183 GetMimeTypeFromExt(location
),
185 wxDateTime(wxFileModificationTime(fn
.GetFullPath())));
189 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
191 wxString right
= ms_root
+ GetRightLocation(spec
);
192 return wxFindFirstFile(right
, flags
);
195 wxString
wxLocalFSHandler::FindNext()
197 return wxFindNextFile();
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
206 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
207 IMPLEMENT_ABSTRACT_CLASS(wxFSFile
, wxObject
)
210 wxList
wxFileSystem::m_Handlers
;
213 static wxString
MakeCorrectPath(const wxString
& path
)
220 for (i
= 0; i
< cnt
; i
++)
221 if (p
.GetChar(i
) == wxT('\\')) p
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
223 if (p
.Left(2) == wxT("./")) { p
= p
.Mid(2); cnt
-= 2; }
225 if (cnt
< 3) return p
;
227 r
<< p
.GetChar(0) << p
.GetChar(1);
229 // skip trailing ../.., if any
230 for (i
= 2; i
< cnt
&& (p
.GetChar(i
) == wxT('/') || p
.GetChar(i
) == wxT('.')); i
++) r
<< p
.GetChar(i
);
232 // remove back references: translate dir1/../dir2 to dir2
236 if (p
.GetChar(i
) == wxT('/') && p
.GetChar(i
-1) == wxT('.') && p
.GetChar(i
-2) == wxT('.'))
238 for (j
= r
.Length() - 2; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
239 if (j
>= 0 && r
.GetChar(j
) != wxT(':'))
241 for (j
= j
- 1; j
>= 0 && r
.GetChar(j
) != wxT('/') && r
.GetChar(j
) != wxT(':'); j
--) {}
247 for (; i
< cnt
; i
++) r
<< p
.GetChar(i
);
253 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
257 m_Path
= MakeCorrectPath(location
);
261 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
267 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
269 if (m_Path
[(unsigned int) i
] == wxT('/'))
271 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
282 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
289 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
291 if (m_Path
[(unsigned int) i
] == wxT(':'))
297 if (i
== (int) m_Path
.Length())
298 m_Path
= wxEmptyString
;
302 m_Path
.Remove(pathpos
+1);
309 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
311 wxString loc
= MakeCorrectPath(location
);
319 for (i
= 0; i
< ln
; i
++)
323 case wxT('/') : case wxT(':') : case wxT('#') :
327 if (meta
!= 0) break;
329 m_LastName
= wxEmptyString
;
331 // try relative paths first :
332 if (meta
!= wxT(':'))
334 node
= m_Handlers
.GetFirst();
337 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
338 if (h
->CanOpen(m_Path
+ loc
))
340 s
= h
->OpenFile(*this, m_Path
+ loc
);
341 if (s
) { m_LastName
= m_Path
+ loc
; break; }
343 node
= node
->GetNext();
347 // if failed, try absolute paths :
350 node
= m_Handlers
.GetFirst();
353 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
356 s
= h
->OpenFile(*this, loc
);
357 if (s
) { m_LastName
= loc
; break; }
359 node
= node
->GetNext();
367 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
370 wxString
spec2(spec
);
372 m_FindFileHandler
= NULL
;
374 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
375 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // Want to be windows-safe
377 node
= m_Handlers
.GetFirst();
380 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
381 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
382 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
383 node
= node
->GetNext();
386 node
= m_Handlers
.GetFirst();
389 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
390 if (m_FindFileHandler
-> CanOpen(spec2
))
391 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
392 node
= node
->GetNext();
395 return wxEmptyString
;
400 wxString
wxFileSystem::FindNext()
402 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
403 else return m_FindFileHandler
-> FindNext();
408 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
410 m_Handlers
.Append(handler
);
414 void wxFileSystem::CleanUpHandlers()
416 m_Handlers
.DeleteContents(TRUE
);
425 class wxFileSystemModule
: public wxModule
427 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
430 virtual bool OnInit()
432 wxFileSystem::AddHandler(new wxLocalFSHandler
);
435 gs_FSMimeFallbacks
= new wxFileTypeInfo
[6];
436 gs_FSMimeFallbacks
[0] =
437 wxFileTypeInfo("image/jpeg",
440 "JPEG image (from fallback)",
441 "jpg", "jpeg", NULL
);
442 gs_FSMimeFallbacks
[1] =
443 wxFileTypeInfo("image/gif",
446 "GIF image (from fallback)",
448 gs_FSMimeFallbacks
[2] =
449 wxFileTypeInfo("image/png",
452 "PNG image (from fallback)",
454 gs_FSMimeFallbacks
[3] =
455 wxFileTypeInfo("image/bmp",
458 "windows bitmap image (from fallback)",
460 gs_FSMimeFallbacks
[4] =
461 wxFileTypeInfo("text/html",
464 "HTML document (from fallback)",
465 "htm", "html", NULL
);
466 gs_FSMimeFallbacks
[5] =
467 // must terminate the table with this!
472 virtual void OnExit()
475 delete [] gs_FSMimeFallbacks
;
477 wxFileSystem::CleanUpHandlers();
481 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)