From 0eaca7d0c920d04d1a91da8ddb5a1cba5c9f528e Mon Sep 17 00:00:00 2001 From: modeco80 Date: Mon, 21 Oct 2024 20:50:52 -0400 Subject: [PATCH] fix subvariant parsing/insertion into tree --- vxheaven_parse.cpp | 54 +++++++++++++++++++++++++++++++++++----------- vxheaven_parse.hpp | 4 ++++ vxorg.cpp | 31 +++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/vxheaven_parse.cpp b/vxheaven_parse.cpp index 799488b..ce2d897 100644 --- a/vxheaven_parse.cpp +++ b/vxheaven_parse.cpp @@ -21,6 +21,25 @@ namespace vxorg { return res; } + template + void walk_parents_in_tree_order(VxHeavenTree::Node* node, Fn&& fn) { + std::string sample_name {}; + std::vector parent_list {}; + vxorg::VxHeavenTree::Node* parent = node->parent_node(); + + while(parent) { + if(parent->is_root()) + break; + + parent_list.push_back(parent); + parent = parent->parent_node(); + } + + for(auto& item : std::views::reverse(parent_list)) { + fn(item); + } + } + std::string get_sample_name(VxHeavenTree::Node* node) { if(node == nullptr) return ""; @@ -29,26 +48,28 @@ namespace vxorg { return node->data().name; std::string sample_name {}; - std::vector parent_list {}; - vxorg::VxHeavenTree::Node* parent = node->parent_node(); - while(parent) { - if(parent->data().name.empty()) - break; - - parent_list.push_back(&parent->data()); - parent = parent->parent_node(); - } - - for(auto& item : std::views::reverse(parent_list)) { - sample_name += std::format("{}.", item->name); - } + walk_parents_in_tree_order(node, [&](auto* node) { + sample_name += std::format("{}.", node->data().name); + }); sample_name += node->data().name; return sample_name; } + std::filesystem::path get_sample_path(VxHeavenTree::Node* node) { + if(node == nullptr) + return {}; + + std::filesystem::path path; +walk_parents_in_tree_order(node, [&](auto* node) { + path /= node->data().name; + }); + + return path; + } + void parse_into_tree(VxHeavenTree& tree, std::istream& is) { std::string line {}; while(std::getline(is, line)) { @@ -106,10 +127,17 @@ namespace vxorg { for(auto& subvariant : subvariants) { if(auto* node = leaf->find_child([&](auto* node) { return node->data().name == subvariant; }); node == nullptr) { + //printf("creating variant %.*s %.*s\n", split[2].length(), split[2].data(), subvariant.length(), subvariant.data()); leaf = leaf->create_leaf({ .name = std::string(subvariant.data(), subvariant.length()), .is_sample = false }); + } else { + // existing node for a subvariant + //printf("existing variant %.*s %.*s\n", split[2].length(), split[2].data(), subvariant.length(), subvariant.data()); + leaf = node; + continue; } } + // The last node we visit is the sample leaf->data().is_sample = true; } else { auto subvariant = split[3]; diff --git a/vxheaven_parse.hpp b/vxheaven_parse.hpp index 75f0e16..31b1192 100644 --- a/vxheaven_parse.hpp +++ b/vxheaven_parse.hpp @@ -3,6 +3,8 @@ #include "tree.hpp" +#include + namespace vxorg { struct VxHeavenItem { @@ -15,5 +17,7 @@ namespace vxorg { std::string get_sample_name(VxHeavenTree::Node* node); + std::filesystem::path get_sample_path(VxHeavenTree::Node* node); + void parse_into_tree(VxHeavenTree& tree, std::istream& is); } // namespace vxorg \ No newline at end of file diff --git a/vxorg.cpp b/vxorg.cpp index 951e6c6..21715a7 100644 --- a/vxorg.cpp +++ b/vxorg.cpp @@ -11,11 +11,19 @@ int main() { vxorg::parse_into_tree(tree, ifs); + std::filesystem::path root = std::filesystem::current_path() / "testdata"; + std::filesystem::path unorg = root / "unorg"; + std::filesystem::path org = root / "org"; + + if(!std::filesystem::exists(org)) + std::filesystem::create_directories(org); + // walk the resulting tree - tree.walk([](auto* node) { + tree.walk([&](auto* node) { auto tabulation_level = node->parent_count(); auto& data = node->data(); +#if 0 if(tabulation_level != 0) { for(auto i = 0; i < tabulation_level; ++i) { std::printf("\t"); @@ -32,6 +40,27 @@ int main() { std::printf("%s\n", data.name.c_str()); } } +#endif + +#if 1 + + if(!node->is_root()) { + if(data.is_sample) { + std::string sample_name = vxorg::get_sample_name(node); + + // paths + auto path = org / vxorg::get_sample_path(node); + auto source_path = unorg / vxorg::get_sample_name(node); + + if(!std::filesystem::exists(source_path)) { + std::printf("WARNING: sample %s/%s in tree (source disk file %s) does not exist\n", path.string().c_str(), sample_name.c_str(), source_path.string().c_str()); + } + + + //std::printf("sample %s/%s in tree (source disk file %s)\n", path.string().c_str(), sample_name.c_str(), source_path.string().c_str()); + } + } +#endif }); return 0; } \ No newline at end of file