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