151 lines
4.6 KiB
C++
151 lines
4.6 KiB
C++
/* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "notebook_tree.hpp"
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <stdexcept>
|
|
#include <filesystem>
|
|
#include <simdjson.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace duck {
|
|
namespace {
|
|
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;
|
|
}
|
|
|
|
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
|
|
|
|
std::vector<Node> build_notebook_tree (std::string_view base_path) {
|
|
auto notebooks = build_notebook_infos(base_path);
|
|
return to_tree(notebooks);
|
|
}
|
|
|
|
std::vector<const NotebookInfo*> make_list_of_deleted (const std::vector<Node>& roots) {
|
|
std::vector<const NotebookInfo*> dele_list;
|
|
for (const auto& root : roots) {
|
|
dele_list += make_dele_list(root);
|
|
}
|
|
return dele_list;
|
|
}
|
|
} //namespace duck
|