From 4216c86c09f9ec61b9ec72e71463533a624945d8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 26 Mar 2021 21:51:37 +0100 Subject: [PATCH] Refactor code out of main.cpp --- main.cpp | 160 ++-------------------------------------------- meson.build | 1 + notebook_tree.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++ notebook_tree.hpp | 57 +++++++++++++++++ 4 files changed, 214 insertions(+), 155 deletions(-) create mode 100644 notebook_tree.cpp create mode 100644 notebook_tree.hpp diff --git a/main.cpp b/main.cpp index f055b82..084acaa 100644 --- a/main.cpp +++ b/main.cpp @@ -16,142 +16,18 @@ */ #include "main_window.hpp" +#include "notebook_tree.hpp" #include #include -#include #include -#include #include -#include -#include -#include -#include #include -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 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 children; - std::string_view name; - const NotebookInfo* info; - const Node* parent; -}; - -typedef std::unordered_map NotebookMapType; - -template -std::vector& operator+= (std::vector& dst, const std::vector& 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 to_tree (const NotebookMapType& nbs) { - std::unordered_map> 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 roots; - std::unordered_map 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 to_flat_notebook_list (const Node& tree) { - std::vector 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 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 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 roots = to_tree(notebooks); - std::vector dele_list; + std::vector roots = duck::build_notebook_tree(BASE_PATH); + std::vector 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'; } diff --git a/meson.build b/meson.build index a22843f..35deab5 100644 --- a/meson.build +++ b/meson.build @@ -32,6 +32,7 @@ executable(meson.project_name(), 'main.cpp', 'main_window.cpp', moc_files, + 'notebook_tree.cpp', dependencies: [ simdjson_dep, qt5_dep, diff --git a/notebook_tree.cpp b/notebook_tree.cpp new file mode 100644 index 0000000..127400f --- /dev/null +++ b/notebook_tree.cpp @@ -0,0 +1,151 @@ +/* Copyright 2021, Michele Santullo + * This file is part of remarkable_tool. + * + * remarkable_tool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Remarkable_tool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with remarkable_tool. If not, see . + */ + +#include "notebook_tree.hpp" +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; + +namespace duck { +namespace { +typedef std::unordered_map NotebookMapType; + +template +std::vector& operator+= (std::vector& dst, const std::vector& 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 to_tree (const NotebookMapType& nbs) { + std::unordered_map> 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 roots; + std::unordered_map 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; +} + +std::vector to_flat_notebook_list (const Node& tree) { + std::vector 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 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 retval; + for (const auto& child : tree.children) { + retval += make_dele_list(child); + } + return retval; +} +} //unnamed namespace + +std::vector build_notebook_tree (std::string_view base_path) { + auto notebooks = build_notebook_infos(base_path); + return to_tree(notebooks); +} + +std::vector make_list_of_deleted (const std::vector& roots) { + std::vector dele_list; + for (const auto& root : roots) { + dele_list += make_dele_list(root); + } + return dele_list; +} +} //namespace duck diff --git a/notebook_tree.hpp b/notebook_tree.hpp new file mode 100644 index 0000000..65b7f61 --- /dev/null +++ b/notebook_tree.hpp @@ -0,0 +1,57 @@ +/* Copyright 2021, Michele Santullo + * This file is part of remarkable_tool. + * + * remarkable_tool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Remarkable_tool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with remarkable_tool. If not, see . + */ + +#pragma once + +#include +#include +#include +#include + +namespace duck { +struct NotebookInfo { + std::string visible_name; + std::string parent; + std::string type; + std::vector 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 children; + std::string_view name; + const NotebookInfo* info; + const Node* parent; +}; + +std::vector build_notebook_tree (std::string_view base_path); +std::vector make_list_of_deleted (const std::vector& roots); +} //namespace duck