-
Notifications
You must be signed in to change notification settings - Fork 0
Add filetree definitions #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: cpp
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #include "filetree.hpp" | ||
|
|
||
| #include <memory> | ||
| #include <map> | ||
| #include <deque> | ||
| #include <filesystem> | ||
| #include <functional> | ||
| #include <tempeh/common/typedefs.hpp> | ||
| #include <cassert> | ||
| #include <system_error> | ||
| #include <list> | ||
|
|
||
| namespace TempehEditor::FileSystem { | ||
|
|
||
| namespace fs = std::filesystem; | ||
|
|
||
| FileTree::FileTree(const fs::path &path_str) | ||
| { | ||
| std::list<FileTreeMap::iterator> list; | ||
|
|
||
| auto [iter, _] = hashmap.insert_or_assign(path_str.string(), | ||
| nullptr); | ||
|
|
||
| list.push_back(iter); | ||
|
|
||
| while (!list.empty()) { | ||
| auto current_entry = list.front(); | ||
| list.pop_front(); | ||
| for (const auto & entry : fs::directory_iterator(current_entry->first)) { | ||
|
|
||
| fs::path p = entry.path(); | ||
|
|
||
| std::shared_ptr<FileTree> tree; | ||
|
|
||
| if (fs::path(p).filename() == ".git") { | ||
|
||
| continue; | ||
| } | ||
| if (fs::is_directory(p)) { | ||
| tree = std::make_shared<FileTree>(p); | ||
| } | ||
|
|
||
| auto [i, _] = hashmap.insert_or_assign(p.string(), tree); | ||
| list.push_back(i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO | ||
| void FileTree::traversal(std::function<void(FileMetaData)> f) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #ifndef _TEMPEH_FILE_FILETREE_HPP | ||
| #define _TEMPEH_FILE_FILETREE_HPP | ||
|
|
||
| #include <memory> | ||
| #include <map> | ||
| #include <deque> | ||
| #include <filesystem> | ||
| #include <functional> | ||
| #include <tempeh/common/typedefs.hpp> | ||
|
|
||
| namespace TempehEditor::FileSystem | ||
| { | ||
|
|
||
| struct FileMetaData | ||
| { | ||
| std::string name; | ||
| u32 size; | ||
| }; | ||
|
|
||
| class FileTree | ||
| { | ||
| private: | ||
| using FileTreeMap = std::map<std::string, std::shared_ptr<FileTree>>; | ||
| FileTreeMap hashmap; | ||
| public: | ||
| FileTree(const std::filesystem::path &path_str); | ||
|
|
||
|
|
||
|
|
||
| // Implemented as BFS | ||
| void traversal(std::function<void(FileMetaData)> f); | ||
| }; | ||
| } | ||
|
|
||
| #endif //_TEMPEH_FILE_FILETREE_HPP |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <boost/predef.h> | ||
| #include <filesystem/filetree.hpp> | ||
|
|
||
| TEST(FileSystem, FileTree_ObjectCreation) | ||
| { | ||
| // For debugging purpose | ||
| TempehEditor::FileSystem::FileTree test_obj("/home/rahman/Projects/tempeh-engine/test/dummy-dir"); | ||
|
|
||
| // ASSERT_DEATH({ | ||
| // TempehEditor::FileSystem::FileTree test_obj(std::string("/blah")); | ||
| // }, "Path did not exists"); | ||
| // | ||
| //#ifdef BOOST_OS_WINDOWS | ||
| // ASSERT_NO_FATAL_FAILURE({ | ||
| // // Uhhh... is C:\\ is not a `standard`? | ||
| // TempehEditor::FileSystem::FileTree test_obj(std::string("C:\\Windows")); | ||
| // }); | ||
| //#elif BOOST_OS_LINUX | ||
| // // TODO linux | ||
| //#endif | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use list.
https://dzone.com/articles/c-benchmark-%E2%80%93-stdvector-vs