]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
fixing mngw32 (cross)compilation
[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
VS
26#include "wx/mimetype.h"
27
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
141class wxLocalFSHandler : public wxFileSystemHandler
142{
143 public:
144 virtual bool CanOpen(const wxString& location);
145 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
aaa66113
VS
146 virtual wxString FindFirst(const wxString& spec, int flags = 0);
147 virtual wxString FindNext();
5526e819
VS
148};
149
150
5526e819
VS
151bool wxLocalFSHandler::CanOpen(const wxString& location)
152{
223d09f6 153 return GetProtocol(location) == wxT("file");
5526e819
VS
154}
155
5526e819
VS
156wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
157{
158 wxString right = GetRightLocation(location);
f6bcfd97
BP
159 if (!wxFileExists(right))
160 return (wxFSFile*) NULL;
161
162 return new wxFSFile(new wxFileInputStream(right),
163 right,
164 GetMimeTypeFromExt(location),
165 GetAnchor(location),
166 wxDateTime(wxFileModificationTime(right)));
167
5526e819
VS
168}
169
aaa66113
VS
170wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
171{
172 wxString right = GetRightLocation(spec);
173 return wxFindFirstFile(right, flags);
174}
175
176wxString wxLocalFSHandler::FindNext()
177{
178 return wxFindNextFile();
179}
180
181
182
5526e819
VS
183//-----------------------------------------------------------------------------
184// wxFileSystem
185//-----------------------------------------------------------------------------
186
187IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
188
189
190wxList wxFileSystem::m_Handlers;
191
192
5be0cf65
VS
193static wxString MakeCorrectPath(const wxString& path)
194{
195 wxString p(path);
196 wxString r;
197 int i, j, cnt;
86b3203f 198
5be0cf65
VS
199 cnt = p.Length();
200 for (i = 0; i < cnt; i++)
f6081a04 201 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
86b3203f 202
5be0cf65 203 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
86b3203f 204
5be0cf65 205 if (cnt < 3) return p;
86b3203f 206
b2f60e03 207 r << p.GetChar(0) << p.GetChar(1);
86b3203f 208
5be0cf65 209 // skip trailing ../.., if any
b2f60e03 210 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
86b3203f 211
5be0cf65
VS
212 // remove back references: translate dir1/../dir2 to dir2
213 for (; i < cnt; i++)
214 {
b2f60e03
BJ
215 r << p.GetChar(i);
216 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
5be0cf65 217 {
b2f60e03
BJ
218 for (j = r.Length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
219 if (j >= 0 && r.GetChar(j) != wxT(':'))
5be0cf65 220 {
b2f60e03 221 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
5be0cf65
VS
222 r.Remove(j + 1);
223 }
224 }
225 }
86b3203f 226
b2f60e03 227 for (; i < cnt; i++) r << p.GetChar(i);
86b3203f 228
5be0cf65
VS
229 return r;
230}
231
5526e819
VS
232
233void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
234{
235 int i, pathpos = -1;
5526e819 236
5be0cf65 237 m_Path = MakeCorrectPath(location);
d30e0edd 238
aaa66113
VS
239 if (is_dir)
240 {
241 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
242 m_Path << wxT('/');
243 }
86b3203f 244
aaa66113 245 else
d30e0edd 246 {
269e8200 247 for (i = m_Path.Length()-1; i >= 0; i--)
aaa66113 248 {
223d09f6 249 if (m_Path[(unsigned int) i] == wxT('/'))
aaa66113 250 {
223d09f6 251 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
aaa66113 252 {
5526e819
VS
253 i -= 2;
254 continue;
255 }
269e8200 256 else
aaa66113 257 {
269e8200 258 pathpos = i;
5526e819
VS
259 break;
260 }
261 }
aaa66113
VS
262 else if (m_Path[(unsigned int) i] == wxT(':')) {
263 pathpos = i;
264 break;
265 }
5526e819 266 }
269e8200 267 if (pathpos == -1)
aaa66113 268 {
269e8200 269 for (i = 0; i < (int) m_Path.Length(); i++)
aaa66113 270 {
223d09f6 271 if (m_Path[(unsigned int) i] == wxT(':'))
aaa66113 272 {
5526e819
VS
273 m_Path.Remove(i+1);
274 break;
275 }
276 }
269e8200 277 if (i == (int) m_Path.Length())
aaa66113 278 m_Path = wxEmptyString;
5526e819 279 }
269e8200 280 else
aaa66113 281 {
5526e819
VS
282 m_Path.Remove(pathpos+1);
283 }
284 }
285}
286
287
288
289wxFSFile* wxFileSystem::OpenFile(const wxString& location)
290{
5be0cf65
VS
291 wxString loc = MakeCorrectPath(location);
292 unsigned i, ln;
5526e819
VS
293 char meta;
294 wxFSFile *s = NULL;
295 wxNode *node;
296
297 ln = loc.Length();
298 meta = 0;
269e8200 299 for (i = 0; i < ln; i++)
d30e0edd 300 {
2148cce2
VS
301 switch (loc[i])
302 {
86b3203f 303 case wxT('/') : case wxT(':') : case wxT('#') :
2148cce2
VS
304 meta = loc[i];
305 break;
306 }
307 if (meta != 0) break;
5526e819
VS
308 }
309 m_LastName = wxEmptyString;
310
311 // try relative paths first :
223d09f6 312 if (meta != wxT(':'))
d30e0edd 313 {
5526e819 314 node = m_Handlers.GetFirst();
d30e0edd 315 while (node)
aaa66113 316 {
5526e819 317 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 318 if (h->CanOpen(m_Path + loc))
aaa66113 319 {
7dee4b2b
VS
320 s = h->OpenFile(*this, m_Path + loc);
321 if (s) { m_LastName = m_Path + loc; break; }
5526e819 322 }
d30e0edd 323 node = node->GetNext();
5526e819
VS
324 }
325 }
326
327 // if failed, try absolute paths :
269e8200 328 if (s == NULL)
d30e0edd 329 {
5526e819 330 node = m_Handlers.GetFirst();
d30e0edd 331 while (node)
aaa66113 332 {
d30e0edd 333 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 334 if (h->CanOpen(loc))
aaa66113 335 {
7dee4b2b
VS
336 s = h->OpenFile(*this, loc);
337 if (s) { m_LastName = loc; break; }
5526e819 338 }
d30e0edd 339 node = node->GetNext();
5526e819
VS
340 }
341 }
342 return (s);
343}
344
345
aaa66113
VS
346
347wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
348{
349 wxNode *node;
350 wxString spec2(spec);
86b3203f 351
aaa66113
VS
352 m_FindFileHandler = NULL;
353
354 for (int i = spec2.Length()-1; i >= 0; i--)
f6081a04 355 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
aaa66113
VS
356
357 node = m_Handlers.GetFirst();
358 while (node)
359 {
360 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
86b3203f 361 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
aaa66113
VS
362 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
363 node = node->GetNext();
86b3203f 364 }
aaa66113
VS
365
366 node = m_Handlers.GetFirst();
367 while (node)
368 {
369 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
370 if (m_FindFileHandler -> CanOpen(spec2))
371 return m_FindFileHandler -> FindFirst(spec2, flags);
372 node = node->GetNext();
86b3203f
DW
373 }
374
375 return wxEmptyString;
aaa66113
VS
376}
377
378
379
380wxString wxFileSystem::FindNext()
381{
382 if (m_FindFileHandler == NULL) return wxEmptyString;
383 else return m_FindFileHandler -> FindNext();
384}
385
386
387
5526e819
VS
388void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
389{
390 m_Handlers.Append(handler);
391}
392
393
269e8200
RD
394void wxFileSystem::CleanUpHandlers()
395{
396 m_Handlers.DeleteContents(TRUE);
397 m_Handlers.Clear();
398}
399
5526e819 400
aaa66113
VS
401
402
5526e819
VS
403///// Module:
404
405class wxFileSystemModule : public wxModule
406{
407 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
408
409 public:
410 virtual bool OnInit()
411 {
412 wxFileSystem::AddHandler(new wxLocalFSHandler);
86b3203f 413
dbb88122 414 gs_FSMimeFallbacks = new wxFileTypeInfo[6];
86b3203f 415 gs_FSMimeFallbacks[0] =
dbb88122
VS
416 wxFileTypeInfo("image/jpeg",
417 "",
418 "",
419 "JPEG image (from fallback)",
420 "jpg", "jpeg", NULL);
86b3203f 421 gs_FSMimeFallbacks[1] =
dbb88122
VS
422 wxFileTypeInfo("image/gif",
423 "",
424 "",
425 "GIF image (from fallback)",
426 "gif", NULL);
86b3203f 427 gs_FSMimeFallbacks[2] =
dbb88122
VS
428 wxFileTypeInfo("image/png",
429 "",
430 "",
431 "PNG image (from fallback)",
432 "png", NULL);
86b3203f 433 gs_FSMimeFallbacks[3] =
dbb88122
VS
434 wxFileTypeInfo("image/bmp",
435 "",
436 "",
437 "windows bitmap image (from fallback)",
438 "bmp", NULL);
86b3203f 439 gs_FSMimeFallbacks[4] =
dbb88122
VS
440 wxFileTypeInfo("text/html",
441 "",
442 "",
443 "HTML document (from fallback)",
444 "htm", "html", NULL);
86b3203f 445 gs_FSMimeFallbacks[5] =
dbb88122
VS
446 // must terminate the table with this!
447 wxFileTypeInfo();
86b3203f 448
5526e819
VS
449 return TRUE;
450 }
269e8200 451 virtual void OnExit()
aaa66113 452 {
86b3203f 453 delete [] gs_FSMimeFallbacks;
269e8200 454 wxFileSystem::CleanUpHandlers();
aaa66113 455 }
5526e819
VS
456};
457
458IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
459
269e8200 460#endif
24528b0c 461 // wxUSE_FILESYSTEM
5526e819 462
a76015e6
VS
463
464