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