filesys.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/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. #ifndef __FILESYS_H__
  9. #define __FILESYS_H__
  10. #include "wx/defs.h"
  11. #if wxUSE_FILESYSTEM
  12. #if !wxUSE_STREAMS
  13. #error You cannot compile virtual file systems without wxUSE_STREAMS
  14. #endif
  15. #if wxUSE_HTML && !wxUSE_FILESYSTEM
  16. #error You cannot compile wxHTML without virtual file systems
  17. #endif
  18. #include "wx/stream.h"
  19. #include "wx/datetime.h"
  20. #include "wx/filename.h"
  21. #include "wx/hashmap.h"
  22. class WXDLLIMPEXP_FWD_BASE wxFSFile;
  23. class WXDLLIMPEXP_FWD_BASE wxFileSystemHandler;
  24. class WXDLLIMPEXP_FWD_BASE wxFileSystem;
  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. class WXDLLIMPEXP_BASE wxFSFile : public wxObject
  32. {
  33. public:
  34. wxFSFile(wxInputStream *stream, const wxString& loc,
  35. const wxString& mimetype, const wxString& anchor
  36. #if wxUSE_DATETIME
  37. , wxDateTime modif
  38. #endif // wxUSE_DATETIME
  39. )
  40. {
  41. m_Stream = stream;
  42. m_Location = loc;
  43. m_MimeType = mimetype.Lower();
  44. m_Anchor = anchor;
  45. #if wxUSE_DATETIME
  46. m_Modif = modif;
  47. #endif // wxUSE_DATETIME
  48. }
  49. virtual ~wxFSFile() { delete m_Stream; }
  50. // returns stream. This doesn't give away ownership of the stream object.
  51. wxInputStream *GetStream() const { return m_Stream; }
  52. // gives away the ownership of the current stream.
  53. wxInputStream *DetachStream()
  54. {
  55. wxInputStream *stream = m_Stream;
  56. m_Stream = NULL;
  57. return stream;
  58. }
  59. // deletes the current stream and takes ownership of another.
  60. void SetStream(wxInputStream *stream)
  61. {
  62. delete m_Stream;
  63. m_Stream = stream;
  64. }
  65. // returns file's mime type
  66. const wxString& GetMimeType() const;
  67. // returns the original location (aka filename) of the file
  68. const wxString& GetLocation() const { return m_Location; }
  69. const wxString& GetAnchor() const { return m_Anchor; }
  70. #if wxUSE_DATETIME
  71. wxDateTime GetModificationTime() const { return m_Modif; }
  72. #endif // wxUSE_DATETIME
  73. private:
  74. wxInputStream *m_Stream;
  75. wxString m_Location;
  76. wxString m_MimeType;
  77. wxString m_Anchor;
  78. #if wxUSE_DATETIME
  79. wxDateTime m_Modif;
  80. #endif // wxUSE_DATETIME
  81. DECLARE_ABSTRACT_CLASS(wxFSFile)
  82. wxDECLARE_NO_COPY_CLASS(wxFSFile);
  83. };
  84. //--------------------------------------------------------------------------------
  85. // wxFileSystemHandler
  86. // This class is FS handler for wxFileSystem. It provides
  87. // interface to access certain
  88. // kinds of files (HTPP, FTP, local, tar.gz etc..)
  89. //--------------------------------------------------------------------------------
  90. class WXDLLIMPEXP_BASE wxFileSystemHandler : public wxObject
  91. {
  92. public:
  93. wxFileSystemHandler() : wxObject() {}
  94. // returns true if this handler is able to open given location
  95. virtual bool CanOpen(const wxString& location) = 0;
  96. // opens given file and returns pointer to input stream.
  97. // Returns NULL if opening failed.
  98. // The location is always absolute path.
  99. virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0;
  100. // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
  101. // the query to directories or wxFILE for files only or 0 for either.
  102. // Returns filename or empty string if no more matching file exists
  103. virtual wxString FindFirst(const wxString& spec, int flags = 0);
  104. virtual wxString FindNext();
  105. // Returns MIME type of the file - w/o need to open it
  106. // (default behaviour is that it returns type based on extension)
  107. static wxString GetMimeTypeFromExt(const wxString& location);
  108. protected:
  109. // returns protocol ("file", "http", "tar" etc.) The last (most right)
  110. // protocol is used:
  111. // {it returns "tar" for "file:subdir/archive.tar.gz#tar:/README.txt"}
  112. static wxString GetProtocol(const wxString& location);
  113. // returns left part of address:
  114. // {it returns "file:subdir/archive.tar.gz" for "file:subdir/archive.tar.gz#tar:/README.txt"}
  115. static wxString GetLeftLocation(const wxString& location);
  116. // returns anchor part of address:
  117. // {it returns "anchor" for "file:subdir/archive.tar.gz#tar:/README.txt#anchor"}
  118. // NOTE: anchor is NOT a part of GetLeftLocation()'s return value
  119. static wxString GetAnchor(const wxString& location);
  120. // returns right part of address:
  121. // {it returns "/README.txt" for "file:subdir/archive.tar.gz#tar:/README.txt"}
  122. static wxString GetRightLocation(const wxString& location);
  123. DECLARE_ABSTRACT_CLASS(wxFileSystemHandler)
  124. };
  125. //--------------------------------------------------------------------------------
  126. // wxFileSystem
  127. // This class provides simple interface for opening various
  128. // kinds of files (HTPP, FTP, local, tar.gz etc..)
  129. //--------------------------------------------------------------------------------
  130. // Open Bit Flags
  131. enum wxFileSystemOpenFlags
  132. {
  133. wxFS_READ = 1, // Open for reading
  134. wxFS_SEEKABLE = 4 // Returned stream will be seekable
  135. };
  136. WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL(wxFileSystemHandler*, wxFSHandlerHash, class WXDLLIMPEXP_BASE);
  137. class WXDLLIMPEXP_BASE wxFileSystem : public wxObject
  138. {
  139. public:
  140. wxFileSystem() : wxObject() { m_FindFileHandler = NULL;}
  141. virtual ~wxFileSystem();
  142. // sets the current location. Every call to OpenFile is
  143. // relative to this location.
  144. // NOTE !!
  145. // unless is_dir = true 'location' is *not* the directory but
  146. // file contained in this directory
  147. // (so ChangePathTo("dir/subdir/xh.htm") sets m_Path to "dir/subdir/")
  148. void ChangePathTo(const wxString& location, bool is_dir = false);
  149. wxString GetPath() const {return m_Path;}
  150. // opens given file and returns pointer to input stream.
  151. // Returns NULL if opening failed.
  152. // It first tries to open the file in relative scope
  153. // (based on ChangePathTo()'s value) and then as an absolute
  154. // path.
  155. wxFSFile* OpenFile(const wxString& location, int flags = wxFS_READ);
  156. // Finds first/next file that matches spec wildcard. flags can be wxDIR for restricting
  157. // the query to directories or wxFILE for files only or 0 for either.
  158. // Returns filename or empty string if no more matching file exists
  159. wxString FindFirst(const wxString& spec, int flags = 0);
  160. wxString FindNext();
  161. // find a file in a list of directories, returns false if not found
  162. bool FindFileInPath(wxString *pStr,
  163. const wxString& path, const wxString& file);
  164. // Adds FS handler.
  165. // In fact, this class is only front-end to the FS handlers :-)
  166. static void AddHandler(wxFileSystemHandler *handler);
  167. // Removes FS handler
  168. static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
  169. // Returns true if there is a handler which can open the given location.
  170. static bool HasHandlerForPath(const wxString& location);
  171. // remove all items from the m_Handlers list
  172. static void CleanUpHandlers();
  173. // Returns the native path for a file URL
  174. static wxFileName URLToFileName(const wxString& url);
  175. // Returns the file URL for a native path
  176. static wxString FileNameToURL(const wxFileName& filename);
  177. protected:
  178. wxFileSystemHandler *MakeLocal(wxFileSystemHandler *h);
  179. wxString m_Path;
  180. // the path (location) we are currently in
  181. // this is path, not file!
  182. // (so if you opened test/demo.htm, it is
  183. // "test/", not "test/demo.htm")
  184. wxString m_LastName;
  185. // name of last opened file (full path)
  186. static wxList m_Handlers;
  187. // list of FS handlers
  188. wxFileSystemHandler *m_FindFileHandler;
  189. // handler that succeed in FindFirst query
  190. wxFSHandlerHash m_LocalHandlers;
  191. // Handlers local to this instance
  192. DECLARE_DYNAMIC_CLASS(wxFileSystem)
  193. wxDECLARE_NO_COPY_CLASS(wxFileSystem);
  194. };
  195. /*
  196. 'location' syntax:
  197. To determine FS type, we're using standard KDE notation:
  198. file:/absolute/path/file.htm
  199. file:relative_path/xxxxx.html
  200. /some/path/x.file ('file:' is default)
  201. http://www.gnome.org
  202. file:subdir/archive.tar.gz#tar:/README.txt
  203. special characters :
  204. ':' - FS identificator is before this char
  205. '#' - separator. It can be either HTML anchor ("index.html#news")
  206. (in case there is no ':' in the string to the right from it)
  207. or FS separator
  208. (example : http://www.wxhtml.org/wxhtml-0.1.tar.gz#tar:/include/wxhtml/filesys.h"
  209. this would access tgz archive stored on web)
  210. '/' - directory (path) separator. It is used to determine upper-level path.
  211. HEY! Don't use \ even if you're on Windows!
  212. */
  213. class WXDLLIMPEXP_BASE wxLocalFSHandler : public wxFileSystemHandler
  214. {
  215. public:
  216. virtual bool CanOpen(const wxString& location);
  217. virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
  218. virtual wxString FindFirst(const wxString& spec, int flags = 0);
  219. virtual wxString FindNext();
  220. // wxLocalFSHandler will prefix all filenames with 'root' before accessing
  221. // files on disk. This effectively makes 'root' the top-level directory
  222. // and prevents access to files outside this directory.
  223. // (This is similar to Unix command 'chroot'.)
  224. static void Chroot(const wxString& root) { ms_root = root; }
  225. protected:
  226. static wxString ms_root;
  227. };
  228. // Stream reading data from wxFSFile: this allows to use virtual files with any
  229. // wx functions accepting streams.
  230. class WXDLLIMPEXP_BASE wxFSInputStream : public wxWrapperInputStream
  231. {
  232. public:
  233. // Notice that wxFS_READ is implied in flags.
  234. wxFSInputStream(const wxString& filename, int flags = 0);
  235. virtual ~wxFSInputStream();
  236. private:
  237. wxFSFile* m_file;
  238. wxDECLARE_NO_COPY_CLASS(wxFSInputStream);
  239. };
  240. #endif
  241. // wxUSE_FILESYSTEM
  242. #endif
  243. // __FILESYS_H__