]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/filesys.cpp
RemoveAt() added to array classes
[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& WXUNUSED(spec),
165 int WXUNUSED(flags))
166{
167 return wxEmptyString;
168}
169
170wxString wxFileSystemHandler::FindNext()
171{
172 return wxEmptyString;
173}
174
175//--------------------------------------------------------------------------------
176// wxLocalFSHandler
177//--------------------------------------------------------------------------------
178
179class wxLocalFSHandler : public wxFileSystemHandler
180{
181 public:
182 virtual bool CanOpen(const wxString& location);
183 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
184 virtual wxString FindFirst(const wxString& spec, int flags = 0);
185 virtual wxString FindNext();
186};
187
188
189bool wxLocalFSHandler::CanOpen(const wxString& location)
190{
191 return GetProtocol(location) == wxT("file");
192}
193
194wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
195{
196 wxString right = GetRightLocation(location);
197 if (wxFileExists(right))
198 return new wxFSFile(new wxFileInputStream(right),
199 right,
200 GetMimeTypeFromExt(location),
201 GetAnchor(location));
202 else return (wxFSFile*) NULL;
203}
204
205wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
206{
207 wxString right = GetRightLocation(spec);
208 return wxFindFirstFile(right, flags);
209}
210
211wxString wxLocalFSHandler::FindNext()
212{
213 return wxFindNextFile();
214}
215
216
217
218//-----------------------------------------------------------------------------
219// wxFileSystem
220//-----------------------------------------------------------------------------
221
222IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
223
224
225wxList wxFileSystem::m_Handlers;
226
227
228
229void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
230{
231 int i, pathpos = -1;
232 m_Path = location;
233
234 for (i = m_Path.Length()-1; i >= 0; i--)
235 if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
236
237 if (is_dir)
238 {
239 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
240 m_Path << wxT('/');
241 }
242
243 else
244 {
245 for (i = m_Path.Length()-1; i >= 0; i--)
246 {
247 if (m_Path[(unsigned int) i] == wxT('/'))
248 {
249 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
250 {
251 i -= 2;
252 continue;
253 }
254 else
255 {
256 pathpos = i;
257 break;
258 }
259 }
260 else if (m_Path[(unsigned int) i] == wxT(':')) {
261 pathpos = i;
262 break;
263 }
264 }
265 if (pathpos == -1)
266 {
267 for (i = 0; i < (int) m_Path.Length(); i++)
268 {
269 if (m_Path[(unsigned int) i] == wxT(':'))
270 {
271 m_Path.Remove(i+1);
272 break;
273 }
274 }
275 if (i == (int) m_Path.Length())
276 m_Path = wxEmptyString;
277 }
278 else
279 {
280 m_Path.Remove(pathpos+1);
281 }
282 }
283}
284
285
286
287wxFSFile* wxFileSystem::OpenFile(const wxString& location)
288{
289 wxString loc = location;
290 int i, ln;
291 char meta;
292 wxFSFile *s = NULL;
293 wxNode *node;
294
295 ln = loc.Length();
296 meta = 0;
297 for (i = 0; i < ln; i++)
298 {
299 if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
300 if (!meta)
301 switch (loc[(unsigned int) i])
302 {
303 case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i];
304 }
305 }
306 m_LastName = wxEmptyString;
307
308 // try relative paths first :
309 if (meta != wxT(':'))
310 {
311 node = m_Handlers.GetFirst();
312 while (node)
313 {
314 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
315 if (h->CanOpen(m_Path + location))
316 {
317 s = h->OpenFile(*this, m_Path + location);
318 if (s) { m_LastName = m_Path + location; break; }
319 }
320 node = node->GetNext();
321 }
322 }
323
324 // if failed, try absolute paths :
325 if (s == NULL)
326 {
327 node = m_Handlers.GetFirst();
328 while (node)
329 {
330 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
331 if (h->CanOpen(location))
332 {
333 s = h->OpenFile(*this, location);
334 if (s) { m_LastName = location; break; }
335 }
336 node = node->GetNext();
337 }
338 }
339 return (s);
340}
341
342
343
344wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
345{
346 wxNode *node;
347 wxString spec2(spec);
348
349 m_FindFileHandler = NULL;
350
351 for (int i = spec2.Length()-1; i >= 0; i--)
352 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
353
354 node = m_Handlers.GetFirst();
355 while (node)
356 {
357 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
358 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
359 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
360 node = node->GetNext();
361 }
362
363 node = m_Handlers.GetFirst();
364 while (node)
365 {
366 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
367 if (m_FindFileHandler -> CanOpen(spec2))
368 return m_FindFileHandler -> FindFirst(spec2, flags);
369 node = node->GetNext();
370 }
371
372 return wxEmptyString;
373}
374
375
376
377wxString wxFileSystem::FindNext()
378{
379 if (m_FindFileHandler == NULL) return wxEmptyString;
380 else return m_FindFileHandler -> FindNext();
381}
382
383
384
385void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
386{
387 m_Handlers.Append(handler);
388}
389
390
391void wxFileSystem::CleanUpHandlers()
392{
393 m_Handlers.DeleteContents(TRUE);
394 m_Handlers.Clear();
395}
396
397
398
399
400///// Module:
401
402class wxFileSystemModule : public wxModule
403{
404 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
405
406 public:
407 virtual bool OnInit()
408 {
409 wxFileSystem::AddHandler(new wxLocalFSHandler);
410 return TRUE;
411 }
412 virtual void OnExit()
413 {
414 wxFileSystemHandler::CleanUpStatics();
415 wxFileSystem::CleanUpHandlers();
416 }
417};
418
419IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
420
421#endif
422 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
423
424
425