]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
use of div_t.quot and div_t.rem were changed to / and %
[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
d30e0edd 13#include "wx/wxprec.h"
5526e819
VS
14
15#ifdef __BORDLANDC__
16#pragma hdrstop
17#endif
18
31528cd3
VZ
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
d30e0edd 26#if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 27
d30e0edd
RR
28#include "wx/wfstream.h"
29#include "wx/module.h"
30#include "wx/filesys.h"
5526e819
VS
31
32//--------------------------------------------------------------------------------
33// wxFileSystemHandler
34//--------------------------------------------------------------------------------
35
36IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
37
a76015e6
VS
38wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL;
39
40void wxFileSystemHandler::CleanUpStatics()
41{
42 if (m_MimeMng) delete m_MimeMng;
43 m_MimeMng = NULL;
44}
5526e819
VS
45
46
47wxString 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];
d30e0edd
RR
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;}
5526e819 61 }
a76015e6
VS
62 if (m_MimeMng == NULL) m_MimeMng = new wxMimeTypesManager;
63 ft = m_MimeMng -> GetFileTypeFromExtension(ext);
5526e819
VS
64 if (ft && (ft -> GetMimeType(&mime))) return mime;
65 else return wxEmptyString;
66}
67
68
69
70wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
71{
72 wxString s = wxEmptyString;
73 int i, l = location.Length();
74 bool fnd;
75
76 fnd = FALSE;
d30e0edd
RR
77 for (i = l-1; (i >= 0) && ((location[i] != _T('#')) || (!fnd)); i--) {
78 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 79 }
d30e0edd
RR
80 if (!fnd) return _T("file");
81 for (++i; (i < l) && (location[i] != _T(':')); i++) s << location[i];
5526e819
VS
82 return s;
83}
84
85
5526e819
VS
86wxString 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--) {
d30e0edd
RR
93 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
94 else if (fnd && (location[i] == _T('#'))) return location.Left(i);
5526e819
VS
95 }
96 return wxEmptyString;
97}
98
5526e819
VS
99wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
100{
101 int i, l = location.Length();
102 int l2 = l + 1;
d30e0edd 103 for (i = l-1; (i >= 0) && ((location[i] != _T(':')) || (i == 1) || (location[i-2] == _T(':'))); i--) {if (location[i] == _T('#')) l2 = i + 1;}
5526e819
VS
104 if (i == 0) return wxEmptyString;
105 else return location.Mid(i + 1, l2 - i - 2);
106}
107
5526e819
VS
108wxString 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];
d30e0edd
RR
115 if (c == _T('#')) return location.Right(l-i-1);
116 else if ((c == _T('.')) || (c == _T('/')) || (c == _T('\\')) || (c == _T(':'))) return wxEmptyString;
5526e819
VS
117 }
118 return wxEmptyString;
119}
120
5526e819
VS
121//--------------------------------------------------------------------------------
122// wxLocalFSHandler
123//--------------------------------------------------------------------------------
124
125class 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
5526e819
VS
133bool wxLocalFSHandler::CanOpen(const wxString& location)
134{
d30e0edd 135 return GetProtocol(location) == _T("file");
5526e819
VS
136}
137
5526e819
VS
138wxFSFile* 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));
d30e0edd 146 else return (wxFSFile*) NULL;
5526e819
VS
147}
148
5526e819
VS
149//-----------------------------------------------------------------------------
150// wxFileSystem
151//-----------------------------------------------------------------------------
152
153IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
154
155
156wxList wxFileSystem::m_Handlers;
157
158
159
160void 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--)
d30e0edd
RR
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 {
5526e819
VS
176 i -= 2;
177 continue;
178 }
d30e0edd
RR
179 else
180 {
5526e819
VS
181 pathpos = i;
182 break;
183 }
184 }
d30e0edd 185 else if (m_Path[i] == _T(':')) {
5526e819
VS
186 pathpos = i;
187 break;
188 }
189 }
d30e0edd
RR
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('/');
5526e819
VS
197 m_Path.Remove(i+1);
198 break;
199 }
200 }
d30e0edd
RR
201 if (i == (int) m_Path.Length())
202 m_Path = wxEmptyString;
5526e819 203 }
d30e0edd
RR
204 else
205 {
206 if (m_Path[m_Path.Length()-1] != _T('/'))
207 m_Path << _T('/');
5526e819
VS
208 m_Path.Remove(pathpos+1);
209 }
210 }
211}
212
213
214
215wxFSFile* 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;
d30e0edd
RR
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];
5526e819
VS
231 }
232 }
233 m_LastName = wxEmptyString;
234
235 // try relative paths first :
d30e0edd
RR
236 if (meta != _T(':'))
237 {
5526e819 238 node = m_Handlers.GetFirst();
d30e0edd
RR
239 while (node)
240 {
5526e819 241 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
d30e0edd
RR
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; }
5526e819 246 }
d30e0edd 247 node = node->GetNext();
5526e819
VS
248 }
249 }
250
251 // if failed, try absolute paths :
d30e0edd
RR
252 if (s == NULL)
253 {
5526e819 254 node = m_Handlers.GetFirst();
d30e0edd
RR
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; }
5526e819 262 }
d30e0edd 263 node = node->GetNext();
5526e819
VS
264 }
265 }
266 return (s);
267}
268
269
5526e819
VS
270void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
271{
272 m_Handlers.Append(handler);
273}
274
275
276
5526e819
VS
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 }
a76015e6
VS
289 virtual void OnExit()
290 {
291 wxFileSystemHandler::CleanUpStatics();
292 }
5526e819
VS
293};
294
295IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
296
d30e0edd
RR
297#endif
298 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 299
a76015e6
VS
300
301