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