]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
mimetype.cpp/.h split into unix,mac,msw
[wxWidgets.git] / src / common / filesys.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: filesys.cpp
3// Purpose: wxFileSystem class - interface for opening files
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
6// Licence: wxWindows Licence
7/////////////////////////////////////////////////////////////////////////////
8
9#ifdef __GNUG__
10#pragma implementation
11#endif
12
d30e0edd 13#include "wx/wxprec.h"
5526e819
VS
14
15#ifdef __BORDLANDC__
16#pragma hdrstop
17#endif
18
31528cd3 19#if !wxUSE_SOCKETS
31528cd3 20 #undef wxUSE_FS_INET
31528cd3
VZ
21 #define wxUSE_FS_INET 0
22#endif
23
f22e0c3c 24#if (wxUSE_HTML || wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 25
d30e0edd
RR
26#include "wx/wfstream.h"
27#include "wx/module.h"
28#include "wx/filesys.h"
5526e819
VS
29
30//--------------------------------------------------------------------------------
31// wxFileSystemHandler
32//--------------------------------------------------------------------------------
33
34IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
35
a76015e6
VS
36wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL;
37
38void wxFileSystemHandler::CleanUpStatics()
39{
40 if (m_MimeMng) delete m_MimeMng;
41 m_MimeMng = NULL;
42}
5526e819
VS
43
44
45wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location)
46{
47 wxString ext = wxEmptyString, mime = wxEmptyString;
48 wxString loc = GetRightLocation(location);
49 char c;
50 int l = loc.Length(), l2;
51 wxFileType *ft;
52
53 l2 = l;
54 for (int i = l-1; i >= 0; i--) {
ea4f5235 55 c = loc[(unsigned int) i];
223d09f6
KB
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;}
5526e819 59 }
956418ab
VS
60
61 if (m_MimeMng == NULL) {
62 m_MimeMng = new wxMimeTypesManager;
63
64 static const wxFileTypeInfo fallbacks[] =
65 {
66 wxFileTypeInfo("image/jpeg",
67 "",
68 "",
69 "JPEG image (from fallback)",
70 "jpg", "jpeg", NULL),
71 wxFileTypeInfo("image/gif",
72 "",
73 "",
74 "GIF image (from fallback)",
75 "gif", NULL),
76 wxFileTypeInfo("image/png",
77 "",
78 "",
79 "PNG image (from fallback)",
80 "png", NULL),
81 wxFileTypeInfo("image/bmp",
82 "",
83 "",
84 "windows bitmap image (from fallback)",
85 "bmp", NULL),
86 wxFileTypeInfo("text/html",
87 "",
88 "",
89 "HTML document (from fallback)",
90 "htm", "html", NULL),
91
92 // must terminate the table with this!
93 wxFileTypeInfo()
94 };
95
96 m_MimeMng -> AddFallbacks(fallbacks);
97 }
98
a76015e6 99 ft = m_MimeMng -> GetFileTypeFromExtension(ext);
51f79d5c
VS
100 if (ft && (ft -> GetMimeType(&mime))) {
101 delete ft;
102 return mime;
103 }
104 else {
105 delete ft;
106 return wxEmptyString;
107 }
5526e819
VS
108}
109
110
111
112wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
113{
114 wxString s = wxEmptyString;
115 int i, l = location.Length();
116 bool fnd;
117
118 fnd = FALSE;
223d09f6
KB
119 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
120 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 121 }
223d09f6
KB
122 if (!fnd) return wxT("file");
123 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
5526e819
VS
124 return s;
125}
126
127
5526e819
VS
128wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
129{
130 int i;
131 bool fnd;
132
133 fnd = FALSE;
134 for (i = location.Length()-1; i >= 0; i--) {
223d09f6
KB
135 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
136 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
5526e819
VS
137 }
138 return wxEmptyString;
139}
140
5526e819
VS
141wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
142{
143 int i, l = location.Length();
144 int l2 = l + 1;
223d09f6 145 for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;}
5526e819
VS
146 if (i == 0) return wxEmptyString;
147 else return location.Mid(i + 1, l2 - i - 2);
148}
149
5526e819
VS
150wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
151{
152 char c;
153 int l = location.Length();
154
155 for (int i = l-1; i >= 0; i--) {
156 c = location[i];
223d09f6
KB
157 if (c == wxT('#')) return location.Right(l-i-1);
158 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
5526e819
VS
159 }
160 return wxEmptyString;
161}
162
aaa66113 163
f76dbc4d
VZ
164wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
165 int WXUNUSED(flags))
166{
167 return wxEmptyString;
168}
aaa66113 169
f76dbc4d
VZ
170wxString wxFileSystemHandler::FindNext()
171{
172 return wxEmptyString;
173}
aaa66113 174
5526e819
VS
175//--------------------------------------------------------------------------------
176// wxLocalFSHandler
177//--------------------------------------------------------------------------------
178
179class wxLocalFSHandler : public wxFileSystemHandler
180{
181 public:
182 virtual bool CanOpen(const wxString& location);
183 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
aaa66113
VS
184 virtual wxString FindFirst(const wxString& spec, int flags = 0);
185 virtual wxString FindNext();
5526e819
VS
186};
187
188
5526e819
VS
189bool wxLocalFSHandler::CanOpen(const wxString& location)
190{
223d09f6 191 return GetProtocol(location) == wxT("file");
5526e819
VS
192}
193
5526e819
VS
194wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
195{
196 wxString right = GetRightLocation(location);
197 if (wxFileExists(right))
198 return new wxFSFile(new wxFileInputStream(right),
199 right,
200 GetMimeTypeFromExt(location),
6ee654e6
VS
201 GetAnchor(location),
202 wxDateTime(wxFileModificationTime(right)));
d30e0edd 203 else return (wxFSFile*) NULL;
5526e819
VS
204}
205
aaa66113
VS
206wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
207{
208 wxString right = GetRightLocation(spec);
209 return wxFindFirstFile(right, flags);
210}
211
212wxString wxLocalFSHandler::FindNext()
213{
214 return wxFindNextFile();
215}
216
217
218
5526e819
VS
219//-----------------------------------------------------------------------------
220// wxFileSystem
221//-----------------------------------------------------------------------------
222
223IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
224
225
226wxList wxFileSystem::m_Handlers;
227
228
229
230void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
231{
232 int i, pathpos = -1;
233 m_Path = location;
234
235 for (i = m_Path.Length()-1; i >= 0; i--)
223d09f6 236 if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
d30e0edd 237
aaa66113
VS
238 if (is_dir)
239 {
240 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
241 m_Path << wxT('/');
242 }
243
244 else
d30e0edd 245 {
269e8200 246 for (i = m_Path.Length()-1; i >= 0; i--)
aaa66113 247 {
223d09f6 248 if (m_Path[(unsigned int) i] == wxT('/'))
aaa66113 249 {
223d09f6 250 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
aaa66113 251 {
5526e819
VS
252 i -= 2;
253 continue;
254 }
269e8200 255 else
aaa66113 256 {
269e8200 257 pathpos = i;
5526e819
VS
258 break;
259 }
260 }
aaa66113
VS
261 else if (m_Path[(unsigned int) i] == wxT(':')) {
262 pathpos = i;
263 break;
264 }
5526e819 265 }
269e8200 266 if (pathpos == -1)
aaa66113 267 {
269e8200 268 for (i = 0; i < (int) m_Path.Length(); i++)
aaa66113 269 {
223d09f6 270 if (m_Path[(unsigned int) i] == wxT(':'))
aaa66113 271 {
5526e819
VS
272 m_Path.Remove(i+1);
273 break;
274 }
275 }
269e8200 276 if (i == (int) m_Path.Length())
aaa66113 277 m_Path = wxEmptyString;
5526e819 278 }
269e8200 279 else
aaa66113 280 {
5526e819
VS
281 m_Path.Remove(pathpos+1);
282 }
283 }
284}
285
286
287
288wxFSFile* wxFileSystem::OpenFile(const wxString& location)
289{
290 wxString loc = location;
291 int i, ln;
292 char meta;
293 wxFSFile *s = NULL;
294 wxNode *node;
295
296 ln = loc.Length();
297 meta = 0;
269e8200 298 for (i = 0; i < ln; i++)
d30e0edd 299 {
223d09f6 300 if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
aaa66113
VS
301 if (!meta)
302 switch (loc[(unsigned int) i])
303 {
304 case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i];
305 }
5526e819
VS
306 }
307 m_LastName = wxEmptyString;
308
309 // try relative paths first :
223d09f6 310 if (meta != wxT(':'))
d30e0edd 311 {
5526e819 312 node = m_Handlers.GetFirst();
d30e0edd 313 while (node)
aaa66113 314 {
5526e819 315 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 316 if (h->CanOpen(m_Path + loc))
aaa66113 317 {
7dee4b2b
VS
318 s = h->OpenFile(*this, m_Path + loc);
319 if (s) { m_LastName = m_Path + loc; break; }
5526e819 320 }
d30e0edd 321 node = node->GetNext();
5526e819
VS
322 }
323 }
324
325 // if failed, try absolute paths :
269e8200 326 if (s == NULL)
d30e0edd 327 {
5526e819 328 node = m_Handlers.GetFirst();
d30e0edd 329 while (node)
aaa66113 330 {
d30e0edd 331 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 332 if (h->CanOpen(loc))
aaa66113 333 {
7dee4b2b
VS
334 s = h->OpenFile(*this, loc);
335 if (s) { m_LastName = loc; break; }
5526e819 336 }
d30e0edd 337 node = node->GetNext();
5526e819
VS
338 }
339 }
340 return (s);
341}
342
343
aaa66113
VS
344
345wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
346{
347 wxNode *node;
348 wxString spec2(spec);
349
350 m_FindFileHandler = NULL;
351
352 for (int i = spec2.Length()-1; i >= 0; i--)
353 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
354
355 node = m_Handlers.GetFirst();
356 while (node)
357 {
358 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
359 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
360 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
361 node = node->GetNext();
362 }
363
364 node = m_Handlers.GetFirst();
365 while (node)
366 {
367 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
368 if (m_FindFileHandler -> CanOpen(spec2))
369 return m_FindFileHandler -> FindFirst(spec2, flags);
370 node = node->GetNext();
371 }
372
373 return wxEmptyString;
374}
375
376
377
378wxString wxFileSystem::FindNext()
379{
380 if (m_FindFileHandler == NULL) return wxEmptyString;
381 else return m_FindFileHandler -> FindNext();
382}
383
384
385
5526e819
VS
386void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
387{
388 m_Handlers.Append(handler);
389}
390
391
269e8200
RD
392void wxFileSystem::CleanUpHandlers()
393{
394 m_Handlers.DeleteContents(TRUE);
395 m_Handlers.Clear();
396}
397
5526e819 398
aaa66113
VS
399
400
5526e819
VS
401///// Module:
402
403class wxFileSystemModule : public wxModule
404{
405 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
406
407 public:
408 virtual bool OnInit()
409 {
410 wxFileSystem::AddHandler(new wxLocalFSHandler);
411 return TRUE;
412 }
269e8200 413 virtual void OnExit()
aaa66113
VS
414 {
415 wxFileSystemHandler::CleanUpStatics();
269e8200 416 wxFileSystem::CleanUpHandlers();
aaa66113 417 }
5526e819
VS
418};
419
420IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
421
269e8200 422#endif
d30e0edd 423 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 424
a76015e6
VS
425
426