]> git.saurik.com Git - wxWidgets.git/blob - src/common/filesys.cpp
VisualAge C++ V4.0 configuration files
[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 // 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
34 IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
35
36 wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL;
37
38 void wxFileSystemHandler::CleanUpStatics()
39 {
40 if (m_MimeMng) delete m_MimeMng;
41 m_MimeMng = NULL;
42 }
43
44
45 wxString 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 == _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;}
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))) return mime;
101 else return wxEmptyString;
102 }
103
104
105
106 wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
107 {
108 wxString s = wxEmptyString;
109 int i, l = location.Length();
110 bool fnd;
111
112 fnd = FALSE;
113 for (i = l-1; (i >= 0) && ((location[i] != _T('#')) || (!fnd)); i--) {
114 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
115 }
116 if (!fnd) return _T("file");
117 for (++i; (i < l) && (location[i] != _T(':')); i++) s << location[i];
118 return s;
119 }
120
121
122 wxString 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--) {
129 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
130 else if (fnd && (location[i] == _T('#'))) return location.Left(i);
131 }
132 return wxEmptyString;
133 }
134
135 wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
136 {
137 int i, l = location.Length();
138 int l2 = l + 1;
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);
142 }
143
144 wxString 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];
151 if (c == _T('#')) return location.Right(l-i-1);
152 else if ((c == _T('.')) || (c == _T('/')) || (c == _T('\\')) || (c == _T(':'))) return wxEmptyString;
153 }
154 return wxEmptyString;
155 }
156
157 //--------------------------------------------------------------------------------
158 // wxLocalFSHandler
159 //--------------------------------------------------------------------------------
160
161 class 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
169 bool wxLocalFSHandler::CanOpen(const wxString& location)
170 {
171 return GetProtocol(location) == _T("file");
172 }
173
174 wxFSFile* 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));
182 else return (wxFSFile*) NULL;
183 }
184
185 //-----------------------------------------------------------------------------
186 // wxFileSystem
187 //-----------------------------------------------------------------------------
188
189 IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
190
191
192 wxList wxFileSystem::m_Handlers;
193
194
195
196 void 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--)
202 if (m_Path[(unsigned int) i] == _T('\\')) m_Path.GetWritableChar(i) = _T('/'); // wanna be windows-safe
203
204 if (is_dir == FALSE)
205 {
206 for (i = m_Path.Length()-1; i >= 0; i--)
207 {
208 if (m_Path[(unsigned int) i] == _T('/'))
209 {
210 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == _T('/')) && (m_Path[(unsigned int) (i-2)] == _T(':')))
211 {
212 i -= 2;
213 continue;
214 }
215 else
216 {
217 pathpos = i;
218 break;
219 }
220 }
221 else if (m_Path[(unsigned int) i] == _T(':')) {
222 pathpos = i;
223 break;
224 }
225 }
226 if (pathpos == -1)
227 {
228 for (i = 0; i < (int) m_Path.Length(); i++)
229 {
230 if (m_Path[(unsigned int) i] == _T(':'))
231 {
232 //m_Path << _T('/');
233 m_Path.Remove(i+1);
234 break;
235 }
236 }
237 if (i == (int) m_Path.Length())
238 m_Path = wxEmptyString;
239 }
240 else
241 {
242 if (m_Path[m_Path.Length()-1] != _T('/'))
243 m_Path << _T('/');
244 m_Path.Remove(pathpos+1);
245 }
246 }
247 }
248
249
250
251 wxFSFile* 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;
261 for (i = 0; i < ln; i++)
262 {
263 if (loc[(unsigned int) i] == _T('\\')) loc.GetWritableChar(i) = _T('/'); // wanna be windows-safe
264 if (!meta) switch (loc[(unsigned int) i])
265 {
266 case _T('/') : case _T(':') : case _T('#') : meta = loc[(unsigned int) i];
267 }
268 }
269 m_LastName = wxEmptyString;
270
271 // try relative paths first :
272 if (meta != _T(':'))
273 {
274 node = m_Handlers.GetFirst();
275 while (node)
276 {
277 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
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; }
282 }
283 node = node->GetNext();
284 }
285 }
286
287 // if failed, try absolute paths :
288 if (s == NULL)
289 {
290 node = m_Handlers.GetFirst();
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; }
298 }
299 node = node->GetNext();
300 }
301 }
302 return (s);
303 }
304
305
306 void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
307 {
308 m_Handlers.Append(handler);
309 }
310
311
312
313 ///// Module:
314
315 class 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 }
325 virtual void OnExit()
326 {
327 wxFileSystemHandler::CleanUpStatics();
328 }
329 };
330
331 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
332
333 #endif
334 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
335
336
337