]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
unused parameter warnings suppressed
[wxWidgets.git] / src / common / filesys.cpp
CommitLineData
5526e819
VS
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
d30e0edd 13#include "wx/wxprec.h"
5526e819
VS
14
15#ifdef __BORDLANDC__
16#pragma hdrstop
17#endif
18
31528cd3 19#if !wxUSE_SOCKETS
31528cd3 20 #undef wxUSE_FS_INET
31528cd3
VZ
21 #define wxUSE_FS_INET 0
22#endif
23
d30e0edd 24#if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 25
d30e0edd
RR
26#include "wx/wfstream.h"
27#include "wx/module.h"
28#include "wx/filesys.h"
5526e819
VS
29
30//--------------------------------------------------------------------------------
31// wxFileSystemHandler
32//--------------------------------------------------------------------------------
33
34IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
35
a76015e6
VS
36wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL;
37
38void wxFileSystemHandler::CleanUpStatics()
39{
40 if (m_MimeMng) delete m_MimeMng;
41 m_MimeMng = NULL;
42}
5526e819
VS
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--) {
ea4f5235 55 c = loc[(unsigned int) i];
d30e0edd
RR
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;}
5526e819 59 }
956418ab
VS
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
a76015e6 99 ft = m_MimeMng -> GetFileTypeFromExtension(ext);
5526e819
VS
100 if (ft && (ft -> GetMimeType(&mime))) return mime;
101 else return wxEmptyString;
102}
103
104
105
106wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
107{
108 wxString s = wxEmptyString;
109 int i, l = location.Length();
110 bool fnd;
111
112 fnd = FALSE;
d30e0edd
RR
113 for (i = l-1; (i >= 0) && ((location[i] != _T('#')) || (!fnd)); i--) {
114 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 115 }
d30e0edd
RR
116 if (!fnd) return _T("file");
117 for (++i; (i < l) && (location[i] != _T(':')); i++) s << location[i];
5526e819
VS
118 return s;
119}
120
121
5526e819
VS
122wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
123{
124 int i;
125 bool fnd;
126
127 fnd = FALSE;
128 for (i = location.Length()-1; i >= 0; i--) {
d30e0edd
RR
129 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
130 else if (fnd && (location[i] == _T('#'))) return location.Left(i);
5526e819
VS
131 }
132 return wxEmptyString;
133}
134
5526e819
VS
135wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
136{
137 int i, l = location.Length();
138 int l2 = l + 1;
d30e0edd 139 for (i = l-1; (i >= 0) && ((location[i] != _T(':')) || (i == 1) || (location[i-2] == _T(':'))); i--) {if (location[i] == _T('#')) l2 = i + 1;}
5526e819
VS
140 if (i == 0) return wxEmptyString;
141 else return location.Mid(i + 1, l2 - i - 2);
142}
143
5526e819
VS
144wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
145{
146 char c;
147 int l = location.Length();
148
149 for (int i = l-1; i >= 0; i--) {
150 c = location[i];
d30e0edd
RR
151 if (c == _T('#')) return location.Right(l-i-1);
152 else if ((c == _T('.')) || (c == _T('/')) || (c == _T('\\')) || (c == _T(':'))) return wxEmptyString;
5526e819
VS
153 }
154 return wxEmptyString;
155}
156
5526e819
VS
157//--------------------------------------------------------------------------------
158// wxLocalFSHandler
159//--------------------------------------------------------------------------------
160
161class wxLocalFSHandler : public wxFileSystemHandler
162{
163 public:
164 virtual bool CanOpen(const wxString& location);
165 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
166};
167
168
5526e819
VS
169bool wxLocalFSHandler::CanOpen(const wxString& location)
170{
d30e0edd 171 return GetProtocol(location) == _T("file");
5526e819
VS
172}
173
5526e819
VS
174wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
175{
176 wxString right = GetRightLocation(location);
177 if (wxFileExists(right))
178 return new wxFSFile(new wxFileInputStream(right),
179 right,
180 GetMimeTypeFromExt(location),
181 GetAnchor(location));
d30e0edd 182 else return (wxFSFile*) NULL;
5526e819
VS
183}
184
5526e819
VS
185//-----------------------------------------------------------------------------
186// wxFileSystem
187//-----------------------------------------------------------------------------
188
189IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
190
191
192wxList wxFileSystem::m_Handlers;
193
194
195
196void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
197{
198 int i, pathpos = -1;
199 m_Path = location;
200
201 for (i = m_Path.Length()-1; i >= 0; i--)
ea4f5235 202 if (m_Path[(unsigned int) i] == _T('\\')) m_Path.GetWritableChar(i) = _T('/'); // wanna be windows-safe
d30e0edd
RR
203
204 if (is_dir == FALSE)
205 {
206 for (i = m_Path.Length()-1; i >= 0; i--)
207 {
ea4f5235 208 if (m_Path[(unsigned int) i] == _T('/'))
d30e0edd 209 {
ea4f5235 210 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == _T('/')) && (m_Path[(unsigned int) (i-2)] == _T(':')))
d30e0edd 211 {
5526e819
VS
212 i -= 2;
213 continue;
214 }
d30e0edd
RR
215 else
216 {
5526e819
VS
217 pathpos = i;
218 break;
219 }
220 }
ea4f5235 221 else if (m_Path[(unsigned int) i] == _T(':')) {
5526e819
VS
222 pathpos = i;
223 break;
224 }
225 }
d30e0edd
RR
226 if (pathpos == -1)
227 {
228 for (i = 0; i < (int) m_Path.Length(); i++)
229 {
ea4f5235 230 if (m_Path[(unsigned int) i] == _T(':'))
d30e0edd
RR
231 {
232 //m_Path << _T('/');
5526e819
VS
233 m_Path.Remove(i+1);
234 break;
235 }
236 }
d30e0edd
RR
237 if (i == (int) m_Path.Length())
238 m_Path = wxEmptyString;
5526e819 239 }
d30e0edd
RR
240 else
241 {
242 if (m_Path[m_Path.Length()-1] != _T('/'))
243 m_Path << _T('/');
5526e819
VS
244 m_Path.Remove(pathpos+1);
245 }
246 }
247}
248
249
250
251wxFSFile* wxFileSystem::OpenFile(const wxString& location)
252{
253 wxString loc = location;
254 int i, ln;
255 char meta;
256 wxFSFile *s = NULL;
257 wxNode *node;
258
259 ln = loc.Length();
260 meta = 0;
d30e0edd
RR
261 for (i = 0; i < ln; i++)
262 {
ea4f5235
JS
263 if (loc[(unsigned int) i] == _T('\\')) loc.GetWritableChar(i) = _T('/'); // wanna be windows-safe
264 if (!meta) switch (loc[(unsigned int) i])
d30e0edd 265 {
ea4f5235 266 case _T('/') : case _T(':') : case _T('#') : meta = loc[(unsigned int) i];
5526e819
VS
267 }
268 }
269 m_LastName = wxEmptyString;
270
271 // try relative paths first :
d30e0edd
RR
272 if (meta != _T(':'))
273 {
5526e819 274 node = m_Handlers.GetFirst();
d30e0edd
RR
275 while (node)
276 {
5526e819 277 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
d30e0edd
RR
278 if (h->CanOpen(m_Path + location))
279 {
280 s = h->OpenFile(*this, m_Path + location);
281 if (s) { m_LastName = m_Path + location; break; }
5526e819 282 }
d30e0edd 283 node = node->GetNext();
5526e819
VS
284 }
285 }
286
287 // if failed, try absolute paths :
d30e0edd
RR
288 if (s == NULL)
289 {
5526e819 290 node = m_Handlers.GetFirst();
d30e0edd
RR
291 while (node)
292 {
293 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
294 if (h->CanOpen(location))
295 {
296 s = h->OpenFile(*this, location);
297 if (s) { m_LastName = location; break; }
5526e819 298 }
d30e0edd 299 node = node->GetNext();
5526e819
VS
300 }
301 }
302 return (s);
303}
304
305
5526e819
VS
306void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
307{
308 m_Handlers.Append(handler);
309}
310
311
312
5526e819
VS
313///// Module:
314
315class wxFileSystemModule : public wxModule
316{
317 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
318
319 public:
320 virtual bool OnInit()
321 {
322 wxFileSystem::AddHandler(new wxLocalFSHandler);
323 return TRUE;
324 }
a76015e6
VS
325 virtual void OnExit()
326 {
327 wxFileSystemHandler::CleanUpStatics();
328 }
5526e819
VS
329};
330
331IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
332
d30e0edd
RR
333#endif
334 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 335
a76015e6
VS
336
337