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