]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
Changes to the XRC library:
[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
10#ifdef __GNUG__
11#pragma implementation
12#endif
13
d30e0edd 14#include "wx/wxprec.h"
5526e819
VS
15
16#ifdef __BORDLANDC__
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"
73725567
VS
28
29
5526e819
VS
30
31//--------------------------------------------------------------------------------
32// wxFileSystemHandler
33//--------------------------------------------------------------------------------
34
35IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
36
5526e819 37
659a6064 38#if wxUSE_MIMETYPE
dbb88122 39static wxFileTypeInfo *gs_FSMimeFallbacks = NULL;
659a6064 40#endif
dbb88122 41
5526e819
VS
42wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location)
43{
659a6064 44#if wxUSE_MIMETYPE
5526e819
VS
45 wxString ext = wxEmptyString, mime = wxEmptyString;
46 wxString loc = GetRightLocation(location);
47 char c;
48 int l = loc.Length(), l2;
49 wxFileType *ft;
50
51 l2 = l;
52 for (int i = l-1; i >= 0; i--) {
ea4f5235 53 c = loc[(unsigned int) i];
223d09f6
KB
54 if (c == wxT('#')) l2 = i + 1;
55 if (c == wxT('.')) {ext = loc.Right(l2-i-1); break;}
56 if ((c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) {return wxEmptyString;}
5526e819 57 }
956418ab 58
73725567
VS
59 static bool s_MinimalMimeEnsured = FALSE;
60 if (!s_MinimalMimeEnsured) {
dbb88122
VS
61 wxTheMimeTypesManager -> AddFallbacks(gs_FSMimeFallbacks);
62 s_MinimalMimeEnsured = TRUE;
956418ab
VS
63 }
64
73725567 65 ft = wxTheMimeTypesManager -> GetFileTypeFromExtension(ext);
f6bcfd97
BP
66 if ( !ft || !ft -> GetMimeType(&mime) ) {
67 mime = wxEmptyString;
51f79d5c 68 }
f6bcfd97
BP
69
70 delete ft;
71
72 return mime;
659a6064
VS
73#else
74 return wxEmptyString;
75#endif
5526e819
VS
76}
77
78
79
80wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
81{
82 wxString s = wxEmptyString;
83 int i, l = location.Length();
84 bool fnd;
85
86 fnd = FALSE;
223d09f6
KB
87 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
88 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 89 }
223d09f6
KB
90 if (!fnd) return wxT("file");
91 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
5526e819
VS
92 return s;
93}
94
95
5526e819
VS
96wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
97{
98 int i;
99 bool fnd;
100
101 fnd = FALSE;
102 for (i = location.Length()-1; i >= 0; i--) {
223d09f6
KB
103 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
104 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
5526e819
VS
105 }
106 return wxEmptyString;
107}
108
5526e819
VS
109wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
110{
111 int i, l = location.Length();
112 int l2 = l + 1;
223d09f6 113 for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;}
5526e819
VS
114 if (i == 0) return wxEmptyString;
115 else return location.Mid(i + 1, l2 - i - 2);
116}
117
5526e819
VS
118wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
119{
120 char c;
121 int l = location.Length();
122
123 for (int i = l-1; i >= 0; i--) {
124 c = location[i];
223d09f6
KB
125 if (c == wxT('#')) return location.Right(l-i-1);
126 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
5526e819
VS
127 }
128 return wxEmptyString;
129}
130
aaa66113 131
f76dbc4d
VZ
132wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
133 int WXUNUSED(flags))
134{
135 return wxEmptyString;
136}
aaa66113 137
f76dbc4d
VZ
138wxString wxFileSystemHandler::FindNext()
139{
140 return wxEmptyString;
141}
aaa66113 142
5526e819
VS
143//--------------------------------------------------------------------------------
144// wxLocalFSHandler
145//--------------------------------------------------------------------------------
146
5526e819 147
19008b7b 148wxString wxLocalFSHandler::ms_root;
5526e819 149
5526e819
VS
150bool wxLocalFSHandler::CanOpen(const wxString& location)
151{
223d09f6 152 return GetProtocol(location) == wxT("file");
5526e819
VS
153}
154
5526e819
VS
155wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
156{
6464f4cb 157 // location has Unix path separators
19008b7b
VS
158 wxString right = ms_root + GetRightLocation(location);
159 wxFileName fn(right, wxPATH_UNIX);
6464f4cb 160
19008b7b 161 if (!wxFileExists(fn.GetFullPath()))
f6bcfd97 162 return (wxFSFile*) NULL;
6464f4cb 163
19008b7b 164 return new wxFSFile(new wxFileInputStream(fn.GetFullPath()),
f6bcfd97
BP
165 right,
166 GetMimeTypeFromExt(location),
167 GetAnchor(location),
19008b7b 168 wxDateTime(wxFileModificationTime(fn.GetFullPath())));
f6bcfd97 169
5526e819
VS
170}
171
aaa66113
VS
172wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
173{
19008b7b 174 wxString right = ms_root + GetRightLocation(spec);
aaa66113
VS
175 return wxFindFirstFile(right, flags);
176}
177
178wxString wxLocalFSHandler::FindNext()
179{
180 return wxFindNextFile();
181}
182
183
184
5526e819
VS
185//-----------------------------------------------------------------------------
186// wxFileSystem
187//-----------------------------------------------------------------------------
188
189IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
190
191
192wxList wxFileSystem::m_Handlers;
193
194
5be0cf65
VS
195static wxString MakeCorrectPath(const wxString& path)
196{
197 wxString p(path);
198 wxString r;
199 int i, j, cnt;
86b3203f 200
5be0cf65
VS
201 cnt = p.Length();
202 for (i = 0; i < cnt; i++)
f6081a04 203 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
86b3203f 204
5be0cf65 205 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
86b3203f 206
5be0cf65 207 if (cnt < 3) return p;
86b3203f 208
b2f60e03 209 r << p.GetChar(0) << p.GetChar(1);
86b3203f 210
5be0cf65 211 // skip trailing ../.., if any
b2f60e03 212 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
86b3203f 213
5be0cf65
VS
214 // remove back references: translate dir1/../dir2 to dir2
215 for (; i < cnt; i++)
216 {
b2f60e03
BJ
217 r << p.GetChar(i);
218 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
5be0cf65 219 {
b2f60e03
BJ
220 for (j = r.Length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
221 if (j >= 0 && r.GetChar(j) != wxT(':'))
5be0cf65 222 {
b2f60e03 223 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
5be0cf65
VS
224 r.Remove(j + 1);
225 }
226 }
227 }
86b3203f 228
b2f60e03 229 for (; i < cnt; i++) r << p.GetChar(i);
86b3203f 230
5be0cf65
VS
231 return r;
232}
233
5526e819
VS
234
235void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
236{
237 int i, pathpos = -1;
5526e819 238
5be0cf65 239 m_Path = MakeCorrectPath(location);
d30e0edd 240
aaa66113
VS
241 if (is_dir)
242 {
243 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
244 m_Path << wxT('/');
245 }
86b3203f 246
aaa66113 247 else
d30e0edd 248 {
269e8200 249 for (i = m_Path.Length()-1; i >= 0; i--)
aaa66113 250 {
223d09f6 251 if (m_Path[(unsigned int) i] == wxT('/'))
aaa66113 252 {
223d09f6 253 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
aaa66113 254 {
5526e819
VS
255 i -= 2;
256 continue;
257 }
269e8200 258 else
aaa66113 259 {
269e8200 260 pathpos = i;
5526e819
VS
261 break;
262 }
263 }
aaa66113
VS
264 else if (m_Path[(unsigned int) i] == wxT(':')) {
265 pathpos = i;
266 break;
267 }
5526e819 268 }
269e8200 269 if (pathpos == -1)
aaa66113 270 {
269e8200 271 for (i = 0; i < (int) m_Path.Length(); i++)
aaa66113 272 {
223d09f6 273 if (m_Path[(unsigned int) i] == wxT(':'))
aaa66113 274 {
5526e819
VS
275 m_Path.Remove(i+1);
276 break;
277 }
278 }
269e8200 279 if (i == (int) m_Path.Length())
aaa66113 280 m_Path = wxEmptyString;
5526e819 281 }
269e8200 282 else
aaa66113 283 {
5526e819
VS
284 m_Path.Remove(pathpos+1);
285 }
286 }
287}
288
289
290
291wxFSFile* wxFileSystem::OpenFile(const wxString& location)
292{
5be0cf65
VS
293 wxString loc = MakeCorrectPath(location);
294 unsigned i, ln;
5526e819
VS
295 char meta;
296 wxFSFile *s = NULL;
297 wxNode *node;
298
299 ln = loc.Length();
300 meta = 0;
269e8200 301 for (i = 0; i < ln; i++)
d30e0edd 302 {
2148cce2
VS
303 switch (loc[i])
304 {
86b3203f 305 case wxT('/') : case wxT(':') : case wxT('#') :
2148cce2
VS
306 meta = loc[i];
307 break;
308 }
309 if (meta != 0) break;
5526e819
VS
310 }
311 m_LastName = wxEmptyString;
312
313 // try relative paths first :
223d09f6 314 if (meta != wxT(':'))
d30e0edd 315 {
5526e819 316 node = m_Handlers.GetFirst();
d30e0edd 317 while (node)
aaa66113 318 {
5526e819 319 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 320 if (h->CanOpen(m_Path + loc))
aaa66113 321 {
7dee4b2b
VS
322 s = h->OpenFile(*this, m_Path + loc);
323 if (s) { m_LastName = m_Path + loc; break; }
5526e819 324 }
d30e0edd 325 node = node->GetNext();
5526e819
VS
326 }
327 }
328
329 // if failed, try absolute paths :
269e8200 330 if (s == NULL)
d30e0edd 331 {
5526e819 332 node = m_Handlers.GetFirst();
d30e0edd 333 while (node)
aaa66113 334 {
d30e0edd 335 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 336 if (h->CanOpen(loc))
aaa66113 337 {
7dee4b2b
VS
338 s = h->OpenFile(*this, loc);
339 if (s) { m_LastName = loc; break; }
5526e819 340 }
d30e0edd 341 node = node->GetNext();
5526e819
VS
342 }
343 }
344 return (s);
345}
346
347
aaa66113
VS
348
349wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
350{
351 wxNode *node;
352 wxString spec2(spec);
86b3203f 353
aaa66113
VS
354 m_FindFileHandler = NULL;
355
356 for (int i = spec2.Length()-1; i >= 0; i--)
f6081a04 357 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
aaa66113
VS
358
359 node = m_Handlers.GetFirst();
360 while (node)
361 {
362 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
86b3203f 363 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
aaa66113
VS
364 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
365 node = node->GetNext();
86b3203f 366 }
aaa66113
VS
367
368 node = m_Handlers.GetFirst();
369 while (node)
370 {
371 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
372 if (m_FindFileHandler -> CanOpen(spec2))
373 return m_FindFileHandler -> FindFirst(spec2, flags);
374 node = node->GetNext();
86b3203f
DW
375 }
376
377 return wxEmptyString;
aaa66113
VS
378}
379
380
381
382wxString wxFileSystem::FindNext()
383{
384 if (m_FindFileHandler == NULL) return wxEmptyString;
385 else return m_FindFileHandler -> FindNext();
386}
387
388
389
5526e819
VS
390void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
391{
392 m_Handlers.Append(handler);
393}
394
395
269e8200
RD
396void wxFileSystem::CleanUpHandlers()
397{
398 m_Handlers.DeleteContents(TRUE);
399 m_Handlers.Clear();
400}
401
5526e819 402
aaa66113
VS
403
404
5526e819
VS
405///// Module:
406
407class wxFileSystemModule : public wxModule
408{
409 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
410
411 public:
412 virtual bool OnInit()
413 {
414 wxFileSystem::AddHandler(new wxLocalFSHandler);
86b3203f 415
659a6064 416 #if wxUSE_MIMETYPE
dbb88122 417 gs_FSMimeFallbacks = new wxFileTypeInfo[6];
86b3203f 418 gs_FSMimeFallbacks[0] =
dbb88122
VS
419 wxFileTypeInfo("image/jpeg",
420 "",
421 "",
422 "JPEG image (from fallback)",
423 "jpg", "jpeg", NULL);
86b3203f 424 gs_FSMimeFallbacks[1] =
dbb88122
VS
425 wxFileTypeInfo("image/gif",
426 "",
427 "",
428 "GIF image (from fallback)",
429 "gif", NULL);
86b3203f 430 gs_FSMimeFallbacks[2] =
dbb88122
VS
431 wxFileTypeInfo("image/png",
432 "",
433 "",
434 "PNG image (from fallback)",
435 "png", NULL);
86b3203f 436 gs_FSMimeFallbacks[3] =
dbb88122
VS
437 wxFileTypeInfo("image/bmp",
438 "",
439 "",
440 "windows bitmap image (from fallback)",
441 "bmp", NULL);
86b3203f 442 gs_FSMimeFallbacks[4] =
dbb88122
VS
443 wxFileTypeInfo("text/html",
444 "",
445 "",
446 "HTML document (from fallback)",
447 "htm", "html", NULL);
86b3203f 448 gs_FSMimeFallbacks[5] =
dbb88122
VS
449 // must terminate the table with this!
450 wxFileTypeInfo();
659a6064 451 #endif
5526e819
VS
452 return TRUE;
453 }
269e8200 454 virtual void OnExit()
aaa66113 455 {
659a6064 456 #if wxUSE_MIMETYPE
86b3203f 457 delete [] gs_FSMimeFallbacks;
659a6064 458 #endif
269e8200 459 wxFileSystem::CleanUpHandlers();
aaa66113 460 }
5526e819
VS
461};
462
463IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
464
269e8200 465#endif
24528b0c 466 // wxUSE_FILESYSTEM
5526e819 467
a76015e6
VS
468
469