1#pragma once
2#include "taichi/common/core.h"
3
4namespace taichi {
5namespace io {
6
7// A universal filesystem interface for read-only access.
8struct TI_DLL_EXPORT VirtualDir {
9 virtual ~VirtualDir() {
10 }
11
12 // Open a virtual directory based on what `path` points to. Zip files and
13 // filesystem directories are supported.
14 static std::unique_ptr<VirtualDir> open(const std::string &path);
15 static std::unique_ptr<VirtualDir> from_zip(const void *data, size_t size);
16 static std::unique_ptr<VirtualDir> from_fs_dir(const std::string &base_dir);
17
18 // Get the `size` of the file at `path` in the virtual directory. Returns
19 // false when the file doesn't exist.
20 virtual bool get_file_size(const std::string &path, size_t &size) const = 0;
21 // Load the first `size` bytes from the file at `path` in the virtual
22 // directory. Returns the number of bytes read. Returns 0 if the file doesn't
23 // exist.
24 virtual size_t load_file(const std::string &path,
25 void *data,
26 size_t size) const = 0;
27
28 template <typename T>
29 bool load_file(const std::string &path, std::vector<T> &data) const {
30 size_t size = 0;
31
32 if (!get_file_size(path, size)) {
33 return false;
34 }
35 if (size % sizeof(T) != 0) {
36 return false;
37 }
38 data.resize(size / sizeof(T));
39 if (load_file(path, data.data(), size) != size) {
40 return false;
41 }
42 return true;
43 }
44};
45
46} // namespace io
47} // namespace taichi
48