]>
Commit | Line | Data |
---|---|---|
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 | |
5be0cf65 | 6 | // CVS-ID: $Id$ |
5526e819 VS |
7 | // Licence: wxWindows Licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation | |
12 | #endif | |
13 | ||
d30e0edd | 14 | #include "wx/wxprec.h" |
5526e819 VS |
15 | |
16 | #ifdef __BORDLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
31528cd3 | 20 | |
24528b0c | 21 | #if wxUSE_FILESYSTEM |
5526e819 | 22 | |
d30e0edd RR |
23 | #include "wx/wfstream.h" |
24 | #include "wx/module.h" | |
25 | #include "wx/filesys.h" | |
73725567 VS |
26 | #include "wx/mimetype.h" |
27 | ||
28 | ||
29 | ||
5526e819 VS |
30 | |
31 | //-------------------------------------------------------------------------------- | |
32 | // wxFileSystemHandler | |
33 | //-------------------------------------------------------------------------------- | |
34 | ||
35 | IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject) | |
36 | ||
5526e819 VS |
37 | |
38 | wxString wxFileSystemHandler::GetMimeTypeFromExt(const wxString& location) | |
39 | { | |
40 | wxString ext = wxEmptyString, mime = wxEmptyString; | |
41 | wxString loc = GetRightLocation(location); | |
42 | char c; | |
43 | int l = loc.Length(), l2; | |
44 | wxFileType *ft; | |
45 | ||
46 | l2 = l; | |
47 | for (int i = l-1; i >= 0; i--) { | |
ea4f5235 | 48 | c = loc[(unsigned int) i]; |
223d09f6 KB |
49 | if (c == wxT('#')) l2 = i + 1; |
50 | if (c == wxT('.')) {ext = loc.Right(l2-i-1); break;} | |
51 | if ((c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) {return wxEmptyString;} | |
5526e819 | 52 | } |
956418ab | 53 | |
73725567 VS |
54 | static bool s_MinimalMimeEnsured = FALSE; |
55 | if (!s_MinimalMimeEnsured) { | |
956418ab VS |
56 | static const wxFileTypeInfo fallbacks[] = |
57 | { | |
58 | wxFileTypeInfo("image/jpeg", | |
59 | "", | |
60 | "", | |
61 | "JPEG image (from fallback)", | |
62 | "jpg", "jpeg", NULL), | |
63 | wxFileTypeInfo("image/gif", | |
64 | "", | |
65 | "", | |
66 | "GIF image (from fallback)", | |
67 | "gif", NULL), | |
68 | wxFileTypeInfo("image/png", | |
69 | "", | |
70 | "", | |
71 | "PNG image (from fallback)", | |
72 | "png", NULL), | |
73 | wxFileTypeInfo("image/bmp", | |
74 | "", | |
75 | "", | |
76 | "windows bitmap image (from fallback)", | |
77 | "bmp", NULL), | |
78 | wxFileTypeInfo("text/html", | |
79 | "", | |
80 | "", | |
81 | "HTML document (from fallback)", | |
82 | "htm", "html", NULL), | |
83 | ||
84 | // must terminate the table with this! | |
85 | wxFileTypeInfo() | |
86 | }; | |
87 | ||
73725567 | 88 | wxTheMimeTypesManager -> AddFallbacks(fallbacks); |
956418ab VS |
89 | } |
90 | ||
73725567 | 91 | ft = wxTheMimeTypesManager -> GetFileTypeFromExtension(ext); |
f6bcfd97 BP |
92 | if ( !ft || !ft -> GetMimeType(&mime) ) { |
93 | mime = wxEmptyString; | |
51f79d5c | 94 | } |
f6bcfd97 BP |
95 | |
96 | delete ft; | |
97 | ||
98 | return mime; | |
5526e819 VS |
99 | } |
100 | ||
101 | ||
102 | ||
103 | wxString wxFileSystemHandler::GetProtocol(const wxString& location) const | |
104 | { | |
105 | wxString s = wxEmptyString; | |
106 | int i, l = location.Length(); | |
107 | bool fnd; | |
108 | ||
109 | fnd = FALSE; | |
223d09f6 KB |
110 | for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) { |
111 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; | |
5526e819 | 112 | } |
223d09f6 KB |
113 | if (!fnd) return wxT("file"); |
114 | for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i]; | |
5526e819 VS |
115 | return s; |
116 | } | |
117 | ||
118 | ||
5526e819 VS |
119 | wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const |
120 | { | |
121 | int i; | |
122 | bool fnd; | |
123 | ||
124 | fnd = FALSE; | |
125 | for (i = location.Length()-1; i >= 0; i--) { | |
223d09f6 KB |
126 | if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; |
127 | else if (fnd && (location[i] == wxT('#'))) return location.Left(i); | |
5526e819 VS |
128 | } |
129 | return wxEmptyString; | |
130 | } | |
131 | ||
5526e819 VS |
132 | wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const |
133 | { | |
134 | int i, l = location.Length(); | |
135 | int l2 = l + 1; | |
223d09f6 | 136 | for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;} |
5526e819 VS |
137 | if (i == 0) return wxEmptyString; |
138 | else return location.Mid(i + 1, l2 - i - 2); | |
139 | } | |
140 | ||
5526e819 VS |
141 | wxString wxFileSystemHandler::GetAnchor(const wxString& location) const |
142 | { | |
143 | char c; | |
144 | int l = location.Length(); | |
145 | ||
146 | for (int i = l-1; i >= 0; i--) { | |
147 | c = location[i]; | |
223d09f6 KB |
148 | if (c == wxT('#')) return location.Right(l-i-1); |
149 | else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString; | |
5526e819 VS |
150 | } |
151 | return wxEmptyString; | |
152 | } | |
153 | ||
aaa66113 | 154 | |
f76dbc4d VZ |
155 | wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec), |
156 | int WXUNUSED(flags)) | |
157 | { | |
158 | return wxEmptyString; | |
159 | } | |
aaa66113 | 160 | |
f76dbc4d VZ |
161 | wxString wxFileSystemHandler::FindNext() |
162 | { | |
163 | return wxEmptyString; | |
164 | } | |
aaa66113 | 165 | |
5526e819 VS |
166 | //-------------------------------------------------------------------------------- |
167 | // wxLocalFSHandler | |
168 | //-------------------------------------------------------------------------------- | |
169 | ||
170 | class wxLocalFSHandler : public wxFileSystemHandler | |
171 | { | |
172 | public: | |
173 | virtual bool CanOpen(const wxString& location); | |
174 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
aaa66113 VS |
175 | virtual wxString FindFirst(const wxString& spec, int flags = 0); |
176 | virtual wxString FindNext(); | |
5526e819 VS |
177 | }; |
178 | ||
179 | ||
5526e819 VS |
180 | bool wxLocalFSHandler::CanOpen(const wxString& location) |
181 | { | |
223d09f6 | 182 | return GetProtocol(location) == wxT("file"); |
5526e819 VS |
183 | } |
184 | ||
5526e819 VS |
185 | wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
186 | { | |
187 | wxString right = GetRightLocation(location); | |
f6bcfd97 BP |
188 | if (!wxFileExists(right)) |
189 | return (wxFSFile*) NULL; | |
190 | ||
191 | return new wxFSFile(new wxFileInputStream(right), | |
192 | right, | |
193 | GetMimeTypeFromExt(location), | |
194 | GetAnchor(location), | |
195 | wxDateTime(wxFileModificationTime(right))); | |
196 | ||
5526e819 VS |
197 | } |
198 | ||
aaa66113 VS |
199 | wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags) |
200 | { | |
201 | wxString right = GetRightLocation(spec); | |
202 | return wxFindFirstFile(right, flags); | |
203 | } | |
204 | ||
205 | wxString wxLocalFSHandler::FindNext() | |
206 | { | |
207 | return wxFindNextFile(); | |
208 | } | |
209 | ||
210 | ||
211 | ||
5526e819 VS |
212 | //----------------------------------------------------------------------------- |
213 | // wxFileSystem | |
214 | //----------------------------------------------------------------------------- | |
215 | ||
216 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject) | |
217 | ||
218 | ||
219 | wxList wxFileSystem::m_Handlers; | |
220 | ||
221 | ||
5be0cf65 VS |
222 | static wxString MakeCorrectPath(const wxString& path) |
223 | { | |
224 | wxString p(path); | |
225 | wxString r; | |
226 | int i, j, cnt; | |
227 | ||
228 | cnt = p.Length(); | |
229 | for (i = 0; i < cnt; i++) | |
f6081a04 | 230 | if (p.GetChar(i) == wxT('\\')) p.GetWritableChar(i) = wxT('/'); // Want to be windows-safe |
5be0cf65 VS |
231 | |
232 | if (p.Left(2) == wxT("./")) { p = p.Mid(2); cnt -= 2; } | |
233 | ||
234 | if (cnt < 3) return p; | |
235 | ||
b2f60e03 | 236 | r << p.GetChar(0) << p.GetChar(1); |
5be0cf65 VS |
237 | |
238 | // skip trailing ../.., if any | |
b2f60e03 | 239 | for (i = 2; i < cnt && (p.GetChar(i) == wxT('/') || p.GetChar(i) == wxT('.')); i++) r << p.GetChar(i); |
5be0cf65 VS |
240 | |
241 | // remove back references: translate dir1/../dir2 to dir2 | |
242 | for (; i < cnt; i++) | |
243 | { | |
b2f60e03 BJ |
244 | r << p.GetChar(i); |
245 | if (p.GetChar(i) == wxT('/') && p.GetChar(i-1) == wxT('.') && p.GetChar(i-2) == wxT('.')) | |
5be0cf65 | 246 | { |
b2f60e03 BJ |
247 | for (j = r.Length() - 2; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {} |
248 | if (j >= 0 && r.GetChar(j) != wxT(':')) | |
5be0cf65 | 249 | { |
b2f60e03 | 250 | for (j = j - 1; j >= 0 && r.GetChar(j) != wxT('/') && r.GetChar(j) != wxT(':'); j--) {} |
5be0cf65 VS |
251 | r.Remove(j + 1); |
252 | } | |
253 | } | |
254 | } | |
255 | ||
b2f60e03 | 256 | for (; i < cnt; i++) r << p.GetChar(i); |
5be0cf65 VS |
257 | |
258 | return r; | |
259 | } | |
260 | ||
5526e819 VS |
261 | |
262 | void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir) | |
263 | { | |
264 | int i, pathpos = -1; | |
5526e819 | 265 | |
5be0cf65 | 266 | m_Path = MakeCorrectPath(location); |
d30e0edd | 267 | |
aaa66113 VS |
268 | if (is_dir) |
269 | { | |
270 | if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':')) | |
271 | m_Path << wxT('/'); | |
272 | } | |
273 | ||
274 | else | |
d30e0edd | 275 | { |
269e8200 | 276 | for (i = m_Path.Length()-1; i >= 0; i--) |
aaa66113 | 277 | { |
223d09f6 | 278 | if (m_Path[(unsigned int) i] == wxT('/')) |
aaa66113 | 279 | { |
223d09f6 | 280 | if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':'))) |
aaa66113 | 281 | { |
5526e819 VS |
282 | i -= 2; |
283 | continue; | |
284 | } | |
269e8200 | 285 | else |
aaa66113 | 286 | { |
269e8200 | 287 | pathpos = i; |
5526e819 VS |
288 | break; |
289 | } | |
290 | } | |
aaa66113 VS |
291 | else if (m_Path[(unsigned int) i] == wxT(':')) { |
292 | pathpos = i; | |
293 | break; | |
294 | } | |
5526e819 | 295 | } |
269e8200 | 296 | if (pathpos == -1) |
aaa66113 | 297 | { |
269e8200 | 298 | for (i = 0; i < (int) m_Path.Length(); i++) |
aaa66113 | 299 | { |
223d09f6 | 300 | if (m_Path[(unsigned int) i] == wxT(':')) |
aaa66113 | 301 | { |
5526e819 VS |
302 | m_Path.Remove(i+1); |
303 | break; | |
304 | } | |
305 | } | |
269e8200 | 306 | if (i == (int) m_Path.Length()) |
aaa66113 | 307 | m_Path = wxEmptyString; |
5526e819 | 308 | } |
269e8200 | 309 | else |
aaa66113 | 310 | { |
5526e819 VS |
311 | m_Path.Remove(pathpos+1); |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | ||
317 | ||
318 | wxFSFile* wxFileSystem::OpenFile(const wxString& location) | |
319 | { | |
5be0cf65 VS |
320 | wxString loc = MakeCorrectPath(location); |
321 | unsigned i, ln; | |
5526e819 VS |
322 | char meta; |
323 | wxFSFile *s = NULL; | |
324 | wxNode *node; | |
325 | ||
326 | ln = loc.Length(); | |
327 | meta = 0; | |
269e8200 | 328 | for (i = 0; i < ln; i++) |
d30e0edd | 329 | { |
aaa66113 | 330 | if (!meta) |
5be0cf65 | 331 | switch (loc[i]) |
aaa66113 | 332 | { |
5be0cf65 | 333 | case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[i]; |
aaa66113 | 334 | } |
5526e819 VS |
335 | } |
336 | m_LastName = wxEmptyString; | |
337 | ||
338 | // try relative paths first : | |
223d09f6 | 339 | if (meta != wxT(':')) |
d30e0edd | 340 | { |
5526e819 | 341 | node = m_Handlers.GetFirst(); |
d30e0edd | 342 | while (node) |
aaa66113 | 343 | { |
5526e819 | 344 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); |
7dee4b2b | 345 | if (h->CanOpen(m_Path + loc)) |
aaa66113 | 346 | { |
7dee4b2b VS |
347 | s = h->OpenFile(*this, m_Path + loc); |
348 | if (s) { m_LastName = m_Path + loc; break; } | |
5526e819 | 349 | } |
d30e0edd | 350 | node = node->GetNext(); |
5526e819 VS |
351 | } |
352 | } | |
353 | ||
354 | // if failed, try absolute paths : | |
269e8200 | 355 | if (s == NULL) |
d30e0edd | 356 | { |
5526e819 | 357 | node = m_Handlers.GetFirst(); |
d30e0edd | 358 | while (node) |
aaa66113 | 359 | { |
d30e0edd | 360 | wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData(); |
7dee4b2b | 361 | if (h->CanOpen(loc)) |
aaa66113 | 362 | { |
7dee4b2b VS |
363 | s = h->OpenFile(*this, loc); |
364 | if (s) { m_LastName = loc; break; } | |
5526e819 | 365 | } |
d30e0edd | 366 | node = node->GetNext(); |
5526e819 VS |
367 | } |
368 | } | |
369 | return (s); | |
370 | } | |
371 | ||
372 | ||
aaa66113 VS |
373 | |
374 | wxString wxFileSystem::FindFirst(const wxString& spec, int flags) | |
375 | { | |
376 | wxNode *node; | |
377 | wxString spec2(spec); | |
378 | ||
379 | m_FindFileHandler = NULL; | |
380 | ||
381 | for (int i = spec2.Length()-1; i >= 0; i--) | |
f6081a04 | 382 | if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // Want to be windows-safe |
aaa66113 VS |
383 | |
384 | node = m_Handlers.GetFirst(); | |
385 | while (node) | |
386 | { | |
387 | m_FindFileHandler = (wxFileSystemHandler*) node -> GetData(); | |
388 | if (m_FindFileHandler -> CanOpen(m_Path + spec2)) | |
389 | return m_FindFileHandler -> FindFirst(m_Path + spec2, flags); | |
390 | node = node->GetNext(); | |
391 | } | |
392 | ||
393 | node = m_Handlers.GetFirst(); | |
394 | while (node) | |
395 | { | |
396 | m_FindFileHandler = (wxFileSystemHandler*) node -> GetData(); | |
397 | if (m_FindFileHandler -> CanOpen(spec2)) | |
398 | return m_FindFileHandler -> FindFirst(spec2, flags); | |
399 | node = node->GetNext(); | |
400 | } | |
401 | ||
402 | return wxEmptyString; | |
403 | } | |
404 | ||
405 | ||
406 | ||
407 | wxString wxFileSystem::FindNext() | |
408 | { | |
409 | if (m_FindFileHandler == NULL) return wxEmptyString; | |
410 | else return m_FindFileHandler -> FindNext(); | |
411 | } | |
412 | ||
413 | ||
414 | ||
5526e819 VS |
415 | void wxFileSystem::AddHandler(wxFileSystemHandler *handler) |
416 | { | |
417 | m_Handlers.Append(handler); | |
418 | } | |
419 | ||
420 | ||
269e8200 RD |
421 | void wxFileSystem::CleanUpHandlers() |
422 | { | |
423 | m_Handlers.DeleteContents(TRUE); | |
424 | m_Handlers.Clear(); | |
425 | } | |
426 | ||
5526e819 | 427 | |
aaa66113 VS |
428 | |
429 | ||
5526e819 VS |
430 | ///// Module: |
431 | ||
432 | class wxFileSystemModule : public wxModule | |
433 | { | |
434 | DECLARE_DYNAMIC_CLASS(wxFileSystemModule) | |
435 | ||
436 | public: | |
437 | virtual bool OnInit() | |
438 | { | |
439 | wxFileSystem::AddHandler(new wxLocalFSHandler); | |
440 | return TRUE; | |
441 | } | |
269e8200 | 442 | virtual void OnExit() |
aaa66113 | 443 | { |
269e8200 | 444 | wxFileSystem::CleanUpHandlers(); |
aaa66113 | 445 | } |
5526e819 VS |
446 | }; |
447 | ||
448 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule) | |
449 | ||
269e8200 | 450 | #endif |
24528b0c | 451 | // wxUSE_FILESYSTEM |
5526e819 | 452 | |
a76015e6 VS |
453 | |
454 |