]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
_really_ fixed translation messages
[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 }
444
445 path.Replace(wxT("%25"), wxT("%"));
446 path.Replace(wxT("%3A"), wxT(":"));
2b5f62a0 447
8ad944dc 448#ifdef __WXMSW__
2b5f62a0
VZ
449 // file urls either start with a forward slash (local harddisk),
450 // otherwise they have a servername/sharename notation,
451 // which only exists on msw and corresponds to a unc
452 if ( path[0u] == wxT('/') && path [1u] != wxT('/'))
453 {
9548c49a 454 path = path.Mid(1);
2b5f62a0 455 }
2b5f62a0
VZ
456 else if ( (url.Find(wxT("file://")) == 0) &&
457 (path.Find(wxT('/')) != wxNOT_FOUND) &&
458 (path.Length() > 1) && (path[1u] != wxT(':')) )
459 {
9548c49a 460 path = wxT("//") + path;
2b5f62a0
VZ
461 }
462#endif
9b798c66 463
9548c49a 464 path.Replace(g_unixPathString, g_nativePathString);
2b5f62a0 465
9548c49a 466 return wxFileName(path, wxPATH_NATIVE);
2b5f62a0
VZ
467}
468
469// Returns the file URL for a native path
9548c49a 470wxString wxFileSystem::FileNameToURL(const wxFileName& filename)
2b5f62a0 471{
9548c49a
VS
472 wxFileName fn = filename;
473 fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE);
474 wxString url = fn.GetFullPath(wxPATH_NATIVE);
9b798c66 475
2a5d3f57
JS
476#ifndef __UNIX__
477 // unc notation, wxMSW
9548c49a
VS
478 if ( url.Find(wxT("\\\\")) == 0 )
479 {
480 url = url.Mid(2);
481 }
482 else
483 {
484 url = wxT("/") + url;
485 }
2b5f62a0 486#endif
9b798c66 487
9548c49a 488 url.Replace(g_nativePathString, g_unixPathString);
008a56c9
VS
489 url.Replace(wxT("%"), wxT("%25"));
490 url.Replace(wxT(":"), wxT("%3A"));
491 url = wxT("file:") + url;
9548c49a 492 return url;
2b5f62a0 493}
aaa66113
VS
494
495
5526e819
VS
496///// Module:
497
498class wxFileSystemModule : public wxModule
499{
500 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
501
502 public:
503 virtual bool OnInit()
504 {
505 wxFileSystem::AddHandler(new wxLocalFSHandler);
86b3203f 506
659a6064 507 #if wxUSE_MIMETYPE
dbb88122 508 gs_FSMimeFallbacks = new wxFileTypeInfo[6];
86b3203f 509 gs_FSMimeFallbacks[0] =
57e67135
VZ
510 wxFileTypeInfo(_T("image/jpeg"),
511 _T(""),
512 _T(""),
513 _T("JPEG image (from fallback)"),
514 _T("jpg"), _T("jpeg"), NULL);
86b3203f 515 gs_FSMimeFallbacks[1] =
57e67135
VZ
516 wxFileTypeInfo(_T("image/gif"),
517 _T(""),
518 _T(""),
519 _T("GIF image (from fallback)"),
520 _T("gif"), NULL);
86b3203f 521 gs_FSMimeFallbacks[2] =
57e67135
VZ
522 wxFileTypeInfo(_T("image/png"),
523 _T(""),
524 _T(""),
525 _T("PNG image (from fallback)"),
526 _T("png"), NULL);
86b3203f 527 gs_FSMimeFallbacks[3] =
57e67135
VZ
528 wxFileTypeInfo(_T("image/bmp"),
529 _T(""),
530 _T(""),
531 _T("windows bitmap image (from fallback)"),
532 _T("bmp"), NULL);
86b3203f 533 gs_FSMimeFallbacks[4] =
57e67135
VZ
534 wxFileTypeInfo(_T("text/html"),
535 _T(""),
536 _T(""),
537 _T("HTML document (from fallback)"),
538 _T("htm"), _T("html"), NULL);
86b3203f 539 gs_FSMimeFallbacks[5] =
dbb88122
VS
540 // must terminate the table with this!
541 wxFileTypeInfo();
659a6064 542 #endif
5526e819
VS
543 return TRUE;
544 }
269e8200 545 virtual void OnExit()
d81152f4 546 {
659a6064 547 #if wxUSE_MIMETYPE
86b3203f 548 delete [] gs_FSMimeFallbacks;
659a6064 549 #endif
269e8200 550 wxFileSystem::CleanUpHandlers();
d81152f4 551 }
5526e819
VS
552};
553
554IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
555
269e8200 556#endif
24528b0c 557 // wxUSE_FILESYSTEM
5526e819 558
a76015e6
VS
559
560