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