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