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