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