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