]> git.saurik.com Git - wxWidgets.git/blame - src/common/filesys.cpp
added some utils (tex2rtf, helpgen, makegen) to make system
[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
f22e0c3c 24#if (wxUSE_HTML || 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];
223d09f6
KB
56 if (c == wxT('#')) l2 = i + 1;
57 if (c == wxT('.')) {ext = loc.Right(l2-i-1); break;}
58 if ((c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) {return wxEmptyString;}
5526e819 59 }
956418ab
VS
60
61 if (m_MimeMng == NULL) {
62 m_MimeMng = new wxMimeTypesManager;
63
64 static const wxFileTypeInfo fallbacks[] =
65 {
66 wxFileTypeInfo("image/jpeg",
67 "",
68 "",
69 "JPEG image (from fallback)",
70 "jpg", "jpeg", NULL),
71 wxFileTypeInfo("image/gif",
72 "",
73 "",
74 "GIF image (from fallback)",
75 "gif", NULL),
76 wxFileTypeInfo("image/png",
77 "",
78 "",
79 "PNG image (from fallback)",
80 "png", NULL),
81 wxFileTypeInfo("image/bmp",
82 "",
83 "",
84 "windows bitmap image (from fallback)",
85 "bmp", NULL),
86 wxFileTypeInfo("text/html",
87 "",
88 "",
89 "HTML document (from fallback)",
90 "htm", "html", NULL),
91
92 // must terminate the table with this!
93 wxFileTypeInfo()
94 };
95
96 m_MimeMng -> AddFallbacks(fallbacks);
97 }
98
a76015e6 99 ft = m_MimeMng -> GetFileTypeFromExtension(ext);
51f79d5c
VS
100 if (ft && (ft -> GetMimeType(&mime))) {
101 delete ft;
102 return mime;
103 }
104 else {
105 delete ft;
106 return wxEmptyString;
107 }
5526e819
VS
108}
109
110
111
112wxString wxFileSystemHandler::GetProtocol(const wxString& location) const
113{
114 wxString s = wxEmptyString;
115 int i, l = location.Length();
116 bool fnd;
117
118 fnd = FALSE;
223d09f6
KB
119 for (i = l-1; (i >= 0) && ((location[i] != wxT('#')) || (!fnd)); i--) {
120 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
5526e819 121 }
223d09f6
KB
122 if (!fnd) return wxT("file");
123 for (++i; (i < l) && (location[i] != wxT(':')); i++) s << location[i];
5526e819
VS
124 return s;
125}
126
127
5526e819
VS
128wxString wxFileSystemHandler::GetLeftLocation(const wxString& location) const
129{
130 int i;
131 bool fnd;
132
133 fnd = FALSE;
134 for (i = location.Length()-1; i >= 0; i--) {
223d09f6
KB
135 if ((location[i] == wxT(':')) && (i != 1 /*win: C:\path*/)) fnd = TRUE;
136 else if (fnd && (location[i] == wxT('#'))) return location.Left(i);
5526e819
VS
137 }
138 return wxEmptyString;
139}
140
5526e819
VS
141wxString wxFileSystemHandler::GetRightLocation(const wxString& location) const
142{
143 int i, l = location.Length();
144 int l2 = l + 1;
223d09f6 145 for (i = l-1; (i >= 0) && ((location[i] != wxT(':')) || (i == 1) || (location[i-2] == wxT(':'))); i--) {if (location[i] == wxT('#')) l2 = i + 1;}
5526e819
VS
146 if (i == 0) return wxEmptyString;
147 else return location.Mid(i + 1, l2 - i - 2);
148}
149
5526e819
VS
150wxString wxFileSystemHandler::GetAnchor(const wxString& location) const
151{
152 char c;
153 int l = location.Length();
154
155 for (int i = l-1; i >= 0; i--) {
156 c = location[i];
223d09f6
KB
157 if (c == wxT('#')) return location.Right(l-i-1);
158 else if ((c == wxT('.')) || (c == wxT('/')) || (c == wxT('\\')) || (c == wxT(':'))) return wxEmptyString;
5526e819
VS
159 }
160 return wxEmptyString;
161}
162
aaa66113 163
f76dbc4d
VZ
164wxString wxFileSystemHandler::FindFirst(const wxString& WXUNUSED(spec),
165 int WXUNUSED(flags))
166{
167 return wxEmptyString;
168}
aaa66113 169
f76dbc4d
VZ
170wxString wxFileSystemHandler::FindNext()
171{
172 return wxEmptyString;
173}
aaa66113 174
5526e819
VS
175//--------------------------------------------------------------------------------
176// wxLocalFSHandler
177//--------------------------------------------------------------------------------
178
179class wxLocalFSHandler : public wxFileSystemHandler
180{
181 public:
182 virtual bool CanOpen(const wxString& location);
183 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
aaa66113
VS
184 virtual wxString FindFirst(const wxString& spec, int flags = 0);
185 virtual wxString FindNext();
5526e819
VS
186};
187
188
5526e819
VS
189bool wxLocalFSHandler::CanOpen(const wxString& location)
190{
223d09f6 191 return GetProtocol(location) == wxT("file");
5526e819
VS
192}
193
5526e819
VS
194wxFSFile* wxLocalFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
195{
196 wxString right = GetRightLocation(location);
197 if (wxFileExists(right))
198 return new wxFSFile(new wxFileInputStream(right),
199 right,
200 GetMimeTypeFromExt(location),
201 GetAnchor(location));
d30e0edd 202 else return (wxFSFile*) NULL;
5526e819
VS
203}
204
aaa66113
VS
205wxString wxLocalFSHandler::FindFirst(const wxString& spec, int flags)
206{
207 wxString right = GetRightLocation(spec);
208 return wxFindFirstFile(right, flags);
209}
210
211wxString wxLocalFSHandler::FindNext()
212{
213 return wxFindNextFile();
214}
215
216
217
5526e819
VS
218//-----------------------------------------------------------------------------
219// wxFileSystem
220//-----------------------------------------------------------------------------
221
222IMPLEMENT_DYNAMIC_CLASS(wxFileSystem, wxObject)
223
224
225wxList wxFileSystem::m_Handlers;
226
227
228
229void wxFileSystem::ChangePathTo(const wxString& location, bool is_dir)
230{
231 int i, pathpos = -1;
232 m_Path = location;
233
234 for (i = m_Path.Length()-1; i >= 0; i--)
223d09f6 235 if (m_Path[(unsigned int) i] == wxT('\\')) m_Path.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
d30e0edd 236
aaa66113
VS
237 if (is_dir)
238 {
239 if (m_Path.Length() > 0 && m_Path.Last() != wxT('/') && m_Path.Last() != wxT(':'))
240 m_Path << wxT('/');
241 }
242
243 else
d30e0edd 244 {
269e8200 245 for (i = m_Path.Length()-1; i >= 0; i--)
aaa66113 246 {
223d09f6 247 if (m_Path[(unsigned int) i] == wxT('/'))
aaa66113 248 {
223d09f6 249 if ((i > 1) && (m_Path[(unsigned int) (i-1)] == wxT('/')) && (m_Path[(unsigned int) (i-2)] == wxT(':')))
aaa66113 250 {
5526e819
VS
251 i -= 2;
252 continue;
253 }
269e8200 254 else
aaa66113 255 {
269e8200 256 pathpos = i;
5526e819
VS
257 break;
258 }
259 }
aaa66113
VS
260 else if (m_Path[(unsigned int) i] == wxT(':')) {
261 pathpos = i;
262 break;
263 }
5526e819 264 }
269e8200 265 if (pathpos == -1)
aaa66113 266 {
269e8200 267 for (i = 0; i < (int) m_Path.Length(); i++)
aaa66113 268 {
223d09f6 269 if (m_Path[(unsigned int) i] == wxT(':'))
aaa66113 270 {
5526e819
VS
271 m_Path.Remove(i+1);
272 break;
273 }
274 }
269e8200 275 if (i == (int) m_Path.Length())
aaa66113 276 m_Path = wxEmptyString;
5526e819 277 }
269e8200 278 else
aaa66113 279 {
5526e819
VS
280 m_Path.Remove(pathpos+1);
281 }
282 }
283}
284
285
286
287wxFSFile* wxFileSystem::OpenFile(const wxString& location)
288{
289 wxString loc = location;
290 int i, ln;
291 char meta;
292 wxFSFile *s = NULL;
293 wxNode *node;
294
295 ln = loc.Length();
296 meta = 0;
269e8200 297 for (i = 0; i < ln; i++)
d30e0edd 298 {
223d09f6 299 if (loc[(unsigned int) i] == wxT('\\')) loc.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
aaa66113
VS
300 if (!meta)
301 switch (loc[(unsigned int) i])
302 {
303 case wxT('/') : case wxT(':') : case wxT('#') : meta = loc[(unsigned int) i];
304 }
5526e819
VS
305 }
306 m_LastName = wxEmptyString;
307
308 // try relative paths first :
223d09f6 309 if (meta != wxT(':'))
d30e0edd 310 {
5526e819 311 node = m_Handlers.GetFirst();
d30e0edd 312 while (node)
aaa66113 313 {
5526e819 314 wxFileSystemHandler *h = (wxFileSystemHandler*) node -> GetData();
7dee4b2b 315 if (h->CanOpen(m_Path + loc))
aaa66113 316 {
7dee4b2b
VS
317 s = h->OpenFile(*this, m_Path + loc);
318 if (s) { m_LastName = m_Path + loc; break; }
5526e819 319 }
d30e0edd 320 node = node->GetNext();
5526e819
VS
321 }
322 }
323
324 // if failed, try absolute paths :
269e8200 325 if (s == NULL)
d30e0edd 326 {
5526e819 327 node = m_Handlers.GetFirst();
d30e0edd 328 while (node)
aaa66113 329 {
d30e0edd 330 wxFileSystemHandler *h = (wxFileSystemHandler*) node->GetData();
7dee4b2b 331 if (h->CanOpen(loc))
aaa66113 332 {
7dee4b2b
VS
333 s = h->OpenFile(*this, loc);
334 if (s) { m_LastName = loc; break; }
5526e819 335 }
d30e0edd 336 node = node->GetNext();
5526e819
VS
337 }
338 }
339 return (s);
340}
341
342
aaa66113
VS
343
344wxString wxFileSystem::FindFirst(const wxString& spec, int flags)
345{
346 wxNode *node;
347 wxString spec2(spec);
348
349 m_FindFileHandler = NULL;
350
351 for (int i = spec2.Length()-1; i >= 0; i--)
352 if (spec2[(unsigned int) i] == wxT('\\')) spec2.GetWritableChar(i) = wxT('/'); // wanna be windows-safe
353
354 node = m_Handlers.GetFirst();
355 while (node)
356 {
357 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
358 if (m_FindFileHandler -> CanOpen(m_Path + spec2))
359 return m_FindFileHandler -> FindFirst(m_Path + spec2, flags);
360 node = node->GetNext();
361 }
362
363 node = m_Handlers.GetFirst();
364 while (node)
365 {
366 m_FindFileHandler = (wxFileSystemHandler*) node -> GetData();
367 if (m_FindFileHandler -> CanOpen(spec2))
368 return m_FindFileHandler -> FindFirst(spec2, flags);
369 node = node->GetNext();
370 }
371
372 return wxEmptyString;
373}
374
375
376
377wxString wxFileSystem::FindNext()
378{
379 if (m_FindFileHandler == NULL) return wxEmptyString;
380 else return m_FindFileHandler -> FindNext();
381}
382
383
384
5526e819
VS
385void wxFileSystem::AddHandler(wxFileSystemHandler *handler)
386{
387 m_Handlers.Append(handler);
388}
389
390
269e8200
RD
391void wxFileSystem::CleanUpHandlers()
392{
393 m_Handlers.DeleteContents(TRUE);
394 m_Handlers.Clear();
395}
396
5526e819 397
aaa66113
VS
398
399
5526e819
VS
400///// Module:
401
402class wxFileSystemModule : public wxModule
403{
404 DECLARE_DYNAMIC_CLASS(wxFileSystemModule)
405
406 public:
407 virtual bool OnInit()
408 {
409 wxFileSystem::AddHandler(new wxLocalFSHandler);
410 return TRUE;
411 }
269e8200 412 virtual void OnExit()
aaa66113
VS
413 {
414 wxFileSystemHandler::CleanUpStatics();
269e8200 415 wxFileSystem::CleanUpHandlers();
aaa66113 416 }
5526e819
VS
417};
418
419IMPLEMENT_DYNAMIC_CLASS(wxFileSystemModule, wxModule)
420
269e8200 421#endif
d30e0edd 422 // (wxUSE_FS_INET || wxUSE_FS_ZIP) && wxUSE_STREAMS
5526e819 423
a76015e6
VS
424
425