libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ramfs.cpp
1#include "ramfs.hpp"
2
3using namespace LibXR;
4
10RamFS::RamFS(const char* name) : root_(name), bin_("bin") { root_.Add(bin_); }
11
20int RamFS::CompareStr(const char* const& a, const char* const& b) { return strcmp(a, b); }
21
29char* RamFS::DuplicateName(const char* name)
30{
31 ASSERT(name != nullptr);
32 if (name == nullptr)
33 {
34 return nullptr;
35 }
36
37 char* name_buff = new char[strlen(name) + 1];
38 strcpy(name_buff, name);
39 return name_buff;
40}
41
47RamFS::FsNode::FsNode(FsNodeType node_type) : type_(node_type), tree_node_(this) {}
48
55 : name_(other.name_), type_(other.type_), parent_(nullptr), tree_node_(this)
56{
57}
58
63RamFS::File::File() : FsNode(FsNodeType::FILE), addr_(nullptr), arg_(nullptr), size_(0) {}
64
70RamFS::File::File(const char* name) : File() { this->name_ = DuplicateName(name); }
71
79int RamFS::File::Run(int argc, char** argv)
80{
81 ASSERT(file_type_ == FileType::EXEC);
82 ASSERT(exec_ != nullptr);
83 return exec_(arg_, argc, argv);
84}
85
91
99RamFS::Custom::Custom(const char* name, uint32_t kind, void* context) : Custom()
100{
101 this->name_ = DuplicateName(name);
102 this->kind_ = kind;
103 this->context_ = context;
104}
105
111
117RamFS::Dir::Dir(const char* name) : Dir() { this->name_ = DuplicateName(name); }
118
125{
126 ASSERT(node.name_ != nullptr);
127 ASSERT(FindNode(node.name_) == nullptr);
128 node.parent_ = this;
129 rbt_.Insert(node.tree_node_, node.name_);
130}
131
140{
141 if (name == nullptr)
142 {
143 return nullptr;
144 }
145
146 auto* node = rbt_.Search<FsNode*>(name);
147 return node != nullptr ? node->data_ : nullptr;
148}
149
159{
160 auto* ans = FindNode(name);
161 if (ans == nullptr || ans->GetNodeType() != type)
162 {
163 return nullptr;
164 }
165 return ans;
166}
167
182{
183 auto* ans = FindNodeByType(name, type);
184 if (ans != nullptr)
185 {
186 return ans;
187 }
188
189 struct FindNodeRevFn
190 {
191 const char* name_;
192 FsNodeType type_;
193 FsNode*& ans_;
194
195 ErrorCode operator()(Tree::Node<FsNode*>& item)
196 {
197 auto* node = item.data_;
198 if (node->GetNodeType() != FsNodeType::DIR)
199 {
200 return ErrorCode::OK;
201 }
202
203 ans_ = static_cast<Dir*>(node)->FindNodeRevByType(name_, type_);
204 return ans_ != nullptr ? ErrorCode::FAILED : ErrorCode::OK;
205 }
206 };
207
208 FindNodeRevFn find{name, type, ans};
209 rbt_.Foreach<FsNode*>(find);
210 return ans;
211}
212
221{
222 return static_cast<File*>(FindNodeByType(name, FsNodeType::FILE));
223}
224
233{
234 return static_cast<File*>(FindNodeRevByType(name, FsNodeType::FILE));
235}
236
245{
246 if (name == nullptr)
247 {
248 return nullptr;
249 }
250
251 if (name[0] == '.' && name[1] == '\0')
252 {
253 return this;
254 }
255
256 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
257 {
258 return parent_;
259 }
260
261 return static_cast<Dir*>(FindNodeByType(name, FsNodeType::DIR));
262}
263
272{
273 if (name == nullptr)
274 {
275 return nullptr;
276 }
277
278 if (name[0] == '.' && name[1] == '\0')
279 {
280 return this;
281 }
282
283 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
284 {
285 return parent_;
286 }
287
288 return static_cast<Dir*>(FindNodeRevByType(name, FsNodeType::DIR));
289}
290
299{
300 return static_cast<Custom*>(FindNodeByType(name, FsNodeType::CUSTOM));
301}
302
311{
312 return static_cast<Custom*>(FindNodeRevByType(name, FsNodeType::CUSTOM));
313}
自定义节点片段 / Custom-node fragment
Definition ramfs.hpp:11
void * context_
用户自管上下文指针 / Caller-owned user context pointer.
Definition ramfs.hpp:24
Custom()
构造一个空自定义节点壳 / Construct one empty custom-node shell
Definition ramfs.cpp:90
uint32_t kind_
用户定义类型标签 / User-defined kind tag.
Definition ramfs.hpp:23
目录节点片段 / Directory-node fragment
Definition ramfs.hpp:10
Dir()
构造一个空目录壳 / Construct one empty directory shell
Definition ramfs.cpp:110
FsNode * FindNode(const char *name)
查找直属子节点 / Find a direct child node
Definition ramfs.cpp:139
File * FindFile(const char *name)
查找直属文件 / Find a direct child file
Definition ramfs.cpp:220
Dir * FindDir(const char *name)
查找直属目录,支持 "." 和 ".." / Find a direct child directory, supporting "." and ".."
Definition ramfs.cpp:244
File * FindFileRev(const char *name)
递归查找文件 / Find a file recursively
Definition ramfs.cpp:232
FsNode * FindNodeRevByType(const char *name, FsNodeType type)
递归查找指定类型节点 / Find one node of a given type recursively
Definition ramfs.cpp:181
void Add(File &file)
添加直属文件节点 / Add a direct child file node
Definition ramfs.hpp:16
void AddNode(FsNode &node)
把一个节点挂到当前目录下 / Attach one node under the current directory
Definition ramfs.cpp:124
Custom * FindCustom(const char *name)
查找直属自定义节点 / Find a direct child custom node
Definition ramfs.cpp:298
Custom * FindCustomRev(const char *name)
递归查找自定义节点 / Find a custom node recursively
Definition ramfs.cpp:310
FsNode * FindNodeByType(const char *name, FsNodeType type)
在当前目录按类型查找直属节点 / Find one direct child node of a given type
Definition ramfs.cpp:158
Dir * FindDirRev(const char *name)
递归查找目录,支持 "." 和 ".." / Find a directory recursively, supporting "." and ".."
Definition ramfs.cpp:271
文件节点片段 / File-node fragment
Definition ramfs.hpp:10
int Run(int argc, char **argv)
执行可执行文件 / Run an executable file
Definition ramfs.cpp:79
File()
构造一个空文件壳 / Construct one empty file shell
Definition ramfs.cpp:63
节点基类片段 / Base-node fragment
Definition ramfs.hpp:10
FsNode(FsNodeType node_type)
用指定节点类型构造基类部分 / Construct the base node with a given node type
Definition ramfs.cpp:47
Tree::Node< FsNode * > tree_node_
当前节点挂进目录树时使用的树节点包装 / Tree node wrapper used when inserted into a directory tree.
Definition ramfs.hpp:47
Dir * parent_
父目录;根目录保持为空 / Parent directory; stays null for the root.
Definition ramfs.hpp:27
const char * name_
节点名称缓冲区 / Retained node-name buffer.
Definition ramfs.hpp:25
轻量级内存文件系统 / Lightweight in-memory file system
Definition ramfs.hpp:23
RamFS(const char *name="ramfs")
构造 RamFS,并创建根目录和 bin 目录 / Construct RamFS with root and bin directories
Definition ramfs.cpp:10
FsNodeType
文件系统节点类型 / File-system node type
Definition ramfs.hpp:38
@ CUSTOM
用户自定义节点 / User-defined node
@ DIR
目录 / Directory
static char * DuplicateName(const char *name)
复制并持有一个节点名称 / Duplicate and retain one node name
Definition ramfs.cpp:29
static int CompareStr(const char *const &a, const char *const &b)
RamFS 名称比较函数 / Name comparator used by RamFS trees.
Definition ramfs.cpp:20
@ EXEC
可执行命令入口 / Executable command entry
Dir bin_
预留的可执行文件目录 / Reserved executable-file directory.
Definition ramfs.hpp:149
Dir root_
根目录;所有外部 Add()/Find*() 默认都从这里进入 / Root directory; all external Add()/Find*() entry through here.
Definition ramfs.hpp:148
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举