Some more cleanup.
This commit is contained in:
parent
b1b8f6917c
commit
a55a86d00e
|
@ -47,7 +47,9 @@ void help() {
|
||||||
"@A Artist\n" <<
|
"@A Artist\n" <<
|
||||||
"@a Album name\n" <<
|
"@a Album name\n" <<
|
||||||
"@t Title\n" <<
|
"@t Title\n" <<
|
||||||
"@n Track number\n";
|
"@n Track number\n" <<
|
||||||
|
"@y Year\n" <<
|
||||||
|
"@g Genre\n";
|
||||||
|
|
||||||
std::exit(EXIT_SUCCESS);
|
std::exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
@ -57,11 +59,24 @@ TagLib::FileRef get_file(const std::string& str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_metadata_from_file(const Settings& settings, const std::string& format, const std::string& file) {
|
void get_metadata_from_file(const Settings& settings, const std::string& format, const std::string& file) {
|
||||||
|
if (!std::filesystem::is_regular_file(file)) {
|
||||||
|
std::cerr << "mcopy: invalid file '" << file << "' is skipped.\n";
|
||||||
|
if (settings.strict) {
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
TagLib::FileRef _file = get_file(file);
|
TagLib::FileRef _file = get_file(file);
|
||||||
TagLib::Tag* tag = _file.tag();
|
TagLib::Tag* tag = _file.tag();
|
||||||
|
|
||||||
if (_file.isNull() || !_file.audioProperties()) {
|
if (_file.isNull() || !_file.audioProperties()) {
|
||||||
std::cerr << "mcopy: invalid file: '" << file << "' is skipped.\n";
|
std::cerr << "mcopy: invalid file '" << file << "' is skipped.\n";
|
||||||
|
if (settings.strict) {
|
||||||
|
std::exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,46 +85,62 @@ void get_metadata_from_file(const Settings& settings, const std::string& format,
|
||||||
std::string album{};
|
std::string album{};
|
||||||
std::string artist{};
|
std::string artist{};
|
||||||
std::string track{};
|
std::string track{};
|
||||||
|
std::string year{};
|
||||||
|
std::string genre{};
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto get_title = [&tag]() -> std::string { return tag->title().to8Bit(true); };
|
const auto get_title = [&tag]() -> std::string { return tag->title().to8Bit(true); };
|
||||||
const auto get_album = [&tag]() -> std::string { return tag->album().to8Bit(true); };
|
const auto get_album = [&tag]() -> std::string { return tag->album().to8Bit(true); };
|
||||||
const auto get_artist = [&tag]() -> std::string { return tag->artist().to8Bit(true); };
|
const auto get_artist = [&tag]() -> std::string { return tag->artist().to8Bit(true); };
|
||||||
const auto get_track = [&tag]() -> std::string { return std::to_string(tag->track()); };
|
const auto get_track = [&tag]() -> std::string { return std::to_string(tag->track()); };
|
||||||
|
const auto get_year = [&tag]() -> std::string { return std::to_string(tag->year()); };
|
||||||
|
const auto get_genre = [&tag]() -> std::string { return tag->genre().to8Bit(true); };
|
||||||
|
|
||||||
FileData data{get_title(), get_album(), get_artist(), get_track()};
|
FileData data{
|
||||||
|
get_title(),
|
||||||
|
get_album(),
|
||||||
|
get_artist(),
|
||||||
|
get_track(),
|
||||||
|
get_year(),
|
||||||
|
get_genre(),
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::vector<char> the_forbidden_characters{'<', '>', ':', '"', '/', '\\', '|', '?', '*'};
|
std::vector<char> the_forbidden_characters{'<', '>', ':', '"', '/', '\\', '|', '?', '*'};
|
||||||
#else
|
#else
|
||||||
std::vector<char> the_forbidden_characters{'/'};
|
std::vector<char> the_forbidden_characters{'/'};
|
||||||
#endif
|
#endif
|
||||||
|
for (const auto& it : {&data.title, &data.album, &data.artist, &data.track, &data.year, &data.genre}) {
|
||||||
for (auto& it : std::vector<std::string*>{&data.title, &data.album, &data.artist, &data.track}) {
|
|
||||||
for (const auto& c : the_forbidden_characters)
|
for (const auto& c : the_forbidden_characters)
|
||||||
static_cast<void>(std::remove_if(it->begin(), it->end(), [&c](char _c) { return c == _c; }));
|
static_cast<void>(std::remove_if(it->begin(), it->end(), [&c](char _c) { return c == _c; }));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& it : std::unordered_map<std::string, std::string&>{{"title", data.title}, {"album", data.album}, {"artist", data.artist}, {"track", data.track}}) {
|
std::unordered_map<std::string, std::string> delim_map{
|
||||||
if (it.second.empty()) {
|
{"@t", data.title},
|
||||||
it.second = "Unknown";
|
{"@a", data.album},
|
||||||
|
{"@A", data.artist},
|
||||||
if (settings.ask) {
|
{"@n", data.track},
|
||||||
std::cerr << "mcopy: failed to retrieve " << it.first << ", enter an appropriate replacement: ";
|
{"@y", data.year},
|
||||||
std::getline(std::cin, it.second);
|
{"@g", data.genre},
|
||||||
}
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string output_filename = format;
|
std::string output_filename = format;
|
||||||
for (const auto& it : std::unordered_map<std::string, std::string>{{"@t", data.title}, {"@a", data.album}, {"@A", data.artist}, {"@n", data.track}}) {
|
for (auto& it : delim_map) {
|
||||||
while (output_filename.find(it.first) != std::string::npos) {
|
while (output_filename.find(it.first) != std::string::npos) {
|
||||||
|
if (it.second.empty()) {
|
||||||
|
it.second = "Unknown";
|
||||||
|
|
||||||
|
if (settings.ask) {
|
||||||
|
std::cerr << "mcopy: failed to retrieve data for " << it.first << ", enter an appropriate replacement: ";
|
||||||
|
std::getline(std::cin, it.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
output_filename.replace(output_filename.find(it.first), it.first.length(), it.second);
|
output_filename.replace(output_filename.find(it.first), it.first.length(), it.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream ef(output_filename);
|
std::ifstream ef(output_filename);
|
||||||
|
|
||||||
if (ef.good() && settings.skip_existing) {
|
if (ef.good() && settings.skip_existing) {
|
||||||
std::cerr << "mcopy: file << '" << file << "' already exists, skipping\n";
|
std::cerr << "mcopy: file << '" << file << "' already exists, skipping\n";
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue