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