]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
Applied patch [ 638561 ] Allow SetFont(wxNullFont) in wxGTK
[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"
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 175 wxString right = ms_root + GetRightLocation(location);
9548c49a 176 wxFileName fn = wxFileSystem::URLToFileName(right);
46837272 177
19008b7b 178 if (!wxFileExists(fn.GetFullPath()))
f6bcfd97 179 return (wxFSFile*) NULL;
46837272 180
d81152f4 181 return new wxFSFile(new wxFFileInputStream(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);
9548c49a
VS
192 return wxFindFirstFile(wxFileSystem::URLToFileName(right).GetFullPath(),
193 flags);
aaa66113
VS
194}
195
196wxString wxLocalFSHandler::FindNext()
197{
198 return wxFindNextFile();
199}
200
201
202
5526e819
VS
203//-----------------------------------------------------------------------------
204// wxFileSystem
205//-----------------------------------------------------------------------------
206
207IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
46837272 208IMPLEMENT_ABSTRACT_CLASS(wxFSFile, wxObject)
5526e819
VS
209
210
211wxList wxFileSystem::m_Handlers;
212
213
5be0cf65
VS
214static wxString MakeCorrectPath(const wxString& path)
215{
216 wxString p(path);
217 wxString r;
218 int i, j, cnt;
86b3203f 219
5be0cf65
VS
220 cnt = p.Length();
221 for (i = 0; i < cnt; i++)
f6081a04 222 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
86b3203f 223
5be0cf65 224 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
86b3203f 225
5be0cf65 226 if (cnt < 3) return p;
86b3203f 227
b2f60e03 228 r << p.GetChar(0) << p.GetChar(1);
86b3203f 229
5be0cf65 230 // skip trailing ../.., if any
b2f60e03 231 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
86b3203f 232
5be0cf65
VS
233 // remove back references: translate dir1/../dir2 to dir2
234 for (; i < cnt; i++)
235 {
b2f60e03
BJ
236 r << p.GetChar(i);
237 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
5be0cf65 238 {
b2f60e03
BJ
239 for (j = r.Length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
240 if (j >= 0 && r.GetChar(j) != wxT(':'))
5be0cf65 241 {
b2f60e03 242 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
5be0cf65
VS
243 r.Remove(j + 1);
244 }
245 }
246 }
86b3203f 247
b2f60e03 248 for (; i < cnt; i++) r << p.GetChar(i);
86b3203f 249
5be0cf65
VS
250 return r;
251}
252
5526e819
VS
253
254void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
255{
256 int i, pathpos = -1;
5526e819 257
5be0cf65 258 m_Path = MakeCorrectPath(location);
d30e0edd 259
aaa66113
VS
260 if (is_dir)
261 {
262 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
d81152f4 263 m_Path << wxT('/');
aaa66113 264 }
86b3203f 265
aaa66113 266 else
d30e0edd 267 {
269e8200 268 for (i = m_Path.Length()-1; i >= 0; i--)
d81152f4 269 {
223d09f6 270 if (m_Path[(unsigned int) i] == wxT('/'))
d81152f4 271 {
223d09f6 272 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
d81152f4 273 {
5526e819
VS
274 i -= 2;
275 continue;
276 }
269e8200 277 else
d81152f4 278 {
269e8200 279 pathpos = i;
5526e819
VS
280 break;
281 }
282 }
aaa66113
VS
283 else if (m_Path[(unsigned int) i] == wxT(':')) {
284 pathpos = i;
285 break;
286 }
5526e819 287 }
269e8200 288 if (pathpos == -1)
d81152f4 289 {
269e8200 290 for (i = 0; i < (int) m_Path.Length(); i++)
d81152f4 291 {
223d09f6 292 if (m_Path[(unsigned int) i] == wxT(':'))
d81152f4 293 {
5526e819
VS
294 m_Path.Remove(i+1);
295 break;
296 }
297 }
269e8200 298 if (i == (int) m_Path.Length())
d81152f4 299 m_Path = wxEmptyString;
5526e819 300 }
269e8200 301 else
d81152f4 302 {
5526e819
VS
303 m_Path.Remove(pathpos+1);
304 }
305 }
306}
307
308
309
310wxFSFile* wxFileSystem::OpenFile(const wxString& location)
311{
5be0cf65
VS
312 wxString loc = MakeCorrectPath(location);
313 unsigned i, ln;
5526e819
VS
314 char meta;
315 wxFSFile *s = NULL;
316 wxNode *node;
317
318 ln = loc.Length();
319 meta = 0;
269e8200 320 for (i = 0; i < ln; i++)
d30e0edd 321 {
2148cce2
VS
322 switch (loc[i])
323 {
86b3203f 324 case wxT('/') : case wxT(':') : case wxT('#') :
2148cce2
VS
325 meta = loc[i];
326 break;
327 }
328 if (meta != 0) break;
5526e819
VS
329 }
330 m_LastName = wxEmptyString;
331
332 // try relative paths first :
223d09f6 333 if (meta != wxT(':'))
d30e0edd 334 {
5526e819 335 node = m_Handlers.GetFirst();
d30e0edd 336 while (node)
d81152f4 337 {
5526e819 338 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 339 if (h->CanOpen(m_Path + loc))
d81152f4 340 {
7dee4b2b
VS
341 s = h->OpenFile(*this, m_Path + loc);
342 if (s) { m_LastName = m_Path + loc; break; }
5526e819 343 }
d30e0edd 344 node = node->GetNext();
5526e819
VS
345 }
346 }
347
348 // if failed, try absolute paths :
269e8200 349 if (s == NULL)
d30e0edd 350 {
5526e819 351 node = m_Handlers.GetFirst();
d30e0edd 352 while (node)
d81152f4 353 {
d30e0edd 354 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 355 if (h->CanOpen(loc))
d81152f4 356 {
7dee4b2b
VS
357 s = h->OpenFile(*this, loc);
358 if (s) { m_LastName = loc; break; }
5526e819 359 }
d30e0edd 360 node = node->GetNext();
5526e819
VS
361 }
362 }
363 return (s);
364}
365
366
aaa66113
VS
367
368wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
369{
370 wxNode *node;
371 wxString spec2(spec);
86b3203f 372
aaa66113
VS
373 m_FindFileHandler = NULL;
374
375 for (int i = spec2.Length()-1; i >= 0; i--)
f6081a04 376 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
aaa66113
VS
377
378 node = m_Handlers.GetFirst();
379 while (node)
380 {
381 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
86b3203f 382 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
aaa66113
VS
383 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
384 node = node->GetNext();
86b3203f 385 }
aaa66113
VS
386
387 node = m_Handlers.GetFirst();
388 while (node)
389 {
390 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
391 if (m_FindFileHandler -> CanOpen(spec2))
392 return m_FindFileHandler -> FindFirst(spec2, flags);
393 node = node->GetNext();
86b3203f
DW
394 }
395
396 return wxEmptyString;
aaa66113
VS
397}
398
399
400
401wxString wxFileSystem::FindNext()
402{
403 if (m_FindFileHandler == NULL) return wxEmptyString;
404 else return m_FindFileHandler -> FindNext();
405}
406
407
408
5526e819
VS
409void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
410{
411 m_Handlers.Append(handler);
412}
413
414
269e8200
RD
415void wxFileSystem::CleanUpHandlers()
416{
417 m_Handlers.DeleteContents(TRUE);
418 m_Handlers.Clear();
419}
420
2b5f62a0
VZ
421const static wxString g_unixPathString(wxT("/"));
422const static wxString g_nativePathString(wxFILE_SEP_PATH);
5526e819 423
2b5f62a0 424// Returns the native path for a file URL
9548c49a 425wxFileName wxFileSystem::URLToFileName(const wxString& url)
2b5f62a0 426{
9548c49a 427 wxString path = url;
2b5f62a0
VZ
428
429 if ( path.Find(wxT("file://")) == 0 )
430 {
9548c49a 431 path = path.Mid(7);
2b5f62a0
VZ
432 }
433
7912e633 434#ifndef __UNIX__
2b5f62a0
VZ
435 // file urls either start with a forward slash (local harddisk),
436 // otherwise they have a servername/sharename notation,
437 // which only exists on msw and corresponds to a unc
438 if ( path[0u] == wxT('/') && path [1u] != wxT('/'))
439 {
9548c49a 440 path = path.Mid(1);
2b5f62a0 441 }
2b5f62a0
VZ
442 else if ( (url.Find(wxT("file://")) == 0) &&
443 (path.Find(wxT('/')) != wxNOT_FOUND) &&
444 (path.Length() > 1) && (path[1u] != wxT(':')) )
445 {
9548c49a 446 path = wxT("//") + path;
2b5f62a0
VZ
447 }
448#endif
9b798c66 449
9548c49a 450 path.Replace(g_unixPathString, g_nativePathString);
2b5f62a0 451
9548c49a 452 return wxFileName(path, wxPATH_NATIVE);
2b5f62a0
VZ
453}
454
455// Returns the file URL for a native path
9548c49a 456wxString wxFileSystem::FileNameToURL(const wxFileName& filename)
2b5f62a0 457{
9548c49a
VS
458 wxFileName fn = filename;
459 fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE);
460 wxString url = fn.GetFullPath(wxPATH_NATIVE);
9b798c66 461
2b5f62a0 462#ifdef __WXMSW__
9548c49a
VS
463 // unc notation
464 if ( url.Find(wxT("\\\\")) == 0 )
465 {
466 url = url.Mid(2);
467 }
468 else
469 {
470 url = wxT("/") + url;
471 }
2b5f62a0 472#endif
9b798c66 473
9548c49a
VS
474 url.Replace(g_nativePathString, g_unixPathString);
475 url = wxT("file://") + url;
476 return url;
2b5f62a0 477}
aaa66113
VS
478
479
5526e819
VS
480///// Module:
481
482class wxFileSystemModule : public wxModule
483{
484 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
485
486 public:
487 virtual bool OnInit()
488 {
489 wxFileSystem::AddHandler(new wxLocalFSHandler);
86b3203f 490
659a6064 491 #if wxUSE_MIMETYPE
dbb88122 492 gs_FSMimeFallbacks = new wxFileTypeInfo[6];
86b3203f 493 gs_FSMimeFallbacks[0] =
57e67135
VZ
494 wxFileTypeInfo(_T("image/jpeg"),
495 _T(""),
496 _T(""),
497 _T("JPEG image (from fallback)"),
498 _T("jpg"), _T("jpeg"), NULL);
86b3203f 499 gs_FSMimeFallbacks[1] =
57e67135
VZ
500 wxFileTypeInfo(_T("image/gif"),
501 _T(""),
502 _T(""),
503 _T("GIF image (from fallback)"),
504 _T("gif"), NULL);
86b3203f 505 gs_FSMimeFallbacks[2] =
57e67135
VZ
506 wxFileTypeInfo(_T("image/png"),
507 _T(""),
508 _T(""),
509 _T("PNG image (from fallback)"),
510 _T("png"), NULL);
86b3203f 511 gs_FSMimeFallbacks[3] =
57e67135
VZ
512 wxFileTypeInfo(_T("image/bmp"),
513 _T(""),
514 _T(""),
515 _T("windows bitmap image (from fallback)"),
516 _T("bmp"), NULL);
86b3203f 517 gs_FSMimeFallbacks[4] =
57e67135
VZ
518 wxFileTypeInfo(_T("text/html"),
519 _T(""),
520 _T(""),
521 _T("HTML document (from fallback)"),
522 _T("htm"), _T("html"), NULL);
86b3203f 523 gs_FSMimeFallbacks[5] =
dbb88122
VS
524 // must terminate the table with this!
525 wxFileTypeInfo();
659a6064 526 #endif
5526e819
VS
527 return TRUE;
528 }
269e8200 529 virtual void OnExit()
d81152f4 530 {
659a6064 531 #if wxUSE_MIMETYPE
86b3203f 532 delete [] gs_FSMimeFallbacks;
659a6064 533 #endif
269e8200 534 wxFileSystem::CleanUpHandlers();
d81152f4 535 }
5526e819
VS
536};
537
538IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
539
269e8200 540#endif
24528b0c 541 // wxUSE_FILESYSTEM
5526e819 542
a76015e6
VS
543
544