]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
Committing 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);
51f79d5c
VS
100 if (ft && (ft -> GetMimeType(&mime))) {
101 delete ft;
102 return mime;
103 }
104 else {
105 delete ft;
106 return wxEmptyString;
107 }
5526e819
VS
108}
109
110
111
112wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
113{
114 wxString s = wxEmptyString;
115 int i, l = location.Length();
116 bool fnd;
117
118 fnd = FALSE;
223d09f6
KB
119 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
120 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 121 }
223d09f6
KB
122 if (!fnd) return wxT("file");
123 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
5526e819
VS
124 return s;
125}
126
127
5526e819
VS
128wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
129{
130 int i;
131 bool fnd;
132
133 fnd = FALSE;
134 for (i = location.Length()-1; i >= 0; i--) {
223d09f6
KB
135 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
136 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
5526e819
VS
137 }
138 return wxEmptyString;
139}
140
5526e819
VS
141wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
142{
143 int i, l = location.Length();
144 int l2 = l + 1;
223d09f6 145 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
146 if (i == 0) return wxEmptyString;
147 else return location.Mid(i + 1, l2 - i - 2);
148}
149
5526e819
VS
150wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
151{
152 char c;
153 int l = location.Length();
154
155 for (int i = l-1; i >= 0; i--) {
156 c = location[i];
223d09f6
KB
157 if (c == wxT('#')) return location.Right(l-i-1);
158 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
5526e819
VS
159 }
160 return wxEmptyString;
161}
162
aaa66113
VS
163
164wxString wxFileSystemHandler::FindFirst(const wxString& spec, int flags) { return wxEmptyString; }
165
166wxString wxFileSystemHandler::FindNext() { return wxEmptyString; }
167
168
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),
196 GetAnchor(location));
d30e0edd 197 else return (wxFSFile*) NULL;
5526e819
VS
198}
199
aaa66113
VS
200wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
201{
202 wxString right = GetRightLocation(spec);
203 return wxFindFirstFile(right, flags);
204}
205
206wxString wxLocalFSHandler::FindNext()
207{
208 return wxFindNextFile();
209}
210
211
212
5526e819
VS
213//-----------------------------------------------------------------------------
214// wxFileSystem
215//-----------------------------------------------------------------------------
216
217IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
218
219
220wxList wxFileSystem::m_Handlers;
221
222
223
224void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
225{
226 int i, pathpos = -1;
227 m_Path = location;
228
229 for (i = m_Path.Length()-1; i >= 0; i--)
223d09f6 230 if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
d30e0edd 231
aaa66113
VS
232 if (is_dir)
233 {
234 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
235 m_Path << wxT('/');
236 }
237
238 else
d30e0edd 239 {
269e8200 240 for (i = m_Path.Length()-1; i >= 0; i--)
aaa66113 241 {
223d09f6 242 if (m_Path[(unsigned int) i] == wxT('/'))
aaa66113 243 {
223d09f6 244 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
aaa66113 245 {
5526e819
VS
246 i -= 2;
247 continue;
248 }
269e8200 249 else
aaa66113 250 {
269e8200 251 pathpos = i;
5526e819
VS
252 break;
253 }
254 }
aaa66113
VS
255 else if (m_Path[(unsigned int) i] == wxT(':')) {
256 pathpos = i;
257 break;
258 }
5526e819 259 }
269e8200 260 if (pathpos == -1)
aaa66113 261 {
269e8200 262 for (i = 0; i < (int) m_Path.Length(); i++)
aaa66113 263 {
223d09f6 264 if (m_Path[(unsigned int) i] == wxT(':'))
aaa66113 265 {
5526e819
VS
266 m_Path.Remove(i+1);
267 break;
268 }
269 }
269e8200 270 if (i == (int) m_Path.Length())
aaa66113 271 m_Path = wxEmptyString;
5526e819 272 }
269e8200 273 else
aaa66113 274 {
5526e819
VS
275 m_Path.Remove(pathpos+1);
276 }
277 }
278}
279
280
281
282wxFSFile* wxFileSystem::OpenFile(const wxString& location)
283{
284 wxString loc = location;
285 int i, ln;
286 char meta;
287 wxFSFile *s = NULL;
288 wxNode *node;
289
290 ln = loc.Length();
291 meta = 0;
269e8200 292 for (i = 0; i < ln; i++)
d30e0edd 293 {
223d09f6 294 if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
aaa66113
VS
295 if (!meta)
296 switch (loc[(unsigned int) i])
297 {
298 case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i];
299 }
5526e819
VS
300 }
301 m_LastName = wxEmptyString;
302
303 // try relative paths first :
223d09f6 304 if (meta != wxT(':'))
d30e0edd 305 {
5526e819 306 node = m_Handlers.GetFirst();
d30e0edd 307 while (node)
aaa66113 308 {
5526e819 309 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
269e8200 310 if (h->CanOpen(m_Path + location))
aaa66113 311 {
d30e0edd
RR
312 s = h->OpenFile(*this, m_Path + location);
313 if (s) { m_LastName = m_Path + location; break; }
5526e819 314 }
d30e0edd 315 node = node->GetNext();
5526e819
VS
316 }
317 }
318
319 // if failed, try absolute paths :
269e8200 320 if (s == NULL)
d30e0edd 321 {
5526e819 322 node = m_Handlers.GetFirst();
d30e0edd 323 while (node)
aaa66113 324 {
d30e0edd 325 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
269e8200 326 if (h->CanOpen(location))
aaa66113 327 {
d30e0edd
RR
328 s = h->OpenFile(*this, location);
329 if (s) { m_LastName = location; break; }
5526e819 330 }
d30e0edd 331 node = node->GetNext();
5526e819
VS
332 }
333 }
334 return (s);
335}
336
337
aaa66113
VS
338
339wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
340{
341 wxNode *node;
342 wxString spec2(spec);
343
344 m_FindFileHandler = NULL;
345
346 for (int i = spec2.Length()-1; i >= 0; i--)
347 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
348
349 node = m_Handlers.GetFirst();
350 while (node)
351 {
352 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
353 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
354 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
355 node = node->GetNext();
356 }
357
358 node = m_Handlers.GetFirst();
359 while (node)
360 {
361 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
362 if (m_FindFileHandler -> CanOpen(spec2))
363 return m_FindFileHandler -> FindFirst(spec2, flags);
364 node = node->GetNext();
365 }
366
367 return wxEmptyString;
368}
369
370
371
372wxString wxFileSystem::FindNext()
373{
374 if (m_FindFileHandler == NULL) return wxEmptyString;
375 else return m_FindFileHandler -> FindNext();
376}
377
378
379
5526e819
VS
380void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
381{
382 m_Handlers.Append(handler);
383}
384
385
269e8200
RD
386void wxFileSystem::CleanUpHandlers()
387{
388 m_Handlers.DeleteContents(TRUE);
389 m_Handlers.Clear();
390}
391
5526e819 392
aaa66113
VS
393
394
5526e819
VS
395///// Module:
396
397class wxFileSystemModule : public wxModule
398{
399 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
400
401 public:
402 virtual bool OnInit()
403 {
404 wxFileSystem::AddHandler(new wxLocalFSHandler);
405 return TRUE;
406 }
269e8200 407 virtual void OnExit()
aaa66113
VS
408 {
409 wxFileSystemHandler::CleanUpStatics();
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