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