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