]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
Commented setsockopt as it doesn't work anywhere. I will use signals for timeout.
[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 19#if !wxUSE_SOCKETS
31528cd3 20 #undef wxUSE_FS_INET
31528cd3
VZ
21 #define wxUSE_FS_INET 0
22#endif
23
d30e0edd 24#if (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 25
d30e0edd
RR
26#include "wx/wfstream.h"
27#include "wx/module.h"
28#include "wx/filesys.h"
5526e819
VS
29
30//--------------------------------------------------------------------------------
31// wxFileSystemHandler
32//--------------------------------------------------------------------------------
33
34IMPLEMENT_ABSTRACT_CLASS(wxFileSystemHandler, wxObject)
35
a76015e6
VS
36wxMimeTypesManager *wxFileSystemHandler::m_MimeMng = NULL;
37
38void wxFileSystemHandler::CleanUpStatics()
39{
40 if (m_MimeMng) delete m_MimeMng;
41 m_MimeMng = NULL;
42}
5526e819
VS
43
44
45wxString 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--) {
ea4f5235 55 c = loc[(unsigned int) i];
d30e0edd
RR
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;}
5526e819 59 }
a76015e6
VS
60 if (m_MimeMng == NULL) m_MimeMng = new wxMimeTypesManager;
61 ft = m_MimeMng -> GetFileTypeFromExtension(ext);
5526e819
VS
62 if (ft && (ft -> GetMimeType(&mime))) return mime;
63 else return wxEmptyString;
64}
65
66
67
68wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
69{
70 wxString s = wxEmptyString;
71 int i, l = location.Length();
72 bool fnd;
73
74 fnd = FALSE;
d30e0edd
RR
75 for (i = l-1; (i >= 0) && ((location[i] != _T('#')) || (!fnd)); i--) {
76 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 77 }
d30e0edd
RR
78 if (!fnd) return _T("file");
79 for (++i; (i < l) && (location[i] != _T(':')); i++) s << location[i];
5526e819
VS
80 return s;
81}
82
83
5526e819
VS
84wxString 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--) {
d30e0edd
RR
91 if ((location[i] == _T(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
92 else if (fnd && (location[i] == _T('#'))) return location.Left(i);
5526e819
VS
93 }
94 return wxEmptyString;
95}
96
5526e819
VS
97wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
98{
99 int i, l = location.Length();
100 int l2 = l + 1;
d30e0edd 101 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
102 if (i == 0) return wxEmptyString;
103 else return location.Mid(i + 1, l2 - i - 2);
104}
105
5526e819
VS
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];
d30e0edd
RR
113 if (c == _T('#')) return location.Right(l-i-1);
114 else if ((c == _T('.')) || (c == _T('/')) || (c == _T('\\')) || (c == _T(':'))) return wxEmptyString;
5526e819
VS
115 }
116 return wxEmptyString;
117}
118
5526e819
VS
119//--------------------------------------------------------------------------------
120// wxLocalFSHandler
121//--------------------------------------------------------------------------------
122
123class 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
5526e819
VS
131bool wxLocalFSHandler::CanOpen(const wxString& location)
132{
d30e0edd 133 return GetProtocol(location) == _T("file");
5526e819
VS
134}
135
5526e819
VS
136wxFSFile* 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));
d30e0edd 144 else return (wxFSFile*) NULL;
5526e819
VS
145}
146
5526e819
VS
147//-----------------------------------------------------------------------------
148// wxFileSystem
149//-----------------------------------------------------------------------------
150
151IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
152
153
154wxList wxFileSystem::m_Handlers;
155
156
157
158void 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--)
ea4f5235 164 if (m_Path[(unsigned int) i] == _T('\\')) m_Path.GetWritableChar(i) = _T('/'); // wanna be windows-safe
d30e0edd
RR
165
166 if (is_dir == FALSE)
167 {
168 for (i = m_Path.Length()-1; i >= 0; i--)
169 {
ea4f5235 170 if (m_Path[(unsigned int) i] == _T('/'))
d30e0edd 171 {
ea4f5235 172 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == _T('/')) && (m_Path[(unsigned int) (i-2)] == _T(':')))
d30e0edd 173 {
5526e819
VS
174 i -= 2;
175 continue;
176 }
d30e0edd
RR
177 else
178 {
5526e819
VS
179 pathpos = i;
180 break;
181 }
182 }
ea4f5235 183 else if (m_Path[(unsigned int) i] == _T(':')) {
5526e819
VS
184 pathpos = i;
185 break;
186 }
187 }
d30e0edd
RR
188 if (pathpos == -1)
189 {
190 for (i = 0; i < (int) m_Path.Length(); i++)
191 {
ea4f5235 192 if (m_Path[(unsigned int) i] == _T(':'))
d30e0edd
RR
193 {
194 //m_Path << _T('/');
5526e819
VS
195 m_Path.Remove(i+1);
196 break;
197 }
198 }
d30e0edd
RR
199 if (i == (int) m_Path.Length())
200 m_Path = wxEmptyString;
5526e819 201 }
d30e0edd
RR
202 else
203 {
204 if (m_Path[m_Path.Length()-1] != _T('/'))
205 m_Path << _T('/');
5526e819
VS
206 m_Path.Remove(pathpos+1);
207 }
208 }
209}
210
211
212
213wxFSFile* 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;
d30e0edd
RR
223 for (i = 0; i < ln; i++)
224 {
ea4f5235
JS
225 if (loc[(unsigned int) i] == _T('\\')) loc.GetWritableChar(i) = _T('/'); // wanna be windows-safe
226 if (!meta) switch (loc[(unsigned int) i])
d30e0edd 227 {
ea4f5235 228 case _T('/') : case _T(':') : case _T('#') : meta = loc[(unsigned int) i];
5526e819
VS
229 }
230 }
231 m_LastName = wxEmptyString;
232
233 // try relative paths first :
d30e0edd
RR
234 if (meta != _T(':'))
235 {
5526e819 236 node = m_Handlers.GetFirst();
d30e0edd
RR
237 while (node)
238 {
5526e819 239 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
d30e0edd
RR
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; }
5526e819 244 }
d30e0edd 245 node = node->GetNext();
5526e819
VS
246 }
247 }
248
249 // if failed, try absolute paths :
d30e0edd
RR
250 if (s == NULL)
251 {
5526e819 252 node = m_Handlers.GetFirst();
d30e0edd
RR
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; }
5526e819 260 }
d30e0edd 261 node = node->GetNext();
5526e819
VS
262 }
263 }
264 return (s);
265}
266
267
5526e819
VS
268void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
269{
270 m_Handlers.Append(handler);
271}
272
273
274
5526e819
VS
275///// Module:
276
277class 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 }
a76015e6
VS
287 virtual void OnExit()
288 {
289 wxFileSystemHandler::CleanUpStatics();
290 }
5526e819
VS
291};
292
293IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
294
d30e0edd
RR
295#endif
296 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 297
a76015e6
VS
298
299