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