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