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