libnedit
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
fs_Base.hpp
1
2#pragma once
3#include <ntr/ntr_Include.hpp>
4
5namespace ntr::fs {
6
7 constexpr size_t CopyBufferSize = 0x10000;
8 constexpr size_t ReallocBufferSize = 0x10000;
9
10 enum class OpenMode : u8 {
11 Read,
12 Write,
13 Update
14 };
15
16 inline constexpr bool CanReadWithMode(const OpenMode mode) {
17 return mode == OpenMode::Read;
18 }
19
20 inline constexpr bool CanWriteWithMode(const OpenMode mode) {
21 return (mode == OpenMode::Write) || (mode == OpenMode::Update);
22 }
23
24 enum class Position : u8 {
25 Begin,
26 Current
27 };
28
29 enum class FileCompression : u8 {
30 None,
31 LZ77
32 };
33
34 struct FileHandle {
35 virtual bool Exists(const std::string &path, size_t &out_size) = 0;
36 virtual Result Open(const std::string &path, const OpenMode mode) = 0;
37 virtual Result GetSize(size_t &out_size) = 0;
38 virtual Result SetOffset(const size_t offset, const Position pos) = 0;
39 virtual Result GetOffset(size_t &out_offset) = 0;
40 virtual Result Read(void *read_buf, const size_t read_size, size_t &out_read_size) = 0;
41 virtual Result Write(const void *write_buf, const size_t write_size) = 0;
42 virtual Result Close() = 0;
43 };
44
45 struct FileFormat {
46 std::string read_path;
47 std::shared_ptr<fs::FileHandle> read_file_handle;
48 std::string write_path;
49 std::shared_ptr<fs::FileHandle> write_file_handle;
50 fs::FileCompression comp;
51
52 virtual Result ValidateImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) = 0;
53
54 inline Result Validate(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp = fs::FileCompression::None) {
55 return this->ValidateImpl(path, file_handle, comp);
56 }
57
58 virtual Result ReadImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) = 0;
59
60 inline Result ReadFrom(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp = fs::FileCompression::None) {
61 NTR_R_TRY(this->ValidateImpl(path, file_handle, comp));
62 NTR_R_TRY(this->ReadImpl(path, file_handle, comp));
63
64 this->read_path = path;
65 this->read_file_handle = file_handle;
66 this->comp = comp;
67 NTR_R_SUCCEED();
68 }
69
70 inline Result ReadCompressedFrom(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle) {
71 return this->ReadFrom(path, file_handle, fs::FileCompression::LZ77);
72 }
73
74 virtual Result WriteImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) = 0;
75
76 inline Result WriteTo(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle) {
77 NTR_R_TRY(this->WriteImpl(path, file_handle, this->comp));
78
79 this->write_path = path;
80 this->write_file_handle = file_handle;
81 NTR_R_SUCCEED();
82 }
83
84 inline Result WriteTo(std::shared_ptr<fs::FileHandle> file_handle) {
85 return this->WriteTo(this->read_path, file_handle);
86 }
87 };
88
89 void SetExternalFsDirectory(const std::string &path);
90 std::string &GetExternalFsDirectory();
91
93 u32 ext_fs_id;
94 std::string ext_fs_root_path;
95
97
98 inline std::string GetExternalFsPath(const std::string &path) {
99 return this->ext_fs_root_path + "/" + path;
100 }
101
102 inline std::string GetBasePath(const std::string &ext_fs_path) {
103 return ext_fs_path.substr(this->ext_fs_root_path.length() + 1);
104 }
105
106 Result WriteImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) override {
107 NTR_R_SUCCEED();
108 }
109
110 virtual Result SaveFileSystem() = 0;
111 };
112
113 std::string GetAbsolutePath(const std::string &path);
114
115 inline std::string RemoveLastEntry(const std::string &path) {
116 const auto find_pos = path.find_last_of("/");
117 if(find_pos == std::string::npos) {
118 return "";
119 }
120 else {
121 return path.substr(0, find_pos);
122 }
123 }
124
125 inline std::string GetFileName(const std::string &path) {
126 return path.substr(path.find_last_of("/") + 1);
127 }
128
129 inline std::string GetBaseDirectory(const std::string &path) {
130 return RemoveLastEntry(path);
131 }
132
133 inline std::string GetFileExtension(const std::string &path) {
134 return path.substr(path.find_last_of(".") + 1);
135 }
136
137}
Definition ntr_Include.hpp:75
Definition fs_Base.hpp:92
Definition fs_Base.hpp:45
Definition fs_Base.hpp:34