TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_file_system.h
Go to the documentation of this file.
1#pragma once
2
3// File systems a file view browses and changes, behind one interface: a folder's entries,
4// moving, renaming and copying in from the disk. A view relists when Generation() changes.
5//
6// Paths are relative to the file system's root, with '/' between names; "" is the root.
7
8#include <cstdint>
9#include <filesystem>
10#include <memory>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace tachyon
16{
19
21 struct FileEntry
22 {
23 std::string name;
24 bool isFolder = false;
25 uint64_t size = 0;
28 std::filesystem::path disk;
29 std::filesystem::file_time_type modified {};
30 };
31
35 {
36 public:
37 virtual ~IFileSystem() = default;
38
41 virtual std::vector<FileEntry> List(std::string_view path) = 0;
46 virtual bool Move(std::string_view path, std::string_view toFolder, std::string& error) = 0;
50 virtual bool Rename(std::string_view path, std::string_view newName, std::string& error) = 0;
55 virtual bool Import(const std::filesystem::path& source, std::string_view toFolder, std::string& error) = 0;
57 virtual uint64_t Generation() const = 0;
58 };
59
61 std::string JoinPath(std::string_view folder, std::string_view name);
63 std::string_view ParentPath(std::string_view path);
65 std::string_view FileName(std::string_view path);
66
71 class MemoryFileSystem final : public IFileSystem
72 {
73 public:
77 explicit MemoryFileSystem(std::filesystem::path diskRoot);
79
82 bool AddFolder(std::string_view path);
85 bool AddFile(std::string_view path, uint64_t size = 0);
86
87 std::vector<FileEntry> List(std::string_view path) override;
88 bool Move(std::string_view path, std::string_view toFolder, std::string& error) override;
89 bool Rename(std::string_view path, std::string_view newName, std::string& error) override;
90 bool Import(const std::filesystem::path& source, std::string_view toFolder, std::string& error) override;
91 uint64_t Generation() const override { return m_generation; }
92
93 private:
94 struct Node;
95 Node* Find(std::string_view path);
96 Node* AddPath(std::string_view path, bool isFolder);
97 void Load(Node& folder);
98
99 std::unique_ptr<Node> m_root;
100 uint64_t m_generation = 0;
101 };
102
104}
What a file view reads and changes.
Definition gui_file_system.h:35
virtual uint64_t Generation() const =0
Changes whenever what List returns may have: after a move, a rename or an import.
virtual bool Move(std::string_view path, std::string_view toFolder, std::string &error)=0
Moves the file or folder at path into the folder toFolder, keeping its name.
virtual bool Import(const std::filesystem::path &source, std::string_view toFolder, std::string &error)=0
Copies the file or folder at source, on the disk (a file dropped from another application),...
virtual bool Rename(std::string_view path, std::string_view newName, std::string &error)=0
Renames the file or folder at path in its folder.
virtual ~IFileSystem()=default
virtual std::vector< FileEntry > List(std::string_view path)=0
The entries in the folder at path, in no particular order; none when there's no such folder.
bool AddFolder(std::string_view path)
Adds the folder at path, and the folders on the way to it.
uint64_t Generation() const override
Changes whenever what List returns may have: after a move, a rename or an import.
Definition gui_file_system.h:91
MemoryFileSystem(std::filesystem::path diskRoot)
A copy of the disk folder diskRoot, read as it's browsed.
bool Rename(std::string_view path, std::string_view newName, std::string &error) override
Renames the file or folder at path in its folder.
bool Import(const std::filesystem::path &source, std::string_view toFolder, std::string &error) override
Copies the file or folder at source, on the disk (a file dropped from another application),...
bool Move(std::string_view path, std::string_view toFolder, std::string &error) override
Moves the file or folder at path into the folder toFolder, keeping its name.
bool AddFile(std::string_view path, uint64_t size=0)
Adds a file at path (and the folders on the way to it), size bytes.
std::vector< FileEntry > List(std::string_view path) override
The entries in the folder at path, in no particular order; none when there's no such folder.
std::string JoinPath(std::string_view folder, std::string_view name)
folder and name as one path.
std::string_view ParentPath(std::string_view path)
The folder path is in; "" for an entry in the root (and for the root).
std::string_view FileName(std::string_view path)
The last name in path.
Definition gui_logging.h:24
A file or folder in a folder.
Definition gui_file_system.h:22
std::string name
Definition gui_file_system.h:23
std::filesystem::path disk
The file or folder on the disk it is, or is a copy of; empty for one that's only in the file system (...
Definition gui_file_system.h:28
uint64_t size
bytes; 0 for a folder
Definition gui_file_system.h:25
std::filesystem::file_time_type modified
when the file on the disk was last written
Definition gui_file_system.h:29
bool isFolder
Definition gui_file_system.h:24