Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
FileSystem.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 Ultralight, Inc. *
7 *****************************************************************************/
8#pragma once
10#include <Ultralight/String.h>
11#include <Ultralight/Buffer.h>
12
13namespace ultralight {
14
15///
16/// User-defined file system interface.
17///
18/// The library uses this to load all file URLs (eg, <file:///page.html>).
19///
20/// You can provide the library with your own FileSystem implementation so that file assets are
21/// loaded from your own pipeline.
22///
23/// ## Usage
24///
25/// To provide your own custom FileSystem implementation, you should inherit from this class,
26/// handle the virtual member functions, and then pass an instance of your class to
27/// Platform::set_file_system() before calling Renderer::Create() or App::Create().
28///
29/// @note
30/// \parblock
31/// AppCore uses a default OS-specific FileSystem implementation when you call App::Create().
32///
33/// If you are using Renderer::Create(), you can still use AppCore's implementation-- see the
34/// helper functions defined in <AppCore/Platform.h>.
35/// \endparblock
36///
38 public:
39 virtual ~FileSystem();
40
41 ///
42 /// Check if a file exists within the file system.
43 ///
44 /// @param file_path Relative file path (the string following the file:/// prefix)
45 ///
46 /// @return Returns whether or not a file exists at the path specified.
47 ///
48 virtual bool FileExists(const String& file_path) = 0;
49
50 ///
51 /// Get the mime-type of a file (eg "text/html").
52 ///
53 /// This is usually determined by analyzing the file extension.
54 ///
55 /// If a mime-type cannot be determined, this should return "application/unknown".
56 ///
57 /// @param file_path Relative file path (the string following the file:/// prefix)
58 ///
59 /// @return Returns whether or not a file exists at the path specified.
60 ///
61 virtual String GetFileMimeType(const String& file_path) = 0;
62
63 ///
64 /// Get the charset / encoding of a file (eg "utf-8", "iso-8859-1").
65 ///
66 /// @note This is only applicable for text-based files (eg, "text/html", "text/plain") and is
67 /// usually determined by analyzing the contents of the file.
68 ///
69 /// @param file_path Relative file path (the string following the file:/// prefix)
70 ///
71 /// @return Returns the charset of the specified file. If a charset cannot be determined, a safe
72 /// default to return is "utf-8".
73 ///
74 virtual String GetFileCharset(const String& file_path) = 0;
75
76 ///
77 /// Open a file for reading and map it to a Buffer.
78 ///
79 /// To minimize copies, you should map the requested file into memory and use Buffer::Create()
80 /// to wrap the data pointer (unmapping should be performed in the destruction callback).
81 ///
82 /// @note
83 /// \parblock
84 /// File data addresses returned from this function should generally be aligned to 16-byte
85 /// boundaries (the default alignment on most operating systems-- if you're using C stdlib or
86 /// C++ STL functions this is already handled for you).
87 ///
88 /// This requirement is currently necessary when loading the ICU data file (eg, icudt67l.dat),
89 /// and may be relaxed for other files (but you may still see a performance benefit due to cache
90 /// line alignment).
91 ///
92 /// If you can't guarantee alignment or are unsure, you can use Buffer::CreateFromCopy to copy
93 /// the file data content to an aligned block (at the expense of data duplication).
94 /// \endparblock
95 ///
96 /// @param file_path Relative file path (the string following the file:/// prefix)
97 ///
98 /// @return If the file was able to be opened, this returns a Buffer object representing the
99 /// contents of the file. If the file was unable to be opened, you should return nullptr.
100 ///
101 virtual RefPtr<Buffer> OpenFile(const String& file_path) = 0;
102};
103
104} // namespace ultralight
#define UExport
Definition Defines.h:65
User-defined file system interface.
Definition FileSystem.h:37
virtual bool FileExists(const String &file_path)=0
Check if a file exists within the file system.
virtual String GetFileMimeType(const String &file_path)=0
Get the mime-type of a file (eg "text/html").
virtual String GetFileCharset(const String &file_path)=0
Get the charset / encoding of a file (eg "utf-8", "iso-8859-1").
virtual RefPtr< Buffer > OpenFile(const String &file_path)=0
Open a file for reading and map it to a Buffer.
A nullable smart pointer.
Definition RefPtr.h:79
UTF-8 String container with conversions for UTF-16 and UTF-32.
Definition String.h:21
Definition App.h:14