fix subvariant parsing/insertion into tree
This commit is contained in:
parent
6f41bc9180
commit
0eaca7d0c9
3 changed files with 75 additions and 14 deletions
|
@ -21,6 +21,25 @@ namespace vxorg {
|
|||
return res;
|
||||
}
|
||||
|
||||
template<class Fn>
|
||||
void walk_parents_in_tree_order(VxHeavenTree::Node* node, Fn&& fn) {
|
||||
std::string sample_name {};
|
||||
std::vector<VxHeavenTree::Node*> 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<vxorg::VxHeavenItem*> 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];
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "tree.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
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
|
31
vxorg.cpp
31
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;
|
||||
}
|
Loading…
Reference in a new issue