Refactor code out of main.cpp
This commit is contained in:
parent
463efe1773
commit
4216c86c09
4 changed files with 214 additions and 155 deletions
160
main.cpp
160
main.cpp
|
@ -16,142 +16,18 @@
|
|||
*/
|
||||
|
||||
#include "main_window.hpp"
|
||||
#include "notebook_tree.hpp"
|
||||
#include <QApplication>
|
||||
#include <iostream>
|
||||
#include <simdjson.h>
|
||||
#include <string_view>
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
#define BASE_PATH "/run/media/duckz/Sabrent/deleme/remarkable_20210324/xochitl/"
|
||||
//#define BASE_PATH "/home/duckz/dev/code/cpp/remarkable_tool/lib/lines-are-beautiful/share/rmlab/examples/"
|
||||
|
||||
struct NotebookInfo {
|
||||
std::string visible_name;
|
||||
std::string parent;
|
||||
std::string type;
|
||||
std::vector<std::string> files;
|
||||
std::uint64_t version = 0;
|
||||
std::uintmax_t total_size = 0;
|
||||
bool deleted = false;
|
||||
bool isolated = true;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
Node() :
|
||||
info(nullptr),
|
||||
parent(nullptr)
|
||||
{ }
|
||||
|
||||
Node (std::string_view name, const NotebookInfo* payload, Node* parent) :
|
||||
name(name),
|
||||
info(payload),
|
||||
parent(parent)
|
||||
{ }
|
||||
|
||||
std::vector<Node> children;
|
||||
std::string_view name;
|
||||
const NotebookInfo* info;
|
||||
const Node* parent;
|
||||
};
|
||||
|
||||
typedef std::unordered_map<std::string, NotebookInfo> NotebookMapType;
|
||||
|
||||
template <typename T>
|
||||
std::vector<T>& operator+= (std::vector<T>& dst, const std::vector<T>& src) {
|
||||
dst.insert(dst.end(), src.begin(), src.end());
|
||||
return dst;
|
||||
}
|
||||
|
||||
NotebookMapType build_notebook_infos (
|
||||
const fs::path& from_path
|
||||
) {
|
||||
simdjson::dom::parser parser;
|
||||
NotebookMapType retval;
|
||||
|
||||
for (const auto& entry : fs::directory_iterator(from_path)) {
|
||||
NotebookInfo& curr_info = retval[entry.path().stem()];
|
||||
|
||||
if (entry.path().extension() == ".metadata") {
|
||||
simdjson::dom::object json_doc = parser.load(entry.path());
|
||||
|
||||
curr_info.deleted = json_doc["deleted"];
|
||||
curr_info.parent = json_doc["parent"];
|
||||
curr_info.type = json_doc["type"];
|
||||
curr_info.version = json_doc["version"];
|
||||
curr_info.visible_name = json_doc["visibleName"];
|
||||
curr_info.isolated = false;
|
||||
}
|
||||
else if (fs::is_directory(entry)) {
|
||||
for (const auto& subentry : fs::recursive_directory_iterator(entry)) {
|
||||
curr_info.files.push_back(fs::relative(subentry, from_path));
|
||||
if (fs::is_regular_file(subentry))
|
||||
curr_info.total_size += fs::file_size(subentry);
|
||||
}
|
||||
}
|
||||
|
||||
curr_info.files.push_back(fs::relative(entry, from_path));
|
||||
if (fs::is_regular_file(entry))
|
||||
curr_info.total_size += fs::file_size(entry);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::vector<Node> to_tree (const NotebookMapType& nbs) {
|
||||
std::unordered_map<std::string_view, std::pair<unsigned int, Node>> parents;
|
||||
for (const auto& nb : nbs) {
|
||||
auto it_ins = parents.emplace(nb.second.parent, std::make_pair(0U, Node{nb.second.parent, nullptr, nullptr}));
|
||||
it_ins.first->second.first++;
|
||||
}
|
||||
for (const auto& nb : nbs) {
|
||||
auto it_parent = parents.find(nb.first);
|
||||
if (parents.cend() != it_parent) {
|
||||
Node& node = it_parent->second.second;
|
||||
node.info = &nb.second;
|
||||
}
|
||||
}
|
||||
for (auto& par : parents) {
|
||||
Node& node = par.second.second;
|
||||
const unsigned int children_count = par.second.first;
|
||||
node.children.reserve(children_count);
|
||||
}
|
||||
|
||||
for (const auto& nb : nbs) {
|
||||
if (0 == parents.count(nb.first)) {
|
||||
Node& parent = parents[nb.second.parent].second;
|
||||
parent.children.emplace_back(nb.first, &nb.second, &parent);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Node> roots;
|
||||
std::unordered_map<std::string_view, Node*> grouped_nodes;
|
||||
for (auto it = parents.begin(), it_end = parents.end(); it != it_end; it = parents.erase(it)) {
|
||||
auto& node = it->second.second;
|
||||
if (node.info) {
|
||||
auto it_parent = parents.find(node.info->parent);
|
||||
Node* dst_node = (parents.end() == it_parent ? grouped_nodes[node.info->parent] : &it_parent->second.second);
|
||||
dst_node->children.push_back(std::move(node));
|
||||
grouped_nodes[dst_node->children.back().name] = &dst_node->children.back();
|
||||
}
|
||||
else {
|
||||
roots.push_back(std::move(node));
|
||||
grouped_nodes[node.name] = &roots.back();
|
||||
}
|
||||
}
|
||||
|
||||
return roots;
|
||||
}
|
||||
|
||||
void print_tree (const Node& tree, std::string_view desc={}, std::string indent={}) {
|
||||
void print_tree (const duck::Node& tree, std::string_view desc={}, std::string indent={}) {
|
||||
std::cout << indent << desc << " (\"" << tree.name << "\")";
|
||||
if (tree.info && tree.info->isolated)
|
||||
std::cout << " ISOLATED";
|
||||
|
@ -174,27 +50,6 @@ std::uintmax_t recursive_size (const Node& tree) {
|
|||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::vector<const NotebookInfo*> to_flat_notebook_list (const Node& tree) {
|
||||
std::vector<const NotebookInfo*> retval;
|
||||
for (const auto& child : tree.children) {
|
||||
retval += to_flat_notebook_list(child);
|
||||
}
|
||||
if (tree.info)
|
||||
retval.push_back(tree.info);
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::vector<const NotebookInfo*> make_dele_list (const Node& tree) {
|
||||
if (tree.info and (tree.info->deleted or tree.info->isolated))
|
||||
return to_flat_notebook_list(tree);
|
||||
|
||||
std::vector<const NotebookInfo*> retval;
|
||||
for (const auto& child : tree.children) {
|
||||
retval += make_dele_list(child);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
|
@ -204,15 +59,10 @@ int main (int argc, char* argv[]) {
|
|||
duck::MainWindow win;
|
||||
win.show();
|
||||
|
||||
auto notebooks = build_notebook_infos(BASE_PATH);
|
||||
std::vector<Node> roots = to_tree(notebooks);
|
||||
std::vector<const NotebookInfo*> dele_list;
|
||||
std::vector<duck::Node> roots = duck::build_notebook_tree(BASE_PATH);
|
||||
std::vector<const duck::NotebookInfo*> dele_list = duck::make_list_of_deleted(roots);
|
||||
|
||||
for (const auto& root : roots) {
|
||||
dele_list += make_dele_list(root);
|
||||
}
|
||||
|
||||
for (const NotebookInfo* nb : dele_list) {
|
||||
for (const duck::NotebookInfo* nb : dele_list) {
|
||||
for (const auto& f : nb->files) {
|
||||
std::cout << "/home/root/.local/share/remarkable/xochitl/" << f << '\n';
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue