]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
Updates to match recent CVS changes.
[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
5be0cf65 6// CVS-ID: $Id$
5526e819
VS
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation
12#endif
13
d30e0edd 14#include "wx/wxprec.h"
5526e819
VS
15
16#ifdef __BORDLANDC__
17#pragma hdrstop
18#endif
19
31528cd3 20
24528b0c 21#if wxUSE_FILESYSTEM
5526e819 22
d30e0edd
RR
23#include "wx/wfstream.h"
24#include "wx/module.h"
25#include "wx/filesys.h"
73725567 26#include "wx/mimetype.h"
6464f4cb 27#include "wx/filename.h"
73725567
VS
28
29
5526e819
VS
30
31//--------------------------------------------------------------------------------
32// wxFileSystemHandler
33//--------------------------------------------------------------------------------
34
35IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
36
5526e819 37
659a6064 38#if wxUSE_MIMETYPE
dbb88122 39static wxFileTypeInfo *gs_FSMimeFallbacks = NULL;
659a6064 40#endif
dbb88122 41
5526e819
VS
42wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location)
43{
45681cd7 44 wxString ext, mime;
5526e819
VS
45 wxString loc = GetRightLocation(location);
46 char c;
47 int l = loc.Length(), l2;
5526e819
VS
48
49 l2 = l;
46837272 50 for (int i = l-1; i >= 0; i--)
45681cd7 51 {
ea4f5235 52 c = loc[(unsigned int) i];
45681cd7
VS
53 if ( c == wxT('#') )
54 l2 = i + 1;
55 if ( c == wxT('.') )
56 {
46837272 57 ext = loc.Right(l2-i-1);
45681cd7
VS
58 break;
59 }
60 if ( (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':')) )
61 return wxEmptyString;
5526e819 62 }
956418ab 63
45681cd7 64#if wxUSE_MIMETYPE
73725567
VS
65 static bool s_MinimalMimeEnsured = FALSE;
66 if (!s_MinimalMimeEnsured) {
45681cd7 67 wxTheMimeTypesManager->AddFallbacks(gs_FSMimeFallbacks);
dbb88122 68 s_MinimalMimeEnsured = TRUE;
956418ab
VS
69 }
70
45681cd7
VS
71 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
72 if ( !ft || !ft -> GetMimeType(&mime) )
73 {
f6bcfd97 74 mime = wxEmptyString;
51f79d5c 75 }
f6bcfd97
BP
76
77 delete ft;
78
79 return mime;
659a6064 80#else
45681cd7
VS
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");
659a6064
VS
91 return wxEmptyString;
92#endif
5526e819
VS
93}
94
95
96
97wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
98{
99 wxString s = wxEmptyString;
100 int i, l = location.Length();
101 bool fnd;
102
103 fnd = FALSE;
223d09f6
KB
104 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
105 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 106 }
223d09f6
KB
107 if (!fnd) return wxT("file");
108 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
5526e819
VS
109 return s;
110}
111
112
5526e819
VS
113wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
114{
115 int i;
116 bool fnd;
117
118 fnd = FALSE;
119 for (i = location.Length()-1; i >= 0; i--) {
223d09f6
KB
120 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
121 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
5526e819
VS
122 }
123 return wxEmptyString;
124}
125
5526e819
VS
126wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
127{
128 int i, l = location.Length();
129 int l2 = l + 1;
223d09f6 130 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
131 if (i == 0) return wxEmptyString;
132 else return location.Mid(i + 1, l2 - i - 2);
133}
134
5526e819
VS
135wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
136{
137 char c;
138 int l = location.Length();
139
140 for (int i = l-1; i >= 0; i--) {
141 c = location[i];
223d09f6
KB
142 if (c == wxT('#')) return location.Right(l-i-1);
143 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
5526e819
VS
144 }
145 return wxEmptyString;
146}
147
aaa66113 148
f76dbc4d
VZ
149wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
150 int WXUNUSED(flags))
151{
152 return wxEmptyString;
153}
aaa66113 154
f76dbc4d
VZ
155wxString wxFileSystemHandler::FindNext()
156{
157 return wxEmptyString;
158}
aaa66113 159
5526e819
VS
160//--------------------------------------------------------------------------------
161// wxLocalFSHandler
162//--------------------------------------------------------------------------------
163
5526e819 164
19008b7b 165wxString wxLocalFSHandler::ms_root;
5526e819 166
5526e819
VS
167bool wxLocalFSHandler::CanOpen(const wxString& location)
168{
223d09f6 169 return GetProtocol(location) == wxT("file");
5526e819
VS
170}
171
5526e819
VS
172wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
173{
6464f4cb 174 // location has Unix path separators
19008b7b
VS
175 wxString right = ms_root + GetRightLocation(location);
176 wxFileName fn(right, wxPATH_UNIX);
46837272 177
19008b7b 178 if (!wxFileExists(fn.GetFullPath()))
f6bcfd97 179 return (wxFSFile*) NULL;
46837272 180
19008b7b 181 return new wxFSFile(new wxFileInputStream(fn.GetFullPath()),
f6bcfd97
BP
182 right,
183 GetMimeTypeFromExt(location),
184 GetAnchor(location),
19008b7b 185 wxDateTime(wxFileModificationTime(fn.GetFullPath())));
f6bcfd97 186
5526e819
VS
187}
188
aaa66113
VS
189wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
190{
19008b7b 191 wxString right = ms_root + GetRightLocation(spec);
aaa66113
VS
192 return wxFindFirstFile(right, flags);
193}
194
195wxString wxLocalFSHandler::FindNext()
196{
197 return wxFindNextFile();
198}
199
200
201
5526e819
VS
202//-----------------------------------------------------------------------------
203// wxFileSystem
204//-----------------------------------------------------------------------------
205
206IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
46837272 207IMPLEMENT_ABSTRACT_CLASS(wxFSFile, wxObject)
5526e819
VS
208
209
210wxList wxFileSystem::m_Handlers;
211
212
5be0cf65
VS
213static wxString MakeCorrectPath(const wxString& path)
214{
215 wxString p(path);
216 wxString r;
217 int i, j, cnt;
86b3203f 218
5be0cf65
VS
219 cnt = p.Length();
220 for (i = 0; i < cnt; i++)
f6081a04 221 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
86b3203f 222
5be0cf65 223 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
86b3203f 224
5be0cf65 225 if (cnt < 3) return p;
86b3203f 226
b2f60e03 227 r << p.GetChar(0) << p.GetChar(1);
86b3203f 228
5be0cf65 229 // skip trailing ../.., if any
b2f60e03 230 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
86b3203f 231
5be0cf65
VS
232 // remove back references: translate dir1/../dir2 to dir2
233 for (; i < cnt; i++)
234 {
b2f60e03
BJ
235 r << p.GetChar(i);
236 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
5be0cf65 237 {
b2f60e03
BJ
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(':'))
5be0cf65 240 {
b2f60e03 241 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
5be0cf65
VS
242 r.Remove(j + 1);
243 }
244 }
245 }
86b3203f 246
b2f60e03 247 for (; i < cnt; i++) r << p.GetChar(i);
86b3203f 248
5be0cf65
VS
249 return r;
250}
251
5526e819
VS
252
253void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
254{
255 int i, pathpos = -1;
5526e819 256
5be0cf65 257 m_Path = MakeCorrectPath(location);
d30e0edd 258
aaa66113
VS
259 if (is_dir)
260 {
261 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
262 m_Path << wxT('/');
263 }
86b3203f 264
aaa66113 265 else
d30e0edd 266 {
269e8200 267 for (i = m_Path.Length()-1; i >= 0; i--)
aaa66113 268 {
223d09f6 269 if (m_Path[(unsigned int) i] == wxT('/'))
aaa66113 270 {
223d09f6 271 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
aaa66113 272 {
5526e819
VS
273 i -= 2;
274 continue;
275 }
269e8200 276 else
aaa66113 277 {
269e8200 278 pathpos = i;
5526e819
VS
279 break;
280 }
281 }
aaa66113
VS
282 else if (m_Path[(unsigned int) i] == wxT(':')) {
283 pathpos = i;
284 break;
285 }
5526e819 286 }
269e8200 287 if (pathpos == -1)
aaa66113 288 {
269e8200 289 for (i = 0; i < (int) m_Path.Length(); i++)
aaa66113 290 {
223d09f6 291 if (m_Path[(unsigned int) i] == wxT(':'))
aaa66113 292 {
5526e819
VS
293 m_Path.Remove(i+1);
294 break;
295 }
296 }
269e8200 297 if (i == (int) m_Path.Length())
aaa66113 298 m_Path = wxEmptyString;
5526e819 299 }
269e8200 300 else
aaa66113 301 {
5526e819
VS
302 m_Path.Remove(pathpos+1);
303 }
304 }
305}
306
307
308
309wxFSFile* wxFileSystem::OpenFile(const wxString& location)
310{
5be0cf65
VS
311 wxString loc = MakeCorrectPath(location);
312 unsigned i, ln;
5526e819
VS
313 char meta;
314 wxFSFile *s = NULL;
315 wxNode *node;
316
317 ln = loc.Length();
318 meta = 0;
269e8200 319 for (i = 0; i < ln; i++)
d30e0edd 320 {
2148cce2
VS
321 switch (loc[i])
322 {
86b3203f 323 case wxT('/') : case wxT(':') : case wxT('#') :
2148cce2
VS
324 meta = loc[i];
325 break;
326 }
327 if (meta != 0) break;
5526e819
VS
328 }
329 m_LastName = wxEmptyString;
330
331 // try relative paths first :
223d09f6 332 if (meta != wxT(':'))
d30e0edd 333 {
5526e819 334 node = m_Handlers.GetFirst();
d30e0edd 335 while (node)
aaa66113 336 {
5526e819 337 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 338 if (h->CanOpen(m_Path + loc))
aaa66113 339 {
7dee4b2b
VS
340 s = h->OpenFile(*this, m_Path + loc);
341 if (s) { m_LastName = m_Path + loc; break; }
5526e819 342 }
d30e0edd 343 node = node->GetNext();
5526e819
VS
344 }
345 }
346
347 // if failed, try absolute paths :
269e8200 348 if (s == NULL)
d30e0edd 349 {
5526e819 350 node = m_Handlers.GetFirst();
d30e0edd 351 while (node)
aaa66113 352 {
d30e0edd 353 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 354 if (h->CanOpen(loc))
aaa66113 355 {
7dee4b2b
VS
356 s = h->OpenFile(*this, loc);
357 if (s) { m_LastName = loc; break; }
5526e819 358 }
d30e0edd 359 node = node->GetNext();
5526e819
VS
360 }
361 }
362 return (s);
363}
364
365
aaa66113
VS
366
367wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
368{
369 wxNode *node;
370 wxString spec2(spec);
86b3203f 371
aaa66113
VS
372 m_FindFileHandler = NULL;
373
374 for (int i = spec2.Length()-1; i >= 0; i--)
f6081a04 375 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
aaa66113
VS
376
377 node = m_Handlers.GetFirst();
378 while (node)
379 {
380 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
86b3203f 381 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
aaa66113
VS
382 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
383 node = node->GetNext();
86b3203f 384 }
aaa66113
VS
385
386 node = m_Handlers.GetFirst();
387 while (node)
388 {
389 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
390 if (m_FindFileHandler -> CanOpen(spec2))
391 return m_FindFileHandler -> FindFirst(spec2, flags);
392 node = node->GetNext();
86b3203f
DW
393 }
394
395 return wxEmptyString;
aaa66113
VS
396}
397
398
399
400wxString wxFileSystem::FindNext()
401{
402 if (m_FindFileHandler == NULL) return wxEmptyString;
403 else return m_FindFileHandler -> FindNext();
404}
405
406
407
5526e819
VS
408void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
409{
410 m_Handlers.Append(handler);
411}
412
413
269e8200
RD
414void wxFileSystem::CleanUpHandlers()
415{
416 m_Handlers.DeleteContents(TRUE);
417 m_Handlers.Clear();
418}
419
5526e819 420
aaa66113
VS
421
422
5526e819
VS
423///// Module:
424
425class wxFileSystemModule : public wxModule
426{
427 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
428
429 public:
430 virtual bool OnInit()
431 {
432 wxFileSystem::AddHandler(new wxLocalFSHandler);
86b3203f 433
659a6064 434 #if wxUSE_MIMETYPE
dbb88122 435 gs_FSMimeFallbacks = new wxFileTypeInfo[6];
86b3203f 436 gs_FSMimeFallbacks[0] =
dbb88122
VS
437 wxFileTypeInfo("image/jpeg",
438 "",
439 "",
440 "JPEG image (from fallback)",
441 "jpg", "jpeg", NULL);
86b3203f 442 gs_FSMimeFallbacks[1] =
dbb88122
VS
443 wxFileTypeInfo("image/gif",
444 "",
445 "",
446 "GIF image (from fallback)",
447 "gif", NULL);
86b3203f 448 gs_FSMimeFallbacks[2] =
dbb88122
VS
449 wxFileTypeInfo("image/png",
450 "",
451 "",
452 "PNG image (from fallback)",
453 "png", NULL);
86b3203f 454 gs_FSMimeFallbacks[3] =
dbb88122
VS
455 wxFileTypeInfo("image/bmp",
456 "",
457 "",
458 "windows bitmap image (from fallback)",
459 "bmp", NULL);
86b3203f 460 gs_FSMimeFallbacks[4] =
dbb88122
VS
461 wxFileTypeInfo("text/html",
462 "",
463 "",
464 "HTML document (from fallback)",
465 "htm", "html", NULL);
86b3203f 466 gs_FSMimeFallbacks[5] =
dbb88122
VS
467 // must terminate the table with this!
468 wxFileTypeInfo();
659a6064 469 #endif
5526e819
VS
470 return TRUE;
471 }
269e8200 472 virtual void OnExit()
aaa66113 473 {
659a6064 474 #if wxUSE_MIMETYPE
86b3203f 475 delete [] gs_FSMimeFallbacks;
659a6064 476 #endif
269e8200 477 wxFileSystem::CleanUpHandlers();
aaa66113 478 }
5526e819
VS
479};
480
481IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
482
269e8200 483#endif
24528b0c 484 // wxUSE_FILESYSTEM
5526e819 485
a76015e6
VS
486
487