Finish the code, add more checks

This commit is contained in:
Jacob 2023-08-16 07:59:24 +02:00
parent 03b9022bf4
commit 160d26c47b

View file

@ -1,18 +1,24 @@
/* mcopy
*
* Rename music by specifying a format
* Licensed under the GNU General Public License version 3.
* See included LICENSE file for copyright and license details.
*/
#include <iostream>
#include <filesystem>
#include <string>
#include <fstream>
#include <regex>
#include <tag.h>
#include <fileref.h>
#include <id3v1tag.h>
#include <id3v2tag.h>
#ifndef VERSION
#define VERSION "0.1"
#endif
bool ask = true;
void help() {
std::cout <<
"mcopy " << VERSION << "\n\n" <<
@ -45,6 +51,7 @@ void getMetadataFromFile(std::string format, std::string str) {
TagLib::Tag *tag = file.tag();
if (file.isNull() || !file.audioProperties()) {
std::cout << "File '" << str << "' is not valid. Skipping.\n";
return;
}
@ -53,14 +60,54 @@ void getMetadataFromFile(std::string format, std::string str) {
std::string artist = tag->artist().to8Bit(true);
std::string track = std::to_string(tag->track());
std::cout << "title: " << title << "\n";
// If information is missing, ask for the missing details
if (title.empty()) {
title = "Unknown";
if (ask) {
std::cout << "mcopy: Could not retrieve title; please specify\n> ";
std::getline(std::cin, title);
}
} else if (album.empty()) {
album = "Unknown";
if (ask) {
std::cout << "mcopy: Could not retrieve album; please specify\n> ";
std::getline(std::cin, album);
}
} else if (artist.empty()) {
artist = "Unknown";
if (ask) {
std::cout << "mcopy: Could not retrieve artist; please specify\n> ";
std::getline(std::cin, artist);
}
} else if (track.empty()) {
track = "Unknown";
if (ask) {
std::cout << "mcopy: Could not retrieve track; please specify\n> ";
std::getline(std::cin, track);
}
}
std::string filename = format;
// TODO: Replace this junk
filename = std::regex_replace(format, std::regex("@t"), title);
filename = std::regex_replace(filename, std::regex("@t"), title);
filename = std::regex_replace(filename, std::regex("@a"), album);
filename = std::regex_replace(filename, std::regex("@A"), artist);
filename = std::regex_replace(filename, std::regex("@n"), track);
std::ifstream ef(filename);
if (static_cast<bool>(ef.good())) {
std::cout << "mcopy: File already exists, skipping.\n";
return;
}
std::filesystem::path fs;
std::string dir = (fs = filename).remove_filename();
std::filesystem::create_directories(dir);
@ -82,20 +129,26 @@ int main(int argc, char **argv) {
for (int i{1}; i < argc; i++) {
std::string arg = argv[i];
if (!arg.compare("-h")) {
if (!arg.compare("-h") || !arg.compare("--help")) {
help();
exit(0);
} else if (!arg.compare("-v")) {
} else if (!arg.compare("-v") || !arg.compare("--version")) {
std::cout << "mcopy " << VERSION << '\n';
exit(0);
} else if (!arg.compare("-f") && i < argc) {
format = argv[++i];
} else if (!arg.compare("-a") || !arg.compare("--ask")) {
ask = true;
} else if (!arg.compare("-na") || !arg.compare("--no-ask")) {
ask = false;
} else if (!arg.compare("-f") || !arg.compare("--format")) {
if (argc > i+1) {
format = argv[++i];
}
continue;
}
}
if (!format.compare("")) {
std::cout << "You must specify a format.\n";
std::cout << "mcopy: You must specify a format.\n";
exit(1);
}