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