Euphoria
vfs.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "base/memorychunk.h"
4 
5 #include <map>
6 #include <memory>
7 #include <optional>
8 
9 
10 namespace eu::io
11 {
12  struct FilePath;
13  struct DirPath;
14 
15  struct ListedFile
16  {
17  std::string name;
18  bool is_builtin = false;
19  bool is_file = false;
20 
21  ListedFile() = default;
22  ListedFile(const std::string& n, bool b, bool f);
23  };
24 
25  struct FileList
26  {
27  std::map<std::string, ListedFile> files;
28  std::map<std::string, ListedFile> folders;
29 
30  void
31  add(const ListedFile& file);
32 
33  void
34  add(const std::string& n, bool b, bool f);
35  };
36 
37  // todo(Gustav): use path class
38 
39  struct ReadRoot
40  {
41  ReadRoot() = default;
42  virtual ~ReadRoot();
43 
44  ReadRoot(const ReadRoot&) = delete;
45  ReadRoot(ReadRoot&&) = delete;
46  void operator=(const ReadRoot&) = delete;
47  void operator=(ReadRoot&&) = delete;
48 
49  virtual void
50  add_description(std::vector<std::string>* strings) = 0;
51 
52  virtual std::shared_ptr<MemoryChunk>
53  read_file(const FilePath& path) = 0;
54 
55  virtual FileList
56  list_files(const DirPath& path) = 0;
57  };
58 
59  struct WriteRoot
60  {
61  WriteRoot() = default;
62  virtual ~WriteRoot();
63 
64  WriteRoot(const WriteRoot&) = delete;
65  WriteRoot(WriteRoot&&) = delete;
66  void operator=(const WriteRoot&) = delete;
67  void operator=(WriteRoot&&) = delete;
68 
69  virtual void
70  write_file(const FilePath& path, std::shared_ptr<MemoryChunk> data) = 0;
71  };
72 
73  struct FileSystem
74  {
75  std::vector<std::shared_ptr<ReadRoot>> read_roots;
76  std::shared_ptr<WriteRoot> write_root;
77 
80 
81  FileSystem(const FileSystem&) = delete;
82  FileSystem(FileSystem&&) = delete;
83  void operator=(const FileSystem&) = delete;
84  void operator=(FileSystem&&) = delete;
85 
86  void add_read_root(const std::shared_ptr<ReadRoot>& root);
87  void set_write_root(const std::shared_ptr<WriteRoot>& root);
88  std::shared_ptr<MemoryChunk> read_file(const FilePath& path);
89  void write_file(const FilePath& path, std::shared_ptr<MemoryChunk> data);
90  std::vector<ListedFile> list_files(const DirPath& path);
91  std::string get_roots_as_string();
92 
93  // todo(Gustav): need to support paging too
94  std::optional<std::string> read_file_to_string(const FilePath& path);
95 
96  // todo(Gustav): support different roots such as real file system, zip/container file etc
97  // todo(Gustav): support encryption
98  // todo(Gustav): support listing/enumerating files
99  };
100 
102  {
104 
105  // todo(Gustav): allow registering string_view and embedded binary chunks
106 
107  void
109  (
110  const FilePath& path,
111  const std::string& content
112  );
113 
114  void
116  (
117  const FilePath& path,
118  const std::shared_ptr<MemoryChunk>& content
119  );
120 
121  static std::shared_ptr<ReadRootCatalog>
123 
124  std::shared_ptr<MemoryChunk>
125  read_file(const FilePath& path) override;
126 
127  void
128  add_description(std::vector<std::string>* strings) override;
129 
130  FileList
131  list_files(const DirPath& path) override;
132 
133  private:
134  std::map<FilePath, std::shared_ptr<MemoryChunk>> catalog;
135  };
136 
138  {
139  explicit ReadRootPhysicalFolder(std::string folder);
140 
141  std::shared_ptr<MemoryChunk>
142  read_file(const FilePath& path) override;
143 
144  static void
145  add(FileSystem* fs, const std::string& folder);
146 
147  static void
149 
150  void
151  add_description(std::vector<std::string>* strings) override;
152 
153  FileList
154  list_files(const DirPath& path) override;
155 
156  std::string folder;
157  };
158 
160  {
161  explicit WriteRootPhysicalFolder(const std::string& f);
162 
163  void
164  write_file
165  (
166  const FilePath& path,
167  std::shared_ptr<MemoryChunk> data
168  ) override;
169 
170  std::string folder;
171  };
172 }
173 
Definition: enum.h:8
void add(const ListedFile &file)
Definition: vfs.cc:35
std::map< std::string, ListedFile > files
Definition: vfs.h:27
std::map< std::string, ListedFile > folders
Definition: vfs.h:28
std::vector< std::shared_ptr< ReadRoot > > read_roots
Definition: vfs.h:75
std::optional< std::string > read_file_to_string(const FilePath &path)
Definition: vfs.cc:171
void add_read_root(const std::shared_ptr< ReadRoot > &root)
Definition: vfs.cc:72
void write_file(const FilePath &path, std::shared_ptr< MemoryChunk > data)
Definition: vfs.cc:116
std::shared_ptr< MemoryChunk > read_file(const FilePath &path)
Definition: vfs.cc:86
FileSystem(const FileSystem &)=delete
void operator=(FileSystem &&)=delete
std::string get_roots_as_string()
Definition: vfs.cc:157
std::vector< ListedFile > list_files(const DirPath &path)
Definition: vfs.cc:127
void set_write_root(const std::shared_ptr< WriteRoot > &root)
Definition: vfs.cc:79
FileSystem(FileSystem &&)=delete
std::shared_ptr< WriteRoot > write_root
Definition: vfs.h:76
void operator=(const FileSystem &)=delete
bool is_file
Definition: vfs.h:19
ListedFile()=default
bool is_builtin
Definition: vfs.h:18
std::string name
Definition: vfs.h:17
void register_file_data(const FilePath &path, const std::shared_ptr< MemoryChunk > &content)
Definition: vfs.cc:206
void add_description(std::vector< std::string > *strings) override
Definition: vfs.cc:240
static std::shared_ptr< ReadRootCatalog > create_and_add(FileSystem *fs)
Definition: vfs.cc:216
FileList list_files(const DirPath &path) override
Definition: vfs.cc:247
std::shared_ptr< MemoryChunk > read_file(const FilePath &path) override
Definition: vfs.cc:226
void register_file_string(const FilePath &path, const std::string &content)
Definition: vfs.cc:194
void add_description(std::vector< std::string > *strings) override
Definition: vfs.cc:310
static void add(FileSystem *fs, const std::string &folder)
Definition: vfs.cc:317
static void add_current_directory(FileSystem *fs)
Definition: vfs.cc:328
FileList list_files(const DirPath &path) override
Definition: vfs.cc:336
std::shared_ptr< MemoryChunk > read_file(const FilePath &path) override
Definition: vfs.cc:302
ReadRootPhysicalFolder(std::string folder)
Definition: vfs.cc:295
ReadRoot()=default
virtual ~ReadRoot()
ReadRoot(ReadRoot &&)=delete
void operator=(ReadRoot &&)=delete
virtual void add_description(std::vector< std::string > *strings)=0
virtual FileList list_files(const DirPath &path)=0
ReadRoot(const ReadRoot &)=delete
void operator=(const ReadRoot &)=delete
virtual std::shared_ptr< MemoryChunk > read_file(const FilePath &path)=0
WriteRootPhysicalFolder(const std::string &f)
Definition: vfs.cc:368
void write_file(const FilePath &path, std::shared_ptr< MemoryChunk > data) override
Definition: vfs.cc:376
WriteRoot(const WriteRoot &)=delete
void operator=(const WriteRoot &)=delete
void operator=(WriteRoot &&)=delete
virtual ~WriteRoot()
WriteRoot(WriteRoot &&)=delete
WriteRoot()=default
virtual void write_file(const FilePath &path, std::shared_ptr< MemoryChunk > data)=0