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