]> git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
corrected assert in wxSocket::Initialize()
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filesys.h"
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
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 #include "wx/log.h"
29
30
31 //--------------------------------------------------------------------------------
32 // wxFileSystemHandler
33 //--------------------------------------------------------------------------------
34
35 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
36
37
38 wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location)
39 {
40 wxString ext, mime;
41 wxString loc = GetRightLocation(location);
42 char c;
43 int l = loc.Length(), l2;
44
45 l2 = l;
46 for (int i = l-1; i >= 0; i--)
47 {
48 c = loc[(unsigned int) i];
49 if ( c == wxT('#') )
50 l2 = i + 1;
51 if ( c == wxT('.') )
52 {
53 ext = loc.Right(l2-i-1);
54 break;
55 }
56 if ( (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':')) )
57 return wxEmptyString;
58 }
59
60 #if wxUSE_MIMETYPE
61 static bool s_MinimalMimeEnsured = false;
62 if (!s_MinimalMimeEnsured)
63 {
64 static const wxFileTypeInfo fallbacks[] =
65 {
66 wxFileTypeInfo(_T("image/jpeg"),
67 _T(""),
68 _T(""),
69 _T("JPEG image (from fallback)"),
70 _T("jpg"), _T("jpeg"), _T("JPG"), _T("JPEG"), NULL),
71 wxFileTypeInfo(_T("image/gif"),
72 _T(""),
73 _T(""),
74 _T("GIF image (from fallback)"),
75 _T("gif"), _T("GIF"), NULL),
76 wxFileTypeInfo(_T("image/png"),
77 _T(""),
78 _T(""),
79 _T("PNG image (from fallback)"),
80 _T("png"), _T("PNG"), NULL),
81 wxFileTypeInfo(_T("image/bmp"),
82 _T(""),
83 _T(""),
84 _T("windows bitmap image (from fallback)"),
85 _T("bmp"), _T("BMP"), NULL),
86 wxFileTypeInfo(_T("text/html"),
87 _T(""),
88 _T(""),
89 _T("HTML document (from fallback)"),
90 _T("htm"), _T("html"), _T("HTM"), _T("HTML"), NULL),
91 // must terminate the table with this!
92 wxFileTypeInfo()
93 };
94 wxTheMimeTypesManager->AddFallbacks(fallbacks);
95 s_MinimalMimeEnsured = true;
96 }
97
98 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
99 if ( !ft || !ft -> GetMimeType(&mime) )
100 {
101 mime = wxEmptyString;
102 }
103
104 delete ft;
105
106 return mime;
107 #else
108 if ( ext.IsSameAs(wxT("htm"), FALSE) || ext.IsSameAs(_T("html"), FALSE) )
109 return wxT("text/html");
110 if ( ext.IsSameAs(wxT("jpg"), FALSE) || ext.IsSameAs(_T("jpeg"), FALSE) )
111 return wxT("image/jpeg");
112 if ( ext.IsSameAs(wxT("gif"), FALSE) )
113 return wxT("image/gif");
114 if ( ext.IsSameAs(wxT("png"), FALSE) )
115 return wxT("image/png");
116 if ( ext.IsSameAs(wxT("bmp"), FALSE) )
117 return wxT("image/bmp");
118 return wxEmptyString;
119 #endif
120 }
121
122
123
124 wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
125 {
126 wxString s = wxEmptyString;
127 int i, l = location.Length();
128 bool fnd;
129
130 fnd = FALSE;
131 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
132 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
133 }
134 if (!fnd) return wxT("file");
135 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
136 return s;
137 }
138
139
140 wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
141 {
142 int i;
143 bool fnd;
144
145 fnd = FALSE;
146 for (i = location.Length()-1; i >= 0; i--) {
147 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
148 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
149 }
150 return wxEmptyString;
151 }
152
153 wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
154 {
155 int i, l = location.Length();
156 int l2 = l + 1;
157
158 for (i = l-1;
159 (i >= 0) &&
160 ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':')));
161 i--)
162 {
163 if (location[i] == wxT('#')) l2 = i + 1;
164 }
165 if (i == 0) return wxEmptyString;
166 else return location.Mid(i + 1, l2 - i - 2);
167 }
168
169 wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
170 {
171 char c;
172 int l = location.Length();
173
174 for (int i = l-1; i >= 0; i--) {
175 c = location[i];
176 if (c == wxT('#')) return location.Right(l-i-1);
177 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
178 }
179 return wxEmptyString;
180 }
181
182
183 wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
184 int WXUNUSED(flags))
185 {
186 return wxEmptyString;
187 }
188
189 wxString wxFileSystemHandler::FindNext()
190 {
191 return wxEmptyString;
192 }
193
194 //--------------------------------------------------------------------------------
195 // wxLocalFSHandler
196 //--------------------------------------------------------------------------------
197
198
199 wxString wxLocalFSHandler::ms_root;
200
201 bool wxLocalFSHandler::CanOpen(const wxString& location)
202 {
203 return GetProtocol(location) == wxT("file");
204 }
205
206 wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
207 {
208 // location has Unix path separators
209 wxString right = GetRightLocation(location);
210 wxFileName fn = wxFileSystem::URLToFileName(right);
211 wxString fullpath = ms_root + fn.GetFullPath();
212
213 if (!wxFileExists(fullpath))
214 return (wxFSFile*) NULL;
215
216 // we need to check whether we can really read from this file, otherwise
217 // wxFSFile is not going to work
218 wxFFileInputStream *is = new wxFFileInputStream(fullpath);
219 if ( !is->Ok() )
220 {
221 delete is;
222 return (wxFSFile*) NULL;
223 }
224
225 return new wxFSFile(is,
226 right,
227 GetMimeTypeFromExt(location),
228 GetAnchor(location)
229 #if wxUSE_DATETIME
230 ,wxDateTime(wxFileModificationTime(fullpath))
231 #endif // wxUSE_DATETIME
232 );
233 }
234
235 wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
236 {
237 wxFileName fn = wxFileSystem::URLToFileName(GetRightLocation(spec));
238 return wxFindFirstFile(ms_root + fn.GetFullPath(), flags);
239 }
240
241 wxString wxLocalFSHandler::FindNext()
242 {
243 return wxFindNextFile();
244 }
245
246
247
248 //-----------------------------------------------------------------------------
249 // wxFileSystem
250 //-----------------------------------------------------------------------------
251
252 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
253 IMPLEMENT_ABSTRACT_CLASS(wxFSFile, wxObject)
254
255
256 wxList wxFileSystem::m_Handlers;
257
258
259 static wxString MakeCorrectPath(const wxString& path)
260 {
261 wxString p(path);
262 wxString r;
263 int i, j, cnt;
264
265 cnt = p.Length();
266 for (i = 0; i < cnt; i++)
267 if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
268
269 if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; }
270
271 if (cnt < 3) return p;
272
273 r << p.GetChar(0) << p.GetChar(1);
274
275 // skip trailing ../.., if any
276 for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i);
277
278 // remove back references: translate dir1/../dir2 to dir2
279 for (; i < cnt; i++)
280 {
281 r << p.GetChar(i);
282 if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.'))
283 {
284 for (j = r.Length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
285 if (j >= 0 && r.GetChar(j) != wxT(':'))
286 {
287 for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {}
288 r.Remove(j + 1);
289 }
290 }
291 }
292
293 for (; i < cnt; i++) r << p.GetChar(i);
294
295 return r;
296 }
297
298
299 void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
300 {
301 int i, pathpos = -1;
302
303 m_Path = MakeCorrectPath(location);
304
305 if (is_dir)
306 {
307 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
308 m_Path << wxT('/');
309 }
310
311 else
312 {
313 for (i = m_Path.Length()-1; i >= 0; i--)
314 {
315 if (m_Path[(unsigned int) i] == wxT('/'))
316 {
317 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
318 {
319 i -= 2;
320 continue;
321 }
322 else
323 {
324 pathpos = i;
325 break;
326 }
327 }
328 else if (m_Path[(unsigned int) i] == wxT(':')) {
329 pathpos = i;
330 break;
331 }
332 }
333 if (pathpos == -1)
334 {
335 for (i = 0; i < (int) m_Path.Length(); i++)
336 {
337 if (m_Path[(unsigned int) i] == wxT(':'))
338 {
339 m_Path.Remove(i+1);
340 break;
341 }
342 }
343 if (i == (int) m_Path.Length())
344 m_Path = wxEmptyString;
345 }
346 else
347 {
348 m_Path.Remove(pathpos+1);
349 }
350 }
351 }
352
353
354
355 wxFSFile* wxFileSystem::OpenFile(const wxString& location)
356 {
357 wxString loc = MakeCorrectPath(location);
358 unsigned i, ln;
359 char meta;
360 wxFSFile *s = NULL;
361 wxList::compatibility_iterator node;
362
363 ln = loc.Length();
364 meta = 0;
365 for (i = 0; i < ln; i++)
366 {
367 switch (loc[i])
368 {
369 case wxT('/') : case wxT(':') : case wxT('#') :
370 meta = loc[i];
371 break;
372 }
373 if (meta != 0) break;
374 }
375 m_LastName = wxEmptyString;
376
377 // try relative paths first :
378 if (meta != wxT(':'))
379 {
380 node = m_Handlers.GetFirst();
381 while (node)
382 {
383 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
384 if (h->CanOpen(m_Path + loc))
385 {
386 s = h->OpenFile(*this, m_Path + loc);
387 if (s) { m_LastName = m_Path + loc; break; }
388 }
389 node = node->GetNext();
390 }
391 }
392
393 // if failed, try absolute paths :
394 if (s == NULL)
395 {
396 node = m_Handlers.GetFirst();
397 while (node)
398 {
399 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
400 if (h->CanOpen(loc))
401 {
402 s = h->OpenFile(*this, loc);
403 if (s) { m_LastName = loc; break; }
404 }
405 node = node->GetNext();
406 }
407 }
408 return (s);
409 }
410
411
412
413 wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
414 {
415 wxList::compatibility_iterator node;
416 wxString spec2(spec);
417
418 m_FindFileHandler = NULL;
419
420 for (int i = spec2.Length()-1; i >= 0; i--)
421 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe
422
423 node = m_Handlers.GetFirst();
424 while (node)
425 {
426 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
427 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
428 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
429 node = node->GetNext();
430 }
431
432 node = m_Handlers.GetFirst();
433 while (node)
434 {
435 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
436 if (m_FindFileHandler -> CanOpen(spec2))
437 return m_FindFileHandler -> FindFirst(spec2, flags);
438 node = node->GetNext();
439 }
440
441 return wxEmptyString;
442 }
443
444
445
446 wxString wxFileSystem::FindNext()
447 {
448 if (m_FindFileHandler == NULL) return wxEmptyString;
449 else return m_FindFileHandler -> FindNext();
450 }
451
452
453
454 void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
455 {
456 m_Handlers.Append(handler);
457 }
458
459
460 void wxFileSystem::CleanUpHandlers()
461 {
462 WX_CLEAR_LIST(wxList, m_Handlers);
463 }
464
465 const static wxString g_unixPathString(wxT("/"));
466 const static wxString g_nativePathString(wxFILE_SEP_PATH);
467
468 // Returns the native path for a file URL
469 wxFileName wxFileSystem::URLToFileName(const wxString& url)
470 {
471 wxString path = url;
472
473 if ( path.Find(wxT("file://")) == 0 )
474 {
475 path = path.Mid(7);
476 }
477 else if ( path.Find(wxT("file:")) == 0 )
478 {
479 path = path.Mid(5);
480 }
481 // Remove preceding double slash on Mac Classic
482 #if defined(__WXMAC__) && !defined(__UNIX__)
483 else if ( path.Find(wxT("//")) == 0 )
484 path = path.Mid(2);
485 #endif
486
487 path.Replace(wxT("%25"), wxT("%"));
488 path.Replace(wxT("%3A"), wxT(":"));
489
490 #ifdef __WXMSW__
491 // file urls either start with a forward slash (local harddisk),
492 // otherwise they have a servername/sharename notation,
493 // which only exists on msw and corresponds to a unc
494 if ( path[0u] == wxT('/') && path [1u] != wxT('/'))
495 {
496 path = path.Mid(1);
497 }
498 else if ( (url.Find(wxT("file://")) == 0) &&
499 (path.Find(wxT('/')) != wxNOT_FOUND) &&
500 (path.Length() > 1) && (path[1u] != wxT(':')) )
501 {
502 path = wxT("//") + path;
503 }
504 #endif
505
506 path.Replace(g_unixPathString, g_nativePathString);
507
508 return wxFileName(path, wxPATH_NATIVE);
509 }
510
511 // Returns the file URL for a native path
512 wxString wxFileSystem::FileNameToURL(const wxFileName& filename)
513 {
514 wxFileName fn = filename;
515 fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE);
516 wxString url = fn.GetFullPath(wxPATH_NATIVE);
517
518 #ifndef __UNIX__
519 // unc notation, wxMSW
520 if ( url.Find(wxT("\\\\")) == 0 )
521 {
522 url = wxT("//") + url.Mid(2);
523 }
524 else
525 {
526 url = wxT("/") + url;
527 #ifdef __WXMAC__
528 url = wxT("/") + url;
529 #endif
530
531 }
532 #endif
533
534 url.Replace(g_nativePathString, g_unixPathString);
535 url.Replace(wxT("%"), wxT("%25"));
536 url.Replace(wxT(":"), wxT("%3A"));
537 url = wxT("file:") + url;
538 return url;
539 }
540
541
542 ///// Module:
543
544 class wxFileSystemModule : public wxModule
545 {
546 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
547
548 public:
549 virtual bool OnInit()
550 {
551 wxFileSystem::AddHandler(new wxLocalFSHandler);
552 return true;
553 }
554 virtual void OnExit()
555 {
556 wxFileSystem::CleanUpHandlers();
557 }
558 };
559
560 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
561
562 #endif
563 // wxUSE_FILESYSTEM
564
565
566