]>
Commit | Line | Data |
---|---|---|
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_HTML || 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 == wxT('#')) l2 = i + 1; | |
57 | if (c == wxT('.')) {ext = loc.Right(l2-i-1); break;} | |
58 | if ((c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) {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))) { | |
101 | delete ft; | |
102 | return mime; | |
103 | } | |
104 | else { | |
105 | delete ft; | |
106 | return wxEmptyString; | |
107 | } | |
108 | } | |
109 | ||
110 | ||
111 | ||
112 | wxString wxFileSystemHandler::GetProtocol(const wxString& location) const | |
113 | { | |
114 | wxString s = wxEmptyString; | |
115 | int i, l = location.Length(); | |
116 | bool fnd; | |
117 | ||
118 | fnd = FALSE; | |
119 | for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) { | |
120 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; | |
121 | } | |
122 | if (!fnd) return wxT("file"); | |
123 | for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i]; | |
124 | return s; | |
125 | } | |
126 | ||
127 | ||
128 | wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const | |
129 | { | |
130 | int i; | |
131 | bool fnd; | |
132 | ||
133 | fnd = FALSE; | |
134 | for (i = location.Length()-1; i >= 0; i--) { | |
135 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; | |
136 | else if (fnd && (location[i] == wxT('#'))) return location.Left(i); | |
137 | } | |
138 | return wxEmptyString; | |
139 | } | |
140 | ||
141 | wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const | |
142 | { | |
143 | int i, l = location.Length(); | |
144 | int l2 = l + 1; | |
145 | for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;} | |
146 | if (i == 0) return wxEmptyString; | |
147 | else return location.Mid(i + 1, l2 - i - 2); | |
148 | } | |
149 | ||
150 | wxString wxFileSystemHandler::GetAnchor(const wxString& location) const | |
151 | { | |
152 | char c; | |
153 | int l = location.Length(); | |
154 | ||
155 | for (int i = l-1; i >= 0; i--) { | |
156 | c = location[i]; | |
157 | if (c == wxT('#')) return location.Right(l-i-1); | |
158 | else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString; | |
159 | } | |
160 | return wxEmptyString; | |
161 | } | |
162 | ||
163 | ||
164 | wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec), | |
165 | int WXUNUSED(flags)) | |
166 | { | |
167 | return wxEmptyString; | |
168 | } | |
169 | ||
170 | wxString wxFileSystemHandler::FindNext() | |
171 | { | |
172 | return wxEmptyString; | |
173 | } | |
174 | ||
175 | //-------------------------------------------------------------------------------- | |
176 | // wxLocalFSHandler | |
177 | //-------------------------------------------------------------------------------- | |
178 | ||
179 | class wxLocalFSHandler : public wxFileSystemHandler | |
180 | { | |
181 | public: | |
182 | virtual bool CanOpen(const wxString& location); | |
183 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
184 | virtual wxString FindFirst(const wxString& spec, int flags = 0); | |
185 | virtual wxString FindNext(); | |
186 | }; | |
187 | ||
188 | ||
189 | bool wxLocalFSHandler::CanOpen(const wxString& location) | |
190 | { | |
191 | return GetProtocol(location) == wxT("file"); | |
192 | } | |
193 | ||
194 | wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
195 | { | |
196 | wxString right = GetRightLocation(location); | |
197 | if (wxFileExists(right)) | |
198 | return new wxFSFile(new wxFileInputStream(right), | |
199 | right, | |
200 | GetMimeTypeFromExt(location), | |
201 | GetAnchor(location), | |
202 | wxDateTime(wxFileModificationTime(right))); | |
203 | else return (wxFSFile*) NULL; | |
204 | } | |
205 | ||
206 | wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags) | |
207 | { | |
208 | wxString right = GetRightLocation(spec); | |
209 | return wxFindFirstFile(right, flags); | |
210 | } | |
211 | ||
212 | wxString wxLocalFSHandler::FindNext() | |
213 | { | |
214 | return wxFindNextFile(); | |
215 | } | |
216 | ||
217 | ||
218 | ||
219 | //----------------------------------------------------------------------------- | |
220 | // wxFileSystem | |
221 | //----------------------------------------------------------------------------- | |
222 | ||
223 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject) | |
224 | ||
225 | ||
226 | wxList wxFileSystem::m_Handlers; | |
227 | ||
228 | ||
229 | ||
230 | void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir) | |
231 | { | |
232 | int i, pathpos = -1; | |
233 | m_Path = location; | |
234 | ||
235 | for (i = m_Path.Length()-1; i >= 0; i--) | |
236 | if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe | |
237 | ||
238 | if (is_dir) | |
239 | { | |
240 | if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':')) | |
241 | m_Path << wxT('/'); | |
242 | } | |
243 | ||
244 | else | |
245 | { | |
246 | for (i = m_Path.Length()-1; i >= 0; i--) | |
247 | { | |
248 | if (m_Path[(unsigned int) i] == wxT('/')) | |
249 | { | |
250 | if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':'))) | |
251 | { | |
252 | i -= 2; | |
253 | continue; | |
254 | } | |
255 | else | |
256 | { | |
257 | pathpos = i; | |
258 | break; | |
259 | } | |
260 | } | |
261 | else if (m_Path[(unsigned int) i] == wxT(':')) { | |
262 | pathpos = i; | |
263 | break; | |
264 | } | |
265 | } | |
266 | if (pathpos == -1) | |
267 | { | |
268 | for (i = 0; i < (int) m_Path.Length(); i++) | |
269 | { | |
270 | if (m_Path[(unsigned int) i] == wxT(':')) | |
271 | { | |
272 | m_Path.Remove(i+1); | |
273 | break; | |
274 | } | |
275 | } | |
276 | if (i == (int) m_Path.Length()) | |
277 | m_Path = wxEmptyString; | |
278 | } | |
279 | else | |
280 | { | |
281 | m_Path.Remove(pathpos+1); | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
286 | ||
287 | ||
288 | wxFSFile* wxFileSystem::OpenFile(const wxString& location) | |
289 | { | |
290 | wxString loc = location; | |
291 | int i, ln; | |
292 | char meta; | |
293 | wxFSFile *s = NULL; | |
294 | wxNode *node; | |
295 | ||
296 | ln = loc.Length(); | |
297 | meta = 0; | |
298 | for (i = 0; i < ln; i++) | |
299 | { | |
300 | if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe | |
301 | if (!meta) | |
302 | switch (loc[(unsigned int) i]) | |
303 | { | |
304 | case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i]; | |
305 | } | |
306 | } | |
307 | m_LastName = wxEmptyString; | |
308 | ||
309 | // try relative paths first : | |
310 | if (meta != wxT(':')) | |
311 | { | |
312 | node = m_Handlers.GetFirst(); | |
313 | while (node) | |
314 | { | |
315 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); | |
316 | if (h->CanOpen(m_Path + loc)) | |
317 | { | |
318 | s = h->OpenFile(*this, m_Path + loc); | |
319 | if (s) { m_LastName = m_Path + loc; break; } | |
320 | } | |
321 | node = node->GetNext(); | |
322 | } | |
323 | } | |
324 | ||
325 | // if failed, try absolute paths : | |
326 | if (s == NULL) | |
327 | { | |
328 | node = m_Handlers.GetFirst(); | |
329 | while (node) | |
330 | { | |
331 | wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData(); | |
332 | if (h->CanOpen(loc)) | |
333 | { | |
334 | s = h->OpenFile(*this, loc); | |
335 | if (s) { m_LastName = loc; break; } | |
336 | } | |
337 | node = node->GetNext(); | |
338 | } | |
339 | } | |
340 | return (s); | |
341 | } | |
342 | ||
343 | ||
344 | ||
345 | wxString wxFileSystem::FindFirst(const wxString& spec, int flags) | |
346 | { | |
347 | wxNode *node; | |
348 | wxString spec2(spec); | |
349 | ||
350 | m_FindFileHandler = NULL; | |
351 | ||
352 | for (int i = spec2.Length()-1; i >= 0; i--) | |
353 | if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // wanna be windows-safe | |
354 | ||
355 | node = m_Handlers.GetFirst(); | |
356 | while (node) | |
357 | { | |
358 | m_FindFileHandler = (wxFileSystemHandler*) node -> GetData(); | |
359 | if (m_FindFileHandler -> CanOpen(m_Path + spec2)) | |
360 | return m_FindFileHandler -> FindFirst(m_Path + spec2, flags); | |
361 | node = node->GetNext(); | |
362 | } | |
363 | ||
364 | node = m_Handlers.GetFirst(); | |
365 | while (node) | |
366 | { | |
367 | m_FindFileHandler = (wxFileSystemHandler*) node -> GetData(); | |
368 | if (m_FindFileHandler -> CanOpen(spec2)) | |
369 | return m_FindFileHandler -> FindFirst(spec2, flags); | |
370 | node = node->GetNext(); | |
371 | } | |
372 | ||
373 | return wxEmptyString; | |
374 | } | |
375 | ||
376 | ||
377 | ||
378 | wxString wxFileSystem::FindNext() | |
379 | { | |
380 | if (m_FindFileHandler == NULL) return wxEmptyString; | |
381 | else return m_FindFileHandler -> FindNext(); | |
382 | } | |
383 | ||
384 | ||
385 | ||
386 | void wxFileSystem::AddHandler(wxFileSystemHandler *handler) | |
387 | { | |
388 | m_Handlers.Append(handler); | |
389 | } | |
390 | ||
391 | ||
392 | void wxFileSystem::CleanUpHandlers() | |
393 | { | |
394 | m_Handlers.DeleteContents(TRUE); | |
395 | m_Handlers.Clear(); | |
396 | } | |
397 | ||
398 | ||
399 | ||
400 | ||
401 | ///// Module: | |
402 | ||
403 | class wxFileSystemModule : public wxModule | |
404 | { | |
405 | DECLARE_DYNAMIC_CLASS(wxFileSystemModule) | |
406 | ||
407 | public: | |
408 | virtual bool OnInit() | |
409 | { | |
410 | wxFileSystem::AddHandler(new wxLocalFSHandler); | |
411 | return TRUE; | |
412 | } | |
413 | virtual void OnExit() | |
414 | { | |
415 | wxFileSystemHandler::CleanUpStatics(); | |
416 | wxFileSystem::CleanUpHandlers(); | |
417 | } | |
418 | }; | |
419 | ||
420 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule) | |
421 | ||
422 | #endif | |
423 | // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS | |
424 | ||
425 | ||
426 |