diff --git a/src/Archiver.cpp b/src/Archiver.cpp index df32a25..ff74a93 100644 --- a/src/Archiver.cpp +++ b/src/Archiver.cpp @@ -40,24 +40,31 @@ fs::path archive_extension(const fs::path& path) { fs::path strip_archive_extension(const fs::path& path) { // TODO: creates lots of paths, refactor int longest_ext = 0; + int tmp_longest_ext = 0; fs::path tmp_ext; fs::path tmp_path = path; fs::path stem_path = path; + spdlog::debug("Checking {} extensions", path); + while (tmp_path.has_extension()) { tmp_ext = tmp_path.extension() += tmp_ext; - auto search = extensions_format.find(tmp_ext); + spdlog::debug("Looking for {} in known extensions", tmp_ext); + auto search = extensions_format.find(tmp_ext); + tmp_longest_ext++; if (search != extensions_format.end()) { // (Combined) extension known. Remember as `longest_ext` and keep // looking for even longer extensions. - longest_ext++; + longest_ext = tmp_longest_ext; } // else: (Combined) extension not known, keep `longest_ext` as-is but try longer // extensions + spdlog::debug("Stemming {} to {}", tmp_path, tmp_path.stem()); tmp_path = tmp_path.stem(); } + spdlog::debug("Found {} extensions", longest_ext); tmp_path = path; for(int i=0; i +#include #include #include #include #include +#include #include #include @@ -45,12 +47,42 @@ void Xwim::dwim() { break; case Action::EXTRACT: this->archiver->extract(*ins.begin(), out); + sanitize_output(); break; default: spdlog::error("Unknown action"); } } +void Xwim::sanitize_output() { + fs::path in_stripped = xwim::strip_archive_extension(*ins.begin()); + + int count = 0; + fs::directory_entry first_entry; + for(auto& e: fs::directory_iterator(out)) { + count++; + if(first_entry.path().empty()) { + first_entry = e; + } + } + + if (count >= 2) { + spdlog::debug("Found multiple entries in extraction directory. Moving {} to {}", out, in_stripped); + fs::rename(out, in_stripped); + } else { + if(first_entry.is_directory()) { + spdlog::debug("Found single directory in extraction directory. Moving {} to {}", + first_entry.path(), in_stripped); + fs::rename(first_entry, in_stripped); + fs::remove(out); + } else { + spdlog::debug( + "Found single file in extraction directory. Moving {} to {}", out, in_stripped); + fs::rename(out, in_stripped); + } + } +} + void Xwim::infer_action() { if (action != Action::UNKNOWN) return; @@ -131,4 +163,4 @@ void Xwim::setIns(vector ins) { (ins.size() - this->ins.size())); } } -} +} // namespace xwim diff --git a/src/Xwim.hpp b/src/Xwim.hpp index b91f669..6741bb7 100644 --- a/src/Xwim.hpp +++ b/src/Xwim.hpp @@ -28,6 +28,7 @@ class Xwim { void infer_output(); void infer_compression_output(); void infer_extraction_output(); + void sanitize_output(); public: Xwim();