]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filesys.h
Ignore erase failures
[wxWidgets.git] / include / wx / filesys.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filesys.h
3 // Purpose: class for opening files - virtual file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifndef __FILESYS_H__
10 #define __FILESYS_H__
11
12 #ifdef __GNUG__
13 #pragma interface
14 #endif
15
16 #include <wx/stream.h>
17 #include <wx/mimetype.h>
18 #include <wx/url.h>
19
20
21 class wxFSFile;
22 class wxFileSystemHandler;
23 class wxFileSystem;
24
25 //--------------------------------------------------------------------------------
26 // wxFSFile
27 // This class is a file opened using wxFileSystem. It consists of
28 // input stream, location, mime type & optional anchor
29 // (in 'index.htm#chapter2', 'chapter2' is anchor)
30 //--------------------------------------------------------------------------------
31
32 class WXDLLEXPORT wxFSFile : public wxObject
33 {
34 private:
35 wxInputStream *m_Stream;
36 wxString m_Location;
37 wxString m_MimeType;
38 wxString m_Anchor;
39
40 public:
41 wxFSFile(wxInputStream *stream, const wxString& loc, const wxString& mimetype, const wxString& anchor)
42 {
43 m_Stream = stream;
44 m_Location = loc;
45 m_MimeType = mimetype; m_MimeType.MakeLower();
46 m_Anchor = anchor;
47 }
48 virtual ~wxFSFile()
49 {
50 if (m_Stream) delete m_Stream;
51 }
52
53 wxInputStream *GetStream() const {return m_Stream;}
54 // returns stream. This doesn't _create_ stream, it only returns
55 // pointer to it!!
56
57 const wxString& GetMimeType() const {return m_MimeType;}
58 // returns file's mime type
59
60 const wxString& GetLocation() const {return m_Location;}
61 // returns the original location (aka filename) of the file
62
63 const wxString& GetAnchor() const {return m_Anchor;}
64 };
65
66
67
68
69
70 //--------------------------------------------------------------------------------
71 // wxFileSystemHandler
72 // This class is FS handler for wxFileSystem. It provides
73 // interface to access certain
74 // kinds of files (HTPP, FTP, local, tar.gz etc..)
75 //--------------------------------------------------------------------------------
76
77 class WXDLLEXPORT wxFileSystemHandler : public wxObject
78 {
79 DECLARE_ABSTRACT_CLASS(wxFileSystemHandler)
80
81 public:
82 wxFileSystemHandler() : wxObject() {}
83
84 virtual bool CanOpen(const wxString& location) = 0;
85 // returns TRUE if this handler is able to open given location
86
87 virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0;
88 // opens given file and returns pointer to input stream.
89 // Returns NULL if opening failed.
90 // The location is always absolute path.
91
92 protected:
93 wxString GetProtocol(const wxString& location) const;
94 // returns protocol ("file", "http", "tar" etc.) The last (most right)
95 // protocol is used:
96 // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
97
98 wxString GetLeftLocation(const wxString& location) const;
99 // returns left part of address:
100 // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
101
102 wxString GetAnchor(const wxString& location) const;
103 // returns anchor part of address:
104 // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
105 // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
106
107 wxString GetRightLocation(const wxString& location) const;
108 // returns right part of address:
109 // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
110
111 wxString GetMimeTypeFromExt(const wxString& location);
112 // Returns MIME type of the file - w/o need to open it
113 // (default behaviour is that it returns type based on extension)
114
115 private:
116 static wxMimeTypesManager m_MimeMng;
117 // MIME manager
118 // (it's static and thus shared by all instances and derived classes)
119 };
120
121
122
123
124 //--------------------------------------------------------------------------------
125 // wxFileSystem
126 // This class provides simple interface for opening various
127 // kinds of files (HTPP, FTP, local, tar.gz etc..)
128 //--------------------------------------------------------------------------------
129
130 class WXDLLEXPORT wxFileSystem : public wxObject
131 {
132 DECLARE_DYNAMIC_CLASS(wxFileSystem)
133
134 private:
135 wxString m_Path;
136 // the path (location) we are currently in
137 // this is path, not file!
138 // (so if you opened test/demo.htm, it is
139 // "test/", not "test/demo.htm")
140 wxString m_LastName;
141 // name of last opened file (full path)
142 static wxList m_Handlers;
143 // list of FS handlers
144
145 public:
146 wxFileSystem() : wxObject() {m_Path = m_LastName = wxEmptyString; m_Handlers.DeleteContents(TRUE);}
147
148 void ChangePathTo(const wxString& location, bool is_dir = FALSE);
149 // sets the current location. Every call to OpenFile is
150 // relative to this location.
151 // NOTE !!
152 // unless is_dir = TRUE 'location' is *not* the directory but
153 // file contained in this directory
154 // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
155
156 wxString GetPath() const {return m_Path;}
157
158 wxFSFile* OpenFile(const wxString& location);
159 // opens given file and returns pointer to input stream.
160 // Returns NULL if opening failed.
161 // It first tries to open the file in relative scope
162 // (based on ChangePathTo()'s value) and then as an absolute
163 // path.
164
165 static void AddHandler(wxFileSystemHandler *handler);
166 // Adds FS handler.
167 // In fact, this class is only front-end to the FS hanlers :-)
168 };
169
170
171 /*
172
173 'location' syntax:
174
175 To determine FS type, we're using standard KDE notation:
176 file:/absolute/path/file.htm
177 file:relative_path/xxxxx.html
178 /some/path/x.file ('file:' is default)
179 http://www.gnome.org
180 file:subdir/archive.tar.gz#tar:/README.txt
181
182 special characters :
183 ':' - FS identificator is before this char
184 '#' - separator. It can be either HTML anchor ("index.html#news")
185 (in case there is no ':' in the string to the right from it)
186 or FS separator
187 (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
188 this would access tgz archive stored on web)
189 '/' - directory (path) separator. It is used to determine upper-level path.
190 HEY! Don't use \ even if you're on Windows!
191
192 */
193
194 #endif // __FILESYS_H__