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