From fb66c6f510dae62dae11afe50e5e55b791b3768a Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Sun, 19 Jan 2025 22:16:12 +0100 Subject: [PATCH] BUGFIX for getting correct files --- tree_structurer/cpp/tree_structurer.cpp | 79 +++++++++++++------------ 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/tree_structurer/cpp/tree_structurer.cpp b/tree_structurer/cpp/tree_structurer.cpp index db3834d..23ff0db 100644 --- a/tree_structurer/cpp/tree_structurer.cpp +++ b/tree_structurer/cpp/tree_structurer.cpp @@ -7,20 +7,26 @@ namespace fs = std::filesystem; bool TreeStructurer::should_ignore_dir(const std::string& dirname) { static const std::vector ignore_list = { "build", "venv", "myenv", "dist", "node_modules", "CMakeFiles", - ".git", ".idea", ".vscode", "__pycache__" + ".git", ".idea", ".vscode", "__pycache__", "**pycache**" }; if (std::find(ignore_list.begin(), ignore_list.end(), dirname) != ignore_list.end()) { return true; } - if (!dirname.empty() && (dirname[0] == '.' || dirname[0] == '_')) { - return true; + if (!dirname.empty()) { + if (dirname[0] == '.' || dirname[0] == '_') { + return true; + } + if (dirname.find("__") == 0 && dirname.find("__", 2) != std::string::npos) { + return true; + } } return false; } +// Add the missing should_ignore_file implementation bool TreeStructurer::should_ignore_file(const std::string& filename) { static const std::vector ignore_extensions = { ".pyc", ".pyo", ".pyd", ".so", ".dll", ".dylib", @@ -40,6 +46,12 @@ bool TreeStructurer::should_ignore_file(const std::string& filename) { return false; } +// Add the missing get_relative_path implementation +std::string TreeStructurer::get_relative_path(const fs::path& path, const fs::path& base) { + fs::path rel = fs::relative(path, base); + return rel.string(); +} + std::vector TreeStructurer::get_filtered_paths(const fs::path& start) { std::vector paths; fs::directory_options options = fs::directory_options::skip_permission_denied; @@ -51,7 +63,6 @@ std::vector TreeStructurer::get_filtered_paths(const fs::path& start) for (const auto& entry : fs::recursive_directory_iterator(start, options)) { const auto& path = entry.path(); - // Check if any parent directory should be ignored bool should_skip = false; for (const auto& component : path) { if (should_ignore_dir(component.string())) { @@ -62,11 +73,11 @@ std::vector TreeStructurer::get_filtered_paths(const fs::path& start) if (should_skip) continue; if (entry.is_directory()) { - if (should_ignore_dir(path.filename().string())) { + if (!should_ignore_dir(path.filename().string())) { paths.push_back(path); } } else { - if (should_ignore_file(path.filename().string())) { + if (!should_ignore_file(path.filename().string())) { paths.push_back(path); } } @@ -80,55 +91,47 @@ std::vector TreeStructurer::get_filtered_paths(const fs::path& start) return paths; } -std::string TreeStructurer::get_relative_path(const fs::path& path, const fs::path& base) { - fs::path rel = fs::relative(path, base); - return rel.string(); -} - std::vector TreeStructurer::get_directory_structure(const std::string& startpath) { std::vector result; - fs::path start = startpath.empty() ? fs::current_path() : fs::path(startpath); + + // Normalize the input path by removing ./ or .\ prefix if present + std::string normalized_path = startpath; + if (startpath.substr(0, 2) == ".\\" || startpath.substr(0, 2) == "./") { + normalized_path = startpath.substr(2); + } + + fs::path start = normalized_path.empty() ? fs::current_path() : fs::path(normalized_path); try { auto paths = get_filtered_paths(start); if (paths.empty()) return result; - // Don't add the root directory name if we're in the current directory - if (start != fs::current_path()) { - result.push_back(start.filename().string()); - } + // Don't add the start directory name to the output + // Remove the following lines: + // if (start != fs::current_path()) { + // result.push_back(start.filename().string()); + // } std::vector is_last_at_level(256, false); - std::string last_prefix; - + for (size_t i = 1; i < paths.size(); ++i) { const auto& path = paths[i]; std::string rel_path = get_relative_path(path, start); - - // Split the path into components and check each component - bool should_skip = false; - for (const auto& component : fs::path(rel_path)) { - if (should_ignore_dir(component.string())) { - should_skip = true; - break; - } - } - if (should_skip) continue; - + int level = std::count(rel_path.begin(), rel_path.end(), fs::path::preferred_separator); - // Check if this is the last item at this level - bool is_last = false; - for (size_t j = i + 1; j < paths.size(); j++) { - const auto& next_path = paths[j]; - std::string next_rel_path = get_relative_path(next_path, start); + bool is_last = true; + for (size_t j = i + 1; j < paths.size(); ++j) { + std::string next_rel_path = get_relative_path(paths[j], start); int next_level = std::count(next_rel_path.begin(), next_rel_path.end(), fs::path::preferred_separator); - if (next_level <= level) { - is_last = (j == paths.size() - 1) || (next_level < level); + if (next_level == level) { + is_last = false; + break; + } + if (next_level < level) { break; } } - if (i == paths.size() - 1) is_last = true; is_last_at_level[level] = is_last; @@ -152,4 +155,4 @@ std::vector TreeStructurer::get_directory_structure(const std::stri } return result; -} +} \ No newline at end of file