]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
5935588b9ef963d44143dabba30fc7edaa35e183
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxFileSystem class - interface for opening files 
   4 // Author:      Vaclav Slavik 
   5 // Copyright:   (c) 1999 Vaclav Slavik 
   6 // Licence:     wxWindows Licence 
   7 ///////////////////////////////////////////////////////////////////////////// 
  10 #pragma implementation 
  13 #include "wx/wxprec.h" 
  21     #define wxUSE_FS_INET 0 
  24 #if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS 
  26 #include "wx/wfstream.h" 
  27 #include "wx/module.h" 
  28 #include "wx/filesys.h" 
  30 //-------------------------------------------------------------------------------- 
  31 // wxFileSystemHandler 
  32 //-------------------------------------------------------------------------------- 
  34 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler
, wxObject
) 
  36 wxMimeTypesManager 
*wxFileSystemHandler::m_MimeMng 
= NULL
; 
  38 void wxFileSystemHandler::CleanUpStatics() 
  40     if (m_MimeMng
) delete m_MimeMng
; 
  45 wxString 
wxFileSystemHandler::GetMimeTypeFromExt(const wxString
& location
) 
  47     wxString ext 
= wxEmptyString
, mime 
= wxEmptyString
; 
  48     wxString loc 
= GetRightLocation(location
); 
  50     int l 
= loc
.Length(), l2
; 
  54     for (int i 
= l
-1; i 
>= 0; i
--) { 
  55         c 
= loc
[(unsigned int) i
]; 
  56         if (c 
== _T('#')) l2 
= i 
+ 1; 
  57         if (c 
== _T('.')) {ext 
= loc
.Right(l2
-i
-1); break;} 
  58         if ((c 
== _T('/')) || (c 
== _T('\\')) || (c 
== _T(':'))) {return wxEmptyString
;} 
  61     if (m_MimeMng 
== NULL
) { 
  62         m_MimeMng 
= new wxMimeTypesManager
; 
  64         static const wxFileTypeInfo fallbacks
[] = 
  66             wxFileTypeInfo("image/jpeg", 
  69                            "JPEG image (from fallback)", 
  71             wxFileTypeInfo("image/gif", 
  74                            "GIF image (from fallback)", 
  76             wxFileTypeInfo("image/png", 
  79                            "PNG image (from fallback)", 
  81             wxFileTypeInfo("image/bmp", 
  84                            "windows bitmap image (from fallback)", 
  86             wxFileTypeInfo("text/html", 
  89                            "HTML document (from fallback)", 
  92             // must terminate the table with this! 
  96         m_MimeMng 
-> AddFallbacks(fallbacks
); 
  99     ft 
= m_MimeMng 
-> GetFileTypeFromExtension(ext
); 
 100     if (ft 
&& (ft 
-> GetMimeType(&mime
))) return mime
; 
 101     else return wxEmptyString
; 
 106 wxString 
wxFileSystemHandler::GetProtocol(const wxString
& location
) const 
 108     wxString s 
= wxEmptyString
; 
 109     int i
, l 
= location
.Length(); 
 113     for (i 
= l
-1; (i 
>= 0) && ((location
[i
] != _T('#')) || (!fnd
)); i
--) { 
 114         if ((location
[i
] == _T(':')) && (i 
!= 1 /*win: C:\path*/)) fnd 
= TRUE
; 
 116     if (!fnd
) return _T("file"); 
 117     for (++i
; (i 
< l
) && (location
[i
] != _T(':')); i
++) s 
<< location
[i
]; 
 122 wxString 
wxFileSystemHandler::GetLeftLocation(const wxString
& location
) const 
 128     for (i 
= location
.Length()-1; i 
>= 0; i
--) { 
 129         if ((location
[i
] == _T(':')) && (i 
!= 1 /*win: C:\path*/)) fnd 
= TRUE
; 
 130         else if (fnd 
&& (location
[i
] == _T('#'))) return location
.Left(i
); 
 132     return wxEmptyString
; 
 135 wxString 
wxFileSystemHandler::GetRightLocation(const wxString
& location
) const 
 137     int i
, l 
= location
.Length(); 
 139     for (i 
= l
-1; (i 
>= 0) && ((location
[i
] != _T(':')) || (i 
== 1) || (location
[i
-2] == _T(':'))); i
--) {if (location
[i
] == _T('#')) l2 
= i 
+ 1;} 
 140     if (i 
== 0) return wxEmptyString
; 
 141     else return location
.Mid(i 
+ 1, l2 
- i 
- 2); 
 144 wxString 
wxFileSystemHandler::GetAnchor(const wxString
& location
) const 
 147     int l 
= location
.Length(); 
 149     for (int i 
= l
-1; i 
>= 0; i
--) { 
 151         if (c 
== _T('#')) return location
.Right(l
-i
-1); 
 152         else if ((c 
== _T('.')) || (c 
== _T('/')) || (c 
== _T('\\')) || (c 
== _T(':'))) return wxEmptyString
; 
 154     return wxEmptyString
; 
 157 //-------------------------------------------------------------------------------- 
 159 //-------------------------------------------------------------------------------- 
 161 class wxLocalFSHandler 
: public wxFileSystemHandler
 
 164         virtual bool CanOpen(const wxString
& location
); 
 165         virtual wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
); 
 169 bool wxLocalFSHandler::CanOpen(const wxString
& location
) 
 171     return GetProtocol(location
) == _T("file"); 
 174 wxFSFile
* wxLocalFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
) 
 176     wxString right 
= GetRightLocation(location
); 
 177     if (wxFileExists(right
)) 
 178         return new wxFSFile(new wxFileInputStream(right
), 
 180                             GetMimeTypeFromExt(location
), 
 181                             GetAnchor(location
)); 
 182     else return (wxFSFile
*) NULL
; 
 185 //----------------------------------------------------------------------------- 
 187 //----------------------------------------------------------------------------- 
 189 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem
, wxObject
) 
 192 wxList 
wxFileSystem::m_Handlers
; 
 196 void wxFileSystem::ChangePathTo(const wxString
& location
, bool is_dir
) 
 201     for (i 
= m_Path
.Length()-1; i 
>= 0; i
--) 
 202         if (m_Path
[(unsigned int) i
] == _T('\\')) m_Path
.GetWritableChar(i
) = _T('/');         // wanna be windows-safe 
 206         for (i 
= m_Path
.Length()-1; i 
>= 0; i
--)  
 208             if (m_Path
[(unsigned int) i
] == _T('/')) 
 210                 if ((i 
> 1) && (m_Path
[(unsigned int) (i
-1)] == _T('/')) && (m_Path
[(unsigned int) (i
-2)] == _T(':'))) 
 221         else if (m_Path
[(unsigned int) i
] == _T(':')) { 
 228             for (i 
= 0; i 
< (int) m_Path
.Length(); i
++)  
 230                 if (m_Path
[(unsigned int) i
] == _T(':')) 
 237             if (i 
== (int) m_Path
.Length())  
 238                 m_Path 
= wxEmptyString
; 
 242             if (m_Path
[m_Path
.Length()-1] != _T('/'))  
 244             m_Path
.Remove(pathpos
+1); 
 251 wxFSFile
* wxFileSystem::OpenFile(const wxString
& location
) 
 253     wxString loc 
= location
; 
 261     for (i 
= 0; i 
< ln
; i
++)  
 263         if (loc
[(unsigned int) i
] == _T('\\')) loc
.GetWritableChar(i
) = _T('/');         // wanna be windows-safe 
 264         if (!meta
) switch (loc
[(unsigned int) i
]) 
 266             case _T('/') : case _T(':') : case _T('#') : meta 
= loc
[(unsigned int) i
]; 
 269     m_LastName 
= wxEmptyString
; 
 271     // try relative paths first : 
 274         node 
= m_Handlers
.GetFirst(); 
 277             wxFileSystemHandler 
*h 
= (wxFileSystemHandler
*) node 
-> GetData(); 
 278             if (h
->CanOpen(m_Path 
+ location
))  
 280                 s 
= h
->OpenFile(*this, m_Path 
+ location
); 
 281                 if (s
) { m_LastName 
= m_Path 
+ location
; break; } 
 283             node 
= node
->GetNext(); 
 287     // if failed, try absolute paths : 
 290         node 
= m_Handlers
.GetFirst(); 
 293             wxFileSystemHandler 
*h 
= (wxFileSystemHandler
*) node
->GetData(); 
 294             if (h
->CanOpen(location
))  
 296                 s 
= h
->OpenFile(*this, location
); 
 297                 if (s
) { m_LastName 
= location
; break; } 
 299             node 
= node
->GetNext(); 
 306 void wxFileSystem::AddHandler(wxFileSystemHandler 
*handler
) 
 308     m_Handlers
.Append(handler
); 
 315 class wxFileSystemModule 
: public wxModule
 
 317     DECLARE_DYNAMIC_CLASS(wxFileSystemModule
) 
 320         virtual bool OnInit() 
 322             wxFileSystem::AddHandler(new wxLocalFSHandler
); 
 325         virtual void OnExit()  
 327             wxFileSystemHandler::CleanUpStatics(); 
 331 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule
, wxModule
) 
 334   // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS