filesys.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: filesys.h
  3. // Purpose: interface of wxFileSystem, wxFileSystemHandler, wxFSFile
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Open Bit Flags
  9. */
  10. enum wxFileSystemOpenFlags
  11. {
  12. wxFS_READ = 1, /**< Open for reading */
  13. wxFS_SEEKABLE = 4 /**< Returned stream will be seekable */
  14. };
  15. /**
  16. @class wxFileSystem
  17. This class provides an interface for opening files on different file systems.
  18. It can handle absolute and/or local filenames.
  19. It uses a system of handlers (see wxFileSystemHandler) to provide access to
  20. user-defined virtual file systems.
  21. @library{wxbase}
  22. @category{vfs}
  23. @see wxFileSystemHandler, wxFSFile, @ref overview_fs
  24. */
  25. class wxFileSystem : public wxObject
  26. {
  27. public:
  28. /**
  29. Constructor.
  30. The initial current path of this object will be empty
  31. (i.e. GetPath() == wxEmptyString) which means that e.g. OpenFile()
  32. or FindFirst() functions will use current working directory as
  33. current path (see also wxGetCwd).
  34. */
  35. wxFileSystem();
  36. /**
  37. This static function adds new handler into the list of handlers
  38. (see wxFileSystemHandler) which provide access to virtual FS.
  39. Note that if two handlers for the same protocol are added, the last
  40. added one takes precedence.
  41. @note You can call:
  42. @code
  43. wxFileSystem::AddHandler(new My_FS_Handler);
  44. @endcode
  45. This is because (a) AddHandler is a static method, and (b) the
  46. handlers are deleted in wxFileSystem's destructor so that you
  47. don't have to care about it.
  48. */
  49. static void AddHandler(wxFileSystemHandler* handler);
  50. /**
  51. Remove a filesystem handler from the list of handlers.
  52. */
  53. static wxFileSystemHandler* RemoveHandler(wxFileSystemHandler *handler);
  54. /**
  55. Sets the current location. @a location parameter passed to OpenFile() is
  56. relative to this path.
  57. @remarks Unless @a is_dir is @true the @a location parameter is not the
  58. directory name but the name of the file in this directory.
  59. All these commands change the path to "dir/subdir/":
  60. @code
  61. ChangePathTo("dir/subdir/xh.htm");
  62. ChangePathTo("dir/subdir", true);
  63. ChangePathTo("dir/subdir/", true);
  64. @endcode
  65. Example:
  66. @code
  67. f = fs->OpenFile("hello.htm"); // opens file 'hello.htm'
  68. fs->ChangePathTo("subdir/folder", true);
  69. f = fs->OpenFile("hello.htm"); // opens file 'subdir/folder/hello.htm' !!
  70. @endcode
  71. @param location
  72. the new location. Its meaning depends on the value of is_dir
  73. @param is_dir
  74. if @true location is new directory.
  75. If @false (the default) location is file in the new directory.
  76. */
  77. void ChangePathTo(const wxString& location, bool is_dir = false);
  78. /**
  79. Converts a wxFileName into an URL.
  80. @see URLToFileName(), wxFileName
  81. */
  82. static wxString FileNameToURL(const wxFileName& filename);
  83. /**
  84. Looks for the file with the given name @a file in a colon or semi-colon
  85. (depending on the current platform) separated list of directories in @a path.
  86. If the file is found in any directory, returns @true and the full path
  87. of the file in @a str, otherwise returns @false and doesn't modify @a str.
  88. @param pStr
  89. Receives the full path of the file, must not be @NULL
  90. @param path
  91. wxPATH_SEP-separated list of directories
  92. @param file
  93. the name of the file to look for
  94. */
  95. bool FindFileInPath(wxString* pStr, const wxString& path,
  96. const wxString& file);
  97. /**
  98. Works like ::wxFindFirstFile().
  99. Returns the name of the first filename (within filesystem's current path)
  100. that matches @a wildcard.
  101. @param wildcard
  102. The wildcard that the filename must match
  103. @param flags
  104. One of wxFILE (only files), wxDIR (only directories) or 0 (both).
  105. */
  106. wxString FindFirst(const wxString& wildcard, int flags = 0);
  107. /**
  108. Returns the next filename that matches the parameters passed to FindFirst().
  109. */
  110. wxString FindNext();
  111. /**
  112. Returns the actual path (set by wxFileSystem::ChangePathTo).
  113. */
  114. wxString GetPath() const;
  115. /**
  116. This static function returns @true if there is a registered handler which can
  117. open the given location.
  118. */
  119. static bool HasHandlerForPath(const wxString& location);
  120. /**
  121. Opens the file and returns a pointer to a wxFSFile object or @NULL if failed.
  122. It first tries to open the file in relative scope (based on value passed to
  123. ChangePathTo() method) and then as an absolute path.
  124. Note that the user is responsible for deleting the returned wxFSFile.
  125. @a flags can be one or more of the ::wxFileSystemOpenFlags values
  126. combined together.
  127. A stream opened with just the default @e wxFS_READ flag may
  128. or may not be seekable depending on the underlying source.
  129. Passing @e "wxFS_READ | wxFS_SEEKABLE" for @a flags will back
  130. a stream that is not natively seekable with memory or a file
  131. and return a stream that is always seekable.
  132. @note
  133. The @a location argument is, despite this method's name @em not
  134. a filename. It is a "location", aka wxFileSystem URL (see
  135. @ref overview_fs).
  136. */
  137. wxFSFile* OpenFile(const wxString& location,
  138. int flags = wxFS_READ);
  139. /**
  140. Converts URL into a well-formed filename.
  141. The URL must use the @c file protocol.
  142. */
  143. static wxFileName URLToFileName(const wxString& url);
  144. };
  145. /**
  146. @class wxFSFile
  147. This class represents a single file opened by wxFileSystem.
  148. It provides more information than wxWidgets' input streams
  149. (stream, filename, mime type, anchor).
  150. @note Any pointer returned by a method of wxFSFile is valid only as long as
  151. the wxFSFile object exists. For example a call to GetStream()
  152. doesn't @e create the stream but only returns the pointer to it.
  153. In other words after 10 calls to GetStream() you will have obtained
  154. ten identical pointers.
  155. @library{wxbase}
  156. @category{vfs,file}
  157. @see wxFileSystemHandler, wxFileSystem, @ref overview_fs
  158. */
  159. class wxFSFile : public wxObject
  160. {
  161. public:
  162. /**
  163. Constructor. You probably won't use it. See the Note for details.
  164. It is seldom used by the application programmer but you will need it if
  165. you are writing your own virtual FS. For example you may need something
  166. similar to wxMemoryInputStream, but because wxMemoryInputStream doesn't
  167. free the memory when destroyed and thus passing a memory stream pointer
  168. into wxFSFile constructor would lead to memory leaks, you can write your
  169. own class derived from wxFSFile:
  170. @code
  171. class wxMyFSFile : public wxFSFile
  172. {
  173. private:
  174. void *m_Mem;
  175. public:
  176. wxMyFSFile(.....)
  177. ~wxMyFSFile() {free(m_Mem);}
  178. // of course dtor is virtual ;-)
  179. };
  180. @endcode
  181. If you are not sure of the meaning of these params, see the description
  182. of the GetXXXX() functions.
  183. @param stream
  184. The input stream that will be used to access data
  185. @param location
  186. The full location (aka filename) of the file
  187. @param mimetype
  188. MIME type of this file. It may be left empty, in which
  189. case the type will be determined from file's extension (location must
  190. not be empty in this case).
  191. @param anchor
  192. Anchor. See GetAnchor() for details.
  193. @param modif
  194. Modification date and time for this file.
  195. */
  196. wxFSFile(wxInputStream* stream, const wxString& location,
  197. const wxString& mimetype, const wxString& anchor,
  198. wxDateTime modif);
  199. /**
  200. Detaches the stream from the wxFSFile object. That is, the
  201. stream obtained with GetStream() will continue its existence
  202. after the wxFSFile object is deleted.
  203. You will have to delete the stream yourself.
  204. */
  205. wxInputStream* DetachStream();
  206. /**
  207. Returns anchor (if present). The term of @b anchor can be easily
  208. explained using few examples:
  209. @verbatim
  210. index.htm#anchor // 'anchor' is anchor
  211. index/wx001.htm // NO anchor here!
  212. archive/main.zip#zip:index.htm#global // 'global'
  213. archive/main.zip#zip:index.htm // NO anchor here!
  214. @endverbatim
  215. Usually an anchor is presented only if the MIME type is 'text/html'.
  216. But it may have some meaning with other files; for example myanim.avi#200
  217. may refer to position in animation or reality.wrl#MyView may refer
  218. to a predefined view in VRML.
  219. */
  220. const wxString& GetAnchor() const;
  221. /**
  222. Returns full location of the file, including path and protocol.
  223. Examples:
  224. @verbatim
  225. http://www.wxwidgets.org
  226. http://www.ms.mff.cuni.cz/~vsla8348/wxhtml/archive.zip#zip:info.txt
  227. file:/home/vasek/index.htm
  228. relative-file.htm
  229. @endverbatim
  230. */
  231. const wxString& GetLocation() const;
  232. /**
  233. Returns the MIME type of the content of this file.
  234. It is either extension-based (see wxMimeTypesManager) or extracted from
  235. HTTP protocol Content-Type header.
  236. */
  237. const wxString& GetMimeType() const;
  238. /**
  239. Returns time when this file was modified.
  240. */
  241. wxDateTime GetModificationTime() const;
  242. /**
  243. Returns pointer to the stream.
  244. You can use the returned stream to directly access data.
  245. You may suppose that the stream provide Seek and GetSize functionality
  246. (even in the case of the HTTP protocol which doesn't provide
  247. this by default. wxHtml uses local cache to work around
  248. this and to speed up the connection).
  249. */
  250. wxInputStream* GetStream() const;
  251. };
  252. /**
  253. @class wxFileSystemHandler
  254. Classes derived from wxFileSystemHandler are used to access virtual file systems.
  255. Its public interface consists of two methods: wxFileSystemHandler::CanOpen
  256. and wxFileSystemHandler::OpenFile.
  257. It provides additional protected methods to simplify the process
  258. of opening the file: GetProtocol(), GetLeftLocation(), GetRightLocation(),
  259. GetAnchor(), GetMimeTypeFromExt().
  260. Please have a look at overview (see wxFileSystem) if you don't know how locations
  261. are constructed.
  262. Also consult the @ref overview_fs_wxhtmlfs "list of available handlers".
  263. Note that the handlers are shared by all instances of wxFileSystem.
  264. @remarks
  265. wxHTML library provides handlers for local files and HTTP or FTP protocol.
  266. @note
  267. The location parameter passed to OpenFile() or CanOpen() methods is always an
  268. absolute path. You don't need to check the FS's current path.
  269. @beginWxPerlOnly
  270. In wxPerl, you need to derive your file system handler class
  271. from @c Wx::PlFileSystemHandler.
  272. @endWxPerlOnly
  273. @library{wxbase}
  274. @category{vfs}
  275. @see wxFileSystem, wxFSFile, @ref overview_fs
  276. */
  277. class wxFileSystemHandler : public wxObject
  278. {
  279. public:
  280. /**
  281. Constructor.
  282. */
  283. wxFileSystemHandler();
  284. /**
  285. Returns @true if the handler is able to open this file. This function doesn't
  286. check whether the file exists or not, it only checks if it knows the protocol.
  287. Example:
  288. @code
  289. bool MyHand::CanOpen(const wxString& location)
  290. {
  291. return (GetProtocol(location) == "http");
  292. }
  293. @endcode
  294. Must be overridden in derived handlers.
  295. */
  296. virtual bool CanOpen(const wxString& location) = 0;
  297. /**
  298. Works like ::wxFindFirstFile().
  299. Returns the name of the first filename (within filesystem's current path)
  300. that matches @e wildcard. @a flags may be one of wxFILE (only files),
  301. wxDIR (only directories) or 0 (both).
  302. This method is only called if CanOpen() returns @true.
  303. */
  304. virtual wxString FindFirst(const wxString& wildcard,
  305. int flags = 0);
  306. /**
  307. Returns next filename that matches parameters passed to wxFileSystem::FindFirst.
  308. This method is only called if CanOpen() returns @true and FindFirst()
  309. returned a non-empty string.
  310. */
  311. virtual wxString FindNext();
  312. /**
  313. Returns the MIME type based on @b extension of @a location.
  314. (While wxFSFile::GetMimeType() returns real MIME type - either
  315. extension-based or queried from HTTP.)
  316. Example:
  317. @code
  318. GetMimeTypeFromExt("index.htm") == "text/html"
  319. @endcode
  320. */
  321. static wxString GetMimeTypeFromExt(const wxString& location);
  322. /**
  323. Opens the file and returns wxFSFile pointer or @NULL if failed.
  324. Must be overridden in derived handlers.
  325. @param fs
  326. Parent FS (the FS from that OpenFile was called).
  327. See the ZIP handler for details of how to use it.
  328. @param location
  329. The absolute location of file.
  330. */
  331. virtual wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location) = 0;
  332. protected:
  333. /**
  334. Returns the anchor if present in the location.
  335. See wxFSFile::GetAnchor for details.
  336. Example:
  337. @code
  338. GetAnchor("index.htm#chapter2") == "chapter2"
  339. @endcode
  340. @note the anchor is NOT part of the left location.
  341. */
  342. static wxString GetAnchor(const wxString& location);
  343. /**
  344. Returns the left location string extracted from @e location.
  345. Example:
  346. @code
  347. GetLeftLocation("file:myzipfile.zip#zip:index.htm") == "file:myzipfile.zip"
  348. @endcode
  349. */
  350. static wxString GetLeftLocation(const wxString& location);
  351. /**
  352. Returns the protocol string extracted from @a location.
  353. Example:
  354. @code
  355. GetProtocol("file:myzipfile.zip#zip:index.htm") == "zip"
  356. @endcode
  357. */
  358. static wxString GetProtocol(const wxString& location);
  359. /**
  360. Returns the right location string extracted from @a location.
  361. Example:
  362. @code
  363. GetRightLocation("file:myzipfile.zip#zip:index.htm") == "index.htm"
  364. @endcode
  365. */
  366. static wxString GetRightLocation(const wxString& location);
  367. };
  368. /**
  369. Input stream for virtual file stream files.
  370. The stream reads data from wxFSFile obtained from wxFileSystem. It is
  371. especially useful to allow using virtual files with other wxWidgets
  372. functions and classes working with streams, e.g. for loading images or
  373. animations from virtual files and not only physical ones.
  374. @library{wxbase}
  375. @category{streams}
  376. @see wxWrapperInputStream, wxFSFile
  377. @since 2.9.4
  378. */
  379. class wxFSInputStream : public wxWrapperInputStream
  380. {
  381. public:
  382. /**
  383. Create a stream associated with the data of the given virtual file
  384. system file.
  385. @param filename
  386. The name of the input file passed to wxFileSystem::OpenFile().
  387. @param flags
  388. Combination of flags from wxFileSystemOpenFlags. ::wxFS_READ is
  389. implied, i.e. it is always added to the flags value.
  390. Use wxStreamBase::IsOk() to verify if the constructor succeeded.
  391. */
  392. wxFileInputStream(const wxString& filename, int flags = 0);
  393. /**
  394. Returns @true if the stream is initialized and ready.
  395. */
  396. bool IsOk() const;
  397. };