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