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