]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
use C++ wrappers around DirectFB API for easier use
[wxWidgets.git] / src / common / filesys.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/common/filesys.cpp
5526e819
VS
3// Purpose: wxFileSystem class - interface for opening files
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
5be0cf65 6// CVS-ID: $Id$
65571936 7// Licence: wxWindows licence
5526e819
VS
8/////////////////////////////////////////////////////////////////////////////
9
d30e0edd 10#include "wx/wxprec.h"
5526e819 11
2b5f62a0 12#ifdef __BORLANDC__
e4db172a 13 #pragma hdrstop
5526e819
VS
14#endif
15
31528cd3 16
24528b0c 17#if wxUSE_FILESYSTEM
5526e819 18
e4db172a
WS
19#include "wx/filesys.h"
20
21#ifndef WX_PRECOMP
22 #include "wx/log.h"
02761f6c 23 #include "wx/module.h"
e4db172a
WS
24#endif
25
d30e0edd 26#include "wx/wfstream.h"
73725567 27#include "wx/mimetype.h"
6464f4cb 28#include "wx/filename.h"
3ab6fcee 29#include "wx/tokenzr.h"
73725567
VS
30
31
5526e819
VS
32//--------------------------------------------------------------------------------
33// wxFileSystemHandler
34//--------------------------------------------------------------------------------
35
36IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
37
5526e819
VS
38
39wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location)
40{
45681cd7 41 wxString ext, mime;
5526e819 42 wxString loc = GetRightLocation(location);
53b99810 43 wxChar c;
e4db172a 44 int l = loc.length(), l2;
5526e819
VS
45
46 l2 = l;
46837272 47 for (int i = l-1; i >= 0; i--)
45681cd7 48 {
ea4f5235 49 c = loc[(unsigned int) i];
45681cd7
VS
50 if ( c == wxT('#') )
51 l2 = i + 1;
52 if ( c == wxT('.') )
53 {
46837272 54 ext = loc.Right(l2-i-1);
45681cd7
VS
55 break;
56 }
57 if ( (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':')) )
58 return wxEmptyString;
5526e819 59 }
956418ab 60
45681cd7 61#if wxUSE_MIMETYPE
121680bf
VS
62 static bool s_MinimalMimeEnsured = false;
63 if (!s_MinimalMimeEnsured)
64 {
65 static const wxFileTypeInfo fallbacks[] =
66 {
67 wxFileTypeInfo(_T("image/jpeg"),
b494c48b
WS
68 wxEmptyString,
69 wxEmptyString,
121680bf
VS
70 _T("JPEG image (from fallback)"),
71 _T("jpg"), _T("jpeg"), _T("JPG"), _T("JPEG"), NULL),
72 wxFileTypeInfo(_T("image/gif"),
b494c48b
WS
73 wxEmptyString,
74 wxEmptyString,
121680bf
VS
75 _T("GIF image (from fallback)"),
76 _T("gif"), _T("GIF"), NULL),
77 wxFileTypeInfo(_T("image/png"),
b494c48b
WS
78 wxEmptyString,
79 wxEmptyString,
121680bf
VS
80 _T("PNG image (from fallback)"),
81 _T("png"), _T("PNG"), NULL),
82 wxFileTypeInfo(_T("image/bmp"),
b494c48b
WS
83 wxEmptyString,
84 wxEmptyString,
121680bf
VS
85 _T("windows bitmap image (from fallback)"),
86 _T("bmp"), _T("BMP"), NULL),
87 wxFileTypeInfo(_T("text/html"),
b494c48b
WS
88 wxEmptyString,
89 wxEmptyString,
121680bf
VS
90 _T("HTML document (from fallback)"),
91 _T("htm"), _T("html"), _T("HTM"), _T("HTML"), NULL),
92 // must terminate the table with this!
93 wxFileTypeInfo()
94 };
95 wxTheMimeTypesManager->AddFallbacks(fallbacks);
96 s_MinimalMimeEnsured = true;
956418ab
VS
97 }
98
45681cd7
VS
99 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
100 if ( !ft || !ft -> GetMimeType(&mime) )
101 {
f6bcfd97 102 mime = wxEmptyString;
51f79d5c 103 }
f6bcfd97
BP
104
105 delete ft;
106
107 return mime;
659a6064 108#else
a62848fd 109 if ( ext.IsSameAs(wxT("htm"), false) || ext.IsSameAs(_T("html"), false) )
45681cd7 110 return wxT("text/html");
a62848fd 111 if ( ext.IsSameAs(wxT("jpg"), false) || ext.IsSameAs(_T("jpeg"), false) )
45681cd7 112 return wxT("image/jpeg");
a62848fd 113 if ( ext.IsSameAs(wxT("gif"), false) )
45681cd7 114 return wxT("image/gif");
a62848fd 115 if ( ext.IsSameAs(wxT("png"), false) )
45681cd7 116 return wxT("image/png");
a62848fd 117 if ( ext.IsSameAs(wxT("bmp"), false) )
45681cd7 118 return wxT("image/bmp");
659a6064
VS
119 return wxEmptyString;
120#endif
5526e819
VS
121}
122
123
124
125wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
126{
127 wxString s = wxEmptyString;
e4db172a 128 int i, l = location.length();
a62848fd 129 bool fnd = false;
5526e819 130
223d09f6 131 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
a62848fd 132 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = true;
5526e819 133 }
223d09f6
KB
134 if (!fnd) return wxT("file");
135 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
5526e819
VS
136 return s;
137}
138
139
5526e819
VS
140wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
141{
142 int i;
a62848fd 143 bool fnd = false;
5526e819 144
e4db172a 145 for (i = location.length()-1; i >= 0; i--) {
a62848fd 146 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = true;
223d09f6 147 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
5526e819
VS
148 }
149 return wxEmptyString;
150}
151
5526e819
VS
152wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
153{
e4db172a 154 int i, l = location.length();
5526e819 155 int l2 = l + 1;
008a56c9 156
a62848fd
WS
157 for (i = l-1;
158 (i >= 0) &&
f8ae31dc 159 ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':')));
008a56c9 160 i--)
c8c29c49
JS
161 {
162 if (location[i] == wxT('#')) l2 = i + 1;
163 }
5526e819
VS
164 if (i == 0) return wxEmptyString;
165 else return location.Mid(i + 1, l2 - i - 2);
166}
167
5526e819
VS
168wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
169{
53b99810 170 wxChar c;
e4db172a 171 int l = location.length();
5526e819
VS
172
173 for (int i = l-1; i >= 0; i--) {
174 c = location[i];
223d09f6
KB
175 if (c == wxT('#')) return location.Right(l-i-1);
176 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
5526e819
VS
177 }
178 return wxEmptyString;
179}
180
aaa66113 181
f76dbc4d
VZ
182wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
183 int WXUNUSED(flags))
184{
185 return wxEmptyString;
186}
aaa66113 187
f76dbc4d
VZ
188wxString wxFileSystemHandler::FindNext()
189{
190 return wxEmptyString;
191}
aaa66113 192
5526e819
VS
193//--------------------------------------------------------------------------------
194// wxLocalFSHandler
195//--------------------------------------------------------------------------------
196
5526e819 197
19008b7b 198wxString wxLocalFSHandler::ms_root;
5526e819 199
5526e819
VS
200bool wxLocalFSHandler::CanOpen(const wxString& location)
201{
223d09f6 202 return GetProtocol(location) == wxT("file");
5526e819
VS
203}
204
5526e819
VS
205wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
206{
6464f4cb 207 // location has Unix path separators
008a56c9 208 wxString right = GetRightLocation(location);
9548c49a 209 wxFileName fn = wxFileSystem::URLToFileName(right);
008a56c9 210 wxString fullpath = ms_root + fn.GetFullPath();
46837272 211
008a56c9 212 if (!wxFileExists(fullpath))
f6bcfd97 213 return (wxFSFile*) NULL;
46837272 214
3e5175b7
VZ
215 // we need to check whether we can really read from this file, otherwise
216 // wxFSFile is not going to work
217 wxFFileInputStream *is = new wxFFileInputStream(fullpath);
218 if ( !is->Ok() )
219 {
220 delete is;
221 return (wxFSFile*) NULL;
222 }
223
224 return new wxFSFile(is,
f6bcfd97
BP
225 right,
226 GetMimeTypeFromExt(location),
e2b87f38
VZ
227 GetAnchor(location)
228#if wxUSE_DATETIME
229 ,wxDateTime(wxFileModificationTime(fullpath))
230#endif // wxUSE_DATETIME
231 );
5526e819
VS
232}
233
aaa66113
VS
234wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
235{
008a56c9
VS
236 wxFileName fn = wxFileSystem::URLToFileName(GetRightLocation(spec));
237 return wxFindFirstFile(ms_root + fn.GetFullPath(), flags);
aaa66113
VS
238}
239
240wxString wxLocalFSHandler::FindNext()
241{
242 return wxFindNextFile();
243}
244
245
246
5526e819
VS
247//-----------------------------------------------------------------------------
248// wxFileSystem
249//-----------------------------------------------------------------------------
250
251IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
46837272 252IMPLEMENT_ABSTRACT_CLASS(wxFSFile, wxObject)
5526e819
VS
253
254
255wxList wxFileSystem::m_Handlers;
256
257
5be0cf65
VS
258static wxString MakeCorrectPath(const wxString& path)
259{
260 wxString p(path);
261 wxString r;
262 int i, j, cnt;
86b3203f 263
e4db172a 264 cnt = p.length();
5be0cf65 265 for (i = 0; i < cnt; i++)
f6081a04 266 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
86b3203f 267
5be0cf65 268 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
86b3203f 269
5be0cf65 270 if (cnt < 3) return p;
86b3203f 271
b2f60e03 272 r << p.GetChar(0) << p.GetChar(1);
86b3203f 273
5be0cf65 274 // skip trailing ../.., if any
b2f60e03 275 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
86b3203f 276
5be0cf65
VS
277 // remove back references: translate dir1/../dir2 to dir2
278 for (; i < cnt; i++)
279 {
b2f60e03
BJ
280 r << p.GetChar(i);
281 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
5be0cf65 282 {
e4db172a 283 for (j = r.length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
b2f60e03 284 if (j >= 0 && r.GetChar(j) != wxT(':'))
5be0cf65 285 {
b2f60e03 286 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
5be0cf65
VS
287 r.Remove(j + 1);
288 }
289 }
290 }
86b3203f 291
b2f60e03 292 for (; i < cnt; i++) r << p.GetChar(i);
86b3203f 293
5be0cf65
VS
294 return r;
295}
296
5526e819
VS
297
298void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
299{
300 int i, pathpos = -1;
5526e819 301
5be0cf65 302 m_Path = MakeCorrectPath(location);
d30e0edd 303
aaa66113
VS
304 if (is_dir)
305 {
e4db172a 306 if (m_Path.length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
d81152f4 307 m_Path << wxT('/');
aaa66113 308 }
86b3203f 309
aaa66113 310 else
d30e0edd 311 {
e4db172a 312 for (i = m_Path.length()-1; i >= 0; i--)
d81152f4 313 {
223d09f6 314 if (m_Path[(unsigned int) i] == wxT('/'))
d81152f4 315 {
223d09f6 316 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
d81152f4 317 {
5526e819
VS
318 i -= 2;
319 continue;
320 }
269e8200 321 else
d81152f4 322 {
269e8200 323 pathpos = i;
5526e819
VS
324 break;
325 }
326 }
aaa66113
VS
327 else if (m_Path[(unsigned int) i] == wxT(':')) {
328 pathpos = i;
329 break;
330 }
5526e819 331 }
269e8200 332 if (pathpos == -1)
d81152f4 333 {
e4db172a 334 for (i = 0; i < (int) m_Path.length(); i++)
d81152f4 335 {
223d09f6 336 if (m_Path[(unsigned int) i] == wxT(':'))
d81152f4 337 {
5526e819
VS
338 m_Path.Remove(i+1);
339 break;
340 }
341 }
e4db172a 342 if (i == (int) m_Path.length())
d81152f4 343 m_Path = wxEmptyString;
5526e819 344 }
269e8200 345 else
d81152f4 346 {
5526e819
VS
347 m_Path.Remove(pathpos+1);
348 }
349 }
350}
351
352
353
354wxFSFile* wxFileSystem::OpenFile(const wxString& location)
355{
5be0cf65
VS
356 wxString loc = MakeCorrectPath(location);
357 unsigned i, ln;
53b99810 358 wxChar meta;
5526e819 359 wxFSFile *s = NULL;
df5168c4 360 wxList::compatibility_iterator node;
5526e819 361
e4db172a 362 ln = loc.length();
5526e819 363 meta = 0;
269e8200 364 for (i = 0; i < ln; i++)
d30e0edd 365 {
2148cce2
VS
366 switch (loc[i])
367 {
86b3203f 368 case wxT('/') : case wxT(':') : case wxT('#') :
2148cce2
VS
369 meta = loc[i];
370 break;
371 }
372 if (meta != 0) break;
5526e819
VS
373 }
374 m_LastName = wxEmptyString;
375
376 // try relative paths first :
223d09f6 377 if (meta != wxT(':'))
d30e0edd 378 {
5526e819 379 node = m_Handlers.GetFirst();
d30e0edd 380 while (node)
d81152f4 381 {
5526e819 382 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 383 if (h->CanOpen(m_Path + loc))
d81152f4 384 {
7dee4b2b
VS
385 s = h->OpenFile(*this, m_Path + loc);
386 if (s) { m_LastName = m_Path + loc; break; }
5526e819 387 }
d30e0edd 388 node = node->GetNext();
5526e819
VS
389 }
390 }
391
392 // if failed, try absolute paths :
269e8200 393 if (s == NULL)
d30e0edd 394 {
5526e819 395 node = m_Handlers.GetFirst();
d30e0edd 396 while (node)
d81152f4 397 {
d30e0edd 398 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 399 if (h->CanOpen(loc))
d81152f4 400 {
7dee4b2b
VS
401 s = h->OpenFile(*this, loc);
402 if (s) { m_LastName = loc; break; }
5526e819 403 }
d30e0edd 404 node = node->GetNext();
5526e819
VS
405 }
406 }
407 return (s);
408}
409
410
aaa66113
VS
411
412wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
413{
df5168c4 414 wxList::compatibility_iterator node;
aaa66113 415 wxString spec2(spec);
86b3203f 416
aaa66113
VS
417 m_FindFileHandler = NULL;
418
e4db172a 419 for (int i = spec2.length()-1; i >= 0; i--)
f6081a04 420 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
aaa66113
VS
421
422 node = m_Handlers.GetFirst();
423 while (node)
424 {
425 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
86b3203f 426 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
aaa66113
VS
427 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
428 node = node->GetNext();
86b3203f 429 }
aaa66113
VS
430
431 node = m_Handlers.GetFirst();
432 while (node)
433 {
434 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
435 if (m_FindFileHandler -> CanOpen(spec2))
436 return m_FindFileHandler -> FindFirst(spec2, flags);
437 node = node->GetNext();
86b3203f
DW
438 }
439
440 return wxEmptyString;
aaa66113
VS
441}
442
443
444
445wxString wxFileSystem::FindNext()
446{
447 if (m_FindFileHandler == NULL) return wxEmptyString;
448 else return m_FindFileHandler -> FindNext();
449}
450
3ab6fcee
VZ
451bool wxFileSystem::FindFileInPath(wxString *pStr,
452 const wxChar *path,
453 const wxChar *basename)
454{
455 // we assume that it's not empty
456 wxCHECK_MSG( !wxIsEmpty(basename), false,
457 _T("empty file name in wxFileSystem::FindFileInPath"));
458
459 // skip path separator in the beginning of the file name if present
460 if ( wxIsPathSeparator(*basename) )
461 basename++;
aaa66113 462
3ab6fcee
VZ
463 wxStringTokenizer tokenizer(path, wxPATH_SEP);
464 while ( tokenizer.HasMoreTokens() )
465 {
466 wxString strFile = tokenizer.GetNextToken();
467 if ( !wxEndsWithPathSeparator(strFile) )
468 strFile += wxFILE_SEP_PATH;
469 strFile += basename;
470
471 wxFSFile *file = OpenFile(strFile);
472 if ( file )
473 {
474 delete file;
475 *pStr = strFile;
476 return true;
477 }
478 }
479
480 return false;
481}
aaa66113 482
5526e819
VS
483void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
484{
4948ebf3
VZ
485 // prepend the handler to the beginning of the list because handlers added
486 // last should have the highest priority to allow overriding them
374b4f1c 487 m_Handlers.Insert((size_t)0, handler);
5526e819
VS
488}
489
b8b37ced
VZ
490bool wxFileSystem::HasHandlerForPath(const wxString &location)
491{
492 for ( wxList::compatibility_iterator node = m_Handlers.GetFirst();
493 node; node = node->GetNext() )
494 {
495 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
496 if (h->CanOpen(location))
497 return true;
498 }
499
500 return false;
501}
5526e819 502
269e8200
RD
503void wxFileSystem::CleanUpHandlers()
504{
df5168c4 505 WX_CLEAR_LIST(wxList, m_Handlers);
269e8200
RD
506}
507
60431236
WS
508static const wxString g_unixPathString(wxT("/"));
509static const wxString g_nativePathString(wxFILE_SEP_PATH);
5526e819 510
2b5f62a0 511// Returns the native path for a file URL
9548c49a 512wxFileName wxFileSystem::URLToFileName(const wxString& url)
2b5f62a0 513{
a62848fd 514 wxString path = url;
2b5f62a0 515
a62848fd
WS
516 if ( path.Find(wxT("file://")) == 0 )
517 {
518 path = path.Mid(7);
519 }
008a56c9 520 else if ( path.Find(wxT("file:")) == 0 )
a62848fd
WS
521 {
522 path = path.Mid(5);
523 }
524 // Remove preceding double slash on Mac Classic
d0200d9e
JS
525#if defined(__WXMAC__) && !defined(__UNIX__)
526 else if ( path.Find(wxT("//")) == 0 )
527 path = path.Mid(2);
528#endif
a62848fd 529
008a56c9
VS
530 path.Replace(wxT("%25"), wxT("%"));
531 path.Replace(wxT("%3A"), wxT(":"));
2b5f62a0 532
8ad944dc 533#ifdef __WXMSW__
a62848fd 534 // file urls either start with a forward slash (local harddisk),
2b5f62a0
VZ
535 // otherwise they have a servername/sharename notation,
536 // which only exists on msw and corresponds to a unc
a62848fd
WS
537 if ( path[0u] == wxT('/') && path [1u] != wxT('/'))
538 {
539 path = path.Mid(1);
540 }
541 else if ( (url.Find(wxT("file://")) == 0) &&
2b5f62a0 542 (path.Find(wxT('/')) != wxNOT_FOUND) &&
e4db172a 543 (path.length() > 1) && (path[1u] != wxT(':')) )
a62848fd
WS
544 {
545 path = wxT("//") + path;
546 }
2b5f62a0 547#endif
9b798c66 548
a62848fd 549 path.Replace(g_unixPathString, g_nativePathString);
2b5f62a0 550
a62848fd 551 return wxFileName(path, wxPATH_NATIVE);
2b5f62a0
VZ
552}
553
554// Returns the file URL for a native path
9548c49a 555wxString wxFileSystem::FileNameToURL(const wxFileName& filename)
2b5f62a0 556{
9548c49a
VS
557 wxFileName fn = filename;
558 fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE);
559 wxString url = fn.GetFullPath(wxPATH_NATIVE);
9b798c66 560
2a5d3f57
JS
561#ifndef __UNIX__
562 // unc notation, wxMSW
a62848fd 563 if ( url.Find(wxT("\\\\")) == 0 )
9548c49a 564 {
60c315ca 565 url = wxT("//") + url.Mid(2);
9548c49a
VS
566 }
567 else
568 {
569 url = wxT("/") + url;
d0200d9e
JS
570#ifdef __WXMAC__
571 url = wxT("/") + url;
572#endif
573
9548c49a 574 }
2b5f62a0 575#endif
9b798c66 576
9548c49a 577 url.Replace(g_nativePathString, g_unixPathString);
008a56c9
VS
578 url.Replace(wxT("%"), wxT("%25"));
579 url.Replace(wxT(":"), wxT("%3A"));
580 url = wxT("file:") + url;
9548c49a 581 return url;
2b5f62a0 582}
aaa66113
VS
583
584
5526e819
VS
585///// Module:
586
587class wxFileSystemModule : public wxModule
588{
589 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
590
591 public:
592 virtual bool OnInit()
593 {
594 wxFileSystem::AddHandler(new wxLocalFSHandler);
121680bf 595 return true;
5526e819 596 }
269e8200 597 virtual void OnExit()
d81152f4 598 {
269e8200 599 wxFileSystem::CleanUpHandlers();
d81152f4 600 }
5526e819
VS
601};
602
603IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
604
269e8200 605#endif
24528b0c 606 // wxUSE_FILESYSTEM