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