]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/filesys.cpp
Committing in .
[wxWidgets.git] / src / common / filesys.cpp
... / ...
CommitLineData
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
13#include "wx/wxprec.h"
14
15#ifdef __BORDLANDC__
16#pragma hdrstop
17#endif
18
19#if !wxUSE_SOCKETS
20 #undef wxUSE_FS_INET
21 #define wxUSE_FS_INET 0
22#endif
23
24#if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
25
26#include "wx/wfstream.h"
27#include "wx/module.h"
28#include "wx/filesys.h"
29
30//--------------------------------------------------------------------------------
31// wxFileSystemHandler
32//--------------------------------------------------------------------------------
33
34IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
35
36wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL;
37
38void wxFileSystemHandler::CleanUpStatics()
39{
40 if (m_MimeMng) delete m_MimeMng;
41 m_MimeMng = NULL;
42}
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--) {
55 c = loc[(unsigned int) i];
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;}
59 }
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
99 ft = m_MimeMng -> GetFileTypeFromExtension(ext);
100 if (ft && (ft -> GetMimeType(&mime))) {
101 delete ft;
102 return mime;
103 }
104 else {
105 delete ft;
106 return wxEmptyString;
107 }
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;
119 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
120 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
121 }
122 if (!fnd) return wxT("file");
123 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
124 return s;
125}
126
127
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--) {
135 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
136 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
137 }
138 return wxEmptyString;
139}
140
141wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
142{
143 int i, l = location.Length();
144 int l2 = l + 1;
145 for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;}
146 if (i == 0) return wxEmptyString;
147 else return location.Mid(i + 1, l2 - i - 2);
148}
149
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];
157 if (c == wxT('#')) return location.Right(l-i-1);
158 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
159 }
160 return wxEmptyString;
161}
162
163
164wxString wxFileSystemHandler::FindFirst(const wxString& spec, int flags) { return wxEmptyString; }
165
166wxString wxFileSystemHandler::FindNext() { return wxEmptyString; }
167
168
169
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);
179 virtual wxString FindFirst(const wxString& spec, int flags = 0);
180 virtual wxString FindNext();
181};
182
183
184bool wxLocalFSHandler::CanOpen(const wxString& location)
185{
186 return GetProtocol(location) == wxT("file");
187}
188
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));
197 else return (wxFSFile*) NULL;
198}
199
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
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--)
230 if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
231
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
239 {
240 for (i = m_Path.Length()-1; i >= 0; i--)
241 {
242 if (m_Path[(unsigned int) i] == wxT('/'))
243 {
244 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
245 {
246 i -= 2;
247 continue;
248 }
249 else
250 {
251 pathpos = i;
252 break;
253 }
254 }
255 else if (m_Path[(unsigned int) i] == wxT(':')) {
256 pathpos = i;
257 break;
258 }
259 }
260 if (pathpos == -1)
261 {
262 for (i = 0; i < (int) m_Path.Length(); i++)
263 {
264 if (m_Path[(unsigned int) i] == wxT(':'))
265 {
266 m_Path.Remove(i+1);
267 break;
268 }
269 }
270 if (i == (int) m_Path.Length())
271 m_Path = wxEmptyString;
272 }
273 else
274 {
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;
292 for (i = 0; i < ln; i++)
293 {
294 if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
295 if (!meta)
296 switch (loc[(unsigned int) i])
297 {
298 case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i];
299 }
300 }
301 m_LastName = wxEmptyString;
302
303 // try relative paths first :
304 if (meta != wxT(':'))
305 {
306 node = m_Handlers.GetFirst();
307 while (node)
308 {
309 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
310 if (h->CanOpen(m_Path + location))
311 {
312 s = h->OpenFile(*this, m_Path + location);
313 if (s) { m_LastName = m_Path + location; break; }
314 }
315 node = node->GetNext();
316 }
317 }
318
319 // if failed, try absolute paths :
320 if (s == NULL)
321 {
322 node = m_Handlers.GetFirst();
323 while (node)
324 {
325 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
326 if (h->CanOpen(location))
327 {
328 s = h->OpenFile(*this, location);
329 if (s) { m_LastName = location; break; }
330 }
331 node = node->GetNext();
332 }
333 }
334 return (s);
335}
336
337
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
380void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
381{
382 m_Handlers.Append(handler);
383}
384
385
386void wxFileSystem::CleanUpHandlers()
387{
388 m_Handlers.DeleteContents(TRUE);
389 m_Handlers.Clear();
390}
391
392
393
394
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 }
407 virtual void OnExit()
408 {
409 wxFileSystemHandler::CleanUpStatics();
410 wxFileSystem::CleanUpHandlers();
411 }
412};
413
414IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
415
416#endif
417 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
418
419
420