Move extraction output according to plan
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Armin Friedl 2021-02-20 07:26:07 +01:00
parent 4c1c5bf00f
commit 6a7e98dbf6
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
3 changed files with 43 additions and 3 deletions

View file

@ -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<longest_ext; i++) tmp_path = tmp_path.stem();

View file

@ -2,10 +2,12 @@
#include <spdlog/spdlog.h>
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <ios>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
@ -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<fs::path> ins) {
(ins.size() - this->ins.size()));
}
}
}
} // namespace xwim

View file

@ -28,6 +28,7 @@ class Xwim {
void infer_output();
void infer_compression_output();
void infer_extraction_output();
void sanitize_output();
public:
Xwim();