]>
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
))) return mime
;
101 else return wxEmptyString
;
106 wxString
wxFileSystemHandler::GetProtocol(const wxString
& location
) const
108 wxString s
= wxEmptyString
;
109 int i
, l
= location
.Length();
113 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT('#')) || (!fnd
)); i
--) {
114 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
116 if (!fnd
) return wxT("file");
117 for (++i
; (i
< l
) && (location
[i
] != wxT(':')); i
++) s
<< location
[i
];
122 wxString
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const
128 for (i
= location
.Length()-1; i
>= 0; i
--) {
129 if ((location
[i
] == wxT(':')) && (i
!= 1 /*win: C:\path*/)) fnd
= TRUE
;
130 else if (fnd
&& (location
[i
] == wxT('#'))) return location
.Left(i
);
132 return wxEmptyString
;
135 wxString
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const
137 int i
, l
= location
.Length();
139 for (i
= l
-1; (i
>= 0) && ((location
[i
] != wxT(':')) || (i
== 1) || (location
[i
-2] == wxT(':'))); i
--) {if (location
[i
] == wxT('#')) l2
= i
+ 1;}
140 if (i
== 0) return wxEmptyString
;
141 else return location
.Mid(i
+ 1, l2
- i
- 2);
144 wxString
wxFileSystemHandler::GetAnchor(const wxString
& location
) const
147 int l
= location
.Length();
149 for (int i
= l
-1; i
>= 0; i
--) {
151 if (c
== wxT('#')) return location
.Right(l
-i
-1);
152 else if ((c
== wxT('.')) || (c
== wxT('/')) || (c
== wxT('\\')) || (c
== wxT(':'))) return wxEmptyString
;
154 return wxEmptyString
;
158 wxString
wxFileSystemHandler::FindFirst(const wxString
& spec
, int flags
) { return wxEmptyString
; }
160 wxString
wxFileSystemHandler::FindNext() { return wxEmptyString
; }
164 //--------------------------------------------------------------------------------
166 //--------------------------------------------------------------------------------
168 class wxLocalFSHandler
: public wxFileSystemHandler
171 virtual bool CanOpen(const wxString
& location
);
172 virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
173 virtual wxString
FindFirst(const wxString
& spec
, int flags
= 0);
174 virtual wxString
FindNext();
178 bool wxLocalFSHandler::CanOpen(const wxString
& location
)
180 return GetProtocol(location
) == wxT("file");
183 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
185 wxString right
= GetRightLocation(location
);
186 if (wxFileExists(right
))
187 return new wxFSFile(new wxFileInputStream(right
),
189 GetMimeTypeFromExt(location
),
190 GetAnchor(location
));
191 else return (wxFSFile
*) NULL
;
194 wxString
wxLocalFSHandler::FindFirst(const wxString
& spec
, int flags
)
196 wxString right
= GetRightLocation(spec
);
197 return wxFindFirstFile(right
, flags
);
200 wxString
wxLocalFSHandler::FindNext()
202 return wxFindNextFile();
207 //-----------------------------------------------------------------------------
209 //-----------------------------------------------------------------------------
211 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
)
214 wxList
wxFileSystem::m_Handlers
;
218 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
)
223 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
224 if (m_Path
[(unsigned int) i
] == wxT('\\')) m_Path
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
228 if (m_Path
.Length() > 0 && m_Path
.Last() != wxT('/') && m_Path
.Last() != wxT(':'))
234 for (i
= m_Path
.Length()-1; i
>= 0; i
--)
236 if (m_Path
[(unsigned int) i
] == wxT('/'))
238 if ((i
> 1) && (m_Path
[(unsigned int) (i
-1)] == wxT('/')) && (m_Path
[(unsigned int) (i
-2)] == wxT(':')))
249 else if (m_Path
[(unsigned int) i
] == wxT(':')) {
256 for (i
= 0; i
< (int) m_Path
.Length(); i
++)
258 if (m_Path
[(unsigned int) i
] == wxT(':'))
264 if (i
== (int) m_Path
.Length())
265 m_Path
= wxEmptyString
;
269 m_Path
.Remove(pathpos
+1);
276 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
)
278 wxString loc
= location
;
286 for (i
= 0; i
< ln
; i
++)
288 if (loc
[(unsigned int) i
] == wxT('\\')) loc
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
290 switch (loc
[(unsigned int) i
])
292 case wxT('/') : case wxT(':') : case wxT('#') : meta
= loc
[(unsigned int) i
];
295 m_LastName
= wxEmptyString
;
297 // try relative paths first :
298 if (meta
!= wxT(':'))
300 node
= m_Handlers
.GetFirst();
303 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
-> GetData();
304 if (h
->CanOpen(m_Path
+ location
))
306 s
= h
->OpenFile(*this, m_Path
+ location
);
307 if (s
) { m_LastName
= m_Path
+ location
; break; }
309 node
= node
->GetNext();
313 // if failed, try absolute paths :
316 node
= m_Handlers
.GetFirst();
319 wxFileSystemHandler
*h
= (wxFileSystemHandler
*) node
->GetData();
320 if (h
->CanOpen(location
))
322 s
= h
->OpenFile(*this, location
);
323 if (s
) { m_LastName
= location
; break; }
325 node
= node
->GetNext();
333 wxString
wxFileSystem::FindFirst(const wxString
& spec
, int flags
)
336 wxString
spec2(spec
);
338 m_FindFileHandler
= NULL
;
340 for (int i
= spec2
.Length()-1; i
>= 0; i
--)
341 if (spec2
[(unsigned int) i
] == wxT('\\')) spec2
.GetWritableChar(i
) = wxT('/'); // wanna be windows-safe
343 node
= m_Handlers
.GetFirst();
346 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
347 if (m_FindFileHandler
-> CanOpen(m_Path
+ spec2
))
348 return m_FindFileHandler
-> FindFirst(m_Path
+ spec2
, flags
);
349 node
= node
->GetNext();
352 node
= m_Handlers
.GetFirst();
355 m_FindFileHandler
= (wxFileSystemHandler
*) node
-> GetData();
356 if (m_FindFileHandler
-> CanOpen(spec2
))
357 return m_FindFileHandler
-> FindFirst(spec2
, flags
);
358 node
= node
->GetNext();
361 return wxEmptyString
;
366 wxString
wxFileSystem::FindNext()
368 if (m_FindFileHandler
== NULL
) return wxEmptyString
;
369 else return m_FindFileHandler
-> FindNext();
374 void wxFileSystem::AddHandler(wxFileSystemHandler
*handler
)
376 m_Handlers
.Append(handler
);
380 void wxFileSystem::CleanUpHandlers()
382 m_Handlers
.DeleteContents(TRUE
);
391 class wxFileSystemModule
: public wxModule
393 DECLARE_DYNAMIC_CLASS(wxFileSystemModule
)
396 virtual bool OnInit()
398 wxFileSystem::AddHandler(new wxLocalFSHandler
);
401 virtual void OnExit()
403 wxFileSystemHandler::CleanUpStatics();
404 wxFileSystem::CleanUpHandlers();
408 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
)
411 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS