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