]>
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_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 | if (m_MimeMng == NULL) m_MimeMng = new wxMimeTypesManager; | |
61 | ft = m_MimeMng -> GetFileTypeFromExtension(ext); | |
62 | if (ft && (ft -> GetMimeType(&mime))) return mime; | |
63 | else return wxEmptyString; | |
64 | } | |
65 | ||
66 | ||
67 | ||
68 | wxString wxFileSystemHandler::GetProtocol(const wxString& location) const | |
69 | { | |
70 | wxString s = wxEmptyString; | |
71 | int i, l = location.Length(); | |
72 | bool fnd; | |
73 | ||
74 | fnd = FALSE; | |
75 | for (i = l-1; (i >= 0) && ((location[i] != _T('#')) || (!fnd)); i--) { | |
76 | if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; | |
77 | } | |
78 | if (!fnd) return _T("file"); | |
79 | for (++i; (i < l) && (location[i] != _T(':')); i++) s << location[i]; | |
80 | return s; | |
81 | } | |
82 | ||
83 | ||
84 | wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const | |
85 | { | |
86 | int i; | |
87 | bool fnd; | |
88 | ||
89 | fnd = FALSE; | |
90 | for (i = location.Length()-1; i >= 0; i--) { | |
91 | if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE; | |
92 | else if (fnd && (location[i] == _T('#'))) return location.Left(i); | |
93 | } | |
94 | return wxEmptyString; | |
95 | } | |
96 | ||
97 | wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const | |
98 | { | |
99 | int i, l = location.Length(); | |
100 | int l2 = l + 1; | |
101 | for (i = l-1; (i >= 0) && ((location[i] != _T(':')) || (i == 1) || (location[i-2] == _T(':'))); i--) {if (location[i] == _T('#')) l2 = i + 1;} | |
102 | if (i == 0) return wxEmptyString; | |
103 | else return location.Mid(i + 1, l2 - i - 2); | |
104 | } | |
105 | ||
106 | wxString wxFileSystemHandler::GetAnchor(const wxString& location) const | |
107 | { | |
108 | char c; | |
109 | int l = location.Length(); | |
110 | ||
111 | for (int i = l-1; i >= 0; i--) { | |
112 | c = location[i]; | |
113 | if (c == _T('#')) return location.Right(l-i-1); | |
114 | else if ((c == _T('.')) || (c == _T('/')) || (c == _T('\\')) || (c == _T(':'))) return wxEmptyString; | |
115 | } | |
116 | return wxEmptyString; | |
117 | } | |
118 | ||
119 | //-------------------------------------------------------------------------------- | |
120 | // wxLocalFSHandler | |
121 | //-------------------------------------------------------------------------------- | |
122 | ||
123 | class wxLocalFSHandler : public wxFileSystemHandler | |
124 | { | |
125 | public: | |
126 | virtual bool CanOpen(const wxString& location); | |
127 | virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
128 | }; | |
129 | ||
130 | ||
131 | bool wxLocalFSHandler::CanOpen(const wxString& location) | |
132 | { | |
133 | return GetProtocol(location) == _T("file"); | |
134 | } | |
135 | ||
136 | wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
137 | { | |
138 | wxString right = GetRightLocation(location); | |
139 | if (wxFileExists(right)) | |
140 | return new wxFSFile(new wxFileInputStream(right), | |
141 | right, | |
142 | GetMimeTypeFromExt(location), | |
143 | GetAnchor(location)); | |
144 | else return (wxFSFile*) NULL; | |
145 | } | |
146 | ||
147 | //----------------------------------------------------------------------------- | |
148 | // wxFileSystem | |
149 | //----------------------------------------------------------------------------- | |
150 | ||
151 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject) | |
152 | ||
153 | ||
154 | wxList wxFileSystem::m_Handlers; | |
155 | ||
156 | ||
157 | ||
158 | void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir) | |
159 | { | |
160 | int i, pathpos = -1; | |
161 | m_Path = location; | |
162 | ||
163 | for (i = m_Path.Length()-1; i >= 0; i--) | |
164 | if (m_Path[(unsigned int) i] == _T('\\')) m_Path.GetWritableChar(i) = _T('/'); // wanna be windows-safe | |
165 | ||
166 | if (is_dir == FALSE) | |
167 | { | |
168 | for (i = m_Path.Length()-1; i >= 0; i--) | |
169 | { | |
170 | if (m_Path[(unsigned int) i] == _T('/')) | |
171 | { | |
172 | if ((i > 1) && (m_Path[(unsigned int) (i-1)] == _T('/')) && (m_Path[(unsigned int) (i-2)] == _T(':'))) | |
173 | { | |
174 | i -= 2; | |
175 | continue; | |
176 | } | |
177 | else | |
178 | { | |
179 | pathpos = i; | |
180 | break; | |
181 | } | |
182 | } | |
183 | else if (m_Path[(unsigned int) i] == _T(':')) { | |
184 | pathpos = i; | |
185 | break; | |
186 | } | |
187 | } | |
188 | if (pathpos == -1) | |
189 | { | |
190 | for (i = 0; i < (int) m_Path.Length(); i++) | |
191 | { | |
192 | if (m_Path[(unsigned int) i] == _T(':')) | |
193 | { | |
194 | //m_Path << _T('/'); | |
195 | m_Path.Remove(i+1); | |
196 | break; | |
197 | } | |
198 | } | |
199 | if (i == (int) m_Path.Length()) | |
200 | m_Path = wxEmptyString; | |
201 | } | |
202 | else | |
203 | { | |
204 | if (m_Path[m_Path.Length()-1] != _T('/')) | |
205 | m_Path << _T('/'); | |
206 | m_Path.Remove(pathpos+1); | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | ||
212 | ||
213 | wxFSFile* wxFileSystem::OpenFile(const wxString& location) | |
214 | { | |
215 | wxString loc = location; | |
216 | int i, ln; | |
217 | char meta; | |
218 | wxFSFile *s = NULL; | |
219 | wxNode *node; | |
220 | ||
221 | ln = loc.Length(); | |
222 | meta = 0; | |
223 | for (i = 0; i < ln; i++) | |
224 | { | |
225 | if (loc[(unsigned int) i] == _T('\\')) loc.GetWritableChar(i) = _T('/'); // wanna be windows-safe | |
226 | if (!meta) switch (loc[(unsigned int) i]) | |
227 | { | |
228 | case _T('/') : case _T(':') : case _T('#') : meta = loc[(unsigned int) i]; | |
229 | } | |
230 | } | |
231 | m_LastName = wxEmptyString; | |
232 | ||
233 | // try relative paths first : | |
234 | if (meta != _T(':')) | |
235 | { | |
236 | node = m_Handlers.GetFirst(); | |
237 | while (node) | |
238 | { | |
239 | wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData(); | |
240 | if (h->CanOpen(m_Path + location)) | |
241 | { | |
242 | s = h->OpenFile(*this, m_Path + location); | |
243 | if (s) { m_LastName = m_Path + location; break; } | |
244 | } | |
245 | node = node->GetNext(); | |
246 | } | |
247 | } | |
248 | ||
249 | // if failed, try absolute paths : | |
250 | if (s == NULL) | |
251 | { | |
252 | node = m_Handlers.GetFirst(); | |
253 | while (node) | |
254 | { | |
255 | wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData(); | |
256 | if (h->CanOpen(location)) | |
257 | { | |
258 | s = h->OpenFile(*this, location); | |
259 | if (s) { m_LastName = location; break; } | |
260 | } | |
261 | node = node->GetNext(); | |
262 | } | |
263 | } | |
264 | return (s); | |
265 | } | |
266 | ||
267 | ||
268 | void wxFileSystem::AddHandler(wxFileSystemHandler *handler) | |
269 | { | |
270 | m_Handlers.Append(handler); | |
271 | } | |
272 | ||
273 | ||
274 | ||
275 | ///// Module: | |
276 | ||
277 | class wxFileSystemModule : public wxModule | |
278 | { | |
279 | DECLARE_DYNAMIC_CLASS(wxFileSystemModule) | |
280 | ||
281 | public: | |
282 | virtual bool OnInit() | |
283 | { | |
284 | wxFileSystem::AddHandler(new wxLocalFSHandler); | |
285 | return TRUE; | |
286 | } | |
287 | virtual void OnExit() | |
288 | { | |
289 | wxFileSystemHandler::CleanUpStatics(); | |
290 | } | |
291 | }; | |
292 | ||
293 | IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule) | |
294 | ||
295 | #endif | |
296 | // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS | |
297 | ||
298 | ||
299 |